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