KickJava   Java API By Example, From Geeks To Geeks.

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


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.Arrays JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.SortedMap JavaDoc;
29 import java.util.TreeMap JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31
32 import org.apache.geronimo.gbean.GBeanData;
33 import org.apache.geronimo.kernel.Kernel;
34 import org.apache.geronimo.kernel.management.State;
35
36 /**
37  * Simple helper bean for dealing with MBeanInfo. Helps dodge such
38  * wacky APIs like HashMap getKeyPropertyLIst() and wrap in
39  * convenient ways for working in Velocity
40  *
41  * @version $Rev: 169154 $ $Date: 2005-05-08 12:35:23 -0700 (Sun, 08 May 2005) $
42  */

43 public class GBeanInfoHelper {
44     private final ObjectName JavaDoc objectName;
45     private final GBeanData info;
46     private final Kernel kernel;
47
48     public GBeanInfoHelper(KernelHelper kernelHelper, String JavaDoc name) throws Exception JavaDoc {
49         kernel = kernelHelper.getKernel();
50         if (kernel != null) {
51             objectName = new ObjectName JavaDoc(name);
52             info = kernel.getGBeanData(objectName);
53         } else {
54             objectName = null;
55             info = null;
56         }
57
58     }
59
60     public String JavaDoc getCanonicalName() {
61         return objectName.getCanonicalName();
62     }
63
64     public String JavaDoc getDomain() {
65         return objectName.getDomain();
66     }
67
68     public String JavaDoc getState() {
69         try {
70             return State.toString(kernel.getGBeanState(objectName));
71         } catch (Exception JavaDoc e) {
72             return ("Could not get state: " + e.getMessage());
73         }
74     }
75
76     /**
77      * Returns the key properties and values a list of
78      * maps, w/ 'key' and 'value' as entryies in each
79      * map. Makes easy in vel to do
80      * #foreach($item in $list)
81      * $item.key
82      * $item.value
83      * #end
84      */

85     public List JavaDoc getKeyProperties() {
86         Hashtable JavaDoc h = objectName.getKeyPropertyList();
87
88         Iterator JavaDoc it = h.keySet().iterator();
89
90         List JavaDoc l = new ArrayList JavaDoc();
91
92         while (it.hasNext()) {
93             String JavaDoc key = (String JavaDoc) it.next();
94
95             Map JavaDoc m = new HashMap JavaDoc();
96
97             m.put("key", key);
98             m.put("value", h.get(key));
99
100             l.add(m);
101         }
102
103         return l;
104     }
105
106     public String JavaDoc getClassName() {
107         return info.getGBeanInfo().getClassName();
108     }
109
110     public SortedMap JavaDoc getAttributes() {
111         TreeMap JavaDoc attributes = new TreeMap JavaDoc(info.getAttributes());
112         for (Iterator JavaDoc iterator = attributes.entrySet().iterator(); iterator.hasNext();) {
113             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
114             if (entry.getValue() == null) {
115                 entry.setValue("(null)");
116             }
117             if (entry.getValue() instanceof Object JavaDoc[]) {
118                 entry .setValue(Arrays.asList((Object JavaDoc[])entry.getValue()));
119             }
120         }
121
122         return attributes;
123     }
124
125     public Set JavaDoc getOperationInfo() {
126         return info.getGBeanInfo().getOperations();
127     }
128 }
129
Popular Tags