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