Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/webapp/admin/Lists.java


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;
21  import java.util.Collections;
22  import java.util.Iterator;
23  import java.util.List;
24  import javax.servlet.http.HttpServletRequest;
25  import javax.management.MBeanServer;
26  import javax.management.ObjectName;
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: 302986 $ $Date: 2004-06-27 22:14:52 -0400 (Sun, 27 Jun 2004) $
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 verbosityLevels = new ArrayList();
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 booleanValues = new ArrayList();
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 clientAuthValues = new ArrayList();
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 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 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 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 getConnectors(MBeanServer mbserver, ObjectName service)
131         throws Exception {
132 
133         StringBuffer sb = new StringBuffer(service.getDomain());
134         sb.append(":type=Connector,*");
135         ObjectName search = new ObjectName(sb.toString());
136         ArrayList connectors = new ArrayList();
137         Iterator 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 getConnectors(MBeanServer mbserver, String service)
157         throws Exception {
158 
159         return (getConnectors(mbserver, new ObjectName(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 getContexts(MBeanServer mbserver, ObjectName host)
174         throws Exception {
175 
176         StringBuffer sb = new StringBuffer(host.getDomain());
177         sb.append(":j2eeType=WebModule,*");
178         ObjectName search = new ObjectName(sb.toString());
179         ArrayList contexts = new ArrayList();
180         Iterator names = mbserver.queryNames(search, null).iterator();
181         String name = null;
182         ObjectName oname = null;
183         String hostPrefix = "//"+host.getKeyProperty("host");
184         String hostAttr = null;
185         while (names.hasNext()) {
186             name = names.next().toString();
187             oname = new ObjectName(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 getDefaultContexts(MBeanServer mbserver, String 
212         container) throws Exception {
213 
214         return (getDefaultContexts(mbserver, new ObjectName(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 getDefaultContexts(MBeanServer mbserver, ObjectName 
232         container) throws Exception {
233 
234         // FIXME
235         StringBuffer sb = new StringBuffer(container.getDomain());
236         sb.append(":type=DefaultContext");
237         String type = container.getKeyProperty("type");
238         String 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 search = new ObjectName(sb.toString());
247         ArrayList defaultContexts = new ArrayList();
248         Iterator names = mbserver.queryNames(search, null).iterator();
249         while (names.hasNext()) {
250             String 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 getContexts(MBeanServer mbserver, String host)
269         throws Exception {
270 
271         return (getContexts(mbserver, new ObjectName(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 getHosts(MBeanServer mbserver, ObjectName service)
285         throws Exception {
286 
287         StringBuffer sb = new StringBuffer(service.getDomain());
288         sb.append(":type=Host,*");
289         ObjectName search = new ObjectName(sb.toString());
290         ArrayList hosts = new ArrayList();
291         Iterator 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 getHosts(MBeanServer mbserver, String service)
311         throws Exception {
312 
313         return (getHosts(mbserver, new ObjectName(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 getRealms(MBeanServer mbserver, ObjectName container)
329         throws Exception {
330 
331         ObjectName search = getSearchObject(container, "Realm");
332         ArrayList realms = new ArrayList();
333         Iterator 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 getRealms(MBeanServer mbserver, String container)
354         throws Exception {
355 
356         return (getRealms(mbserver, new ObjectName(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 getValves(MBeanServer mbserver, ObjectName container)
371         throws Exception {
372 
373         StringBuffer sb = new StringBuffer(container.getDomain());
374         sb.append(":type=Valve");
375         String type = container.getKeyProperty("type");
376         String j2eeType = container.getKeyProperty("j2eeType");
377         sb.append(TomcatTreeBuilder.WILDCARD);
378         String host = "";
379         String path = "";
380         String 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 search = new ObjectName(sb.toString());        
393         ArrayList valves = new ArrayList();
394         Iterator names = mbserver.queryNames(search, null).iterator();
395         while (names.hasNext()) {
396             ObjectName valve = (ObjectName) names.next();
397             String vpath = valve.getKeyProperty("path");            
398             String vhost = valve.getKeyProperty("host");
399             
400             String valveType = null;
401             String className = (String) 
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 getValves(MBeanServer mbserver, String container)
450         throws Exception {
451 
452         return (getValves(mbserver, new ObjectName(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 getServers(MBeanServer mbserver, String domain)
464         throws Exception {
465 
466         ObjectName search = new ObjectName(domain+":type=Server,*");
467         ArrayList servers = new ArrayList();
468         Iterator 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 getServices(MBeanServer mbserver, ObjectName server)
488         throws Exception {
489 
490         //StringBuffer sb = new StringBuffer(server.getDomain());
491         StringBuffer sb = new StringBuffer("*:type=Service,*");
492         //sb.append(":type=Service,*");
493         ObjectName search = new ObjectName(sb.toString());
494         ArrayList services = new ArrayList();
495         Iterator 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 getServices(MBeanServer mbserver, String server)
515         throws Exception {
516 
517         return (getServices(mbserver, new ObjectName(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 getAdminAppService
532         (MBeanServer mbserver, String domain, HttpServletRequest request)
533         throws Exception {
534 
535         String adminDomain = TomcatTreeBuilder.DEFAULT_DOMAIN;
536         // Get the admin app's service name
537         StringBuffer sb = new StringBuffer(adminDomain);
538         sb.append(":type=Service,*");
539         ObjectName search = new ObjectName(sb.toString());
540         Iterator names = mbserver.queryNames(search, null).iterator();
541         String service = null;
542         while (names.hasNext()) {
543             service = ((ObjectName)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 getAdminAppHost
560         (MBeanServer mbserver, String domain, HttpServletRequest request)
561         throws Exception {
562         
563         // Get the admin app's host name
564         String adminDomain = TomcatTreeBuilder.DEFAULT_DOMAIN;
565         StringBuffer sb = new StringBuffer(adminDomain);
566         sb.append(":j2eeType=WebModule,*"); 
567         ObjectName search = new ObjectName(sb.toString());
568         Iterator names = mbserver.queryNames(search, null).iterator();
569         String contextPath = request.getContextPath();
570         String host = null;
571         String name = null;
572         ObjectName oname = null;
573         while (names.hasNext()) {       
574             name = names.next().toString();
575             oname = new ObjectName(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 getSearchObject(ObjectName container, String type)  
598             throws Exception {
599         
600         StringBuffer sb = new StringBuffer(container.getDomain());
601         sb.append(":type="+type);
602         String containerType = container.getKeyProperty("type");
603         String 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 host = name.substring(0,i);
609             String 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 host = container.getKeyProperty("host");
617             sb.append(",host=");
618             sb.append(host);
619         }    
620         
621         return new ObjectName(sb.toString());
622         
623     }
624     
625 }