KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > admin > resources > ResourceUtils


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

16
17
18 package org.apache.webapp.admin.resources;
19
20 import java.util.Arrays JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Collections JavaDoc;
24
25 import javax.management.Attribute JavaDoc;
26 import javax.management.AttributeNotFoundException JavaDoc;
27 import javax.management.MBeanServer JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29 import javax.management.ObjectInstance JavaDoc;
30
31
32 /**
33  * <p>Shared utility methods for the resource administration module.</p>
34  *
35  * @author Manveen Kaur
36  * @author Amy Roh
37  * @version $Revision: 1.11 $ $Date: 2004/04/30 17:53:59 $
38  * @since 4.1
39  */

40
41 public class ResourceUtils {
42
43     public final static String JavaDoc ENVIRONMENT_TYPE = ":type=Environment";
44     public final static String JavaDoc RESOURCE_TYPE = ":type=Resource";
45     public final static String JavaDoc RESOURCELINK_TYPE = ":type=ResourceLink";
46     public final static String JavaDoc NAMINGRESOURCES_TYPE = ":type=NamingResources";
47     public final static String JavaDoc GLOBAL_TYPE = ",resourcetype=Global";
48     public final static String JavaDoc CONTEXT_TYPE = ",resourcetype=Context";
49     public final static String JavaDoc SERVICE_DEFAULTCONTEXT_TYPE =
50                                                 ",resourcetype=ServiceDefaultContext";
51     public final static String JavaDoc HOST_DEFAULTCONTEXT_TYPE =
52                                                 ",resourcetype=HostDefaultContext";
53     
54     // resource class names
55
public final static String JavaDoc USERDB_CLASS = "org.apache.catalina.UserDatabase";
56     public final static String JavaDoc DATASOURCE_CLASS = "javax.sql.DataSource";
57     public final static String JavaDoc MAILSESSION_CLASS = "javax.mail.Session";
58
59     // --------------------------------------------------------- Public Methods
60

61
62     public static ObjectName JavaDoc getNamingResourceObjectName(String JavaDoc domain,
63             String JavaDoc resourcetype, String JavaDoc path, String JavaDoc host) throws Exception JavaDoc {
64
65         ObjectName JavaDoc oname = null;
66         if ((resourcetype==null) || (domain==null)) {
67             return null;
68         }
69         // Construct the MBean Name for the naming source
70
if (resourcetype.equals("Global")) {
71             oname = new ObjectName JavaDoc(domain + NAMINGRESOURCES_TYPE +
72                                 GLOBAL_TYPE);
73         } else if (resourcetype.equals("Context")) {
74             oname = new ObjectName JavaDoc(domain + NAMINGRESOURCES_TYPE +
75                                 CONTEXT_TYPE +
76                                 ",path=" + path + ",host=" + host);
77         } else if (resourcetype.equals("DefaultContext")) {
78             if (host.length() > 0) {
79                 oname = new ObjectName JavaDoc(domain + NAMINGRESOURCES_TYPE +
80                                         HOST_DEFAULTCONTEXT_TYPE +
81                                         ",host=" + host);
82             } else {
83                 oname = new ObjectName JavaDoc(domain + NAMINGRESOURCES_TYPE +
84                                         SERVICE_DEFAULTCONTEXT_TYPE);
85             }
86         }
87         
88         return oname;
89         
90     }
91     
92     /**
93      * Construct and return a ResourcesForm identifying all currently defined
94      * resources in the specified resource database.
95      *
96      * @param mserver MBeanServer to be consulted
97      *
98      * @exception Exception if an error occurs
99      */

100     public static EnvEntriesForm getEnvEntriesForm(MBeanServer JavaDoc mserver,
101         String JavaDoc resourcetype, String JavaDoc path, String JavaDoc host, String JavaDoc domain)
102         throws Exception JavaDoc {
103                            
104         ObjectName JavaDoc ename = null;
105         StringBuffer JavaDoc sb = null;
106         if (resourcetype!=null) {
107             if (resourcetype.equals("Global")) {
108                 ename = new ObjectName JavaDoc( domain + ENVIRONMENT_TYPE +
109                                         GLOBAL_TYPE + ",*");
110             } else if (resourcetype.equals("Context")) {
111                 ename = new ObjectName JavaDoc ( domain + ENVIRONMENT_TYPE +
112                                         CONTEXT_TYPE + ",path=" + path +
113                                         ",host=" + host + ",*");
114             } else if (resourcetype.equals("DefaultContext")) {
115                 if (host.length() > 0) {
116                     ename = new ObjectName JavaDoc( domain + ENVIRONMENT_TYPE +
117                         HOST_DEFAULTCONTEXT_TYPE + ",host=" + host + ",*");
118                 } else {
119                     ename = new ObjectName JavaDoc( domain + ENVIRONMENT_TYPE +
120                         SERVICE_DEFAULTCONTEXT_TYPE + ",*");
121                 }
122             }
123         }
124         
125         Iterator JavaDoc iterator = (mserver.queryMBeans(ename, null).iterator());
126         
127         ArrayList JavaDoc results = new ArrayList JavaDoc();
128         while (iterator.hasNext()) {
129             ObjectInstance JavaDoc instance = (ObjectInstance JavaDoc) iterator.next();
130             results.add(instance.getObjectName().toString());
131         }
132
133         Collections.sort(results);
134         
135         EnvEntriesForm envEntriesForm = new EnvEntriesForm();
136         envEntriesForm.setEnvEntries((String JavaDoc[])
137                         results.toArray(new String JavaDoc[results.size()]));
138         
139         if (resourcetype != null) {
140             envEntriesForm.setResourcetype(resourcetype);
141         } else {
142             envEntriesForm.setResourcetype("");
143         }
144          if (path != null) {
145             envEntriesForm.setPath(path);
146         } else {
147             envEntriesForm.setPath("");
148         }
149         if (host != null) {
150             envEntriesForm.setHost(host);
151         } else {
152             envEntriesForm.setHost("");
153         }
154         if (domain != null) {
155             envEntriesForm.setDomain(domain);
156         } else {
157             envEntriesForm.setDomain("");
158         }
159         
160         return (envEntriesForm);
161
162     }
163
164     /**
165      * Construct and return a DataSourcesForm identifying all currently defined
166      * datasources in the specified resource database.
167      *
168      * @param mserver MBeanServer to be consulted
169      *
170      * @exception Exception if an error occurs
171      */

172     public static DataSourcesForm getDataSourcesForm(MBeanServer JavaDoc mserver,
173         String JavaDoc resourcetype, String JavaDoc path, String JavaDoc host, String JavaDoc domain)
174         throws Exception JavaDoc {
175                             
176         ObjectName JavaDoc rname = null;
177         if (resourcetype!=null) {
178             if (resourcetype.equals("Global")) {
179                 rname = new ObjectName JavaDoc( domain + RESOURCE_TYPE + GLOBAL_TYPE +
180                                         ",class=" + DATASOURCE_CLASS + ",*");
181             } else if (resourcetype.equals("Context")) {
182                 rname = new ObjectName JavaDoc ( domain + RESOURCE_TYPE + CONTEXT_TYPE +
183                     ",path=" + path + ",host=" + host + ",class=" +
184                     DATASOURCE_CLASS + ",*");
185             } else if (resourcetype.equals("DefaultContext")) {
186                 if (host.length() > 0) {
187                     rname = new ObjectName JavaDoc( domain + RESOURCE_TYPE +
188                         HOST_DEFAULTCONTEXT_TYPE + ",host=" + host +
189                         ",class=" + DATASOURCE_CLASS + ",*");
190                 } else {
191                     rname = new ObjectName JavaDoc( domain + RESOURCE_TYPE +
192                         SERVICE_DEFAULTCONTEXT_TYPE +
193                         ",class=" + DATASOURCE_CLASS + ",*");
194                 }
195             }
196         }
197         Iterator JavaDoc iterator = (mserver.queryMBeans(rname, null).iterator());
198         
199         ArrayList JavaDoc results = new ArrayList JavaDoc();
200         while (iterator.hasNext()) {
201             
202             ObjectInstance JavaDoc instance = (ObjectInstance JavaDoc) iterator.next();
203             ObjectName JavaDoc oname = instance.getObjectName();
204             try {
205                 // only add resource mbean if definition exists
206
mserver.getAttribute(oname, "driverClassName");
207                 results.add(oname.toString());
208             } catch (AttributeNotFoundException JavaDoc ex) {
209                 mserver.setAttribute(oname,
210                     new Attribute JavaDoc("driverClassName", ""));
211             }
212         }
213
214         Collections.sort(results);
215         DataSourcesForm dataSourcesForm = new DataSourcesForm();
216         dataSourcesForm.setDataSources((String JavaDoc[])
217                         results.toArray(new String JavaDoc[results.size()]));
218         
219         if (resourcetype != null) {
220             dataSourcesForm.setResourcetype(resourcetype);
221         } else {
222             dataSourcesForm.setResourcetype("");
223         }
224          if (path != null) {
225             dataSourcesForm.setPath(path);
226         } else {
227             dataSourcesForm.setPath("");
228         }
229         if (host != null) {
230             dataSourcesForm.setHost(host);
231         } else {
232             dataSourcesForm.setHost("");
233         }
234         if (domain != null) {
235             dataSourcesForm.setDomain(domain);
236         } else {
237             dataSourcesForm.setDomain("");
238         }
239         return (dataSourcesForm);
240
241     }
242     
243     /**
244      * Construct and return a MailSessionsForm identifying all currently defined
245      * mailsessions in the specified resource database.
246      *
247      * @param mserver MBeanServer to be consulted
248      *
249      * @exception Exception if an error occurs
250      */

251     public static MailSessionsForm getMailSessionsForm(MBeanServer JavaDoc mserver,
252         String JavaDoc resourcetype, String JavaDoc path, String JavaDoc host, String JavaDoc domain)
253         throws Exception JavaDoc {
254                             
255         ObjectName JavaDoc rname = null;
256         if (resourcetype!=null) {
257             if (resourcetype.equals("Global")) {
258                 rname = new ObjectName JavaDoc( domain + RESOURCE_TYPE + GLOBAL_TYPE +
259                                         ",class=" + MAILSESSION_CLASS + ",*");
260             } else if (resourcetype.equals("Context")) {
261                 rname = new ObjectName JavaDoc (domain + RESOURCE_TYPE + CONTEXT_TYPE +
262                     ",path=" + path + ",host=" + host + ",class=" +
263                     MAILSESSION_CLASS + ",*");
264             } else if (resourcetype.equals("DefaultContext")) {
265                 if (host.length() > 0) {
266                     rname = new ObjectName JavaDoc(domain + RESOURCE_TYPE +
267                         HOST_DEFAULTCONTEXT_TYPE + ",host=" + host +
268                         ",class=" + MAILSESSION_CLASS + ",*");
269                 } else {
270                     rname = new ObjectName JavaDoc(domain + RESOURCE_TYPE +
271                         SERVICE_DEFAULTCONTEXT_TYPE +
272                         ",class=" + MAILSESSION_CLASS + ",*");
273                 }
274             }
275         }
276        
277         Iterator JavaDoc iterator = (mserver.queryMBeans(rname, null).iterator());
278         
279         ArrayList JavaDoc results = new ArrayList JavaDoc();
280         while (iterator.hasNext()) {
281             
282             ObjectInstance JavaDoc instance = (ObjectInstance JavaDoc) iterator.next();
283             results.add(instance.getObjectName().toString());
284         }
285
286         Collections.sort(results);
287         MailSessionsForm mailSessionsForm = new MailSessionsForm();
288         mailSessionsForm.setMailSessions((String JavaDoc[])
289                         results.toArray(new String JavaDoc[results.size()]));
290         
291         if (resourcetype != null) {
292             mailSessionsForm.setResourcetype(resourcetype);
293         } else {
294             mailSessionsForm.setResourcetype("");
295         }
296          if (path != null) {
297             mailSessionsForm.setPath(path);
298         } else {
299             mailSessionsForm.setPath("");
300         }
301         if (host != null) {
302             mailSessionsForm.setHost(host);
303         } else {
304             mailSessionsForm.setHost("");
305         }
306         if (domain != null) {
307             mailSessionsForm.setDomain(domain);
308         } else {
309             mailSessionsForm.setDomain("");
310         }
311         
312         return (mailSessionsForm);
313
314     }
315     
316     /**
317      * Construct and return a ResourceLinksForm identifying all currently defined
318      * resourcelinks in the specified resource database.
319      *
320      * @param mserver MBeanServer to be consulted
321      *
322      * @exception Exception if an error occurs
323      */

324     public static ResourceLinksForm getResourceLinksForm(MBeanServer JavaDoc mserver,
325         String JavaDoc resourcetype, String JavaDoc path, String JavaDoc host, String JavaDoc domain)
326         throws Exception JavaDoc {
327
328         ObjectName JavaDoc rname = null;
329         if (resourcetype!=null) {
330             if (resourcetype.equals("Global")) {
331                 rname = new ObjectName JavaDoc( domain + RESOURCELINK_TYPE +
332                                         GLOBAL_TYPE + ",*");
333             } else if (resourcetype.equals("Context")) {
334                 rname = new ObjectName JavaDoc ( domain + RESOURCELINK_TYPE +
335                             CONTEXT_TYPE + ",path=" + path +
336                             ",host=" + host + ",*");
337             } else if (resourcetype.equals("DefaultContext")) {
338                 if (host.length() > 0) {
339                     rname = new ObjectName JavaDoc( domain + RESOURCELINK_TYPE +
340                                 HOST_DEFAULTCONTEXT_TYPE +
341                                 ",host=" + host + ",*");
342                 } else {
343                     rname = new ObjectName JavaDoc( domain + RESOURCELINK_TYPE +
344                                 SERVICE_DEFAULTCONTEXT_TYPE + ",*");
345                 }
346             }
347         }
348        
349         Iterator JavaDoc iterator = (mserver.queryMBeans(rname, null).iterator());
350         
351         ArrayList JavaDoc results = new ArrayList JavaDoc();
352         while (iterator.hasNext()) {
353             ObjectInstance JavaDoc instance = (ObjectInstance JavaDoc) iterator.next();
354             results.add(instance.getObjectName().toString());
355         }
356
357         Collections.sort(results);
358         ResourceLinksForm resourceLinksForm = new ResourceLinksForm();
359         resourceLinksForm.setResourceLinks((String JavaDoc[])
360                         results.toArray(new String JavaDoc[results.size()]));
361         
362         if (resourcetype != null) {
363             resourceLinksForm.setResourcetype(resourcetype);
364         } else {
365             resourceLinksForm.setResourcetype("");
366         }
367          if (path != null) {
368             resourceLinksForm.setPath(path);
369         } else {
370             resourceLinksForm.setPath("");
371         }
372         if (host != null) {
373             resourceLinksForm.setHost(host);
374         } else {
375             resourceLinksForm.setHost("");
376         }
377         if (domain != null) {
378             resourceLinksForm.setDomain(domain);
379         } else {
380             resourceLinksForm.setDomain("");
381         }
382         
383         return (resourceLinksForm);
384         
385     }
386     
387     /**
388      * Construct and return a UserDatabaseForm identifying all currently defined
389      * user databases in the specified resource database.
390      *
391      * @param mserver MBeanServer to be consulted
392      *
393      * @exception Exception if an error occurs
394      */

395     public static UserDatabasesForm getUserDatabasesForm(MBeanServer JavaDoc mserver,String JavaDoc domain)
396         throws Exception JavaDoc {
397             
398         ObjectName JavaDoc rname = new ObjectName JavaDoc( domain + RESOURCE_TYPE + GLOBAL_TYPE +
399                             ",class=" + USERDB_CLASS + ",*");
400         
401         Iterator JavaDoc iterator = (mserver.queryMBeans(rname, null).iterator());
402         
403         ArrayList JavaDoc results = new ArrayList JavaDoc();
404         while (iterator.hasNext()) {
405             ObjectInstance JavaDoc instance = (ObjectInstance JavaDoc) iterator.next();
406             results.add(instance.getObjectName().toString());
407         }
408
409         Collections.sort(results);
410
411         UserDatabasesForm userDatabasesForm = new UserDatabasesForm();
412         userDatabasesForm.setUserDatabases((String JavaDoc[])
413                         results.toArray(new String JavaDoc[results.size()]));
414         return (userDatabasesForm);
415
416     }
417     
418 }
419
Popular Tags