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