KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > admin > Lists


1 /*
2  * Copyright 2001-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 package org.apache.webapp.admin;
18
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.management.MBeanServer JavaDoc;
26 import javax.management.ObjectName JavaDoc;
27
28
29 /**
30  * General purpose utility methods to create lists of objects that are
31  * commonly required in building the user interface. In all cases, if there
32  * are no matching elements, a zero-length list (rather than <code>null</code>)
33  * is returned.
34  *
35  * @author Craig R. McClanahan
36  * @author Amy Roh
37  * @version $Revision: 1.14 $ $Date: 2004/06/28 02:14:50 $
38  */

39
40 public class Lists {
41
42
43     // ----------------------------------------------------------- Constructors
44

45
46     /**
47      * Protected constructor to prevent instantiation.
48      */

49     protected Lists() { }
50
51
52     // ------------------------------------------------------- Static Variables
53

54
55     /**
56      * Precomputed list of verbosity level labels and values.
57      */

58     private static List JavaDoc verbosityLevels = new ArrayList JavaDoc();
59
60     static {
61         verbosityLevels.add(new LabelValueBean("0", "0"));
62         verbosityLevels.add(new LabelValueBean("1", "1"));
63         verbosityLevels.add(new LabelValueBean("2", "2"));
64         verbosityLevels.add(new LabelValueBean("3", "3"));
65         verbosityLevels.add(new LabelValueBean("4", "4"));
66     }
67
68     /**
69      * Precomputed list of (true,false) labels and values.
70      */

71     private static List JavaDoc booleanValues = new ArrayList JavaDoc();
72
73     static {
74             booleanValues.add(new LabelValueBean("True", "true"));
75             booleanValues.add(new LabelValueBean("False", "false"));
76     }
77
78     /**
79      * Precomputed list of clientAuth lables and values.
80      */

81     private static List JavaDoc clientAuthValues = new ArrayList JavaDoc();
82
83     static {
84             clientAuthValues.add(new LabelValueBean("True","true"));
85             clientAuthValues.add(new LabelValueBean("False","false"));
86             clientAuthValues.add(new LabelValueBean("Want","want"));
87     }
88
89     // --------------------------------------------------------- Public Methods
90

91
92     /**
93      * Return a <code>List</code> of {@link LabelValueBean}s for the legal
94      * settings for <code>verbosity</code> properties.
95      */

96     public static List JavaDoc getVerbosityLevels() {
97
98         return (verbosityLevels);
99
100     }
101
102     /**
103      * Return a <code>List</code> of {@link LabelValueBean}s for the legal
104      * settings for <code>boolean</code> properties.
105      */

106     public static List JavaDoc getBooleanValues() {
107
108         return (booleanValues);
109
110     }
111     /**
112      * Return a <code>List</code> of {@link LabelValueBean}s for the legal
113      * settings for <code>clientAuth</code> properties.
114      */

115     public static List JavaDoc getClientAuthValues() {
116
117         return (clientAuthValues);
118
119     }
120
121     /**
122      * Return a list of <code>Connector</code> object name strings
123      * for the specified <code>Service</code> object name.
124      *
125      * @param mbserver MBeanServer from which to retrieve the list
126      * @param service Object name of the service for which to select connectors
127      *
128      * @exception Exception if thrown while retrieving the list
129      */

130     public static List JavaDoc getConnectors(MBeanServer JavaDoc mbserver, ObjectName JavaDoc service)
131         throws Exception JavaDoc {
132
133         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(service.getDomain());
134         sb.append(":type=Connector,*");
135         ObjectName JavaDoc search = new ObjectName JavaDoc(sb.toString());
136         ArrayList JavaDoc connectors = new ArrayList JavaDoc();
137         Iterator JavaDoc names = mbserver.queryNames(search, null).iterator();
138         while (names.hasNext()) {
139             connectors.add(names.next().toString());
140         }
141         Collections.sort(connectors);
142         return (connectors);
143
144     }
145
146
147     /**
148      * Return a list of <code>Connector</code> object name strings
149      * for the specified <code>Service</code> object name.
150      *
151      * @param mbserver MBeanServer from which to retrieve the list
152      * @param service Object name of the service for which to select connectors
153      *
154      * @exception Exception if thrown while retrieving the list
155      */

156     public static List JavaDoc getConnectors(MBeanServer JavaDoc mbserver, String JavaDoc service)
157         throws Exception JavaDoc {
158
159         return (getConnectors(mbserver, new ObjectName JavaDoc(service)));
160
161     }
162
163
164     /**
165      * Return a list of <code>Context</code> object name strings
166      * for the specified <code>Host</code> object name.
167      *
168      * @param mbserver MBeanServer from which to retrieve the list
169      * @param host Object name of the host for which to select contexts
170      *
171      * @exception Exception if thrown while retrieving the list
172      */

173     public static List JavaDoc getContexts(MBeanServer JavaDoc mbserver, ObjectName JavaDoc host)
174         throws Exception JavaDoc {
175
176         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(host.getDomain());
177         sb.append(":j2eeType=WebModule,*");
178         ObjectName JavaDoc search = new ObjectName JavaDoc(sb.toString());
179         ArrayList JavaDoc contexts = new ArrayList JavaDoc();
180         Iterator JavaDoc names = mbserver.queryNames(search, null).iterator();
181         String JavaDoc name = null;
182         ObjectName JavaDoc oname = null;
183         String JavaDoc hostPrefix = "//"+host.getKeyProperty("host");
184         String JavaDoc hostAttr = null;
185         while (names.hasNext()) {
186             name = names.next().toString();
187             oname = new ObjectName JavaDoc(name);
188             hostAttr = oname.getKeyProperty("name");
189             if (hostAttr.startsWith(hostPrefix)) {
190                 contexts.add(name);
191             }
192         }
193         Collections.sort(contexts);
194         return (contexts);
195
196     }
197
198
199     /**
200      * Return a list of <code>DefaultContext</code> object name strings
201      * for the specified <code>Host</code> object name.
202      *
203      * @param mbserver MBeanServer from which to retrieve the list
204      * @param container Object name of the container for which to select default
205      * contexts
206      * @param containerType The type of the container for which to select
207      * default contexts
208      *
209      * @exception Exception if thrown while retrieving the list
210      */

211     public static List JavaDoc getDefaultContexts(MBeanServer JavaDoc mbserver, String JavaDoc
212         container) throws Exception JavaDoc {
213
214         return (getDefaultContexts(mbserver, new ObjectName JavaDoc(container)));
215
216     }
217     
218     
219     /**
220      * Return a list of <code>DefaultContext</code> object name strings
221      * for the specified <code>Host</code> object name.
222      *
223      * @param mbserver MBeanServer from which to retrieve the list
224      * @param container Object name of the container for which to select default
225      * contexts
226      * @param containerType The type of the container for which to select
227      * default contexts
228      *
229      * @exception Exception if thrown while retrieving the list
230      */

231     public static List JavaDoc getDefaultContexts(MBeanServer JavaDoc mbserver, ObjectName JavaDoc
232         container) throws Exception JavaDoc {
233
234         // FIXME
235
StringBuffer JavaDoc sb = new StringBuffer JavaDoc(container.getDomain());
236         sb.append(":type=DefaultContext");
237         String JavaDoc type = container.getKeyProperty("type");
238         String JavaDoc host = container.getKeyProperty("host");
239         if ("Host".equals(type)) {
240             host = container.getKeyProperty("host");
241         }
242         if (host != null) {
243             sb.append(",host=");
244             sb.append(host);
245         }
246         ObjectName JavaDoc search = new ObjectName JavaDoc(sb.toString());
247         ArrayList JavaDoc defaultContexts = new ArrayList JavaDoc();
248         Iterator JavaDoc names = mbserver.queryNames(search, null).iterator();
249         while (names.hasNext()) {
250             String JavaDoc name = names.next().toString();
251             defaultContexts.add(name);
252         }
253         Collections.sort(defaultContexts);
254         return (defaultContexts);
255
256     }
257
258
259     /**
260      * Return a list of <code>Context</code> object name strings
261      * for the specified <code>Host</code> object name.
262      *
263      * @param mbserver MBeanServer from which to retrieve the list
264      * @param host Object name of the host for which to select contexts
265      *
266      * @exception Exception if thrown while retrieving the list
267      */

268     public static List JavaDoc getContexts(MBeanServer JavaDoc mbserver, String JavaDoc host)
269         throws Exception JavaDoc {
270
271         return (getContexts(mbserver, new ObjectName JavaDoc(host)));
272
273     }
274
275     /**
276      * Return a list of <code>Host</code> object name strings
277      * for the specified <code>Service</code> object name.
278      *
279      * @param mbserver MBeanServer from which to retrieve the list
280      * @param service Object name of the service for which to select hosts
281      *
282      * @exception Exception if thrown while retrieving the list
283      */

284     public static List JavaDoc getHosts(MBeanServer JavaDoc mbserver, ObjectName JavaDoc service)
285         throws Exception JavaDoc {
286
287         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(service.getDomain());
288         sb.append(":type=Host,*");
289         ObjectName JavaDoc search = new ObjectName JavaDoc(sb.toString());
290         ArrayList JavaDoc hosts = new ArrayList JavaDoc();
291         Iterator JavaDoc names = mbserver.queryNames(search, null).iterator();
292         while (names.hasNext()) {
293             hosts.add(names.next().toString());
294         }
295         Collections.sort(hosts);
296         return (hosts);
297
298     }
299
300
301     /**
302      * Return a list of <code>Host</code> object name strings
303      * for the specified <code>Service</code> object name.
304      *
305      * @param mbserver MBeanServer from which to retrieve the list
306      * @param service Object name of the service for which to select hosts
307      *
308      * @exception Exception if thrown while retrieving the list
309      */

310     public static List JavaDoc getHosts(MBeanServer JavaDoc mbserver, String JavaDoc service)
311         throws Exception JavaDoc {
312
313         return (getHosts(mbserver, new ObjectName JavaDoc(service)));
314
315     }
316
317
318     /**
319      * Return a list of <code>Realm</code> object name strings
320      * for the specified container (service, host, or context) object name.
321      *
322      * @param mbserver MBeanServer from which to retrieve the list
323      * @param container Object name of the container for which to select
324      * realms
325      *
326      * @exception Exception if thrown while retrieving the list
327      */

328     public static List JavaDoc getRealms(MBeanServer JavaDoc mbserver, ObjectName JavaDoc container)
329         throws Exception JavaDoc {
330
331         ObjectName JavaDoc search = getSearchObject(container, "Realm");
332         ArrayList JavaDoc realms = new ArrayList JavaDoc();
333         Iterator JavaDoc names = mbserver.queryNames(search, null).iterator();
334         while (names.hasNext()) {
335             realms.add(names.next().toString());
336         }
337         Collections.sort(realms);
338         return (realms);
339
340     }
341
342
343     /**
344      * Return a list of <code>Realm</code> object name strings
345      * for the specified container (service, host, or context) object name.
346      *
347      * @param mbserver MBeanServer from which to retrieve the list
348      * @param container Object name of the container for which to select
349      * realms
350      *
351      * @exception Exception if thrown while retrieving the list
352      */

353     public static List JavaDoc getRealms(MBeanServer JavaDoc mbserver, String JavaDoc container)
354         throws Exception JavaDoc {
355
356         return (getRealms(mbserver, new ObjectName JavaDoc(container)));
357
358     }
359
360     /**
361      * Return a list of <code>Valve</code> object name strings
362      * for the specified container (service, host, or context) object name.
363      *
364      * @param mbserver MBeanServer from which to retrieve the list
365      * @param container Object name of the container for which to select
366      * Valves
367      *
368      * @exception Exception if thrown while retrieving the list
369      */

370     public static List JavaDoc getValves(MBeanServer JavaDoc mbserver, ObjectName JavaDoc container)
371         throws Exception JavaDoc {
372
373         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(container.getDomain());
374         sb.append(":type=Valve");
375         String JavaDoc type = container.getKeyProperty("type");
376         String JavaDoc j2eeType = container.getKeyProperty("j2eeType");
377         sb.append(TomcatTreeBuilder.WILDCARD);
378         String JavaDoc host = "";
379         String JavaDoc path = "";
380         String JavaDoc name = container.getKeyProperty("name");
381         if ((name != null) && (name.length() > 0)) {
382             // parent is context
383
name = name.substring(2);
384             int i = name.indexOf("/");
385             host = name.substring(0,i);
386             path = name.substring(i);
387         } else if ("Host".equals(type)) {
388             // parent is host
389
host = container.getKeyProperty("host");
390         }
391         
392         ObjectName JavaDoc search = new ObjectName JavaDoc(sb.toString());
393         ArrayList JavaDoc valves = new ArrayList JavaDoc();
394         Iterator JavaDoc names = mbserver.queryNames(search, null).iterator();
395         while (names.hasNext()) {
396             ObjectName JavaDoc valve = (ObjectName JavaDoc) names.next();
397             String JavaDoc vpath = valve.getKeyProperty("path");
398             String JavaDoc vhost = valve.getKeyProperty("host");
399             
400             String JavaDoc valveType = null;
401             String JavaDoc className = (String JavaDoc)
402                     mbserver.getAttribute(valve, "className");
403             int period = className.lastIndexOf(".");
404             if (period >= 0)
405                 valveType = className.substring(period + 1);
406
407            // Return only user-configurable valves.
408
if ("AccessLogValve".equalsIgnoreCase(valveType) ||
409                "RemoteAddrValve".equalsIgnoreCase(valveType) ||
410                "RemoteHostValve".equalsIgnoreCase(valveType) ||
411                "RequestDumperValve".equalsIgnoreCase(valveType) ||
412                "SingleSignOn".equalsIgnoreCase(valveType)) {
413             // if service is the container, then the valve name
414
// should not contain path or host
415
if ("Service".equalsIgnoreCase(type)) {
416                 if ((vpath == null) && (vhost == null)) {
417                     valves.add(valve.toString());
418                 }
419             }
420             
421             if ("Host".equalsIgnoreCase(type)) {
422                 if ((vpath == null) && (host.equalsIgnoreCase(vhost))) {
423                     valves.add(valve.toString());
424                 }
425             }
426             
427             if ("WebModule".equalsIgnoreCase(j2eeType)) {
428                 if ((path.equalsIgnoreCase(vpath)) && (host.equalsIgnoreCase(vhost))) {
429                     valves.add(valve.toString());
430                 }
431             }
432            }
433         }
434         Collections.sort(valves);
435         return (valves);
436     }
437
438     
439     /**
440      * Return a list of <code>Valve</code> object name strings
441      * for the specified container (service, host, or context) object name.
442      *
443      * @param mbserver MBeanServer from which to retrieve the list
444      * @param container Object name of the container for which to select
445      * valves
446      *
447      * @exception Exception if thrown while retrieving the list
448      */

449     public static List JavaDoc getValves(MBeanServer JavaDoc mbserver, String JavaDoc container)
450         throws Exception JavaDoc {
451
452         return (getValves(mbserver, new ObjectName JavaDoc(container)));
453
454     }
455     
456     /**
457      * Return a list of <code>Server</code> object name strings.
458      *
459      * @param mbserver MBeanServer from which to retrieve the list
460      *
461      * @exception Exception if thrown while retrieving the list
462      */

463     public static List JavaDoc getServers(MBeanServer JavaDoc mbserver, String JavaDoc domain)
464         throws Exception JavaDoc {
465
466         ObjectName JavaDoc search = new ObjectName JavaDoc(domain+":type=Server,*");
467         ArrayList JavaDoc servers = new ArrayList JavaDoc();
468         Iterator JavaDoc names = mbserver.queryNames(search, null).iterator();
469         while (names.hasNext()) {
470             servers.add(names.next().toString());
471         }
472         Collections.sort(servers);
473         return (servers);
474
475     }
476
477
478     /**
479      * Return a list of <code>Service</code> object name strings
480      * for the specified <code>Server</code> object name.
481      *
482      * @param mbserver MBeanServer from which to retrieve the list
483      * @param server Object name of the server for which to select services
484      *
485      * @exception Exception if thrown while retrieving the list
486      */

487     public static List JavaDoc getServices(MBeanServer JavaDoc mbserver, ObjectName JavaDoc server)
488         throws Exception JavaDoc {
489
490         //StringBuffer sb = new StringBuffer(server.getDomain());
491
StringBuffer JavaDoc sb = new StringBuffer JavaDoc("*:type=Service,*");
492         //sb.append(":type=Service,*");
493
ObjectName JavaDoc search = new ObjectName JavaDoc(sb.toString());
494         ArrayList JavaDoc services = new ArrayList JavaDoc();
495         Iterator JavaDoc names = mbserver.queryNames(search, null).iterator();
496         while (names.hasNext()) {
497             services.add(names.next().toString());
498         }
499         Collections.sort(services);
500         return (services);
501
502     }
503
504
505     /**
506      * Return a list of <code>Service</code> object name strings
507      * for the specified <code>Server</code> object name.
508      *
509      * @param mbserver MBeanServer from which to retrieve the list
510      * @param server Object name of the server for which to select services
511      *
512      * @exception Exception if thrown while retrieving the list
513      */

514     public static List JavaDoc getServices(MBeanServer JavaDoc mbserver, String JavaDoc server)
515         throws Exception JavaDoc {
516
517         return (getServices(mbserver, new ObjectName JavaDoc(server)));
518
519     }
520
521
522     /**
523      * Return the <code>Service</code> object name string
524      * that the admin app belongs to.
525      *
526      * @param mbserver MBeanServer from which to retrieve the list
527      * @param request Http request
528      *
529      * @exception Exception if thrown while retrieving the list
530      */

531     public static String JavaDoc getAdminAppService
532         (MBeanServer JavaDoc mbserver, String JavaDoc domain, HttpServletRequest JavaDoc request)
533         throws Exception JavaDoc {
534
535         String JavaDoc adminDomain = TomcatTreeBuilder.DEFAULT_DOMAIN;
536         // Get the admin app's service name
537
StringBuffer JavaDoc sb = new StringBuffer JavaDoc(adminDomain);
538         sb.append(":type=Service,*");
539         ObjectName JavaDoc search = new ObjectName JavaDoc(sb.toString());
540         Iterator JavaDoc names = mbserver.queryNames(search, null).iterator();
541         String JavaDoc service = null;
542         while (names.hasNext()) {
543             service = ((ObjectName JavaDoc)names.next()).getKeyProperty("serviceName");
544         }
545         return service;
546
547     }
548
549
550     /**
551      * Return the <code>Host</code> object name string
552      * that the admin app belongs to.
553      *
554      * @param mbserver MBeanServer from which to retrieve the list
555      * @param request Http request
556      *
557      * @exception Exception if thrown while retrieving the list
558      */

559     public static String JavaDoc getAdminAppHost
560         (MBeanServer JavaDoc mbserver, String JavaDoc domain, HttpServletRequest JavaDoc request)
561         throws Exception JavaDoc {
562         
563         // Get the admin app's host name
564
String JavaDoc adminDomain = TomcatTreeBuilder.DEFAULT_DOMAIN;
565         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(adminDomain);
566         sb.append(":j2eeType=WebModule,*");
567         ObjectName JavaDoc search = new ObjectName JavaDoc(sb.toString());
568         Iterator JavaDoc names = mbserver.queryNames(search, null).iterator();
569         String JavaDoc contextPath = request.getContextPath();
570         String JavaDoc host = null;
571         String JavaDoc name = null;
572         ObjectName JavaDoc oname = null;
573         while (names.hasNext()) {
574             name = names.next().toString();
575             oname = new ObjectName JavaDoc(name);
576             host = oname.getKeyProperty("name");
577             host = host.substring(2);
578             int i = host.indexOf("/");
579             if (contextPath.equals(host.substring(i))) {
580                 host = host.substring(0,i);
581                 return host;
582             }
583         }
584         return host;
585
586     }
587
588     
589     /**
590      * Return search object name to be used to query.
591      *
592      * @param container object name to query
593      * @param type type of the component
594      *
595      * @exception MalformedObjectNameException if thrown while retrieving the list
596      */

597     public static ObjectName JavaDoc getSearchObject(ObjectName JavaDoc container, String JavaDoc type)
598             throws Exception JavaDoc {
599         
600         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(container.getDomain());
601         sb.append(":type="+type);
602         String JavaDoc containerType = container.getKeyProperty("type");
603         String JavaDoc name = container.getKeyProperty("name");
604         if ((name != null) && (name.length() > 0)) {
605             // parent is context
606
name = name.substring(2);
607             int i = name.indexOf("/");
608             String JavaDoc host = name.substring(0,i);
609             String JavaDoc path = name.substring(i);
610             sb.append(",path=");
611             sb.append(path);
612             sb.append(",host=");
613             sb.append(host);
614         } else if ("Host".equals(containerType)) {
615             // parent is host
616
String JavaDoc host = container.getKeyProperty("host");
617             sb.append(",host=");
618             sb.append(host);
619         }
620         
621         return new ObjectName JavaDoc(sb.toString());
622         
623     }
624     
625 }
626
Popular Tags