001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.catalina.mbeans;
018:
019: import java.net.URLDecoder;
020: import java.util.ArrayList;
021:
022: import javax.management.MBeanException;
023: import javax.management.MalformedObjectNameException;
024: import javax.management.ObjectName;
025: import javax.management.RuntimeOperationsException;
026:
027: import org.apache.catalina.core.StandardDefaultContext;
028: import org.apache.catalina.deploy.ContextEnvironment;
029: import org.apache.catalina.deploy.ContextResource;
030: import org.apache.catalina.deploy.ContextResourceLink;
031: import org.apache.catalina.deploy.NamingResources;
032: import org.apache.commons.modeler.BaseModelMBean;
033: import org.apache.commons.modeler.ManagedBean;
034: import org.apache.commons.modeler.Registry;
035: import org.apache.tomcat.util.compat.JdkCompat;
036:
037: /**
038: * <p>A <strong>ModelMBean</strong> implementation for the
039: * <code>org.apache.catalina.core.StandardDefaultContext</code> component.</p>
040: *
041: * @author Amy Roh
042: * @version $Revision: 1.5 $ $Date: 2004/04/15 01:44:09 $
043: */
044:
045: public class DefaultContextMBean extends BaseModelMBean {
046:
047: // ----------------------------------------------------------- Constructors
048:
049: /**
050: * Construct a <code>ModelMBean</code> with default
051: * <code>ModelMBeanInfo</code> information.
052: *
053: * @exception MBeanException if the initializer of an object
054: * throws an exception
055: * @exception RuntimeOperationsException if an IllegalArgumentException
056: * occurs
057: */
058: public DefaultContextMBean() throws MBeanException,
059: RuntimeOperationsException {
060:
061: super ();
062:
063: }
064:
065: // ----------------------------------------------------- Class Variables
066:
067: /**
068: * JDK compatibility support
069: */
070: private static final JdkCompat jdkCompat = JdkCompat.getJdkCompat();
071:
072: // ----------------------------------------------------- Instance Variables
073:
074: /**
075: * The configuration information registry for our managed beans.
076: */
077: protected Registry registry = MBeanUtils.createRegistry();
078:
079: /**
080: * The <code>ManagedBean</code> information describing this MBean.
081: */
082: protected ManagedBean managed = registry
083: .findManagedBean("DefaultContext");
084:
085: // ------------------------------------------------------------- Attributes
086:
087: /**
088: * Return the naming resources associated with this web application.
089: */
090: private NamingResources getNamingResources() {
091:
092: return ((StandardDefaultContext) this .resource)
093: .getNamingResources();
094:
095: }
096:
097: /**
098: * Return the MBean Names of the set of defined environment entries for
099: * this web application
100: */
101: public String[] getEnvironments() {
102: ContextEnvironment[] envs = getNamingResources()
103: .findEnvironments();
104: ArrayList results = new ArrayList();
105: for (int i = 0; i < envs.length; i++) {
106: try {
107: ObjectName oname = MBeanUtils.createObjectName(managed
108: .getDomain(), envs[i]);
109: results.add(oname.toString());
110: } catch (MalformedObjectNameException e) {
111: IllegalArgumentException iae = new IllegalArgumentException(
112: "Cannot create object name for environment "
113: + envs[i]);
114: jdkCompat.chainException(iae, e);
115: throw iae;
116: }
117: }
118: return ((String[]) results.toArray(new String[results.size()]));
119:
120: }
121:
122: /**
123: * Return the MBean Names of all the defined resource references for this
124: * application.
125: */
126: public String[] getResources() {
127:
128: ContextResource[] resources = getNamingResources()
129: .findResources();
130: ArrayList results = new ArrayList();
131: for (int i = 0; i < resources.length; i++) {
132: try {
133: ObjectName oname = MBeanUtils.createObjectName(managed
134: .getDomain(), resources[i]);
135: results.add(oname.toString());
136: } catch (MalformedObjectNameException e) {
137: IllegalArgumentException iae = new IllegalArgumentException(
138: "Cannot create object name for resource "
139: + resources[i]);
140: jdkCompat.chainException(iae, e);
141: throw iae;
142: }
143: }
144: return ((String[]) results.toArray(new String[results.size()]));
145:
146: }
147:
148: /**
149: * Return the MBean Names of all the defined resource links for this
150: * application
151: */
152: public String[] getResourceLinks() {
153:
154: ContextResourceLink[] links = getNamingResources()
155: .findResourceLinks();
156: ArrayList results = new ArrayList();
157: for (int i = 0; i < links.length; i++) {
158: try {
159: ObjectName oname = MBeanUtils.createObjectName(managed
160: .getDomain(), links[i]);
161: results.add(oname.toString());
162: } catch (MalformedObjectNameException e) {
163: IllegalArgumentException iae = new IllegalArgumentException(
164: "Cannot create object name for resource "
165: + links[i]);
166: jdkCompat.chainException(iae, e);
167: throw iae;
168: }
169: }
170: return ((String[]) results.toArray(new String[results.size()]));
171:
172: }
173:
174: // ------------------------------------------------------------- Operations
175:
176: /**
177: * Add an environment entry for this web application.
178: *
179: * @param envName New environment entry name
180: */
181: public String addEnvironment(String envName, String type)
182: throws MalformedObjectNameException {
183:
184: NamingResources nresources = getNamingResources();
185: if (nresources == null) {
186: return null;
187: }
188: ContextEnvironment env = nresources.findEnvironment(envName);
189: if (env != null) {
190: throw new IllegalArgumentException(
191: "Invalid environment name - already exists '"
192: + envName + "'");
193: }
194: env = new ContextEnvironment();
195: env.setName(envName);
196: env.setType(type);
197: nresources.addEnvironment(env);
198:
199: // Return the corresponding MBean name
200: ManagedBean managed = registry
201: .findManagedBean("ContextEnvironment");
202: ObjectName oname = MBeanUtils.createObjectName(managed
203: .getDomain(), env);
204: return (oname.toString());
205:
206: }
207:
208: /**
209: * Add a resource reference for this web application.
210: *
211: * @param resourceName New resource reference name
212: */
213: public String addResource(String resourceName, String type)
214: throws MalformedObjectNameException {
215:
216: NamingResources nresources = getNamingResources();
217: if (nresources == null) {
218: return null;
219: }
220: ContextResource resource = nresources
221: .findResource(resourceName);
222: if (resource != null) {
223: throw new IllegalArgumentException(
224: "Invalid resource name - already exists'"
225: + resourceName + "'");
226: }
227: resource = new ContextResource();
228: resource.setName(resourceName);
229: resource.setType(type);
230: nresources.addResource(resource);
231:
232: // Return the corresponding MBean name
233: ManagedBean managed = registry
234: .findManagedBean("ContextResource");
235: ObjectName oname = MBeanUtils.createObjectName(managed
236: .getDomain(), resource);
237:
238: return (oname.toString());
239: }
240:
241: /**
242: * Add a resource link for this web application.
243: *
244: * @param resourceLinkName New resource link name
245: */
246: public String addResourceLink(String resourceLinkName,
247: String global, String name, String type)
248: throws MalformedObjectNameException {
249:
250: NamingResources nresources = getNamingResources();
251: if (nresources == null) {
252: return null;
253: }
254: ContextResourceLink resourceLink = nresources
255: .findResourceLink(resourceLinkName);
256: if (resourceLink != null) {
257: throw new IllegalArgumentException(
258: "Invalid resource link name - already exists'"
259: + resourceLinkName + "'");
260: }
261: resourceLink = new ContextResourceLink();
262: resourceLink.setGlobal(global);
263: resourceLink.setName(resourceLinkName);
264: resourceLink.setType(type);
265: nresources.addResourceLink(resourceLink);
266:
267: // Return the corresponding MBean name
268: ManagedBean managed = registry
269: .findManagedBean("ContextResourceLink");
270: ObjectName oname = MBeanUtils.createObjectName(managed
271: .getDomain(), resourceLink);
272: return (oname.toString());
273: }
274:
275: /**
276: * Remove any environment entry with the specified name.
277: *
278: * @param name Name of the environment entry to remove
279: */
280: public void removeEnvironment(String envName) {
281:
282: NamingResources nresources = getNamingResources();
283: if (nresources == null) {
284: return;
285: }
286: ContextEnvironment env = nresources.findEnvironment(envName);
287: if (env == null) {
288: throw new IllegalArgumentException(
289: "Invalid environment name '" + envName + "'");
290: }
291: nresources.removeEnvironment(envName);
292:
293: }
294:
295: /**
296: * Remove any resource reference with the specified name.
297: *
298: * @param resourceName Name of the resource reference to remove
299: */
300: public void removeResource(String resourceName) {
301:
302: resourceName = URLDecoder.decode(resourceName);
303: NamingResources nresources = getNamingResources();
304: if (nresources == null) {
305: return;
306: }
307: ContextResource resource = nresources
308: .findResource(resourceName);
309: if (resource == null) {
310: throw new IllegalArgumentException(
311: "Invalid resource name '" + resourceName + "'");
312: }
313: nresources.removeResource(resourceName);
314: }
315:
316: /**
317: * Remove any resource link with the specified name.
318: *
319: * @param resourceName Name of the resource reference to remove
320: */
321: public void removeResourceLink(String resourceLinkName) {
322:
323: resourceLinkName = URLDecoder.decode(resourceLinkName);
324: NamingResources nresources = getNamingResources();
325: if (nresources == null) {
326: return;
327: }
328: ContextResourceLink resource = nresources
329: .findResourceLink(resourceLinkName);
330: if (resource == null) {
331: throw new IllegalArgumentException(
332: "Invalid resource name '" + resourceLinkName + "'");
333: }
334: nresources.removeResourceLink(resourceLinkName);
335: }
336:
337: }
|