KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > mbeanserver > StandardMBeanIntrospector


1 /*
2  * @(#)StandardMBeanIntrospector.java 1.9 05/12/30
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.jmx.mbeanserver;
9
10 import java.lang.reflect.InvocationTargetException JavaDoc;
11 import java.lang.reflect.Method JavaDoc;
12 import java.lang.reflect.Type JavaDoc;
13 import java.util.WeakHashMap JavaDoc;
14 import javax.management.Descriptor JavaDoc;
15 import javax.management.ImmutableDescriptor JavaDoc;
16 import javax.management.IntrospectionException JavaDoc;
17 import javax.management.MBeanAttributeInfo JavaDoc;
18 import javax.management.MBeanException JavaDoc;
19 import javax.management.MBeanOperationInfo JavaDoc;
20 import javax.management.NotCompliantMBeanException JavaDoc;
21 import javax.management.NotificationBroadcaster JavaDoc;
22 import javax.management.NotificationBroadcasterSupport JavaDoc;
23
24 /**
25  * @since 1.6
26  */

27 class StandardMBeanIntrospector extends MBeanIntrospector<Method JavaDoc> {
28     private static final StandardMBeanIntrospector instance =
29     new StandardMBeanIntrospector();
30
31     static StandardMBeanIntrospector getInstance() {
32     return instance;
33     }
34
35     @Override JavaDoc
36     PerInterfaceMap<Method JavaDoc> getPerInterfaceMap() {
37     return perInterfaceMap;
38     }
39
40     @Override JavaDoc
41     MBeanInfoMap getMBeanInfoMap() {
42     return mbeanInfoMap;
43     }
44
45     @Override JavaDoc
46     MBeanAnalyzer<Method JavaDoc> getAnalyzer(Class JavaDoc<?> mbeanInterface)
47         throws NotCompliantMBeanException JavaDoc {
48     return MBeanAnalyzer.analyzer(mbeanInterface, this);
49     }
50
51     @Override JavaDoc
52     boolean isMXBean() {
53     return false;
54     }
55
56     @Override JavaDoc
57     Method JavaDoc mFrom(Method JavaDoc m) {
58     return m;
59     }
60
61     @Override JavaDoc
62     String JavaDoc getName(Method JavaDoc m) {
63     return m.getName();
64     }
65
66     @Override JavaDoc
67     Type JavaDoc getGenericReturnType(Method JavaDoc m) {
68     return m.getGenericReturnType();
69     }
70
71     @Override JavaDoc
72     Type JavaDoc[] getGenericParameterTypes(Method JavaDoc m) {
73     return m.getGenericParameterTypes();
74     }
75
76     @Override JavaDoc
77     String JavaDoc[] getSignature(Method JavaDoc m) {
78         Class JavaDoc<?>[] params = m.getParameterTypes();
79         String JavaDoc[] sig = new String JavaDoc[params.length];
80         for (int i = 0; i < params.length; i++)
81             sig[i] = params[i].getName();
82         return sig;
83     }
84
85     @Override JavaDoc
86     void checkMethod(Method JavaDoc m) {
87     }
88
89     @Override JavaDoc
90     Object JavaDoc invokeM2(Method JavaDoc m, Object JavaDoc target, Object JavaDoc[] args, Object JavaDoc cookie)
91         throws InvocationTargetException JavaDoc, IllegalAccessException JavaDoc,
92            MBeanException JavaDoc {
93     return m.invoke(target, args);
94     }
95
96     @Override JavaDoc
97     boolean validParameter(Method JavaDoc m, Object JavaDoc value, int paramNo, Object JavaDoc cookie) {
98         return isValidParameter(m, value, paramNo);
99     }
100
101     @Override JavaDoc
102     MBeanAttributeInfo JavaDoc getMBeanAttributeInfo(String JavaDoc attributeName,
103             Method JavaDoc getter, Method JavaDoc setter) {
104
105         final String JavaDoc description = "Attribute exposed for management";
106         try {
107             return new MBeanAttributeInfo JavaDoc(attributeName, description,
108                                           getter, setter);
109         } catch (IntrospectionException JavaDoc e) {
110             throw new RuntimeException JavaDoc(e); // should not happen
111
}
112     }
113
114     @Override JavaDoc
115     MBeanOperationInfo JavaDoc getMBeanOperationInfo(String JavaDoc operationName,
116             Method JavaDoc operation) {
117         final String JavaDoc description = "Operation exposed for management";
118         return new MBeanOperationInfo JavaDoc(description, operation);
119     }
120
121     @Override JavaDoc
122     Descriptor JavaDoc getBasicMBeanDescriptor() {
123     /* We don't bother saying mxbean=false, and we can't know whether
124        the info is immutable until we know whether the MBean class
125        (not interface) is a NotificationBroadcaster. */

126     return ImmutableDescriptor.EMPTY_DESCRIPTOR;
127     }
128
129     @Override JavaDoc
130     Descriptor JavaDoc getMBeanDescriptor(Class JavaDoc<?> resourceClass) {
131         boolean immutable = isDefinitelyImmutableInfo(resourceClass);
132         return new ImmutableDescriptor JavaDoc("mxbean=false",
133                                        "immutableInfo=" + immutable);
134     }
135
136     /* Return true if and only if we can be sure that the given MBean implementation
137      * class has immutable MBeanInfo. A Standard MBean that is a
138      * NotificationBroadcaster is allowed to return different values at
139      * different times from its getNotificationInfo() method, which is when
140      * we might not know if it is immutable. But if it is a subclass of
141      * NotificationBroadcasterSupport and does not override
142      * getNotificationInfo(), then we know it won't change.
143      */

144     static boolean isDefinitelyImmutableInfo(Class JavaDoc<?> implClass) {
145         if (!NotificationBroadcaster JavaDoc.class.isAssignableFrom(implClass))
146             return true;
147         synchronized (definitelyImmutable) {
148             Boolean JavaDoc immutable = definitelyImmutable.get(implClass);
149             if (immutable == null) {
150                 final Class JavaDoc<NotificationBroadcasterSupport JavaDoc> nbs =
151                         NotificationBroadcasterSupport JavaDoc.class;
152                 if (nbs.isAssignableFrom(implClass)) {
153                     try {
154                         Method JavaDoc m = implClass.getMethod("getNotificationInfo");
155                         immutable = (m.getDeclaringClass() == nbs);
156                     } catch (Exception JavaDoc e) {
157                         // Too bad, we'll say no for now.
158
return false;
159                     }
160                 } else
161                     immutable = false;
162                 definitelyImmutable.put(implClass, immutable);
163             }
164             return immutable;
165         }
166     }
167     private static final WeakHashMap JavaDoc<Class JavaDoc<?>, Boolean JavaDoc> definitelyImmutable =
168             new WeakHashMap JavaDoc<Class JavaDoc<?>, Boolean JavaDoc>();
169
170     private static final PerInterfaceMap<Method JavaDoc>
171         perInterfaceMap = new PerInterfaceMap<Method JavaDoc>();
172
173     private static final MBeanInfoMap mbeanInfoMap = new MBeanInfoMap();
174 }
175
Popular Tags