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.StandardContext;
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.StandardContext</code> component.</p>
040: *
041: * @author Amy Roh
042: * @version $Revision: 1.7 $ $Date: 2004/04/15 01:44:09 $
043: */
044:
045: public class StandardContextMBean 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 StandardContextMBean() 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("StandardContext");
084:
085: // ------------------------------------------------------------- Attributes
086:
087: /**
088: * Return the naming resources associated with this web application.
089: */
090: private NamingResources getNamingResources() {
091:
092: return ((StandardContext) this .resource).getNamingResources();
093:
094: }
095:
096: /**
097: * Return the naming resources associated with this web application.
098: */
099: public void reload() {
100:
101: ((StandardContext) this .resource).reload();
102:
103: }
104:
105: /**
106: * Return the MBean Names of the set of defined environment entries for
107: * this web application
108: */
109: public String[] getEnvironments() {
110: ContextEnvironment[] envs = getNamingResources()
111: .findEnvironments();
112: ArrayList results = new ArrayList();
113: for (int i = 0; i < envs.length; i++) {
114: try {
115: ObjectName oname = MBeanUtils.createObjectName(managed
116: .getDomain(), envs[i]);
117: results.add(oname.toString());
118: } catch (MalformedObjectNameException e) {
119: IllegalArgumentException iae = new IllegalArgumentException(
120: "Cannot create object name for environment "
121: + envs[i]);
122: jdkCompat.chainException(iae, e);
123: throw iae;
124: }
125: }
126: return ((String[]) results.toArray(new String[results.size()]));
127:
128: }
129:
130: /**
131: * Return the MBean Names of all the defined resource references for this
132: * application.
133: */
134: public String[] getResources() {
135:
136: ContextResource[] resources = getNamingResources()
137: .findResources();
138: ArrayList results = new ArrayList();
139: for (int i = 0; i < resources.length; i++) {
140: try {
141: ObjectName oname = MBeanUtils.createObjectName(managed
142: .getDomain(), resources[i]);
143: results.add(oname.toString());
144: } catch (MalformedObjectNameException e) {
145: IllegalArgumentException iae = new IllegalArgumentException(
146: "Cannot create object name for resource "
147: + resources[i]);
148: jdkCompat.chainException(iae, e);
149: throw iae;
150: }
151: }
152: return ((String[]) results.toArray(new String[results.size()]));
153:
154: }
155:
156: /**
157: * Return the MBean Names of all the defined resource links for this
158: * application
159: */
160: public String[] getResourceLinks() {
161:
162: ContextResourceLink[] links = getNamingResources()
163: .findResourceLinks();
164: ArrayList results = new ArrayList();
165: for (int i = 0; i < links.length; i++) {
166: try {
167: ObjectName oname = MBeanUtils.createObjectName(managed
168: .getDomain(), links[i]);
169: results.add(oname.toString());
170: } catch (MalformedObjectNameException e) {
171: IllegalArgumentException iae = new IllegalArgumentException(
172: "Cannot create object name for resource "
173: + links[i]);
174: jdkCompat.chainException(iae, e);
175: throw iae;
176: }
177: }
178: return ((String[]) results.toArray(new String[results.size()]));
179:
180: }
181:
182: /**
183: * Return the naming resources associated with this web application.
184: */
185: public javax.naming.directory.DirContext getStaticResources() {
186:
187: return ((StandardContext) this .resource).getResources();
188:
189: }
190:
191: /**
192: * Return the naming resources associated with this web application.
193: */
194: public String[] getWelcomeFiles() {
195:
196: return ((StandardContext) this .resource).findWelcomeFiles();
197:
198: }
199:
200: // ------------------------------------------------------------- Operations
201:
202: /**
203: * Add an environment entry for this web application.
204: *
205: * @param envName New environment entry name
206: */
207: public String addEnvironment(String envName, String type)
208: throws MalformedObjectNameException {
209:
210: NamingResources nresources = getNamingResources();
211: if (nresources == null) {
212: return null;
213: }
214: ContextEnvironment env = nresources.findEnvironment(envName);
215: if (env != null) {
216: throw new IllegalArgumentException(
217: "Invalid environment name - already exists '"
218: + envName + "'");
219: }
220: env = new ContextEnvironment();
221: env.setName(envName);
222: env.setType(type);
223: nresources.addEnvironment(env);
224:
225: // Return the corresponding MBean name
226: ManagedBean managed = registry
227: .findManagedBean("ContextEnvironment");
228: ObjectName oname = MBeanUtils.createObjectName(managed
229: .getDomain(), env);
230: return (oname.toString());
231:
232: }
233:
234: /**
235: * Add a resource reference for this web application.
236: *
237: * @param resourceName New resource reference name
238: */
239: public String addResource(String resourceName, String type)
240: throws MalformedObjectNameException {
241:
242: NamingResources nresources = getNamingResources();
243: if (nresources == null) {
244: return null;
245: }
246: ContextResource resource = nresources
247: .findResource(resourceName);
248: if (resource != null) {
249: throw new IllegalArgumentException(
250: "Invalid resource name - already exists'"
251: + resourceName + "'");
252: }
253: resource = new ContextResource();
254: resource.setName(resourceName);
255: resource.setType(type);
256: nresources.addResource(resource);
257:
258: // Return the corresponding MBean name
259: ManagedBean managed = registry
260: .findManagedBean("ContextResource");
261: ObjectName oname = MBeanUtils.createObjectName(managed
262: .getDomain(), resource);
263: return (oname.toString());
264: }
265:
266: /**
267: * Add a resource link for this web application.
268: *
269: * @param resourceLinkName New resource link name
270: */
271: public String addResourceLink(String resourceLinkName,
272: String global, String name, String type)
273: throws MalformedObjectNameException {
274:
275: NamingResources nresources = getNamingResources();
276: if (nresources == null) {
277: return null;
278: }
279: ContextResourceLink resourceLink = nresources
280: .findResourceLink(resourceLinkName);
281: if (resourceLink != null) {
282: throw new IllegalArgumentException(
283: "Invalid resource link name - already exists'"
284: + resourceLinkName + "'");
285: }
286: resourceLink = new ContextResourceLink();
287: resourceLink.setGlobal(global);
288: resourceLink.setName(resourceLinkName);
289: resourceLink.setType(type);
290: nresources.addResourceLink(resourceLink);
291:
292: // Return the corresponding MBean name
293: ManagedBean managed = registry
294: .findManagedBean("ContextResourceLink");
295: ObjectName oname = MBeanUtils.createObjectName(managed
296: .getDomain(), resourceLink);
297: return (oname.toString());
298: }
299:
300: /**
301: * Remove any environment entry with the specified name.
302: *
303: * @param name Name of the environment entry to remove
304: */
305: public void removeEnvironment(String envName) {
306:
307: NamingResources nresources = getNamingResources();
308: if (nresources == null) {
309: return;
310: }
311: ContextEnvironment env = nresources.findEnvironment(envName);
312: if (env == null) {
313: throw new IllegalArgumentException(
314: "Invalid environment name '" + envName + "'");
315: }
316: nresources.removeEnvironment(envName);
317:
318: }
319:
320: /**
321: * Remove any resource reference with the specified name.
322: *
323: * @param resourceName Name of the resource reference to remove
324: */
325: public void removeResource(String resourceName) {
326:
327: resourceName = URLDecoder.decode(resourceName);
328: NamingResources nresources = getNamingResources();
329: if (nresources == null) {
330: return;
331: }
332: ContextResource resource = nresources
333: .findResource(resourceName);
334: if (resource == null) {
335: throw new IllegalArgumentException(
336: "Invalid resource name '" + resourceName + "'");
337: }
338: nresources.removeResource(resourceName);
339: }
340:
341: /**
342: * Remove any resource link with the specified name.
343: *
344: * @param resourceName Name of the resource reference to remove
345: */
346: public void removeResourceLink(String resourceLinkName) {
347:
348: resourceLinkName = URLDecoder.decode(resourceLinkName);
349: NamingResources nresources = getNamingResources();
350: if (nresources == null) {
351: return;
352: }
353: ContextResourceLink resource = nresources
354: .findResourceLink(resourceLinkName);
355: if (resource == null) {
356: throw new IllegalArgumentException(
357: "Invalid resource name '" + resourceLinkName + "'");
358: }
359: nresources.removeResourceLink(resourceLinkName);
360: }
361:
362: }
|