KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > server > core > mbean > meta > MBeanInfoBuilder


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.admin.server.core.mbean.meta;
25
26 //JDK imports
27
import java.util.Vector JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30 import java.lang.reflect.Constructor JavaDoc;
31
32 //JMX imports
33
import javax.management.MBeanAttributeInfo JavaDoc;
34 import javax.management.MBeanConstructorInfo JavaDoc;
35 import javax.management.MBeanOperationInfo JavaDoc;
36 import javax.management.MBeanParameterInfo JavaDoc;
37 import javax.management.MBeanNotificationInfo JavaDoc;
38 import javax.management.MBeanInfo JavaDoc;
39 import javax.management.IntrospectionException JavaDoc;
40
41 //admin imports
42
import com.sun.enterprise.admin.server.core.jmx.Introspector;
43 /**
44     A Class to build the MBeanInfo by providing the class name. Does not
45     validate whether the class passed is an MBean. Things to note is that it
46     provides the data in the form of JMX *Info Data Structures.
47
48     @author Kedar Mhaswade
49     @version 1.0
50 */

51
52 public class MBeanInfoBuilder
53 {
54     public static final String JavaDoc kSetterPrefix = "set";
55     public static final String JavaDoc kGetterPrefix = "get";
56     private Class JavaDoc mClass = null;
57     private MBeanOperationInfo JavaDoc[] mOperations = null;
58     private MBeanAttributeInfo JavaDoc[] mAttributes = null;
59     private MBeanConstructorInfo JavaDoc[] mConstructors = null;
60     private MBeanNotificationInfo JavaDoc[] mNotifications = null;
61     private MBeanInfo JavaDoc mMBeanInfo = null;
62
63     /**
64         Creates new MBeanInfoBuilder
65     */

66     
67     public MBeanInfoBuilder(Class JavaDoc aClass) throws IntrospectionException JavaDoc
68     {
69         if(aClass == null)
70         {
71             throw new IllegalArgumentException JavaDoc();
72         }
73         mClass = aClass;
74         createOperations();
75         createConstructors();
76         createAttributes();
77         createMBeanInfo();
78         createNotifications();
79     }
80     
81     public MBeanInfoBuilder(String JavaDoc aClassName)
82     {
83     }
84     
85     public MBeanInfoBuilder(String JavaDoc aClassName, ClassLoader JavaDoc cl)
86     {
87     }
88     
89     /**
90         Returns only the public methods in the given MBean. These are the
91         operations that can be called on this MBean with invoke method.
92      
93         @return array of MBeanOperationInfo instances. If there is no
94         such method that can be called an array with 0 elements is returned.
95     */

96     
97     public MBeanOperationInfo JavaDoc[] getMBeanOperations()
98     {
99         return ( mOperations );
100     }
101     public MBeanConstructorInfo JavaDoc[] getMBeanConstructors()
102     {
103         return ( mConstructors );
104     }
105     public MBeanInfo JavaDoc getMBeanInfo()
106     {
107         return ( mMBeanInfo );
108     }
109     public MBeanAttributeInfo JavaDoc[] getMBeanAttributes()
110     {
111         return ( mAttributes );
112     }
113     private void createOperations()
114     {
115         Collection JavaDoc excludeList = new Vector JavaDoc();
116         excludeList.add("java.lang.Object");
117         excludeList.add("com.sun.enterprise.admin.server.core.mbean.config.AdminBase");
118         Introspector reflector = new Introspector(mClass);
119         Method JavaDoc[] methods = reflector.getCallableInstanceMethods(excludeList);
120         Vector JavaDoc oprVector = new Vector JavaDoc();
121         for (int i = 0 ; i < methods.length ; i++)
122         {
123             String JavaDoc name = methods[i].getName();
124             boolean isGetter = name.startsWith(kGetterPrefix);
125             boolean isSetter = name.startsWith(kSetterPrefix);
126             if (!isGetter && !isSetter)
127             {
128                 oprVector.add(new MBeanOperationInfo JavaDoc(name, methods[i]));
129             }
130         }
131         mOperations = new MBeanOperationInfo JavaDoc[oprVector.size()];
132         oprVector.toArray (mOperations);
133     }
134     private void createConstructors()
135     {
136         Constructor JavaDoc[] ctors = mClass.getConstructors();
137         mConstructors = new MBeanConstructorInfo JavaDoc[ctors.length];
138         for (int i = 0 ; i < ctors.length ; i++)
139         {
140             String JavaDoc ctorDesc = ctors[i].getName();
141             MBeanConstructorInfo JavaDoc ctorInfo =
142                 new MBeanConstructorInfo JavaDoc(ctorDesc, ctors[i]);
143             mConstructors[i] = ctorInfo;
144         }
145     }
146
147     private void createNotifications()
148     {
149     }
150     
151     private void createMBeanInfo()
152     {
153         mMBeanInfo = new MBeanInfo JavaDoc(mClass.getName(),
154             mClass.getName(),
155             mAttributes, mConstructors, mOperations, mNotifications);
156     }
157     /**
158         This will only give the attributes in getX or setX style.
159         Inheritence is not supported in here.
160     */

161     private void createAttributes() throws IntrospectionException JavaDoc
162     {
163         Vector JavaDoc attrVector = new Vector JavaDoc();
164         Method JavaDoc[] declMethods = new Introspector(mClass).getDeclaredConcretePublicMethods();
165         Method JavaDoc[] getters = this.getGetters(declMethods);
166         Method JavaDoc[] setters = this.getSetters(declMethods);
167         
168         for (int i = 0 ; i < getters.length ; i ++)
169         {
170             MBeanAttributeInfo JavaDoc attr = null;
171             String JavaDoc methodName = getters[i].getName();
172             String JavaDoc attrName = getAttributeNameFromGetter(methodName);
173             Method JavaDoc getter = findAttrIn(attrName, getters);
174             Method JavaDoc setter = findAttrIn(attrName, setters);
175             boolean isReadable = ( getter != null );
176             boolean isWritable = ( setter != null );
177             boolean isIs = false;
178             
179             attr = new MBeanAttributeInfo JavaDoc(attrName, null, attrName,
180                 isReadable, isWritable, isIs);
181             attrVector.add(attr);
182         }
183         mAttributes = new MBeanAttributeInfo JavaDoc[attrVector.size()];
184         attrVector.toArray(mAttributes);
185     }
186     
187     private Method JavaDoc[] getGetters(Method JavaDoc[] methods)
188     {
189         Vector JavaDoc getters = new Vector JavaDoc();
190         for (int i = 0 ; i < methods.length ; i++)
191         {
192             String JavaDoc methodName = methods[i].getName();
193             if (methodName.startsWith (kGetterPrefix))
194             {
195                 getters.add (methods[i]);
196             }
197         }
198         Method JavaDoc[] getterMethods = new Method JavaDoc[getters.size()];
199         
200         return ( (Method JavaDoc[]) getters.toArray (getterMethods) );
201     }
202     
203     private Method JavaDoc[] getSetters(Method JavaDoc[] methods)
204     {
205         Vector JavaDoc setters = new Vector JavaDoc();
206         for (int i = 0 ; i < methods.length ; i++)
207         {
208             String JavaDoc methodName = methods[i].getName();
209             if (methodName.startsWith (kSetterPrefix))
210             {
211                 setters.add (methods[i]);
212             }
213         }
214         Method JavaDoc[] setterMethods = new Method JavaDoc[setters.size()];
215         
216         return ( (Method JavaDoc[]) setters.toArray (setterMethods) );
217     }
218     
219     private String JavaDoc getAttributeNameFromGetter(String JavaDoc getterName)
220     {
221         int getStartsAt = getterName.indexOf(kGetterPrefix);
222         int attributeStartsAt = getStartsAt + kGetterPrefix.length();
223
224         return ( getterName.substring (attributeStartsAt) );
225     }
226     
227     private Method JavaDoc findAttrIn(String JavaDoc attrName, Method JavaDoc[] methods)
228     {
229         Method JavaDoc matcher = null;
230         boolean found = false;
231         
232         for (int i = 0 ; i < methods.length ; i++)
233         {
234             String JavaDoc methodName = methods[i].getName();
235             if (methodName.endsWith(attrName))
236             {
237                 matcher = methods[i];
238                 break;
239             }
240         }
241         return ( matcher );
242     }
243 }
Popular Tags