KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > mbeans > StandardContextMBean


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.catalina.mbeans;
19
20 import java.util.ArrayList JavaDoc;
21
22 import javax.management.MBeanException JavaDoc;
23 import javax.management.MalformedObjectNameException JavaDoc;
24 import javax.management.ObjectName JavaDoc;
25 import javax.management.RuntimeOperationsException JavaDoc;
26
27 import org.apache.catalina.core.StandardContext;
28 import org.apache.catalina.deploy.ContextEnvironment;
29 import org.apache.catalina.deploy.ContextResource;
30 import org.apache.catalina.deploy.ContextResourceLink;
31 import org.apache.catalina.deploy.NamingResources;
32 import org.apache.tomcat.util.modeler.BaseModelMBean;
33 import org.apache.tomcat.util.modeler.ManagedBean;
34 import org.apache.tomcat.util.modeler.Registry;
35
36 /**
37  * <p>A <strong>ModelMBean</strong> implementation for the
38  * <code>org.apache.catalina.core.StandardContext</code> component.</p>
39  *
40  * @author Amy Roh
41  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
42  */

43
44 public class StandardContextMBean extends BaseModelMBean {
45
46
47     // ----------------------------------------------------------- Constructors
48

49
50     /**
51      * Construct a <code>ModelMBean</code> with default
52      * <code>ModelMBeanInfo</code> information.
53      *
54      * @exception MBeanException if the initializer of an object
55      * throws an exception
56      * @exception RuntimeOperationsException if an IllegalArgumentException
57      * occurs
58      */

59     public StandardContextMBean()
60         throws MBeanException JavaDoc, RuntimeOperationsException JavaDoc {
61
62         super();
63
64     }
65     
66
67     // ----------------------------------------------------- Instance Variables
68

69     
70     /**
71      * The configuration information registry for our managed beans.
72      */

73     protected Registry registry = MBeanUtils.createRegistry();
74
75     /**
76      * The <code>ManagedBean</code> information describing this MBean.
77      */

78     protected ManagedBean managed =
79         registry.findManagedBean("StandardContext");
80
81     
82     // ------------------------------------------------------------- Attributes
83

84     
85     /**
86      * Return the naming resources associated with this web application.
87      */

88     private NamingResources getNamingResources() {
89         
90         return ((StandardContext)this.resource).getNamingResources();
91     
92     }
93     
94     /**
95      * Return the naming resources associated with this web application.
96      */

97     public void reload() {
98         
99         ((StandardContext)this.resource).reload();
100     
101     }
102     
103     
104     /**
105      * Return the MBean Names of the set of defined environment entries for
106      * this web application
107      */

108     public String JavaDoc[] getEnvironments() {
109         ContextEnvironment[] envs = getNamingResources().findEnvironments();
110         ArrayList JavaDoc results = new ArrayList JavaDoc();
111         for (int i = 0; i < envs.length; i++) {
112             try {
113                 ObjectName JavaDoc oname =
114                     MBeanUtils.createObjectName(managed.getDomain(), envs[i]);
115                 results.add(oname.toString());
116             } catch (MalformedObjectNameException JavaDoc e) {
117                 IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc
118                     ("Cannot create object name for environment " + envs[i]);
119                 iae.initCause(e);
120                 throw iae;
121             }
122         }
123         return ((String JavaDoc[]) results.toArray(new String JavaDoc[results.size()]));
124
125     }
126     
127     
128     /**
129      * Return the MBean Names of all the defined resource references for this
130      * application.
131      */

132     public String JavaDoc[] getResources() {
133         
134         ContextResource[] resources = getNamingResources().findResources();
135         ArrayList JavaDoc results = new ArrayList JavaDoc();
136         for (int i = 0; i < resources.length; i++) {
137             try {
138                 ObjectName JavaDoc oname =
139                     MBeanUtils.createObjectName(managed.getDomain(), resources[i]);
140                 results.add(oname.toString());
141             } catch (MalformedObjectNameException JavaDoc e) {
142                 IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc
143                     ("Cannot create object name for resource " + resources[i]);
144                 iae.initCause(e);
145                 throw iae;
146             }
147         }
148         return ((String JavaDoc[]) results.toArray(new String JavaDoc[results.size()]));
149
150     }
151
152       
153     /**
154      * Return the MBean Names of all the defined resource links for this
155      * application
156      */

157     public String JavaDoc[] getResourceLinks() {
158         
159         ContextResourceLink[] links = getNamingResources().findResourceLinks();
160         ArrayList JavaDoc results = new ArrayList JavaDoc();
161         for (int i = 0; i < links.length; i++) {
162             try {
163                 ObjectName JavaDoc oname =
164                     MBeanUtils.createObjectName(managed.getDomain(), links[i]);
165                 results.add(oname.toString());
166             } catch (MalformedObjectNameException JavaDoc e) {
167                 IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc
168                     ("Cannot create object name for resource " + links[i]);
169                 iae.initCause(e);
170                 throw iae;
171             }
172         }
173         return ((String JavaDoc[]) results.toArray(new String JavaDoc[results.size()]));
174
175     }
176
177
178     /**
179      * Return the naming resources associated with this web application.
180      */

181     public javax.naming.directory.DirContext JavaDoc getStaticResources() {
182
183         return ((StandardContext)this.resource).getResources();
184
185     }
186
187
188     /**
189      * Return the naming resources associated with this web application.
190      */

191     public String JavaDoc[] getWelcomeFiles() {
192
193         return ((StandardContext)this.resource).findWelcomeFiles();
194
195     }
196
197
198     // ------------------------------------------------------------- Operations
199

200
201     /**
202      * Add an environment entry for this web application.
203      *
204      * @param envName New environment entry name
205      */

206     public String JavaDoc addEnvironment(String JavaDoc envName, String JavaDoc type)
207         throws MalformedObjectNameException JavaDoc {
208
209         NamingResources nresources = getNamingResources();
210         if (nresources == null) {
211             return null;
212         }
213         ContextEnvironment env = nresources.findEnvironment(envName);
214         if (env != null) {
215             throw new IllegalArgumentException JavaDoc
216                 ("Invalid environment name - already exists '" + envName + "'");
217         }
218         env = new ContextEnvironment();
219         env.setName(envName);
220         env.setType(type);
221         nresources.addEnvironment(env);
222         
223         // Return the corresponding MBean name
224
ManagedBean managed = registry.findManagedBean("ContextEnvironment");
225         ObjectName JavaDoc oname =
226             MBeanUtils.createObjectName(managed.getDomain(), env);
227         return (oname.toString());
228         
229     }
230
231     
232     /**
233      * Add a resource reference for this web application.
234      *
235      * @param resourceName New resource reference name
236      */

237     public String JavaDoc addResource(String JavaDoc resourceName, String JavaDoc type)
238         throws MalformedObjectNameException JavaDoc {
239         
240         NamingResources nresources = getNamingResources();
241         if (nresources == null) {
242             return null;
243         }
244         ContextResource resource = nresources.findResource(resourceName);
245         if (resource != null) {
246             throw new IllegalArgumentException JavaDoc
247                 ("Invalid resource name - already exists'" + resourceName + "'");
248         }
249         resource = new ContextResource();
250         resource.setName(resourceName);
251         resource.setType(type);
252         nresources.addResource(resource);
253         
254         // Return the corresponding MBean name
255
ManagedBean managed = registry.findManagedBean("ContextResource");
256         ObjectName JavaDoc oname =
257             MBeanUtils.createObjectName(managed.getDomain(), resource);
258         return (oname.toString());
259     }
260
261     
262     /**
263      * Add a resource link for this web application.
264      *
265      * @param resourceLinkName New resource link name
266      */

267     public String JavaDoc addResourceLink(String JavaDoc resourceLinkName, String JavaDoc global,
268                 String JavaDoc name, String JavaDoc type) throws MalformedObjectNameException JavaDoc {
269         
270         NamingResources nresources = getNamingResources();
271         if (nresources == null) {
272             return null;
273         }
274         ContextResourceLink resourceLink =
275                                 nresources.findResourceLink(resourceLinkName);
276         if (resourceLink != null) {
277             throw new IllegalArgumentException JavaDoc
278                 ("Invalid resource link name - already exists'" +
279                                                         resourceLinkName + "'");
280         }
281         resourceLink = new ContextResourceLink();
282         resourceLink.setGlobal(global);
283         resourceLink.setName(resourceLinkName);
284         resourceLink.setType(type);
285         nresources.addResourceLink(resourceLink);
286         
287         // Return the corresponding MBean name
288
ManagedBean managed = registry.findManagedBean("ContextResourceLink");
289         ObjectName JavaDoc oname =
290             MBeanUtils.createObjectName(managed.getDomain(), resourceLink);
291         return (oname.toString());
292     }
293     
294     
295     /**
296      * Remove any environment entry with the specified name.
297      *
298      * @param envName Name of the environment entry to remove
299      */

300     public void removeEnvironment(String JavaDoc envName) {
301
302         NamingResources nresources = getNamingResources();
303         if (nresources == null) {
304             return;
305         }
306         ContextEnvironment env = nresources.findEnvironment(envName);
307         if (env == null) {
308             throw new IllegalArgumentException JavaDoc
309                 ("Invalid environment name '" + envName + "'");
310         }
311         nresources.removeEnvironment(envName);
312
313     }
314     
315     
316     /**
317      * Remove any resource reference with the specified name.
318      *
319      * @param resourceName Name of the resource reference to remove
320      */

321     public void removeResource(String JavaDoc resourceName) {
322
323         resourceName = ObjectName.unquote(resourceName);
324         NamingResources nresources = getNamingResources();
325         if (nresources == null) {
326             return;
327         }
328         ContextResource resource = nresources.findResource(resourceName);
329         if (resource == null) {
330             throw new IllegalArgumentException JavaDoc
331                 ("Invalid resource name '" + resourceName + "'");
332         }
333         nresources.removeResource(resourceName);
334     }
335     
336     
337     /**
338      * Remove any resource link with the specified name.
339      *
340      * @param resourceLinkName Name of the resource reference to remove
341      */

342     public void removeResourceLink(String JavaDoc resourceLinkName) {
343
344         resourceLinkName = ObjectName.unquote(resourceLinkName);
345         NamingResources nresources = getNamingResources();
346         if (nresources == null) {
347             return;
348         }
349         ContextResourceLink resource = nresources.findResourceLink(resourceLinkName);
350         if (resource == null) {
351             throw new IllegalArgumentException JavaDoc
352                 ("Invalid resource name '" + resourceLinkName + "'");
353         }
354         nresources.removeResourceLink(resourceLinkName);
355     }
356  
357     
358 }
359
Popular Tags