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