001: /*
002: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/mbeans/StandardContextMBean.java,v 1.6 2002/06/06 22:36:47 amyroh Exp $
003: * $Revision: 1.6 $
004: * $Date: 2002/06/06 22:36:47 $
005: *
006: * ====================================================================
007: *
008: * The Apache Software License, Version 1.1
009: *
010: * Copyright (c) 1999 The Apache Software Foundation. All rights
011: * reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016: *
017: * 1. Redistributions of source code must retain the above copyright
018: * notice, this list of conditions and the following disclaimer.
019: *
020: * 2. Redistributions in binary form must reproduce the above copyright
021: * notice, this list of conditions and the following disclaimer in
022: * the documentation and/or other materials provided with the
023: * distribution.
024: *
025: * 3. The end-user documentation included with the redistribution, if
026: * any, must include the following acknowlegement:
027: * "This product includes software developed by the
028: * Apache Software Foundation (http://www.apache.org/)."
029: * Alternately, this acknowlegement may appear in the software itself,
030: * if and wherever such third-party acknowlegements normally appear.
031: *
032: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
033: * Foundation" must not be used to endorse or promote products derived
034: * from this software without prior written permission. For written
035: * permission, please contact apache@apache.org.
036: *
037: * 5. Products derived from this software may not be called "Apache"
038: * nor may "Apache" appear in their names without prior written
039: * permission of the Apache Group.
040: *
041: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: *
055: * This software consists of voluntary contributions made by many
056: * individuals on behalf of the Apache Software Foundation. For more
057: * information on the Apache Software Foundation, please see
058: * <http://www.apache.org/>.
059: *
060: * [Additional notices, if required by prior licensing conditions]
061: *
062: */
063:
064: package org.apache.catalina.mbeans;
065:
066: import java.net.URLDecoder;
067: import java.util.ArrayList;
068: import javax.management.MalformedObjectNameException;
069: import javax.management.MBeanException;
070: import javax.management.ObjectName;
071: import javax.management.RuntimeOperationsException;
072: import org.apache.catalina.core.StandardContext;
073: import org.apache.catalina.deploy.ContextEnvironment;
074: import org.apache.catalina.deploy.ContextResource;
075: import org.apache.catalina.deploy.ContextResourceLink;
076: import org.apache.catalina.deploy.NamingResources;
077: import org.apache.catalina.deploy.ResourceParams;
078: import org.apache.commons.modeler.BaseModelMBean;
079: import org.apache.commons.modeler.ManagedBean;
080: import org.apache.commons.modeler.Registry;
081:
082: /**
083: * <p>A <strong>ModelMBean</strong> implementation for the
084: * <code>org.apache.catalina.core.StandardContext</code> component.</p>
085: *
086: * @author Amy Roh
087: * @version $Revision: 1.6 $ $Date: 2002/06/06 22:36:47 $
088: */
089:
090: public class StandardContextMBean extends BaseModelMBean {
091:
092: // ----------------------------------------------------------- Constructors
093:
094: /**
095: * Construct a <code>ModelMBean</code> with default
096: * <code>ModelMBeanInfo</code> information.
097: *
098: * @exception MBeanException if the initializer of an object
099: * throws an exception
100: * @exception RuntimeOperationsException if an IllegalArgumentException
101: * occurs
102: */
103: public StandardContextMBean() throws MBeanException,
104: RuntimeOperationsException {
105:
106: super ();
107:
108: }
109:
110: // ----------------------------------------------------- Instance Variables
111:
112: /**
113: * The configuration information registry for our managed beans.
114: */
115: protected Registry registry = MBeanUtils.createRegistry();
116:
117: /**
118: * The <code>ManagedBean</code> information describing this MBean.
119: */
120: protected ManagedBean managed = registry
121: .findManagedBean("StandardContext");
122:
123: // ------------------------------------------------------------- Attributes
124:
125: /**
126: * Return the naming resources associated with this web application.
127: */
128: private NamingResources getNamingResources() {
129:
130: return ((StandardContext) this .resource).getNamingResources();
131:
132: }
133:
134: /**
135: * Return the MBean Names of the set of defined environment entries for
136: * this web application
137: */
138: public String[] getEnvironments() {
139: ContextEnvironment[] envs = getNamingResources()
140: .findEnvironments();
141: ArrayList results = new ArrayList();
142: for (int i = 0; i < envs.length; i++) {
143: try {
144: ObjectName oname = MBeanUtils.createObjectName(managed
145: .getDomain(), envs[i]);
146: results.add(oname.toString());
147: } catch (MalformedObjectNameException e) {
148: throw new IllegalArgumentException(
149: "Cannot create object name for environment "
150: + envs[i]);
151: }
152: }
153: return ((String[]) results.toArray(new String[results.size()]));
154:
155: }
156:
157: /**
158: * Return the MBean Names of all the defined resource references for this
159: * application.
160: */
161: public String[] getResources() {
162:
163: ContextResource[] resources = getNamingResources()
164: .findResources();
165: ArrayList results = new ArrayList();
166: for (int i = 0; i < resources.length; i++) {
167: try {
168: ObjectName oname = MBeanUtils.createObjectName(managed
169: .getDomain(), resources[i]);
170: results.add(oname.toString());
171: } catch (MalformedObjectNameException e) {
172: throw new IllegalArgumentException(
173: "Cannot create object name for resource "
174: + resources[i]);
175: }
176: }
177: return ((String[]) results.toArray(new String[results.size()]));
178:
179: }
180:
181: /**
182: * Return the MBean Names of all the defined resource links for this
183: * application
184: */
185: public String[] getResourceLinks() {
186:
187: ContextResourceLink[] links = getNamingResources()
188: .findResourceLinks();
189: ArrayList results = new ArrayList();
190: for (int i = 0; i < links.length; i++) {
191: try {
192: ObjectName oname = MBeanUtils.createObjectName(managed
193: .getDomain(), links[i]);
194: results.add(oname.toString());
195: } catch (MalformedObjectNameException e) {
196: throw new IllegalArgumentException(
197: "Cannot create object name for resource "
198: + links[i]);
199: }
200: }
201: return ((String[]) results.toArray(new String[results.size()]));
202:
203: }
204:
205: // ------------------------------------------------------------- Operations
206:
207: /**
208: * Add an environment entry for this web application.
209: *
210: * @param envName New environment entry name
211: */
212: public String addEnvironment(String envName, String type)
213: throws MalformedObjectNameException {
214:
215: NamingResources nresources = getNamingResources();
216: if (nresources == null) {
217: return null;
218: }
219: ContextEnvironment env = nresources.findEnvironment(envName);
220: if (env != null) {
221: throw new IllegalArgumentException(
222: "Invalid environment name - already exists '"
223: + envName + "'");
224: }
225: env = new ContextEnvironment();
226: env.setName(envName);
227: env.setType(type);
228: nresources.addEnvironment(env);
229:
230: // Return the corresponding MBean name
231: ManagedBean managed = registry
232: .findManagedBean("ContextEnvironment");
233: ObjectName oname = MBeanUtils.createObjectName(managed
234: .getDomain(), env);
235: return (oname.toString());
236:
237: }
238:
239: /**
240: * Add a resource reference for this web application.
241: *
242: * @param resourceName New resource reference name
243: */
244: public String addResource(String resourceName, String type)
245: throws MalformedObjectNameException {
246:
247: NamingResources nresources = getNamingResources();
248: if (nresources == null) {
249: return null;
250: }
251: ContextResource resource = nresources
252: .findResource(resourceName);
253: if (resource != null) {
254: throw new IllegalArgumentException(
255: "Invalid resource name - already exists'"
256: + resourceName + "'");
257: }
258: resource = new ContextResource();
259: resource.setName(resourceName);
260: resource.setType(type);
261: nresources.addResource(resource);
262:
263: // Return the corresponding MBean name
264: ManagedBean managed = registry
265: .findManagedBean("ContextResource");
266: ObjectName oname = MBeanUtils.createObjectName(managed
267: .getDomain(), resource);
268: return (oname.toString());
269: }
270:
271: /**
272: * Add a resource link for this web application.
273: *
274: * @param resourceLinkName New resource link name
275: */
276: public String addResourceLink(String resourceLinkName,
277: String global, String name, String type)
278: throws MalformedObjectNameException {
279:
280: NamingResources nresources = getNamingResources();
281: if (nresources == null) {
282: return null;
283: }
284: ContextResourceLink resourceLink = nresources
285: .findResourceLink(resourceLinkName);
286: if (resourceLink != null) {
287: throw new IllegalArgumentException(
288: "Invalid resource link name - already exists'"
289: + resourceLinkName + "'");
290: }
291: resourceLink = new ContextResourceLink();
292: resourceLink.setGlobal(global);
293: resourceLink.setName(resourceLinkName);
294: resourceLink.setType(type);
295: nresources.addResourceLink(resourceLink);
296:
297: // Return the corresponding MBean name
298: ManagedBean managed = registry
299: .findManagedBean("ContextResourceLink");
300: ObjectName oname = MBeanUtils.createObjectName(managed
301: .getDomain(), resourceLink);
302: return (oname.toString());
303: }
304:
305: /**
306: * Remove any environment entry with the specified name.
307: *
308: * @param name Name of the environment entry to remove
309: */
310: public void removeEnvironment(String envName) {
311:
312: NamingResources nresources = getNamingResources();
313: if (nresources == null) {
314: return;
315: }
316: ContextEnvironment env = nresources.findEnvironment(envName);
317: if (env == null) {
318: throw new IllegalArgumentException(
319: "Invalid environment name '" + envName + "'");
320: }
321: nresources.removeEnvironment(envName);
322:
323: }
324:
325: /**
326: * Remove any resource reference with the specified name.
327: *
328: * @param resourceName Name of the resource reference to remove
329: */
330: public void removeResource(String resourceName) {
331:
332: resourceName = URLDecoder.decode(resourceName);
333: NamingResources nresources = getNamingResources();
334: if (nresources == null) {
335: return;
336: }
337: ContextResource resource = nresources
338: .findResource(resourceName);
339: if (resource == null) {
340: throw new IllegalArgumentException(
341: "Invalid resource name '" + resourceName + "'");
342: }
343: nresources.removeResource(resourceName);
344: }
345:
346: /**
347: * Remove any resource link with the specified name.
348: *
349: * @param resourceName Name of the resource reference to remove
350: */
351: public void removeResourceLink(String resourceLinkName) {
352:
353: resourceLinkName = URLDecoder.decode(resourceLinkName);
354: NamingResources nresources = getNamingResources();
355: if (nresources == null) {
356: return;
357: }
358: ContextResourceLink resource = nresources
359: .findResourceLink(resourceLinkName);
360: if (resource == null) {
361: throw new IllegalArgumentException(
362: "Invalid resource name '" + resourceLinkName + "'");
363: }
364: nresources.removeResourceLink(resourceLinkName);
365: }
366:
367: }
|