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