KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > jmx > JMXUtil


1 /**
2  *
3  * Copyright 2003-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.kernel.jmx;
19
20 import java.util.Set JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import javax.management.MalformedObjectNameException JavaDoc;
24 import javax.management.ObjectName JavaDoc;
25 import javax.management.MBeanInfo JavaDoc;
26 import javax.management.MBeanAttributeInfo JavaDoc;
27 import javax.management.MBeanConstructorInfo JavaDoc;
28 import javax.management.MBeanOperationInfo JavaDoc;
29 import javax.management.MBeanParameterInfo JavaDoc;
30 import javax.management.MBeanNotificationInfo JavaDoc;
31
32 import org.apache.geronimo.gbean.GBeanInfo;
33 import org.apache.geronimo.gbean.GAttributeInfo;
34 import org.apache.geronimo.gbean.GOperationInfo;
35 import org.apache.geronimo.kernel.management.NotificationType;
36
37
38 /**
39  * Helper class for JMX Operations
40  *
41  * @version $Rev: 109957 $ $Date: 2004-12-05 23:52:06 -0800 (Sun, 05 Dec 2004) $
42  */

43 public final class JMXUtil {
44     private JMXUtil() {
45     }
46
47     /**
48      * Convert a String to an ObjectName
49      *
50      * @param name the name
51      * @return the ObjectName from that String
52      * @throws java.lang.IllegalArgumentException if the name is malformed
53      */

54     public static ObjectName JavaDoc getObjectName(String JavaDoc name) throws IllegalArgumentException JavaDoc {
55         try {
56             return new ObjectName JavaDoc(name);
57         } catch (MalformedObjectNameException JavaDoc e) {
58             throw new IllegalArgumentException JavaDoc("Malformed ObjectName: " + name);
59         }
60     }
61
62     public static MBeanInfo JavaDoc toMBeanInfo(GBeanInfo gBeanInfo) {
63         String JavaDoc className = gBeanInfo.getClassName();
64         String JavaDoc description = "No description available";
65
66         // attributes
67
Set JavaDoc gbeanAttributes = gBeanInfo.getAttributes();
68         MBeanAttributeInfo JavaDoc[] attributes = new MBeanAttributeInfo JavaDoc[gbeanAttributes.size()];
69         int a = 0;
70         for (Iterator JavaDoc iterator = gbeanAttributes.iterator(); iterator.hasNext();) {
71             GAttributeInfo gAttributeInfo = (GAttributeInfo) iterator.next();
72             attributes[a] = new MBeanAttributeInfo JavaDoc(gAttributeInfo.getName(), gAttributeInfo.getType(), "no description available", gAttributeInfo.isReadable(), gAttributeInfo.isWritable(), isIs(gAttributeInfo));
73             a++;
74         }
75
76         //we don't expose managed constructors
77
MBeanConstructorInfo JavaDoc[] constructors = new MBeanConstructorInfo JavaDoc[0];
78
79         // operations
80
Set JavaDoc gbeanOperations = gBeanInfo.getOperations();
81         MBeanOperationInfo JavaDoc[] operations = new MBeanOperationInfo JavaDoc[gbeanOperations.size()];
82         int o = 0;
83         for (Iterator JavaDoc iterator = gbeanOperations.iterator(); iterator.hasNext();) {
84             GOperationInfo gOperationInfo = (GOperationInfo) iterator.next();
85             //list of class names
86
List JavaDoc gparameters = gOperationInfo.getParameterList();
87             MBeanParameterInfo JavaDoc[] parameters = new MBeanParameterInfo JavaDoc[gparameters.size()];
88             int p = 0;
89             for (Iterator JavaDoc piterator = gparameters.iterator(); piterator.hasNext();) {
90                 String JavaDoc type = (String JavaDoc) piterator.next();
91                 parameters[p] = new MBeanParameterInfo JavaDoc("parameter" + p, type, "no description available");
92                 p++;
93             }
94             operations[o] = new MBeanOperationInfo JavaDoc(gOperationInfo.getName(), "no description available", parameters, "java.lang.Object", MBeanOperationInfo.UNKNOWN);
95             o++;
96         }
97
98         MBeanNotificationInfo JavaDoc[] notifications = new MBeanNotificationInfo JavaDoc[1];
99         notifications[0] = new MBeanNotificationInfo JavaDoc(NotificationType.TYPES, "javax.management.Notification", "J2EE Notifications");
100
101         MBeanInfo JavaDoc mbeanInfo = new MBeanInfo JavaDoc(className, description, attributes, constructors, operations, notifications);
102         return mbeanInfo;
103     }
104
105     private static boolean isIs(GAttributeInfo gAttributeInfo) {
106         String JavaDoc getterName = gAttributeInfo.getGetterName();
107         if (getterName == null) {
108             return false;
109         }
110         return getterName.startsWith("is");
111     }
112 }
113
Popular Tags