KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > console > jmxmanager > JMXManagerHelper


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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.console.jmxmanager;
19
20 import java.net.URI JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.text.DateFormat JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.TreeMap JavaDoc;
31
32 import javax.management.ObjectName JavaDoc;
33
34 import org.apache.geronimo.gbean.AbstractName;
35 import org.apache.geronimo.gbean.AbstractNameQuery;
36 import org.apache.geronimo.gbean.GAttributeInfo;
37 import org.apache.geronimo.gbean.GBeanInfo;
38 import org.apache.geronimo.gbean.GOperationInfo;
39 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
40 import org.apache.geronimo.kernel.GBeanNotFoundException;
41 import org.apache.geronimo.kernel.Kernel;
42 import org.apache.geronimo.kernel.KernelRegistry;
43
44 /**
45  * The JMX manager helper
46  */

47 public class JMXManagerHelper {
48     /** Used to return all MBeans */
49     private static final String JavaDoc ALL_MBEANS = "AllMBeans";
50     private static final String JavaDoc GBEANINFO_NAME = "GBeanInfo";
51     private static final String JavaDoc SERVICEMODULE_KEY = "ServiceModule";
52
53     private final Kernel kernel;
54
55     /**
56      * Construct an JMX manager helper (default)
57      */

58     public JMXManagerHelper() {
59         kernel = KernelRegistry.getSingleKernel();
60     }
61
62     /**
63      * List MBeans using a domain
64      */

65     public Collection JavaDoc listByDomain(String JavaDoc domain) {
66         Collection JavaDoc result = new ArrayList JavaDoc();
67         if ((domain == null) || (domain.trim().length() == 0)) {
68             return result;
69         }
70
71         return listByPattern(domain + ":*");
72     }
73
74     /**
75      * List MBeans containing a substring in its object name
76      */

77     public Collection JavaDoc listBySubstring(String JavaDoc substring) {
78         Collection JavaDoc result = new ArrayList JavaDoc();
79         if ((substring == null) || (substring.trim().length() == 0)) {
80             return result;
81         }
82         
83         Collection JavaDoc abstractNames = getAbstractNames(substring);
84         for (Iterator JavaDoc it = abstractNames.iterator(); it.hasNext();) {
85             AbstractName aname = (AbstractName) it.next();
86             ObjectName JavaDoc oname = aname.getObjectName();
87             String JavaDoc[] pair = { aname.toString(), oname.toString() };
88             result.add(pair);
89         }
90         
91         return result;
92     }
93     
94     /**
95      * List MBeans using a pattern (ObjectName)
96      */

97     public Collection JavaDoc listByPattern(String JavaDoc pattern) {
98         Collection JavaDoc result = new ArrayList JavaDoc();
99         if ((pattern == null) || (pattern.trim().length() == 0)) {
100             return result;
101         }
102
103         try {
104             // TODO: Use AbstractNameQuery
105
// Uses Object names for query pattern to support
106
// domain searches. Can't find a way to do it using
107
// AbstractNameQuery.
108
Map JavaDoc abstractNames = getAbstractNames();
109             ObjectName JavaDoc onamePattern = new ObjectName JavaDoc(pattern);
110             Set JavaDoc beans = kernel.listGBeans(onamePattern);
111             for (Iterator JavaDoc it = beans.iterator(); it.hasNext();) {
112                 ObjectName JavaDoc oname = (ObjectName JavaDoc) it.next();
113                 AbstractName aname = (AbstractName) abstractNames.get(oname);
114                 String JavaDoc[] pair = { aname.toString(), oname.toString() };
115                 result.add(pair);
116             }
117         } catch (Exception JavaDoc e) {
118             // Malformed object name, just return what you have
119
}
120
121         return result;
122     }
123
124     /**
125      * List MBeans using J2EE type
126      */

127     public Collection JavaDoc listByJ2EEType(String JavaDoc type) {
128         Collection JavaDoc result = new ArrayList JavaDoc();
129         Map JavaDoc m = null;
130
131         if ((type == null) || (type.trim().length() == 0)) {
132             return result;
133         } else {
134             if (ALL_MBEANS.equalsIgnoreCase(type)) {
135                 m = Collections.EMPTY_MAP;
136             } else {
137                 m = Collections.singletonMap(NameFactory.J2EE_TYPE, type);
138             }
139         }
140
141         AbstractNameQuery query = new AbstractNameQuery(null, m,
142                 Collections.EMPTY_SET);
143         Set JavaDoc beans = kernel.listGBeans(query);
144         for (Iterator JavaDoc it = beans.iterator(); it.hasNext();) {
145             AbstractName abstractName = (AbstractName) it.next();
146             ObjectName JavaDoc objectName = abstractName.getObjectName();
147             String JavaDoc[] pair = { abstractName.toString(), objectName.toString() };
148             result.add(pair);
149         }
150
151         return result;
152     }
153
154     /**
155      * Return all service modules
156      */

157     public Collection JavaDoc getServiceModules() {
158         Map JavaDoc svcModules = new TreeMap JavaDoc();
159         Collection JavaDoc svcModuleMBeans = getAbstractNames(SERVICEMODULE_KEY + '=');
160         for (Iterator JavaDoc it = svcModuleMBeans.iterator(); it.hasNext();) {
161             AbstractName aname = (AbstractName) it.next();
162             String JavaDoc svcModule = aname.getNameProperty(SERVICEMODULE_KEY);
163             if (!svcModules.containsKey(svcModule)) {
164                 svcModules.put(svcModule, null);
165             }
166         }
167
168         return svcModules.keySet();
169     }
170
171     /**
172      * Return abstract names containing a substring
173      */

174     private Collection JavaDoc getAbstractNames(String JavaDoc substring) {
175         Collection JavaDoc result = new ArrayList JavaDoc();
176         if ((substring == null) || (substring.trim().length() == 0)) {
177             return result;
178         }
179
180         Map JavaDoc abstractNames = getAbstractNames();
181         for (Iterator JavaDoc it = abstractNames.keySet().iterator(); it.hasNext();) {
182             ObjectName JavaDoc oname = (ObjectName JavaDoc) it.next();
183             if (oname.toString().indexOf(substring) > 0) {
184                 AbstractName aname = (AbstractName) abstractNames.get(oname);
185                 result.add(aname);
186             }
187         }
188         
189         return result;
190     }
191
192     /**
193      * Return all abstract names as a map
194      */

195     private Map JavaDoc getAbstractNames() {
196         Map JavaDoc abstractNames = new HashMap JavaDoc();
197         // Create Map (Key = ObjectName, Value = AbstractName)
198
AbstractNameQuery query = new AbstractNameQuery(null,
199                 Collections.EMPTY_MAP, Collections.EMPTY_SET);
200         Set JavaDoc allBeans = kernel.listGBeans(query);
201         for (Iterator JavaDoc it = allBeans.iterator(); it.hasNext();) {
202             AbstractName abstractName = (AbstractName) it.next();
203             ObjectName JavaDoc objectName = abstractName.getObjectName();
204             abstractNames.put(objectName, abstractName);
205         }
206
207         return abstractNames;
208     }
209
210     /**
211      * Return MBean attributes
212      */

213     public Collection JavaDoc getAttributes(String JavaDoc abstractName) {
214         Map JavaDoc attributes = new TreeMap JavaDoc();
215         try {
216             AbstractName aname = new AbstractName(URI.create(abstractName));
217             GBeanInfo info = kernel.getGBeanInfo(aname);
218             Set JavaDoc attribs = info.getAttributes();
219             for (Iterator JavaDoc i = attribs.iterator(); i.hasNext();) {
220                 GAttributeInfo attribInfo = (GAttributeInfo) i.next();
221                 // Don't include 'GBeanInfo' attributes
222
String JavaDoc attribName = attribInfo.getName();
223                 if (!GBEANINFO_NAME.equals(attribName)) {
224                     Map JavaDoc attribInfoMap = getAttribInfoAsMap(aname, attribInfo);
225                     attributes.put(attribName, attribInfoMap);
226                 }
227             }
228         } catch (GBeanNotFoundException e) {
229             // GBean not found, just ignore
230
}
231
232         return attributes.values();
233     }
234
235     /**
236      * Return attribute info as map
237      */

238     private Map JavaDoc getAttribInfoAsMap(AbstractName abstractName,
239             GAttributeInfo attribInfo) {
240         Map JavaDoc map = new TreeMap JavaDoc();
241         String JavaDoc attribName = attribInfo.getName();
242         map.put("name", attribName);
243         map.put("getterName", attribInfo.getGetterName());
244         map.put("setterName", attribInfo.getSetterName());
245         map.put("type", attribInfo.getType());
246         map.put("manageable", String.valueOf(attribInfo.isManageable()));
247         map.put("persistent", String.valueOf(attribInfo.isPersistent()));
248         map.put("readable", String.valueOf(attribInfo.isReadable()));
249         map.put("writable", String.valueOf(attribInfo.isWritable()));
250         if (attribInfo.isReadable()) {
251             String JavaDoc attribValue = "";
252             try {
253                 Object JavaDoc value = kernel.getAttribute(abstractName, attribName);
254                 if (value != null) {
255                     attribValue = value.toString();
256                 }
257             } catch (Exception JavaDoc e) {
258                 // GBean or attribute not found, just ignore
259
attribValue = "** EXCEPTION: " + e;
260             }
261             map.put("value", attribValue);
262         }
263         return map;
264     }
265
266     /**
267      * Return MBean operations
268      */

269     public Collection JavaDoc getOperations(String JavaDoc abstractName) {
270         Map JavaDoc operations = new TreeMap JavaDoc();
271         try {
272             AbstractName aname = new AbstractName(URI.create(abstractName));
273             GBeanInfo info = kernel.getGBeanInfo(aname);
274             Set JavaDoc opers = info.getOperations();
275             for (Iterator JavaDoc i = opers.iterator(); i.hasNext();) {
276                 GOperationInfo operInfo = (GOperationInfo) i.next();
277                 Map JavaDoc operInfoMap = getOperInfoAsMap(operInfo);
278                 String JavaDoc operName = (String JavaDoc) operInfoMap.get("name");
279                 operations.put(operName, operInfoMap);
280             }
281         } catch (Exception JavaDoc e) {
282             // GBean not found, just ignore
283
}
284
285         return operations.values();
286     }
287
288     /**
289      * Return operation info as map
290      */

291     private Map JavaDoc getOperInfoAsMap(GOperationInfo operInfo) {
292         Map JavaDoc map = new TreeMap JavaDoc();
293         map.put("methodName", operInfo.getMethodName());
294         map.put("name", operInfo.getName());
295         map.put("parameterList", operInfo.getParameterList());
296         return map;
297     }
298
299     /**
300      * Return MBean basic info
301      */

302     public Collection JavaDoc getMBeanInfo(String JavaDoc abstractName) {
303         Collection JavaDoc info = new ArrayList JavaDoc();
304         try {
305             AbstractName aname = new AbstractName(URI.create(abstractName));
306             info.add(new String JavaDoc[] { "abstractName", aname.toString() });
307             ObjectName JavaDoc oname = aname.getObjectName();
308             info.add(new String JavaDoc[] { "objectName", oname.toString() });
309             GBeanInfo beanInfo = kernel.getGBeanInfo(aname);
310             String JavaDoc className = beanInfo.getClassName();
311             info.add(new String JavaDoc[] { "className", className });
312             String JavaDoc domain = oname.getDomain();
313             info.add(new String JavaDoc[] { "domain", domain });
314             String JavaDoc j2eeType = beanInfo.getJ2eeType();
315             info.add(new String JavaDoc[] { "j2eeType", j2eeType });
316             // String sourceClass = beanInfo.getSourceClass();
317
// info.add(new String[] { "sourceClass", sourceClass });
318
} catch (Exception JavaDoc e) {
319             // GBean not found, just ignore
320
}
321
322         return info;
323     }
324
325     /**
326      * Invoke MBean operation with arguments
327      */

328     public String JavaDoc[] invokeOperWithArgs(String JavaDoc abstractName, String JavaDoc methodName,
329             String JavaDoc[] args, String JavaDoc[] types) {
330         String JavaDoc[] result = new String JavaDoc[2]; // return method name & result
331
result[0] = methodName + "(...)";
332
333         try {
334             Object JavaDoc[] newArgs = processOperArgs(args, types);
335             AbstractName aname = new AbstractName(URI.create(abstractName));
336             Object JavaDoc res = kernel.invoke(aname, methodName, newArgs, types);
337             if (result != null) {
338                 result[1] = res.toString();
339             } else {
340                 result[1] = "<null>";
341             }
342         } catch (Exception JavaDoc e) {
343             result[1] = e.toString();
344         }
345
346         return result;
347     }
348
349     /**
350      * Invoke MBean operation without arguments
351      */

352     public String JavaDoc[] invokeOperNoArgs(String JavaDoc abstractName, String JavaDoc methodName) {
353         String JavaDoc[] result = new String JavaDoc[2]; // return method name & result
354
result[0] = methodName + "()";
355
356         try {
357             AbstractName aname = new AbstractName(URI.create(abstractName));
358             Object JavaDoc res = kernel.invoke(aname, methodName);
359             if (result != null) {
360                 result[1] = res.toString();
361             } else {
362                 result[1] = "<null>";
363             }
364         } catch (Exception JavaDoc e) {
365             result[1] = e.toString();
366         }
367
368         return result;
369     }
370
371     /**
372      * Process MBean operation arguments
373      */

374     private Object JavaDoc[] processOperArgs(String JavaDoc[] args, String JavaDoc[] types)
375             throws Exception JavaDoc {
376         // TODO: Modify this algorithm and add other classes
377
Object JavaDoc[] newArgs = new Object JavaDoc[args.length];
378         for (int i = 0; i < args.length; i++) {
379             String JavaDoc type = types[i];
380             String JavaDoc arg = args[i];
381             newArgs[i] = createObject(arg, type);
382         }
383
384         return newArgs;
385     }
386
387     /**
388      * Create MBean operation argument
389      */

390     private Object JavaDoc createObject(String JavaDoc arg, String JavaDoc type) throws Exception JavaDoc {
391         Object JavaDoc newArg = new Object JavaDoc();
392         if ("byte".equals(type) || "java.lang.Byte".equals(type)) {
393             newArg = new Byte JavaDoc(arg);
394         } else if ("short".equals(type) || "java.lang.Short".equals(type)) {
395             newArg = new Short JavaDoc(arg);
396         } else if ("int".equals(type) || "java.lang.Integer".equals(type)) {
397             newArg = new Integer JavaDoc(arg);
398         } else if ("long".equals(type) || "java.lang.Long".equals(type)) {
399             newArg = new Long JavaDoc(arg);
400         } else if ("float".equals(type) || "java.lang.Float".equals(type)) {
401             newArg = new Float JavaDoc(arg);
402         } else if ("double".equals(type) || "java.lang.Double".equals(type)) {
403             newArg = new Double JavaDoc(arg);
404         } else if ("char".equals(type) || "java.lang.Character".equals(type)) {
405             newArg = new Character JavaDoc(arg.charAt(0));
406         } else if ("boolean".equals(type) || "java.lang.Boolean".equals(type)) {
407             newArg = new Boolean JavaDoc(arg);
408         } else if ("java.lang.String".equals(type)) {
409             newArg = arg;
410         } else if ("java.lang.Object".equals(type)) {
411             newArg = arg;
412         } else if ("java.util.Date".equals(type)) {
413             newArg = DateFormat.getInstance().parse(arg);
414         } else if ("java.net.URL".equals(type)) {
415             newArg = new URL JavaDoc(arg);
416         } else if ("java.net.URI".equals(type)) {
417             newArg = new URI JavaDoc(arg);
418         } else if ("javax.management.ObjectName".equals(type)) {
419             newArg = new ObjectName JavaDoc(arg);
420         } else if ("org.apache.geronimo.gbean.AbstractName".equals(type)) {
421             newArg = new AbstractName(URI.create(arg));
422         } else {
423             // Unknown type, throw exception
424
String JavaDoc errorMsg = "Can't create instance of '" + type + "' using '"
425                     + arg + "'.";
426             throw new IllegalArgumentException JavaDoc(errorMsg);
427         }
428
429         return newArg;
430     }
431
432     /**
433      * Set MBean attribute value
434      */

435     public String JavaDoc[] setAttribute(String JavaDoc abstractName, String JavaDoc attribName,
436             String JavaDoc attribValue, String JavaDoc attribType) {
437         String JavaDoc[] result = new String JavaDoc[2]; // return attribute name & result
438
result[0] = attribName;
439         result[1] = "<SUCCESS>";
440         
441         try {
442             AbstractName aname = new AbstractName(URI.create(abstractName));
443             Object JavaDoc newAttribValue = createObject(attribValue, attribType);
444             kernel.setAttribute(aname, attribName, newAttribValue);
445         } catch (Exception JavaDoc e) {
446             result[1] = e.toString();
447         }
448
449         return result;
450     }
451
452 }
453
Popular Tags