KickJava   Java API By Example, From Geeks To Geeks.

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


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.Context;
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.StandardDefaultContext</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 DefaultContextMBean 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 DefaultContextMBean()
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("DefaultContext");
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 ((Context)this.resource).getNamingResources();
91     
92     }
93     
94     
95     /**
96      * Return the MBean Names of the set of defined environment entries for
97      * this web application
98      */

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

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

148     public String JavaDoc[] getResourceLinks() {
149         
150         ContextResourceLink[] links = getNamingResources().findResourceLinks();
151         ArrayList JavaDoc results = new ArrayList JavaDoc();
152         for (int i = 0; i < links.length; i++) {
153             try {
154                 ObjectName JavaDoc oname =
155                     MBeanUtils.createObjectName(managed.getDomain(), links[i]);
156                 results.add(oname.toString());
157             } catch (MalformedObjectNameException JavaDoc e) {
158                 IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc
159                     ("Cannot create object name for resource " + links[i]);
160                 iae.initCause(e);
161                 throw iae;
162             }
163         }
164         return ((String JavaDoc[]) results.toArray(new String JavaDoc[results.size()]));
165
166     }
167
168     // ------------------------------------------------------------- Operations
169

170
171     /**
172      * Add an environment entry for this web application.
173      *
174      * @param envName New environment entry name
175      */

176     public String JavaDoc addEnvironment(String JavaDoc envName, String JavaDoc type)
177         throws MalformedObjectNameException JavaDoc {
178
179         NamingResources nresources = getNamingResources();
180         if (nresources == null) {
181             return null;
182         }
183         ContextEnvironment env = nresources.findEnvironment(envName);
184         if (env != null) {
185             throw new IllegalArgumentException JavaDoc
186                 ("Invalid environment name - already exists '" + envName + "'");
187         }
188         env = new ContextEnvironment();
189         env.setName(envName);
190         env.setType(type);
191         nresources.addEnvironment(env);
192         
193         // Return the corresponding MBean name
194
ManagedBean managed = registry.findManagedBean("ContextEnvironment");
195         ObjectName JavaDoc oname =
196             MBeanUtils.createObjectName(managed.getDomain(), env);
197         return (oname.toString());
198         
199     }
200
201     
202     /**
203      * Add a resource reference for this web application.
204      *
205      * @param resourceName New resource reference name
206      */

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

238     public String JavaDoc addResourceLink(String JavaDoc resourceLinkName, String JavaDoc global,
239                 String JavaDoc name, String JavaDoc type) throws MalformedObjectNameException JavaDoc {
240         
241         NamingResources nresources = getNamingResources();
242         if (nresources == null) {
243             return null;
244         }
245         ContextResourceLink resourceLink =
246                                 nresources.findResourceLink(resourceLinkName);
247         if (resourceLink != null) {
248             throw new IllegalArgumentException JavaDoc
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.findManagedBean("ContextResourceLink");
260         ObjectName JavaDoc oname =
261             MBeanUtils.createObjectName(managed.getDomain(), resourceLink);
262         return (oname.toString());
263     }
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 JavaDoc 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 JavaDoc
280                 ("Invalid environment name '" + envName + "'");
281         }
282         nresources.removeEnvironment(envName);
283
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 JavaDoc resourceName) {
293
294         resourceName = ObjectName.unquote(resourceName);
295         NamingResources nresources = getNamingResources();
296         if (nresources == null) {
297             return;
298         }
299         ContextResource resource = nresources.findResource(resourceName);
300         if (resource == null) {
301             throw new IllegalArgumentException JavaDoc
302                 ("Invalid resource name '" + resourceName + "'");
303         }
304         nresources.removeResource(resourceName);
305     }
306     
307     
308     /**
309      * Remove any resource link with the specified name.
310      *
311      * @param resourceLinkName Name of the resource reference to remove
312      */

313     public void removeResourceLink(String JavaDoc resourceLinkName) {
314
315         resourceLinkName = ObjectName.unquote(resourceLinkName);
316         NamingResources nresources = getNamingResources();
317         if (nresources == null) {
318             return;
319         }
320         ContextResourceLink resource = nresources.findResourceLink(resourceLinkName);
321         if (resource == null) {
322             throw new IllegalArgumentException JavaDoc
323                 ("Invalid resource name '" + resourceLinkName + "'");
324         }
325         nresources.removeResourceLink(resourceLinkName);
326     }
327  
328     
329 }
330
Popular Tags