author | jfdenise |
Tue, 09 Dec 2008 15:36:14 +0100 | |
changeset 1699 | 3611e5fd6da5 |
parent 1510 | e747d3193ef2 |
child 1700 | 4506662fb2ee |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 1999-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.jmx.mbeanserver; |
|
27 |
||
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
28 |
import com.sun.jmx.remote.util.EnvHelp; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
29 |
import java.beans.BeanInfo; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
30 |
import java.beans.PropertyDescriptor; |
2 | 31 |
import java.lang.annotation.Annotation; |
32 |
import java.lang.reflect.AnnotatedElement; |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
33 |
import java.lang.reflect.Array; |
2 | 34 |
import java.lang.reflect.Constructor; |
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
35 |
import java.lang.reflect.InvocationTargetException; |
2 | 36 |
import java.lang.reflect.Method; |
37 |
import java.lang.reflect.Modifier; |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
38 |
import java.lang.reflect.Proxy; |
2 | 39 |
import java.lang.reflect.UndeclaredThrowableException; |
40 |
import java.util.Arrays; |
|
41 |
import java.util.HashMap; |
|
42 |
import java.util.Map; |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
43 |
import java.util.logging.Level; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
44 |
import javax.management.AttributeNotFoundException; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
45 |
import javax.management.Description; |
2 | 46 |
|
47 |
import javax.management.Descriptor; |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
48 |
import javax.management.DescriptorFields; |
2 | 49 |
import javax.management.DescriptorKey; |
50 |
import javax.management.DynamicMBean; |
|
51 |
import javax.management.ImmutableDescriptor; |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
52 |
import javax.management.MBean; |
2 | 53 |
import javax.management.MBeanInfo; |
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
54 |
import javax.management.MXBean; |
2 | 55 |
import javax.management.NotCompliantMBeanException; |
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
56 |
import javax.management.openmbean.CompositeData; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
57 |
import javax.management.openmbean.MXBeanMappingFactory; |
2 | 58 |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
59 |
import static com.sun.jmx.defaults.JmxProperties.MBEANSERVER_LOGGER; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
60 |
import com.sun.jmx.mbeanserver.Util; |
34 | 61 |
import com.sun.jmx.remote.util.EnvHelp; |
62 |
import java.beans.BeanInfo; |
|
63 |
import java.beans.PropertyDescriptor; |
|
64 |
import java.lang.reflect.Array; |
|
65 |
import java.lang.reflect.InvocationTargetException; |
|
1699
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
66 |
import java.util.LinkedHashSet; |
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
67 |
import java.util.Set; |
34 | 68 |
import javax.management.AttributeNotFoundException; |
1699
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
69 |
import javax.management.JMX; |
34 | 70 |
import javax.management.openmbean.CompositeData; |
687 | 71 |
import javax.management.openmbean.MXBeanMappingFactory; |
2 | 72 |
|
73 |
/** |
|
74 |
* This class contains the methods for performing all the tests needed to verify |
|
75 |
* that a class represents a JMX compliant MBean. |
|
76 |
* |
|
77 |
* @since 1.5 |
|
78 |
*/ |
|
79 |
public class Introspector { |
|
80 |
||
81 |
||
82 |
/* |
|
83 |
* ------------------------------------------ |
|
84 |
* PRIVATE CONSTRUCTORS |
|
85 |
* ------------------------------------------ |
|
86 |
*/ |
|
87 |
||
88 |
// private constructor defined to "hide" the default public constructor |
|
89 |
private Introspector() { |
|
90 |
||
91 |
// ------------------------------ |
|
92 |
// ------------------------------ |
|
93 |
||
94 |
} |
|
95 |
||
96 |
/* |
|
97 |
* ------------------------------------------ |
|
98 |
* PUBLIC METHODS |
|
99 |
* ------------------------------------------ |
|
100 |
*/ |
|
101 |
||
102 |
/** |
|
103 |
* Tell whether a MBean of the given class is a Dynamic MBean. |
|
104 |
* This method does nothing more than returning |
|
105 |
* <pre> |
|
106 |
* javax.management.DynamicMBean.class.isAssignableFrom(c) |
|
107 |
* </pre> |
|
108 |
* This method does not check for any JMX MBean compliance: |
|
109 |
* <ul><li>If <code>true</code> is returned, then instances of |
|
110 |
* <code>c</code> are DynamicMBean.</li> |
|
111 |
* <li>If <code>false</code> is returned, then no further |
|
112 |
* assumption can be made on instances of <code>c</code>. |
|
113 |
* In particular, instances of <code>c</code> may, or may not |
|
114 |
* be JMX standard MBeans.</li> |
|
115 |
* </ul> |
|
116 |
* @param c The class of the MBean under examination. |
|
117 |
* @return <code>true</code> if instances of <code>c</code> are |
|
118 |
* Dynamic MBeans, <code>false</code> otherwise. |
|
119 |
* |
|
120 |
**/ |
|
1510
e747d3193ef2
6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents:
1322
diff
changeset
|
121 |
public static final boolean isDynamic(final Class<?> c) { |
2 | 122 |
// Check if the MBean implements the DynamicMBean interface |
123 |
return javax.management.DynamicMBean.class.isAssignableFrom(c); |
|
124 |
} |
|
125 |
||
126 |
/** |
|
127 |
* Basic method for testing that a MBean of a given class can be |
|
128 |
* instantiated by the MBean server.<p> |
|
129 |
* This method checks that: |
|
130 |
* <ul><li>The given class is a concrete class.</li> |
|
131 |
* <li>The given class exposes at least one public constructor.</li> |
|
132 |
* </ul> |
|
133 |
* If these conditions are not met, throws a NotCompliantMBeanException. |
|
134 |
* @param c The class of the MBean we want to create. |
|
135 |
* @exception NotCompliantMBeanException if the MBean class makes it |
|
136 |
* impossible to instantiate the MBean from within the |
|
137 |
* MBeanServer. |
|
138 |
* |
|
139 |
**/ |
|
1510
e747d3193ef2
6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents:
1322
diff
changeset
|
140 |
public static void testCreation(Class<?> c) |
2 | 141 |
throws NotCompliantMBeanException { |
142 |
// Check if the class is a concrete class |
|
143 |
final int mods = c.getModifiers(); |
|
144 |
if (Modifier.isAbstract(mods) || Modifier.isInterface(mods)) { |
|
145 |
throw new NotCompliantMBeanException("MBean class must be concrete"); |
|
146 |
} |
|
147 |
||
148 |
// Check if the MBean has a public constructor |
|
1510
e747d3193ef2
6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents:
1322
diff
changeset
|
149 |
final Constructor<?>[] consList = c.getConstructors(); |
2 | 150 |
if (consList.length == 0) { |
151 |
throw new NotCompliantMBeanException("MBean class must have public constructor"); |
|
152 |
} |
|
153 |
} |
|
154 |
||
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
155 |
public static void checkCompliance(Class<?> mbeanClass) |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
156 |
throws NotCompliantMBeanException { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
157 |
|
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
158 |
// Check that @Resource is used correctly (if it used). |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
159 |
MBeanInjector.validate(mbeanClass); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
160 |
|
2 | 161 |
// Is DynamicMBean? |
162 |
// |
|
163 |
if (DynamicMBean.class.isAssignableFrom(mbeanClass)) |
|
164 |
return; |
|
165 |
// Is Standard MBean? |
|
166 |
// |
|
167 |
final Exception mbeanException; |
|
168 |
try { |
|
169 |
getStandardMBeanInterface(mbeanClass); |
|
170 |
return; |
|
171 |
} catch (NotCompliantMBeanException e) { |
|
172 |
mbeanException = e; |
|
173 |
} |
|
174 |
// Is MXBean? |
|
175 |
// |
|
176 |
final Exception mxbeanException; |
|
177 |
try { |
|
178 |
getMXBeanInterface(mbeanClass); |
|
179 |
return; |
|
180 |
} catch (NotCompliantMBeanException e) { |
|
181 |
mxbeanException = e; |
|
182 |
} |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
183 |
// Is @MBean or @MXBean class? |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
184 |
// In fact we find @MBean or @MXBean as a hacky variant of |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
185 |
// getStandardMBeanInterface or getMXBeanInterface. If we get here |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
186 |
// then nothing worked. |
2 | 187 |
final String msg = |
188 |
"MBean class " + mbeanClass.getName() + " does not implement " + |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
189 |
"DynamicMBean; does not follow the Standard MBean conventions (" + |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
190 |
mbeanException.toString() + "); does not follow the MXBean conventions (" + |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
191 |
mxbeanException.toString() + "); and does not have or inherit the @" + |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
192 |
MBean.class.getSimpleName() + " or @" + MXBean.class.getSimpleName() + |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
193 |
" annotation"; |
2 | 194 |
throw new NotCompliantMBeanException(msg); |
195 |
} |
|
196 |
||
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
197 |
/** |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
198 |
* <p>Make a DynamicMBean out of the existing MBean object. The object |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
199 |
* may already be a DynamicMBean, or it may be a Standard MBean or |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
200 |
* MXBean, possibly defined using {@code @MBean} or {@code @MXBean}.</p> |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
201 |
* @param mbean the object to convert to a DynamicMBean. |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
202 |
* @param <T> a type parameter defined for implementation convenience |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
203 |
* (which would have to be removed if this method were part of the public |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
204 |
* API). |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
205 |
* @return the converted DynamicMBean. |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
206 |
* @throws NotCompliantMBeanException if {@code mbean} is not a compliant |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
207 |
* MBean object, including the case where it is null. |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
208 |
*/ |
687 | 209 |
public static <T> DynamicMBean makeDynamicMBean(T mbean) |
210 |
throws NotCompliantMBeanException { |
|
211 |
if (mbean == null) |
|
212 |
throw new NotCompliantMBeanException("Null MBean object"); |
|
2 | 213 |
if (mbean instanceof DynamicMBean) |
214 |
return (DynamicMBean) mbean; |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
215 |
final Class<?> mbeanClass = mbean.getClass(); |
687 | 216 |
Class<? super T> c = null; |
2 | 217 |
try { |
687 | 218 |
c = Util.cast(getStandardMBeanInterface(mbeanClass)); |
2 | 219 |
} catch (NotCompliantMBeanException e) { |
220 |
// Ignore exception - we need to check whether |
|
221 |
// mbean is an MXBean first. |
|
222 |
} |
|
223 |
if (c != null) |
|
687 | 224 |
return new StandardMBeanSupport(mbean, c); |
2 | 225 |
|
226 |
try { |
|
687 | 227 |
c = Util.cast(getMXBeanInterface(mbeanClass)); |
2 | 228 |
} catch (NotCompliantMBeanException e) { |
229 |
// Ignore exception - we cannot decide whether mbean was supposed |
|
230 |
// to be an MBean or an MXBean. We will call checkCompliance() |
|
231 |
// to generate the appropriate exception. |
|
232 |
} |
|
687 | 233 |
if (c != null) { |
1322
d038148778cc
6750935: The expected NotCompliantMBeanException is not thrown for the custom MXBeanMappingFactory
emcmanus
parents:
834
diff
changeset
|
234 |
MXBeanMappingFactory factory; |
d038148778cc
6750935: The expected NotCompliantMBeanException is not thrown for the custom MXBeanMappingFactory
emcmanus
parents:
834
diff
changeset
|
235 |
try { |
d038148778cc
6750935: The expected NotCompliantMBeanException is not thrown for the custom MXBeanMappingFactory
emcmanus
parents:
834
diff
changeset
|
236 |
factory = MXBeanMappingFactory.forInterface(c); |
d038148778cc
6750935: The expected NotCompliantMBeanException is not thrown for the custom MXBeanMappingFactory
emcmanus
parents:
834
diff
changeset
|
237 |
} catch (IllegalArgumentException e) { |
d038148778cc
6750935: The expected NotCompliantMBeanException is not thrown for the custom MXBeanMappingFactory
emcmanus
parents:
834
diff
changeset
|
238 |
NotCompliantMBeanException ncmbe = |
d038148778cc
6750935: The expected NotCompliantMBeanException is not thrown for the custom MXBeanMappingFactory
emcmanus
parents:
834
diff
changeset
|
239 |
new NotCompliantMBeanException(e.getMessage()); |
d038148778cc
6750935: The expected NotCompliantMBeanException is not thrown for the custom MXBeanMappingFactory
emcmanus
parents:
834
diff
changeset
|
240 |
ncmbe.initCause(e); |
d038148778cc
6750935: The expected NotCompliantMBeanException is not thrown for the custom MXBeanMappingFactory
emcmanus
parents:
834
diff
changeset
|
241 |
throw ncmbe; |
d038148778cc
6750935: The expected NotCompliantMBeanException is not thrown for the custom MXBeanMappingFactory
emcmanus
parents:
834
diff
changeset
|
242 |
} |
687 | 243 |
return new MXBeanSupport(mbean, c, factory); |
244 |
} |
|
2 | 245 |
checkCompliance(mbeanClass); |
246 |
throw new NotCompliantMBeanException("Not compliant"); // not reached |
|
247 |
} |
|
248 |
||
249 |
/** |
|
250 |
* Basic method for testing if a given class is a JMX compliant MBean. |
|
251 |
* |
|
252 |
* @param baseClass The class to be tested |
|
253 |
* |
|
254 |
* @return <code>null</code> if the MBean is a DynamicMBean, |
|
255 |
* the computed {@link javax.management.MBeanInfo} otherwise. |
|
256 |
* @exception NotCompliantMBeanException The specified class is not a |
|
257 |
* JMX compliant MBean |
|
258 |
*/ |
|
1510
e747d3193ef2
6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents:
1322
diff
changeset
|
259 |
public static MBeanInfo testCompliance(Class<?> baseClass) |
2 | 260 |
throws NotCompliantMBeanException { |
261 |
||
262 |
// ------------------------------ |
|
263 |
// ------------------------------ |
|
264 |
||
265 |
// Check if the MBean implements the MBean or the Dynamic |
|
266 |
// MBean interface |
|
267 |
if (isDynamic(baseClass)) |
|
268 |
return null; |
|
269 |
||
270 |
return testCompliance(baseClass, null); |
|
271 |
} |
|
272 |
||
1510
e747d3193ef2
6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents:
1322
diff
changeset
|
273 |
public static void testComplianceMXBeanInterface(Class<?> interfaceClass, |
687 | 274 |
MXBeanMappingFactory factory) |
2 | 275 |
throws NotCompliantMBeanException { |
687 | 276 |
MXBeanIntrospector.getInstance(factory).getAnalyzer(interfaceClass); |
2 | 277 |
} |
278 |
||
279 |
/** |
|
280 |
* Basic method for testing if a given class is a JMX compliant |
|
281 |
* Standard MBean. This method is only called by the legacy code |
|
282 |
* in com.sun.management.jmx. |
|
283 |
* |
|
284 |
* @param baseClass The class to be tested. |
|
285 |
* |
|
286 |
* @param mbeanInterface the MBean interface that the class implements, |
|
287 |
* or null if the interface must be determined by introspection. |
|
288 |
* |
|
289 |
* @return the computed {@link javax.management.MBeanInfo}. |
|
290 |
* @exception NotCompliantMBeanException The specified class is not a |
|
291 |
* JMX compliant Standard MBean |
|
292 |
*/ |
|
293 |
public static synchronized MBeanInfo |
|
294 |
testCompliance(final Class<?> baseClass, |
|
295 |
Class<?> mbeanInterface) |
|
296 |
throws NotCompliantMBeanException { |
|
297 |
if (mbeanInterface == null) |
|
298 |
mbeanInterface = getStandardMBeanInterface(baseClass); |
|
299 |
MBeanIntrospector<?> introspector = StandardMBeanIntrospector.getInstance(); |
|
300 |
return getClassMBeanInfo(introspector, baseClass, mbeanInterface); |
|
301 |
} |
|
302 |
||
303 |
private static <M> MBeanInfo |
|
304 |
getClassMBeanInfo(MBeanIntrospector<M> introspector, |
|
305 |
Class<?> baseClass, Class<?> mbeanInterface) |
|
306 |
throws NotCompliantMBeanException { |
|
307 |
PerInterface<M> perInterface = introspector.getPerInterface(mbeanInterface); |
|
308 |
return introspector.getClassMBeanInfo(baseClass, perInterface); |
|
309 |
} |
|
310 |
||
311 |
/** |
|
312 |
* Get the MBean interface implemented by a JMX Standard |
|
313 |
* MBean class. This method is only called by the legacy |
|
314 |
* code in "com.sun.management.jmx". |
|
315 |
* |
|
316 |
* @param baseClass The class to be tested. |
|
317 |
* |
|
318 |
* @return The MBean interface implemented by the MBean. |
|
319 |
* Return <code>null</code> if the MBean is a DynamicMBean, |
|
320 |
* or if no MBean interface is found. |
|
321 |
*/ |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
322 |
public static Class<?> getMBeanInterface(Class<?> baseClass) { |
2 | 323 |
// Check if the given class implements the MBean interface |
324 |
// or the Dynamic MBean interface |
|
325 |
if (isDynamic(baseClass)) return null; |
|
326 |
try { |
|
327 |
return getStandardMBeanInterface(baseClass); |
|
328 |
} catch (NotCompliantMBeanException e) { |
|
329 |
return null; |
|
330 |
} |
|
331 |
} |
|
332 |
||
333 |
/** |
|
334 |
* Get the MBean interface implemented by a JMX Standard MBean class. |
|
335 |
* |
|
336 |
* @param baseClass The class to be tested. |
|
337 |
* |
|
338 |
* @return The MBean interface implemented by the Standard MBean. |
|
339 |
* |
|
340 |
* @throws NotCompliantMBeanException The specified class is |
|
341 |
* not a JMX compliant Standard MBean. |
|
342 |
*/ |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
343 |
public static <T> Class<? super T> getStandardMBeanInterface(Class<T> baseClass) |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
344 |
throws NotCompliantMBeanException { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
345 |
if (baseClass.isAnnotationPresent(MBean.class)) |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
346 |
return baseClass; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
347 |
Class<? super T> current = baseClass; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
348 |
Class<? super T> mbeanInterface = null; |
2 | 349 |
while (current != null) { |
350 |
mbeanInterface = |
|
351 |
findMBeanInterface(current, current.getName()); |
|
352 |
if (mbeanInterface != null) break; |
|
353 |
current = current.getSuperclass(); |
|
354 |
} |
|
355 |
if (mbeanInterface != null) { |
|
356 |
return mbeanInterface; |
|
357 |
} else { |
|
358 |
final String msg = |
|
359 |
"Class " + baseClass.getName() + |
|
360 |
" is not a JMX compliant Standard MBean"; |
|
361 |
throw new NotCompliantMBeanException(msg); |
|
362 |
} |
|
363 |
} |
|
364 |
||
365 |
/** |
|
366 |
* Get the MXBean interface implemented by a JMX MXBean class. |
|
367 |
* |
|
368 |
* @param baseClass The class to be tested. |
|
369 |
* |
|
370 |
* @return The MXBean interface implemented by the MXBean. |
|
371 |
* |
|
372 |
* @throws NotCompliantMBeanException The specified class is |
|
373 |
* not a JMX compliant MXBean. |
|
374 |
*/ |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
375 |
public static <T> Class<? super T> getMXBeanInterface(Class<T> baseClass) |
2 | 376 |
throws NotCompliantMBeanException { |
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
377 |
if (hasMXBeanAnnotation(baseClass)) |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
378 |
return baseClass; |
2 | 379 |
try { |
380 |
return MXBeanSupport.findMXBeanInterface(baseClass); |
|
381 |
} catch (Exception e) { |
|
382 |
throw throwException(baseClass,e); |
|
383 |
} |
|
384 |
} |
|
385 |
||
687 | 386 |
public static <T> Class<? super T> getStandardOrMXBeanInterface( |
387 |
Class<T> baseClass, boolean mxbean) |
|
388 |
throws NotCompliantMBeanException { |
|
389 |
if (mxbean) |
|
390 |
return getMXBeanInterface(baseClass); |
|
391 |
else |
|
392 |
return getStandardMBeanInterface(baseClass); |
|
393 |
} |
|
394 |
||
2 | 395 |
/* |
396 |
* ------------------------------------------ |
|
397 |
* PRIVATE METHODS |
|
398 |
* ------------------------------------------ |
|
399 |
*/ |
|
400 |
||
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
401 |
static boolean hasMXBeanAnnotation(Class<?> c) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
402 |
MXBean m = c.getAnnotation(MXBean.class); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
403 |
return (m != null && m.value()); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
404 |
} |
2 | 405 |
|
406 |
/** |
|
407 |
* Try to find the MBean interface corresponding to the class aName |
|
408 |
* - i.e. <i>aName</i>MBean, from within aClass and its superclasses. |
|
409 |
**/ |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
410 |
private static <T> Class<? super T> findMBeanInterface( |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
411 |
Class<T> aClass, String aName) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
412 |
Class<? super T> current = aClass; |
2 | 413 |
while (current != null) { |
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
414 |
final Class<?>[] interfaces = current.getInterfaces(); |
2 | 415 |
final int len = interfaces.length; |
416 |
for (int i=0;i<len;i++) { |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
417 |
Class<? super T> inter = Util.cast(interfaces[i]); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
418 |
inter = implementsMBean(inter, aName); |
2 | 419 |
if (inter != null) return inter; |
420 |
} |
|
421 |
current = current.getSuperclass(); |
|
422 |
} |
|
423 |
return null; |
|
424 |
} |
|
425 |
||
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
426 |
public static String descriptionForElement(AnnotatedElement elmt) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
427 |
if (elmt == null) |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
428 |
return null; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
429 |
Description d = elmt.getAnnotation(Description.class); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
430 |
if (d == null) |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
431 |
return null; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
432 |
return d.value(); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
433 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
434 |
|
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
435 |
public static String descriptionForParameter( |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
436 |
Annotation[] parameterAnnotations) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
437 |
for (Annotation a : parameterAnnotations) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
438 |
if (a instanceof Description) |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
439 |
return ((Description) a).value(); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
440 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
441 |
return null; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
442 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
443 |
|
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
444 |
public static String nameForParameter( |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
445 |
Annotation[] parameterAnnotations) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
446 |
for (Annotation a : parameterAnnotations) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
447 |
Class<? extends Annotation> ac = a.annotationType(); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
448 |
// You'd really have to go out of your way to have more than |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
449 |
// one @Name annotation, so we don't check for that. |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
450 |
if (ac.getSimpleName().equals("Name")) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
451 |
try { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
452 |
Method value = ac.getMethod("value"); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
453 |
if (value.getReturnType() == String.class && |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
454 |
value.getParameterTypes().length == 0) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
455 |
return (String) value.invoke(a); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
456 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
457 |
} catch (Exception e) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
458 |
MBEANSERVER_LOGGER.log( |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
459 |
Level.WARNING, |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
460 |
"Unexpected exception getting @" + ac.getName(), |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
461 |
e); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
462 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
463 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
464 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
465 |
return null; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
466 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
467 |
|
1699
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
468 |
public static Descriptor descriptorForElement(final AnnotatedElement elmt, |
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
469 |
boolean isSetter) { |
2 | 470 |
if (elmt == null) |
471 |
return ImmutableDescriptor.EMPTY_DESCRIPTOR; |
|
472 |
final Annotation[] annots = elmt.getAnnotations(); |
|
1699
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
473 |
Descriptor descr = descriptorForAnnotations(annots); |
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
474 |
String[] exceptions = {}; |
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
475 |
if(elmt instanceof Method) |
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
476 |
exceptions = getAllExceptions(((Method) elmt).getExceptionTypes()); |
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
477 |
else |
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
478 |
if(elmt instanceof Constructor<?>) |
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
479 |
exceptions = getAllExceptions(((Constructor<?>) elmt). |
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
480 |
getExceptionTypes()); |
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
481 |
|
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
482 |
if(exceptions.length > 0 ) { |
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
483 |
String fieldName = isSetter ? JMX.SET_EXCEPTIONS_FIELD : |
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
484 |
JMX.EXCEPTIONS_FIELD; |
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
485 |
|
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
486 |
String[] fieldNames = {fieldName}; |
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
487 |
Object[] fieldValues = {exceptions}; |
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
488 |
descr = ImmutableDescriptor.union(descr, |
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
489 |
new ImmutableDescriptor(fieldNames, fieldValues)); |
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
490 |
} |
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
491 |
|
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
492 |
return descr; |
2 | 493 |
} |
494 |
||
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
495 |
public static Descriptor descriptorForAnnotation(Annotation annot) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
496 |
return descriptorForAnnotations(new Annotation[] {annot}); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
497 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
498 |
|
2 | 499 |
public static Descriptor descriptorForAnnotations(Annotation[] annots) { |
500 |
if (annots.length == 0) |
|
501 |
return ImmutableDescriptor.EMPTY_DESCRIPTOR; |
|
502 |
Map<String, Object> descriptorMap = new HashMap<String, Object>(); |
|
503 |
for (Annotation a : annots) { |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
504 |
if (a instanceof DescriptorFields) |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
505 |
addDescriptorFieldsToMap(descriptorMap, (DescriptorFields) a); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
506 |
addAnnotationFieldsToMap(descriptorMap, a); |
2 | 507 |
} |
508 |
||
509 |
if (descriptorMap.isEmpty()) |
|
510 |
return ImmutableDescriptor.EMPTY_DESCRIPTOR; |
|
511 |
else |
|
512 |
return new ImmutableDescriptor(descriptorMap); |
|
513 |
} |
|
514 |
||
1699
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
515 |
/** |
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
516 |
* Array of thrown excepions. |
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
517 |
* @param exceptions can be null; |
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
518 |
* @return An Array of Exception class names. Size is 0 if method is null. |
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
519 |
*/ |
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
520 |
private static String[] getAllExceptions(Class<?>[] exceptions) { |
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
521 |
Set<String> set = new LinkedHashSet<String>(); |
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
522 |
for(Class<?>ex : exceptions) |
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
523 |
set.add(ex.getName()); |
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
524 |
|
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
525 |
String[] arr = new String[set.size()]; |
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
526 |
return set.toArray(arr); |
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
527 |
} |
3611e5fd6da5
6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents:
1510
diff
changeset
|
528 |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
529 |
private static void addDescriptorFieldsToMap( |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
530 |
Map<String, Object> descriptorMap, DescriptorFields df) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
531 |
for (String field : df.value()) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
532 |
int eq = field.indexOf('='); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
533 |
if (eq < 0) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
534 |
throw new IllegalArgumentException( |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
535 |
"@DescriptorFields string must contain '=': " + |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
536 |
field); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
537 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
538 |
String name = field.substring(0, eq); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
539 |
String value = field.substring(eq + 1); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
540 |
addToMap(descriptorMap, name, value); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
541 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
542 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
543 |
|
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
544 |
private static void addAnnotationFieldsToMap( |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
545 |
Map<String, Object> descriptorMap, Annotation a) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
546 |
Class<? extends Annotation> c = a.annotationType(); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
547 |
Method[] elements = c.getMethods(); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
548 |
for (Method element : elements) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
549 |
DescriptorKey key = element.getAnnotation(DescriptorKey.class); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
550 |
if (key != null) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
551 |
String name = key.value(); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
552 |
Object value; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
553 |
try { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
554 |
value = element.invoke(a); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
555 |
} catch (RuntimeException e) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
556 |
// we don't expect this - except for possibly |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
557 |
// security exceptions? |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
558 |
// RuntimeExceptions shouldn't be "UndeclaredThrowable". |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
559 |
// anyway... |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
560 |
throw e; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
561 |
} catch (Exception e) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
562 |
// we don't expect this |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
563 |
throw new UndeclaredThrowableException(e); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
564 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
565 |
if (!key.omitIfDefault() || |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
566 |
!equals(value, element.getDefaultValue())) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
567 |
value = annotationToField(value); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
568 |
addToMap(descriptorMap, name, value); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
569 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
570 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
571 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
572 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
573 |
|
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
574 |
private static void addToMap( |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
575 |
Map<String, Object> descriptorMap, String name, Object value) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
576 |
Object oldValue = descriptorMap.put(name, value); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
577 |
if (oldValue != null && !equals(oldValue, value)) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
578 |
final String msg = |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
579 |
"Inconsistent values for descriptor field " + name + |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
580 |
" from annotations: " + value + " :: " + oldValue; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
581 |
throw new IllegalArgumentException(msg); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
582 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
583 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
584 |
|
2 | 585 |
/** |
586 |
* Throws a NotCompliantMBeanException or a SecurityException. |
|
587 |
* @param notCompliant the class which was under examination |
|
588 |
* @param cause the raeson why NotCompliantMBeanException should |
|
589 |
* be thrown. |
|
590 |
* @return nothing - this method always throw an exception. |
|
591 |
* The return type makes it possible to write |
|
592 |
* <pre> throw throwException(clazz,cause); </pre> |
|
593 |
* @throws SecurityException - if cause is a SecurityException |
|
594 |
* @throws NotCompliantMBeanException otherwise. |
|
595 |
**/ |
|
596 |
static NotCompliantMBeanException throwException(Class<?> notCompliant, |
|
597 |
Throwable cause) |
|
598 |
throws NotCompliantMBeanException, SecurityException { |
|
599 |
if (cause instanceof SecurityException) |
|
600 |
throw (SecurityException) cause; |
|
601 |
if (cause instanceof NotCompliantMBeanException) |
|
602 |
throw (NotCompliantMBeanException)cause; |
|
603 |
final String classname = |
|
604 |
(notCompliant==null)?"null class":notCompliant.getName(); |
|
605 |
final String reason = |
|
606 |
(cause==null)?"Not compliant":cause.getMessage(); |
|
607 |
final NotCompliantMBeanException res = |
|
608 |
new NotCompliantMBeanException(classname+": "+reason); |
|
609 |
res.initCause(cause); |
|
610 |
throw res; |
|
611 |
} |
|
612 |
||
613 |
// Convert a value from an annotation element to a descriptor field value |
|
614 |
// E.g. with @interface Foo {class value()} an annotation @Foo(String.class) |
|
615 |
// will produce a Descriptor field value "java.lang.String" |
|
616 |
private static Object annotationToField(Object x) { |
|
617 |
// An annotation element cannot have a null value but never mind |
|
618 |
if (x == null) |
|
619 |
return null; |
|
620 |
if (x instanceof Number || x instanceof String || |
|
621 |
x instanceof Character || x instanceof Boolean || |
|
622 |
x instanceof String[]) |
|
623 |
return x; |
|
624 |
// Remaining possibilities: array of primitive (e.g. int[]), |
|
625 |
// enum, class, array of enum or class. |
|
626 |
Class<?> c = x.getClass(); |
|
627 |
if (c.isArray()) { |
|
628 |
if (c.getComponentType().isPrimitive()) |
|
629 |
return x; |
|
630 |
Object[] xx = (Object[]) x; |
|
631 |
String[] ss = new String[xx.length]; |
|
632 |
for (int i = 0; i < xx.length; i++) |
|
633 |
ss[i] = (String) annotationToField(xx[i]); |
|
634 |
return ss; |
|
635 |
} |
|
1510
e747d3193ef2
6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents:
1322
diff
changeset
|
636 |
if (x instanceof Class<?>) |
2 | 637 |
return ((Class<?>) x).getName(); |
1510
e747d3193ef2
6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents:
1322
diff
changeset
|
638 |
if (x instanceof Enum<?>) |
e747d3193ef2
6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents:
1322
diff
changeset
|
639 |
return ((Enum<?>) x).name(); |
2 | 640 |
// The only other possibility is that the value is another |
641 |
// annotation, or that the language has evolved since this code |
|
642 |
// was written. We don't allow for either of those currently. |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
643 |
// If it is indeed another annotation, then x will be a proxy |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
644 |
// with an unhelpful name like $Proxy2. So we extract the |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
645 |
// proxy's interface to use that in the exception message. |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
646 |
if (Proxy.isProxyClass(c)) |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
647 |
c = c.getInterfaces()[0]; // array "can't be empty" |
2 | 648 |
throw new IllegalArgumentException("Illegal type for annotation " + |
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
649 |
"element using @DescriptorKey: " + c.getName()); |
2 | 650 |
} |
651 |
||
652 |
// This must be consistent with the check for duplicate field values in |
|
653 |
// ImmutableDescriptor.union. But we don't expect to be called very |
|
654 |
// often so this inefficient check should be enough. |
|
655 |
private static boolean equals(Object x, Object y) { |
|
656 |
return Arrays.deepEquals(new Object[] {x}, new Object[] {y}); |
|
657 |
} |
|
658 |
||
659 |
/** |
|
660 |
* Returns the XXMBean interface or null if no such interface exists |
|
661 |
* |
|
662 |
* @param c The interface to be tested |
|
663 |
* @param clName The name of the class implementing this interface |
|
664 |
*/ |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
665 |
private static <T> Class<? super T> implementsMBean(Class<T> c, String clName) { |
2 | 666 |
String clMBeanName = clName + "MBean"; |
667 |
if (c.getName().equals(clMBeanName)) { |
|
668 |
return c; |
|
669 |
} |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
670 |
Class<?>[] interfaces = c.getInterfaces(); |
2 | 671 |
for (int i = 0;i < interfaces.length; i++) { |
672 |
if (interfaces[i].getName().equals(clMBeanName)) |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
673 |
return Util.cast(interfaces[i]); |
2 | 674 |
} |
675 |
||
676 |
return null; |
|
677 |
} |
|
34 | 678 |
|
679 |
public static Object elementFromComplex(Object complex, String element) |
|
680 |
throws AttributeNotFoundException { |
|
681 |
try { |
|
682 |
if (complex.getClass().isArray() && element.equals("length")) { |
|
683 |
return Array.getLength(complex); |
|
684 |
} else if (complex instanceof CompositeData) { |
|
685 |
return ((CompositeData) complex).get(element); |
|
686 |
} else { |
|
687 |
// Java Beans introspection |
|
688 |
// |
|
689 |
BeanInfo bi = java.beans.Introspector.getBeanInfo(complex.getClass()); |
|
690 |
PropertyDescriptor[] pds = bi.getPropertyDescriptors(); |
|
691 |
for (PropertyDescriptor pd : pds) |
|
692 |
if (pd.getName().equals(element)) |
|
693 |
return pd.getReadMethod().invoke(complex); |
|
694 |
throw new AttributeNotFoundException( |
|
695 |
"Could not find the getter method for the property " + |
|
696 |
element + " using the Java Beans introspector"); |
|
697 |
} |
|
698 |
} catch (InvocationTargetException e) { |
|
699 |
throw new IllegalArgumentException(e); |
|
700 |
} catch (AttributeNotFoundException e) { |
|
701 |
throw e; |
|
702 |
} catch (Exception e) { |
|
703 |
throw EnvHelp.initCause( |
|
704 |
new AttributeNotFoundException(e.getMessage()), e); |
|
705 |
} |
|
706 |
} |
|
2 | 707 |
} |