KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > server > core > jmx > MBeanIntrospector


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.jmx;
25
26 //JDK imports
27
import java.lang.reflect.*;
28
29 //JMX imports
30
import javax.management.NotCompliantMBeanException JavaDoc;
31
32 /**
33         Class which has convinience routines to introspect an MBean. The
34     parameter passed in constructor should be an MBean, otherwise it
35     throws an exception. Provides routines to check whether the MBean
36     is JMX compliant.
37 */

38
39 public class MBeanIntrospector extends Introspector
40 {
41     private boolean mIsStandard;
42     private boolean mIsDynamic;
43     private Class JavaDoc mMBeanInterfaceClass;
44
45     /**
46         Creates new MBeanIntrospector
47     */

48     public MBeanIntrospector(Class JavaDoc c) throws NotCompliantMBeanException JavaDoc
49     {
50         super(c);
51         checkBasic(c);
52         checkMBeanType(c);
53         checkCompliance();
54     }
55
56     public boolean isStandardMBean()
57     {
58         return mIsStandard;
59     }
60
61     public boolean isDynamicMBean()
62     {
63         return mIsDynamic;
64     }
65
66     public Class JavaDoc getMBeanInterfaceClass()
67     {
68         return mMBeanInterfaceClass;
69     }
70
71     public boolean isSupported(String JavaDoc methodName, Class JavaDoc[] signature)
72     {
73         boolean isSupported = false;
74         try
75         {
76             Method m = mMBeanInterfaceClass.getMethod(methodName, signature);
77             isSupported = (m != null);
78         }
79         catch (Exception JavaDoc e)
80         {
81         }
82         return isSupported;
83     }
84
85     private void checkBasic(Class JavaDoc c) throws NotCompliantMBeanException JavaDoc
86     {
87         if (!isInstantiableJavaClass(c))
88         {
89             throw new NotCompliantMBeanException JavaDoc();
90         }
91     }
92
93     private void checkMBeanType(Class JavaDoc c)
94     {
95         /*
96          * StandardMBean Checks :-
97          * 1. It is neither primitive type, nor an interface nor an
98          * abstract class.
99          * 2. Implements <className>MBean interface. If not,
100          * 3. A nearest superclass (can omit java.lang.Object) is a
101          * standard mbean.
102          *
103          * DynamicMBean Checks :-
104          * 1. It is neither primitive type, nor an interface nor an
105          * abstract class.
106          * 2. It implements DynamicMBean interface. If not,
107          * 3. Any of its superclasses implements DynamicMBean interface.
108          */

109
110         boolean isStandard = isStandard(c);
111         boolean isDynamic = isDynamic(c);
112         if (!(isStandard || isDynamic)) // (A U B)' = A' ^ B'
113
{
114             Class JavaDoc superClass = c.getSuperclass();
115             //Can omit java.lang.Object.
116
if ((superClass != null) &&
117                 (superClass != java.lang.Object JavaDoc.class))
118             {
119                 checkMBeanType(superClass);
120             }
121         }
122         /**
123          * Need to check both conditions even though it is either or.
124          * A class that is both standard & dynamic is not compliant
125          * and will be raised in checkCompliance()
126          */

127         if (isStandard)
128         {
129             mIsStandard = true;
130             setStandardMBeanInterface(c);
131         }
132         if (isDynamic)
133         {
134             mIsDynamic = true;
135             setDynamicMBeanInterface();
136         }
137     }
138
139     private void checkCompliance() throws NotCompliantMBeanException JavaDoc
140     {
141         if (!(mIsStandard || mIsDynamic) || // (A U B)' = A' ^ B'
142
(mIsStandard && mIsDynamic))
143         {
144             throw new NotCompliantMBeanException JavaDoc();
145         }
146     }
147
148     private void setStandardMBeanInterface(Class JavaDoc c)
149     {
150         String JavaDoc className = deriveStandardMBeanIntfClassName(c);
151         mMBeanInterfaceClass = getImplementedMBeanClass(c, className);
152     }
153
154     private void setDynamicMBeanInterface()
155     {
156         mMBeanInterfaceClass = javax.management.DynamicMBean JavaDoc.class;
157     }
158
159     private boolean isStandard(Class JavaDoc c)
160     {
161         boolean isStandard = false;
162         /**
163          * Note that mbean impl & mbean interface can be in different
164          * packages.
165          */

166         String JavaDoc className = deriveStandardMBeanIntfClassName(c);
167         if (getImplementedMBeanClass(c, className) != null)
168         {
169             isStandard = true;
170         }
171         return isStandard;
172     }
173
174     private boolean isDynamic(Class JavaDoc c)
175     {
176         boolean isDynamic = false;
177         Class JavaDoc[] interfaces = c.getInterfaces();
178         /* No need to check for null as c.getInterfaces() javadoc claims to
179          * return an array of 0 length if the class does not implement any
180          * interfaces or if the class is a primitive type.
181          */

182         int length = interfaces.length;
183         for (int i = 0; i < length; i++)
184         {
185             if (interfaces[i] == javax.management.DynamicMBean JavaDoc.class)
186             {
187                 isDynamic = true;
188                 break;
189             }
190         }
191         return isDynamic;
192     }
193
194     private boolean isInstantiableJavaClass(Class JavaDoc c)
195     {
196         boolean isInstantiable = false;
197         if (!c.isPrimitive() && !c.isArray())
198         {
199             int modifiers = c.getModifiers();
200             boolean isInterface = Modifier.isInterface(modifiers);
201             boolean isAbstract = Modifier.isAbstract(modifiers);
202             isInstantiable = !(isInterface || isAbstract);
203         }
204         return isInstantiable;
205     }
206
207     /**
208      * If class name is foo.bar, this method returns barMBean
209      */

210     private String JavaDoc deriveStandardMBeanIntfClassName(Class JavaDoc c)
211     {
212         String JavaDoc className = truncateClassName(c);
213         return (className + "MBean");
214     }
215
216     /**
217      * If class name is foo.bar, this method returns bar
218      */

219     private String JavaDoc truncateClassName(Class JavaDoc c)
220     {
221         String JavaDoc className = c.getName();
222         int lastDot = className.lastIndexOf('.');
223         className = className.substring(lastDot + 1);
224         return className;
225     }
226     
227     private Class JavaDoc getImplementedMBeanClass(Class JavaDoc c, String JavaDoc intfName)
228     {
229         Class JavaDoc implementedMBean = null;
230         Class JavaDoc[] interfaces = c.getInterfaces();
231         /* No need to check for null as c.getInterfaces() javadoc claims to
232          * return an array of 0 length if the class does not implement any
233          * interfaces or if the class is a primitive type.
234          */

235         for (int i = 0; i < interfaces.length; i++)
236         {
237             String JavaDoc className = truncateClassName(interfaces[i]);
238             if (className.equals(intfName))
239             {
240                 implementedMBean = interfaces[i];
241                 break;
242             }
243         }
244         return implementedMBean;
245     }
246
247     private final String JavaDoc convertToCamelCase(String JavaDoc str)
248     {
249         String JavaDoc camelCase = "";
250         char c = str.charAt(0);
251         camelCase += Character.toUpperCase(c);
252         if (str.length() > 1)
253         {
254             camelCase += str.substring(1);
255         }
256         return camelCase;
257     }
258 }
259
Popular Tags