KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > jmxdebug > web > beanlib > KernelHelper


1 /**
2  *
3  * Copyright 2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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.geronimo.jmxdebug.web.beanlib;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Set JavaDoc;
25 import javax.management.MalformedObjectNameException JavaDoc;
26 import javax.management.ObjectName JavaDoc;
27
28 import org.apache.geronimo.jmxdebug.util.ObjectNameComparator;
29 import org.apache.geronimo.kernel.KernelRegistry;
30 import org.apache.geronimo.kernel.Kernel;
31 import org.apache.geronimo.kernel.management.State;
32
33 /**
34  * Little helper bean to deal w/ the mbean server
35  *
36  * @version $Rev: 169154 $ $Date: 2005-05-08 12:35:23 -0700 (Sun, 08 May 2005) $
37  */

38 public class KernelHelper {
39     private final Kernel kernel;
40
41     public KernelHelper() {
42         kernel = KernelRegistry.getSingleKernel();
43     }
44
45     public Kernel getKernel() {
46         return kernel;
47     }
48
49     /**
50      * Returns a Collection of InstanceObjects for all mbeans in the server
51      *
52      * @return Collection of InstanceObjects
53      */

54     public Collection JavaDoc getGBeanNames() {
55         return getGBeanNames("*:*");
56     }
57
58     /**
59      * Returns a Collection of InstanceObjects filtered by the input filter
60      *
61      * @param filterString filter to use. Defaults to *:* if null
62      * @return Collection of InstanceObjects that match the filter
63      */

64     public Collection JavaDoc getGBeanNames(String JavaDoc filterString) {
65         if (filterString == null) {
66             filterString = "*:*";
67         }
68
69         if (kernel != null) {
70             ObjectName JavaDoc filter = null;
71             try {
72                 filter = new ObjectName JavaDoc(filterString);
73                 Set JavaDoc names = kernel.listGBeans(filter);
74
75                 List JavaDoc sortedNames = new ArrayList JavaDoc(names);
76                 Collections.sort(sortedNames, ObjectNameComparator.INSTANCE);
77
78                 return sortedNames;
79             } catch (MalformedObjectNameException JavaDoc e) {
80                 e.printStackTrace();
81             }
82         } else {
83             System.out.println("KernelHelper : error : no mbean server");
84         }
85
86         return null;
87     }
88
89     public String JavaDoc getState(ObjectName JavaDoc name) {
90         try {
91             return State.toString(kernel.getGBeanState(name));
92         } catch (Exception JavaDoc e) {
93             return null;
94         }
95     }
96 }
97
Popular Tags