KickJava   Java API By Example, From Geeks To Geeks.

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


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.deploy.ContextEnvironment;
28 import org.apache.catalina.deploy.ContextResource;
29 import org.apache.catalina.deploy.ContextResourceLink;
30 import org.apache.catalina.deploy.NamingResources;
31 import org.apache.tomcat.util.modeler.BaseModelMBean;
32 import org.apache.tomcat.util.modeler.ManagedBean;
33 import org.apache.tomcat.util.modeler.Registry;
34
35 /**
36  * <p>A <strong>ModelMBean</strong> implementation for the
37  * <code>org.apache.catalina.deploy.NamingResources</code> component.</p>
38  *
39  * @author Amy Roh
40  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
41  */

42
43 public class NamingResourcesMBean extends BaseModelMBean {
44
45
46     // ----------------------------------------------------------- Constructors
47

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

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

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

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

77     protected ManagedBean managed =
78         registry.findManagedBean("NamingResources");
79
80     // ------------------------------------------------------------- Attributes
81

82
83     /**
84      * Return the MBean Names of the set of defined environment entries for
85      * this web application
86      */

87     public String JavaDoc[] getEnvironments() {
88         ContextEnvironment[] envs =
89                             ((NamingResources)this.resource).findEnvironments();
90         ArrayList JavaDoc results = new ArrayList JavaDoc();
91         for (int i = 0; i < envs.length; i++) {
92             try {
93                 ObjectName JavaDoc oname =
94                     MBeanUtils.createObjectName(managed.getDomain(), envs[i]);
95                 results.add(oname.toString());
96             } catch (MalformedObjectNameException JavaDoc e) {
97                 IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc
98                     ("Cannot create object name for environment " + envs[i]);
99                 iae.initCause(e);
100                 throw iae;
101             }
102         }
103         return ((String JavaDoc[]) results.toArray(new String JavaDoc[results.size()]));
104
105     }
106     
107     
108     /**
109      * Return the MBean Names of all the defined resource references for this
110      * application.
111      */

112     public String JavaDoc[] getResources() {
113         
114         ContextResource[] resources =
115                             ((NamingResources)this.resource).findResources();
116         ArrayList JavaDoc results = new ArrayList JavaDoc();
117         for (int i = 0; i < resources.length; i++) {
118             try {
119                 ObjectName JavaDoc oname =
120                     MBeanUtils.createObjectName(managed.getDomain(), resources[i]);
121                 results.add(oname.toString());
122             } catch (MalformedObjectNameException JavaDoc e) {
123                 IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc
124                     ("Cannot create object name for resource " + resources[i]);
125                 iae.initCause(e);
126                 throw iae;
127             }
128         }
129         return ((String JavaDoc[]) results.toArray(new String JavaDoc[results.size()]));
130
131     }
132     
133     
134     /**
135      * Return the MBean Names of all the defined resource link references for
136      * this application.
137      */

138     public String JavaDoc[] getResourceLinks() {
139         
140         ContextResourceLink[] resourceLinks =
141                             ((NamingResources)this.resource).findResourceLinks();
142         ArrayList JavaDoc results = new ArrayList JavaDoc();
143         for (int i = 0; i < resourceLinks.length; i++) {
144             try {
145                 ObjectName JavaDoc oname =
146                     MBeanUtils.createObjectName(managed.getDomain(), resourceLinks[i]);
147                 results.add(oname.toString());
148             } catch (MalformedObjectNameException JavaDoc e) {
149                 IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc
150                     ("Cannot create object name for resource " + resourceLinks[i]);
151                 iae.initCause(e);
152                 throw iae;
153             }
154         }
155         return ((String JavaDoc[]) results.toArray(new String JavaDoc[results.size()]));
156
157     }
158
159     // ------------------------------------------------------------- Operations
160

161
162     /**
163      * Add an environment entry for this web application.
164      *
165      * @param envName New environment entry name
166      * @param type The type of the new environment entry
167      * @param value The value of the new environment entry
168      */

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

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

233     public String JavaDoc addResourceLink(String JavaDoc resourceLinkName, String JavaDoc type)
234         throws MalformedObjectNameException JavaDoc {
235         
236         NamingResources nresources = (NamingResources) this.resource;
237         if (nresources == null) {
238             return null;
239         }
240         ContextResourceLink resourceLink =
241                             nresources.findResourceLink(resourceLinkName);
242         if (resourceLink != null) {
243             throw new IllegalArgumentException JavaDoc
244                 ("Invalid resource link name - already exists'" +
245                 resourceLinkName + "'");
246         }
247         resourceLink = new ContextResourceLink();
248         resourceLink.setName(resourceLinkName);
249         resourceLink.setType(type);
250         nresources.addResourceLink(resourceLink);
251         
252         // Return the corresponding MBean name
253
ManagedBean managed = registry.findManagedBean("ContextResourceLink");
254         ObjectName JavaDoc oname =
255             MBeanUtils.createObjectName(managed.getDomain(), resourceLink);
256         return (oname.toString());
257     }
258     
259     
260     /**
261      * Remove any environment entry with the specified name.
262      *
263      * @param envName Name of the environment entry to remove
264      */

265     public void removeEnvironment(String JavaDoc envName) {
266
267         NamingResources nresources = (NamingResources) this.resource;
268         if (nresources == null) {
269             return;
270         }
271         ContextEnvironment env = nresources.findEnvironment(envName);
272         if (env == null) {
273             throw new IllegalArgumentException JavaDoc
274                 ("Invalid environment name '" + envName + "'");
275         }
276         nresources.removeEnvironment(envName);
277
278     }
279     
280     
281     /**
282      * Remove any resource reference with the specified name.
283      *
284      * @param resourceName Name of the resource reference to remove
285      */

286     public void removeResource(String JavaDoc resourceName) {
287
288         resourceName = ObjectName.unquote(resourceName);
289         NamingResources nresources = (NamingResources) this.resource;
290         if (nresources == null) {
291             return;
292         }
293         ContextResource resource = nresources.findResource(resourceName);
294         if (resource == null) {
295             throw new IllegalArgumentException JavaDoc
296                 ("Invalid resource name '" + resourceName + "'");
297         }
298         nresources.removeResource(resourceName);
299     
300     }
301     
302     
303     /**
304      * Remove any resource link reference with the specified name.
305      *
306      * @param resourceLinkName Name of the resource link reference to remove
307      */

308     public void removeResourceLink(String JavaDoc resourceLinkName) {
309
310         resourceLinkName = ObjectName.unquote(resourceLinkName);
311         NamingResources nresources = (NamingResources) this.resource;
312         if (nresources == null) {
313             return;
314         }
315         ContextResourceLink resourceLink =
316                             nresources.findResourceLink(resourceLinkName);
317         if (resourceLink == null) {
318             throw new IllegalArgumentException JavaDoc
319                 ("Invalid resource Link name '" + resourceLinkName + "'");
320         }
321         nresources.removeResourceLink(resourceLinkName);
322     }
323
324 }
325
Popular Tags