author | emcmanus |
Thu, 27 Nov 2008 15:44:32 +0100 | |
changeset 1636 | eb801ce73ac9 |
parent 1510 | e747d3193ef2 |
child 1701 | f5cedaa459bf |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 2002-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 javax.management; |
|
27 |
||
28 |
import com.sun.jmx.mbeanserver.DescriptorCache; |
|
29 |
import com.sun.jmx.mbeanserver.Introspector; |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
30 |
import com.sun.jmx.mbeanserver.MBeanInjector; |
2 | 31 |
import com.sun.jmx.mbeanserver.MBeanSupport; |
32 |
import com.sun.jmx.mbeanserver.MXBeanSupport; |
|
33 |
import com.sun.jmx.mbeanserver.StandardMBeanSupport; |
|
34 |
import com.sun.jmx.mbeanserver.Util; |
|
35 |
import java.security.AccessController; |
|
36 |
import java.security.PrivilegedAction; |
|
37 |
import java.util.HashMap; |
|
38 |
import java.util.Map; |
|
39 |
import java.util.WeakHashMap; |
|
40 |
import java.util.logging.Level; |
|
687 | 41 |
import javax.management.openmbean.MXBeanMappingFactory; |
2 | 42 |
import javax.management.openmbean.OpenMBeanAttributeInfo; |
43 |
import javax.management.openmbean.OpenMBeanAttributeInfoSupport; |
|
44 |
import javax.management.openmbean.OpenMBeanConstructorInfo; |
|
45 |
import javax.management.openmbean.OpenMBeanConstructorInfoSupport; |
|
46 |
import javax.management.openmbean.OpenMBeanOperationInfo; |
|
47 |
import javax.management.openmbean.OpenMBeanOperationInfoSupport; |
|
48 |
import javax.management.openmbean.OpenMBeanParameterInfo; |
|
49 |
import javax.management.openmbean.OpenMBeanParameterInfoSupport; |
|
50 |
||
687 | 51 |
import static com.sun.jmx.defaults.JmxProperties.MISC_LOGGER; |
52 |
import static javax.management.JMX.MBeanOptions; |
|
53 |
||
2 | 54 |
/** |
55 |
* <p>An MBean whose management interface is determined by reflection |
|
56 |
* on a Java interface.</p> |
|
57 |
* |
|
58 |
* <p>This class brings more flexibility to the notion of Management |
|
59 |
* Interface in the use of Standard MBeans. Straightforward use of |
|
60 |
* the patterns for Standard MBeans described in the JMX Specification |
|
61 |
* means that there is a fixed relationship between the implementation |
|
62 |
* class of an MBean and its management interface (i.e., if the |
|
63 |
* implementation class is Thing, the management interface must be |
|
64 |
* ThingMBean). This class makes it possible to keep the convenience |
|
65 |
* of specifying the management interface with a Java interface, |
|
66 |
* without requiring that there be any naming relationship between the |
|
67 |
* implementation and interface classes.</p> |
|
68 |
* |
|
69 |
* <p>By making a DynamicMBean out of an MBean, this class makes |
|
70 |
* it possible to select any interface implemented by the MBean as its |
|
71 |
* management interface, provided that it complies with JMX patterns |
|
72 |
* (i.e., attributes defined by getter/setter etc...).</p> |
|
73 |
* |
|
74 |
* <p> This class also provides hooks that make it possible to supply |
|
75 |
* custom descriptions and names for the {@link MBeanInfo} returned by |
|
76 |
* the DynamicMBean interface.</p> |
|
77 |
* |
|
78 |
* <p>Using this class, an MBean can be created with any |
|
79 |
* implementation class name <i>Impl</i> and with a management |
|
80 |
* interface defined (as for current Standard MBeans) by any interface |
|
81 |
* <i>Intf</i>, in one of two general ways:</p> |
|
82 |
* |
|
83 |
* <ul> |
|
84 |
* |
|
85 |
* <li>Using the public constructor |
|
86 |
* {@link #StandardMBean(java.lang.Object, java.lang.Class, boolean) |
|
87 |
* StandardMBean(impl,interface)}: |
|
88 |
* <pre> |
|
89 |
* MBeanServer mbs; |
|
90 |
* ... |
|
91 |
* Impl impl = new Impl(...); |
|
92 |
* StandardMBean mbean = new StandardMBean(impl, Intf.class, false); |
|
93 |
* mbs.registerMBean(mbean, objectName); |
|
94 |
* </pre></li> |
|
95 |
* |
|
96 |
* <li>Subclassing StandardMBean: |
|
97 |
* <pre> |
|
98 |
* public class Impl extends StandardMBean implements Intf { |
|
99 |
* public Impl() { |
|
100 |
* super(Intf.class, false); |
|
101 |
* } |
|
102 |
* // implement methods of Intf |
|
103 |
* } |
|
104 |
* |
|
105 |
* [...] |
|
106 |
* |
|
107 |
* MBeanServer mbs; |
|
108 |
* .... |
|
109 |
* Impl impl = new Impl(); |
|
110 |
* mbs.registerMBean(impl, objectName); |
|
111 |
* </pre></li> |
|
112 |
* |
|
113 |
* </ul> |
|
114 |
* |
|
115 |
* <p>In either case, the class <i>Impl</i> must implement the |
|
116 |
* interface <i>Intf</i>.</p> |
|
117 |
* |
|
118 |
* <p>Standard MBeans based on the naming relationship between |
|
119 |
* implementation and interface classes are of course still |
|
120 |
* available.</p> |
|
121 |
* |
|
122 |
* <p>This class may also be used to construct MXBeans. The usage |
|
123 |
* is exactly the same as for Standard MBeans except that in the |
|
124 |
* examples above, the {@code false} parameter to the constructor or |
|
125 |
* {@code super(...)} invocation is instead {@code true}.</p> |
|
126 |
* |
|
127 |
* @since 1.5 |
|
128 |
*/ |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
129 |
public class StandardMBean implements DynamicWrapperMBean, MBeanRegistration { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
130 |
|
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
131 |
/** |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
132 |
* <p>Options controlling the behavior of {@code StandardMBean} instances.</p> |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
133 |
*/ |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
134 |
public static class Options extends JMX.MBeanOptions { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
135 |
private static final long serialVersionUID = 5107355471177517164L; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
136 |
|
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
137 |
private boolean wrappedVisible; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
138 |
|
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
139 |
/** |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
140 |
* <p>Construct an {@code Options} object where all options have |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
141 |
* their default values.</p> |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
142 |
*/ |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
143 |
public Options() {} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
144 |
|
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
145 |
@Override |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
146 |
public Options clone() { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
147 |
return (Options) super.clone(); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
148 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
149 |
|
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
150 |
/** |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
151 |
* <p>Defines whether the {@link StandardMBean#getWrappedObject() |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
152 |
* getWrappedObject} method returns the wrapped object.</p> |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
153 |
* |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
154 |
* <p>If this option is true, then {@code getWrappedObject()} will return |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
155 |
* the same object as {@link StandardMBean#getImplementation() |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
156 |
* getImplementation}. Otherwise, it will return the |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
157 |
* StandardMBean instance itself. The setting of this option |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
158 |
* affects the behavior of {@link MBeanServer#getClassLoaderFor |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
159 |
* MBeanServer.getClassLoaderFor} and {@link MBeanServer#isInstanceOf |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
160 |
* MBeanServer.isInstanceOf}. The default value is false for |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
161 |
* compatibility reasons, but true is a better value for most new code.</p> |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
162 |
* |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
163 |
* @return true if this StandardMBean's {@link |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
164 |
* StandardMBean#getWrappedObject getWrappedObject} returns the wrapped |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
165 |
* object. |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
166 |
*/ |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
167 |
public boolean isWrappedObjectVisible() { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
168 |
return this.wrappedVisible; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
169 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
170 |
|
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
171 |
/** |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
172 |
* <p>Set the {@link #isWrappedObjectVisible WrappedObjectVisible} option |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
173 |
* to the given value.</p> |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
174 |
* @param visible the new value. |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
175 |
*/ |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
176 |
public void setWrappedObjectVisible(boolean visible) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
177 |
this.wrappedVisible = visible; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
178 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
179 |
|
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
180 |
// Canonical objects for each of (MXBean,!MXBean) x (WVisible,!WVisible) |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
181 |
private static final Options[] CANONICALS = { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
182 |
new Options(), new Options(), new Options(), new Options(), |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
183 |
}; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
184 |
static { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
185 |
CANONICALS[1].setMXBeanMappingFactory(MXBeanMappingFactory.DEFAULT); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
186 |
CANONICALS[2].setWrappedObjectVisible(true); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
187 |
CANONICALS[3].setMXBeanMappingFactory(MXBeanMappingFactory.DEFAULT); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
188 |
CANONICALS[3].setWrappedObjectVisible(true); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
189 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
190 |
@Override |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
191 |
MBeanOptions[] canonicals() { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
192 |
return CANONICALS; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
193 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
194 |
|
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
195 |
@Override |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
196 |
boolean same(MBeanOptions opts) { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
197 |
return (super.same(opts) && opts instanceof Options && |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
198 |
((Options) opts).wrappedVisible == wrappedVisible); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
199 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
200 |
} |
2 | 201 |
|
202 |
private final static DescriptorCache descriptors = |
|
203 |
DescriptorCache.getInstance(JMX.proof); |
|
204 |
||
205 |
/** |
|
206 |
* The DynamicMBean that wraps the MXBean or Standard MBean implementation. |
|
207 |
**/ |
|
208 |
private volatile MBeanSupport<?> mbean; |
|
209 |
||
210 |
/** |
|
211 |
* The cached MBeanInfo. |
|
212 |
**/ |
|
213 |
private volatile MBeanInfo cachedMBeanInfo; |
|
214 |
||
215 |
/** |
|
687 | 216 |
* The MBeanOptions for this StandardMBean. |
217 |
**/ |
|
218 |
private MBeanOptions options; |
|
219 |
||
220 |
/** |
|
2 | 221 |
* Make a DynamicMBean out of <var>implementation</var>, using the |
222 |
* specified <var>mbeanInterface</var> class. |
|
223 |
* @param implementation The implementation of this MBean. |
|
224 |
* If <code>null</code>, and null implementation is allowed, |
|
225 |
* then the implementation is assumed to be <var>this</var>. |
|
226 |
* @param mbeanInterface The Management Interface exported by this |
|
227 |
* MBean's implementation. If <code>null</code>, then this |
|
228 |
* object will use standard JMX design pattern to determine |
|
229 |
* the management interface associated with the given |
|
230 |
* implementation. |
|
231 |
* @param nullImplementationAllowed <code>true</code> if a null |
|
232 |
* implementation is allowed. If null implementation is allowed, |
|
233 |
* and a null implementation is passed, then the implementation |
|
234 |
* is assumed to be <var>this</var>. |
|
687 | 235 |
* @param options MBeanOptions to apply to this instance. |
2 | 236 |
* @exception IllegalArgumentException if the given |
237 |
* <var>implementation</var> is null, and null is not allowed. |
|
238 |
**/ |
|
687 | 239 |
@SuppressWarnings("unchecked") // cast to T |
2 | 240 |
private <T> void construct(T implementation, Class<T> mbeanInterface, |
241 |
boolean nullImplementationAllowed, |
|
687 | 242 |
MBeanOptions options) |
2 | 243 |
throws NotCompliantMBeanException { |
244 |
if (implementation == null) { |
|
245 |
// Have to use (T)this rather than mbeanInterface.cast(this) |
|
246 |
// because mbeanInterface might be null. |
|
247 |
if (nullImplementationAllowed) |
|
248 |
implementation = Util.<T>cast(this); |
|
249 |
else throw new IllegalArgumentException("implementation is null"); |
|
250 |
} |
|
687 | 251 |
if (options == null) |
252 |
options = new MBeanOptions(); |
|
253 |
MXBeanMappingFactory mappingFactory = options.getMXBeanMappingFactory(); |
|
254 |
boolean mx = (mappingFactory != null); |
|
255 |
if (mbeanInterface == null) { |
|
256 |
mbeanInterface = Util.cast(Introspector.getStandardOrMXBeanInterface( |
|
257 |
implementation.getClass(), mx)); |
|
258 |
} |
|
259 |
if (mx) { |
|
260 |
this.mbean = |
|
261 |
new MXBeanSupport(implementation, mbeanInterface, |
|
262 |
mappingFactory); |
|
2 | 263 |
} else { |
264 |
this.mbean = |
|
265 |
new StandardMBeanSupport(implementation, mbeanInterface); |
|
266 |
} |
|
687 | 267 |
this.options = options.canonical(); |
2 | 268 |
} |
269 |
||
270 |
/** |
|
271 |
* <p>Make a DynamicMBean out of the object |
|
272 |
* <var>implementation</var>, using the specified |
|
273 |
* <var>mbeanInterface</var> class.</p> |
|
274 |
* |
|
275 |
* @param implementation The implementation of this MBean. |
|
276 |
* @param mbeanInterface The Management Interface exported by this |
|
277 |
* MBean's implementation. If <code>null</code>, then this |
|
278 |
* object will use standard JMX design pattern to determine |
|
279 |
* the management interface associated with the given |
|
280 |
* implementation. |
|
281 |
* @param <T> Allows the compiler to check |
|
282 |
* that {@code implementation} does indeed implement the class |
|
283 |
* described by {@code mbeanInterface}. The compiler can only |
|
284 |
* check this if {@code mbeanInterface} is a class literal such |
|
285 |
* as {@code MyMBean.class}. |
|
286 |
* |
|
287 |
* @exception IllegalArgumentException if the given |
|
288 |
* <var>implementation</var> is null. |
|
289 |
* @exception NotCompliantMBeanException if the <var>mbeanInterface</var> |
|
290 |
* does not follow JMX design patterns for Management Interfaces, or |
|
291 |
* if the given <var>implementation</var> does not implement the |
|
292 |
* specified interface. |
|
293 |
**/ |
|
294 |
public <T> StandardMBean(T implementation, Class<T> mbeanInterface) |
|
295 |
throws NotCompliantMBeanException { |
|
687 | 296 |
construct(implementation, mbeanInterface, false, null); |
2 | 297 |
} |
298 |
||
299 |
/** |
|
300 |
* <p>Make a DynamicMBean out of <var>this</var>, using the specified |
|
301 |
* <var>mbeanInterface</var> class.</p> |
|
302 |
* |
|
687 | 303 |
* <p>Calls {@link #StandardMBean(java.lang.Object, java.lang.Class) |
2 | 304 |
* this(this,mbeanInterface)}. |
305 |
* This constructor is reserved to subclasses.</p> |
|
306 |
* |
|
307 |
* @param mbeanInterface The Management Interface exported by this |
|
308 |
* MBean. |
|
309 |
* |
|
310 |
* @exception NotCompliantMBeanException if the <var>mbeanInterface</var> |
|
311 |
* does not follow JMX design patterns for Management Interfaces, or |
|
312 |
* if <var>this</var> does not implement the specified interface. |
|
313 |
**/ |
|
314 |
protected StandardMBean(Class<?> mbeanInterface) |
|
315 |
throws NotCompliantMBeanException { |
|
687 | 316 |
construct(null, mbeanInterface, true, null); |
2 | 317 |
} |
318 |
||
319 |
/** |
|
320 |
* <p>Make a DynamicMBean out of the object |
|
321 |
* <var>implementation</var>, using the specified |
|
687 | 322 |
* <var>mbeanInterface</var> class, and choosing whether the |
323 |
* resultant MBean is an MXBean. This constructor can be used |
|
2 | 324 |
* to make either Standard MBeans or MXBeans. Unlike the |
325 |
* constructor {@link #StandardMBean(Object, Class)}, it |
|
326 |
* does not throw NotCompliantMBeanException.</p> |
|
327 |
* |
|
328 |
* @param implementation The implementation of this MBean. |
|
329 |
* @param mbeanInterface The Management Interface exported by this |
|
330 |
* MBean's implementation. If <code>null</code>, then this |
|
331 |
* object will use standard JMX design pattern to determine |
|
332 |
* the management interface associated with the given |
|
333 |
* implementation. |
|
334 |
* @param isMXBean If true, the {@code mbeanInterface} parameter |
|
335 |
* names an MXBean interface and the resultant MBean is an MXBean. |
|
336 |
* @param <T> Allows the compiler to check |
|
337 |
* that {@code implementation} does indeed implement the class |
|
338 |
* described by {@code mbeanInterface}. The compiler can only |
|
339 |
* check this if {@code mbeanInterface} is a class literal such |
|
340 |
* as {@code MyMBean.class}. |
|
341 |
* |
|
342 |
* @exception IllegalArgumentException if the given |
|
343 |
* <var>implementation</var> is null, or if the <var>mbeanInterface</var> |
|
344 |
* does not follow JMX design patterns for Management Interfaces, or |
|
345 |
* if the given <var>implementation</var> does not implement the |
|
346 |
* specified interface. |
|
347 |
* |
|
348 |
* @since 1.6 |
|
349 |
**/ |
|
350 |
public <T> StandardMBean(T implementation, Class<T> mbeanInterface, |
|
351 |
boolean isMXBean) { |
|
352 |
try { |
|
687 | 353 |
MBeanOptions opts = new MBeanOptions(); |
354 |
if (mbeanInterface == null) { |
|
355 |
mbeanInterface = Util.cast(Introspector.getStandardOrMXBeanInterface( |
|
356 |
implementation.getClass(), isMXBean)); |
|
357 |
} |
|
358 |
if (isMXBean) { |
|
359 |
MXBeanMappingFactory f = MXBeanMappingFactory.forInterface( |
|
360 |
mbeanInterface); |
|
361 |
opts.setMXBeanMappingFactory(f); |
|
362 |
} |
|
363 |
construct(implementation, mbeanInterface, false, opts); |
|
2 | 364 |
} catch (NotCompliantMBeanException e) { |
365 |
throw new IllegalArgumentException(e); |
|
366 |
} |
|
367 |
} |
|
368 |
||
369 |
/** |
|
370 |
* <p>Make a DynamicMBean out of <var>this</var>, using the specified |
|
687 | 371 |
* <var>mbeanInterface</var> class, and choosing whether the resulting |
372 |
* MBean is an MXBean. This constructor can be used |
|
2 | 373 |
* to make either Standard MBeans or MXBeans. Unlike the |
374 |
* constructor {@link #StandardMBean(Object, Class)}, it |
|
375 |
* does not throw NotCompliantMBeanException.</p> |
|
376 |
* |
|
687 | 377 |
* <p>Calls {@link #StandardMBean(java.lang.Object, java.lang.Class, boolean) |
2 | 378 |
* this(this, mbeanInterface, isMXBean)}. |
379 |
* This constructor is reserved to subclasses.</p> |
|
380 |
* |
|
381 |
* @param mbeanInterface The Management Interface exported by this |
|
382 |
* MBean. |
|
383 |
* @param isMXBean If true, the {@code mbeanInterface} parameter |
|
384 |
* names an MXBean interface and the resultant MBean is an MXBean. |
|
385 |
* |
|
386 |
* @exception IllegalArgumentException if the <var>mbeanInterface</var> |
|
387 |
* does not follow JMX design patterns for Management Interfaces, or |
|
388 |
* if <var>this</var> does not implement the specified interface. |
|
389 |
* |
|
390 |
* @since 1.6 |
|
391 |
**/ |
|
392 |
protected StandardMBean(Class<?> mbeanInterface, boolean isMXBean) { |
|
393 |
try { |
|
687 | 394 |
MBeanOptions opts = new MBeanOptions(); |
395 |
if (mbeanInterface == null) { |
|
396 |
mbeanInterface = Introspector.getStandardOrMXBeanInterface( |
|
397 |
getClass(), isMXBean); |
|
398 |
} |
|
399 |
if (isMXBean) { |
|
400 |
MXBeanMappingFactory f = MXBeanMappingFactory.forInterface( |
|
401 |
mbeanInterface); |
|
402 |
opts.setMXBeanMappingFactory(f); |
|
403 |
} |
|
404 |
construct(null, mbeanInterface, true, opts); |
|
405 |
} catch (NotCompliantMBeanException e) { |
|
406 |
throw new IllegalArgumentException(e); |
|
407 |
} |
|
408 |
} |
|
409 |
||
410 |
/** |
|
411 |
* <p>Make a DynamicMBean out of the object |
|
412 |
* <var>implementation</var>, using the specified |
|
413 |
* <var>mbeanInterface</var> class and the specified options.</p> |
|
414 |
* |
|
415 |
* @param implementation The implementation of this MBean. |
|
416 |
* @param mbeanInterface The Management Interface exported by this |
|
417 |
* MBean's implementation. If <code>null</code>, then this |
|
418 |
* object will use standard JMX design pattern to determine |
|
419 |
* the management interface associated with the given |
|
420 |
* implementation. |
|
421 |
* @param options MBeanOptions that control the operation of the resulting |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
422 |
* MBean. |
687 | 423 |
* @param <T> Allows the compiler to check |
424 |
* that {@code implementation} does indeed implement the class |
|
425 |
* described by {@code mbeanInterface}. The compiler can only |
|
426 |
* check this if {@code mbeanInterface} is a class literal such |
|
427 |
* as {@code MyMBean.class}. |
|
428 |
* |
|
429 |
* @exception IllegalArgumentException if the given |
|
430 |
* <var>implementation</var> is null, or if the <var>mbeanInterface</var> |
|
431 |
* does not follow JMX design patterns for Management Interfaces, or |
|
432 |
* if the given <var>implementation</var> does not implement the |
|
433 |
* specified interface. |
|
434 |
**/ |
|
435 |
public <T> StandardMBean(T implementation, |
|
436 |
Class<T> mbeanInterface, |
|
437 |
MBeanOptions options) { |
|
438 |
try { |
|
439 |
construct(implementation, mbeanInterface, false, options); |
|
440 |
} catch (NotCompliantMBeanException e) { |
|
441 |
throw new IllegalArgumentException(e); |
|
442 |
} |
|
443 |
} |
|
444 |
||
445 |
/** |
|
446 |
* <p>Make a DynamicMBean out of <var>this</var>, using the specified |
|
447 |
* <var>mbeanInterface</var> class and the specified options.</p> |
|
448 |
* |
|
449 |
* <p>Calls {@link #StandardMBean(Object, Class, JMX.MBeanOptions) |
|
450 |
* this(this,mbeanInterface,options)}. |
|
451 |
* This constructor is reserved to subclasses.</p> |
|
452 |
* |
|
453 |
* @param mbeanInterface The Management Interface exported by this |
|
454 |
* MBean. |
|
455 |
* @param options MBeanOptions that control the operation of the resulting |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
456 |
* MBean. |
687 | 457 |
* |
458 |
* @exception IllegalArgumentException if the <var>mbeanInterface</var> |
|
459 |
* does not follow JMX design patterns for Management Interfaces, or |
|
460 |
* if <var>this</var> does not implement the specified interface. |
|
461 |
**/ |
|
462 |
protected StandardMBean(Class<?> mbeanInterface, MBeanOptions options) { |
|
463 |
try { |
|
464 |
construct(null, mbeanInterface, true, options); |
|
2 | 465 |
} catch (NotCompliantMBeanException e) { |
466 |
throw new IllegalArgumentException(e); |
|
467 |
} |
|
468 |
} |
|
469 |
||
470 |
/** |
|
471 |
* <p>Replace the implementation object wrapped in this object.</p> |
|
472 |
* |
|
473 |
* @param implementation The new implementation of this Standard MBean |
|
474 |
* (or MXBean). The <code>implementation</code> object must implement |
|
475 |
* the Standard MBean (or MXBean) interface that was supplied when this |
|
476 |
* <code>StandardMBean</code> was constructed. |
|
477 |
* |
|
478 |
* @exception IllegalArgumentException if the given |
|
479 |
* <var>implementation</var> is null. |
|
480 |
* |
|
481 |
* @exception NotCompliantMBeanException if the given |
|
482 |
* <var>implementation</var> does not implement the |
|
483 |
* Standard MBean (or MXBean) interface that was |
|
484 |
* supplied at construction. |
|
485 |
* |
|
486 |
* @see #getImplementation |
|
487 |
**/ |
|
488 |
public void setImplementation(Object implementation) |
|
489 |
throws NotCompliantMBeanException { |
|
490 |
||
491 |
if (implementation == null) |
|
492 |
throw new IllegalArgumentException("implementation is null"); |
|
687 | 493 |
setImplementation2(implementation); |
494 |
} |
|
2 | 495 |
|
687 | 496 |
private <T> void setImplementation2(T implementation) |
497 |
throws NotCompliantMBeanException { |
|
498 |
Class<? super T> intf = Util.cast(getMBeanInterface()); |
|
499 |
||
500 |
if (this.mbean.isMXBean()) { |
|
2 | 501 |
this.mbean = new MXBeanSupport(implementation, |
687 | 502 |
intf, |
503 |
options.getMXBeanMappingFactory()); |
|
2 | 504 |
} else { |
687 | 505 |
this.mbean = new StandardMBeanSupport(implementation, intf); |
2 | 506 |
} |
507 |
} |
|
508 |
||
509 |
/** |
|
510 |
* Get the implementation of this Standard MBean (or MXBean). |
|
511 |
* @return The implementation of this Standard MBean (or MXBean). |
|
512 |
* |
|
513 |
* @see #setImplementation |
|
514 |
**/ |
|
515 |
public Object getImplementation() { |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
516 |
return mbean.getWrappedObject(); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
517 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
518 |
|
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
519 |
/** |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
520 |
* <p>Get the wrapped implementation object or return this object.</p> |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
521 |
* |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
522 |
* <p>For compatibility reasons, this method only returns the wrapped |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
523 |
* implementation object if the {@link Options#isWrappedObjectVisible |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
524 |
* WrappedObjectVisible} option was specified when this StandardMBean |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
525 |
* was created. Otherwise it returns {@code this}.</p> |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
526 |
* |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
527 |
* <p>If you want the MBeanServer's {@link MBeanServer#getClassLoaderFor |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
528 |
* getClassLoaderFor} and {@link MBeanServer#isInstanceOf |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
529 |
* isInstanceOf} methods to refer to the wrapped implementation and |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
530 |
* not this StandardMBean object, then you must set the |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
531 |
* {@code WrappedObjectVisible} option, for example using:</p> |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
532 |
* |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
533 |
* <pre> |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
534 |
* StandardMBean.Options opts = new StandardMBean.Options(); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
535 |
* opts.setWrappedObjectVisible(true); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
536 |
* StandardMBean mbean = new StandardMBean(impl, MyMBean.class, opts); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
537 |
* </pre> |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
538 |
* |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
539 |
* @return The wrapped implementation object, or this StandardMBean |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
540 |
* instance. |
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 |
public Object getWrappedObject() { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
543 |
if (options instanceof Options && |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
544 |
((Options) options).isWrappedObjectVisible()) |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
545 |
return getImplementation(); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
546 |
else |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
547 |
return this; |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
548 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
549 |
|
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
550 |
/** |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
551 |
* <p>Get the ClassLoader of the wrapped implementation object or of this |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
552 |
* object.</p> |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
553 |
* |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
554 |
* <p>For compatibility reasons, this method only returns the ClassLoader |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
555 |
* of the wrapped implementation object if the {@link |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
556 |
* Options#isWrappedObjectVisible WrappedObjectVisible} option was |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
557 |
* specified when this StandardMBean was created. Otherwise it returns |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
558 |
* {@code this.getClass().getClassLoader()}.</p> |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
559 |
* |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
560 |
* <p>If you want the MBeanServer's {@link MBeanServer#getClassLoaderFor |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
561 |
* getClassLoaderFor} and {@link MBeanServer#isInstanceOf |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
562 |
* isInstanceOf} methods to refer to the wrapped implementation and |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
563 |
* not this StandardMBean object, then you must set the |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
564 |
* {@code WrappedObjectVisible} option, for example using:</p> |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
565 |
* |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
566 |
* <pre> |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
567 |
* StandardMBean.Options opts = new StandardMBean.Options(); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
568 |
* opts.setWrappedObjectVisible(true); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
569 |
* StandardMBean mbean = new StandardMBean(impl, MyMBean.class, opts); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
570 |
* </pre> |
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 |
* @return The ClassLoader of the wrapped Cimplementation object, or of |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
573 |
* this StandardMBean instance. |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
574 |
*/ |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
575 |
public ClassLoader getWrappedClassLoader() { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
576 |
return getWrappedObject().getClass().getClassLoader(); |
2 | 577 |
} |
578 |
||
579 |
/** |
|
580 |
* Get the Management Interface of this Standard MBean (or MXBean). |
|
581 |
* @return The management interface of this Standard MBean (or MXBean). |
|
582 |
**/ |
|
583 |
public final Class<?> getMBeanInterface() { |
|
584 |
return mbean.getMBeanInterface(); |
|
585 |
} |
|
586 |
||
587 |
/** |
|
588 |
* Get the class of the implementation of this Standard MBean (or MXBean). |
|
589 |
* @return The class of the implementation of this Standard MBean (or MXBean). |
|
590 |
**/ |
|
591 |
public Class<?> getImplementationClass() { |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
592 |
return mbean.getWrappedObject().getClass(); |
2 | 593 |
} |
594 |
||
687 | 595 |
/** |
596 |
* Return the MBeanOptions that were specified or implied for this StandardMBean |
|
597 |
* instance. If an MBeanOptions object was supplied when this StandardMBean |
|
598 |
* instance was constructed, and if that object has not been modified in the |
|
599 |
* meantime, then the returned object will be equal to that object, although |
|
600 |
* it might not be the same object. |
|
601 |
* @return The MBeanOptions that were specified or implied for this StandardMBean |
|
602 |
* instance. |
|
603 |
*/ |
|
604 |
public MBeanOptions getOptions() { |
|
605 |
return options.uncanonical(); |
|
606 |
} |
|
607 |
||
2 | 608 |
// ------------------------------------------------------------------ |
609 |
// From the DynamicMBean interface. |
|
610 |
// ------------------------------------------------------------------ |
|
611 |
public Object getAttribute(String attribute) |
|
612 |
throws AttributeNotFoundException, |
|
613 |
MBeanException, |
|
614 |
ReflectionException { |
|
615 |
return mbean.getAttribute(attribute); |
|
616 |
} |
|
617 |
||
618 |
// ------------------------------------------------------------------ |
|
619 |
// From the DynamicMBean interface. |
|
620 |
// ------------------------------------------------------------------ |
|
621 |
public void setAttribute(Attribute attribute) |
|
622 |
throws AttributeNotFoundException, |
|
623 |
InvalidAttributeValueException, |
|
624 |
MBeanException, |
|
625 |
ReflectionException { |
|
626 |
mbean.setAttribute(attribute); |
|
627 |
} |
|
628 |
||
629 |
// ------------------------------------------------------------------ |
|
630 |
// From the DynamicMBean interface. |
|
631 |
// ------------------------------------------------------------------ |
|
632 |
public AttributeList getAttributes(String[] attributes) { |
|
633 |
return mbean.getAttributes(attributes); |
|
634 |
} |
|
635 |
||
636 |
// ------------------------------------------------------------------ |
|
637 |
// From the DynamicMBean interface. |
|
638 |
// ------------------------------------------------------------------ |
|
639 |
public AttributeList setAttributes(AttributeList attributes) { |
|
640 |
return mbean.setAttributes(attributes); |
|
641 |
} |
|
642 |
||
643 |
// ------------------------------------------------------------------ |
|
644 |
// From the DynamicMBean interface. |
|
645 |
// ------------------------------------------------------------------ |
|
646 |
public Object invoke(String actionName, Object params[], String signature[]) |
|
647 |
throws MBeanException, ReflectionException { |
|
648 |
return mbean.invoke(actionName, params, signature); |
|
649 |
} |
|
650 |
||
651 |
/** |
|
652 |
* Get the {@link MBeanInfo} for this MBean. |
|
653 |
* <p> |
|
654 |
* This method implements |
|
655 |
* {@link javax.management.DynamicMBean#getMBeanInfo() |
|
656 |
* DynamicMBean.getMBeanInfo()}. |
|
657 |
* <p> |
|
658 |
* This method first calls {@link #getCachedMBeanInfo()} in order to |
|
659 |
* retrieve the cached MBeanInfo for this MBean, if any. If the |
|
660 |
* MBeanInfo returned by {@link #getCachedMBeanInfo()} is not null, |
|
661 |
* then it is returned.<br> |
|
662 |
* Otherwise, this method builds a default MBeanInfo for this MBean, |
|
663 |
* using the Management Interface specified for this MBean. |
|
664 |
* <p> |
|
665 |
* While building the MBeanInfo, this method calls the customization |
|
666 |
* hooks that make it possible for subclasses to supply their custom |
|
667 |
* descriptions, parameter names, etc...<br> |
|
668 |
* Finally, it calls {@link #cacheMBeanInfo(javax.management.MBeanInfo) |
|
669 |
* cacheMBeanInfo()} in order to cache the new MBeanInfo. |
|
670 |
* @return The cached MBeanInfo for that MBean, if not null, or a |
|
671 |
* newly built MBeanInfo if none was cached. |
|
672 |
**/ |
|
673 |
public MBeanInfo getMBeanInfo() { |
|
674 |
try { |
|
675 |
final MBeanInfo cached = getCachedMBeanInfo(); |
|
676 |
if (cached != null) return cached; |
|
677 |
} catch (RuntimeException x) { |
|
678 |
if (MISC_LOGGER.isLoggable(Level.FINEST)) { |
|
679 |
MISC_LOGGER.logp(Level.FINEST, |
|
680 |
MBeanServerFactory.class.getName(), "getMBeanInfo", |
|
681 |
"Failed to get cached MBeanInfo", x); |
|
682 |
} |
|
683 |
} |
|
684 |
||
685 |
if (MISC_LOGGER.isLoggable(Level.FINER)) { |
|
686 |
MISC_LOGGER.logp(Level.FINER, |
|
687 |
MBeanServerFactory.class.getName(), "getMBeanInfo", |
|
688 |
"Building MBeanInfo for " + |
|
689 |
getImplementationClass().getName()); |
|
690 |
} |
|
691 |
||
1510 | 692 |
MBeanSupport<?> msupport = mbean; |
2 | 693 |
final MBeanInfo bi = msupport.getMBeanInfo(); |
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
694 |
final Object impl = msupport.getWrappedObject(); |
2 | 695 |
|
696 |
final boolean immutableInfo = immutableInfo(this.getClass()); |
|
697 |
||
698 |
final String cname = getClassName(bi); |
|
699 |
final String text = getDescription(bi); |
|
700 |
final MBeanConstructorInfo[] ctors = getConstructors(bi,impl); |
|
701 |
final MBeanAttributeInfo[] attrs = getAttributes(bi); |
|
702 |
final MBeanOperationInfo[] ops = getOperations(bi); |
|
703 |
final MBeanNotificationInfo[] ntfs = getNotifications(bi); |
|
704 |
final Descriptor desc = getDescriptor(bi, immutableInfo); |
|
705 |
||
706 |
final MBeanInfo nmbi = new MBeanInfo( |
|
707 |
cname, text, attrs, ctors, ops, ntfs, desc); |
|
708 |
try { |
|
709 |
cacheMBeanInfo(nmbi); |
|
710 |
} catch (RuntimeException x) { |
|
711 |
if (MISC_LOGGER.isLoggable(Level.FINEST)) { |
|
712 |
MISC_LOGGER.logp(Level.FINEST, |
|
713 |
MBeanServerFactory.class.getName(), "getMBeanInfo", |
|
714 |
"Failed to cache MBeanInfo", x); |
|
715 |
} |
|
716 |
} |
|
717 |
||
718 |
return nmbi; |
|
719 |
} |
|
720 |
||
721 |
/** |
|
722 |
* Customization hook: |
|
723 |
* Get the className that will be used in the MBeanInfo returned by |
|
724 |
* this MBean. |
|
725 |
* <br> |
|
726 |
* Subclasses may redefine this method in order to supply their |
|
727 |
* custom class name. The default implementation returns |
|
728 |
* {@link MBeanInfo#getClassName() info.getClassName()}. |
|
729 |
* @param info The default MBeanInfo derived by reflection. |
|
730 |
* @return the class name for the new MBeanInfo. |
|
731 |
**/ |
|
732 |
protected String getClassName(MBeanInfo info) { |
|
733 |
if (info == null) return getImplementationClass().getName(); |
|
734 |
return info.getClassName(); |
|
735 |
} |
|
736 |
||
737 |
/** |
|
738 |
* Customization hook: |
|
739 |
* Get the description that will be used in the MBeanInfo returned by |
|
740 |
* this MBean. |
|
741 |
* <br> |
|
742 |
* Subclasses may redefine this method in order to supply their |
|
743 |
* custom MBean description. The default implementation returns |
|
744 |
* {@link MBeanInfo#getDescription() info.getDescription()}. |
|
745 |
* @param info The default MBeanInfo derived by reflection. |
|
746 |
* @return the description for the new MBeanInfo. |
|
747 |
**/ |
|
748 |
protected String getDescription(MBeanInfo info) { |
|
749 |
if (info == null) return null; |
|
750 |
return info.getDescription(); |
|
751 |
} |
|
752 |
||
753 |
/** |
|
754 |
* <p>Customization hook: |
|
755 |
* Get the description that will be used in the MBeanFeatureInfo |
|
756 |
* returned by this MBean.</p> |
|
757 |
* |
|
758 |
* <p>Subclasses may redefine this method in order to supply |
|
759 |
* their custom description. The default implementation returns |
|
760 |
* {@link MBeanFeatureInfo#getDescription() |
|
761 |
* info.getDescription()}.</p> |
|
762 |
* |
|
763 |
* <p>This method is called by |
|
764 |
* {@link #getDescription(MBeanAttributeInfo)}, |
|
765 |
* {@link #getDescription(MBeanOperationInfo)}, |
|
766 |
* {@link #getDescription(MBeanConstructorInfo)}.</p> |
|
767 |
* |
|
768 |
* @param info The default MBeanFeatureInfo derived by reflection. |
|
769 |
* @return the description for the given MBeanFeatureInfo. |
|
770 |
**/ |
|
771 |
protected String getDescription(MBeanFeatureInfo info) { |
|
772 |
if (info == null) return null; |
|
773 |
return info.getDescription(); |
|
774 |
} |
|
775 |
||
776 |
/** |
|
777 |
* Customization hook: |
|
778 |
* Get the description that will be used in the MBeanAttributeInfo |
|
779 |
* returned by this MBean. |
|
780 |
* |
|
781 |
* <p>Subclasses may redefine this method in order to supply their |
|
782 |
* custom description. The default implementation returns {@link |
|
783 |
* #getDescription(MBeanFeatureInfo) |
|
784 |
* getDescription((MBeanFeatureInfo) info)}. |
|
785 |
* @param info The default MBeanAttributeInfo derived by reflection. |
|
786 |
* @return the description for the given MBeanAttributeInfo. |
|
787 |
**/ |
|
788 |
protected String getDescription(MBeanAttributeInfo info) { |
|
789 |
return getDescription((MBeanFeatureInfo)info); |
|
790 |
} |
|
791 |
||
792 |
/** |
|
793 |
* Customization hook: |
|
794 |
* Get the description that will be used in the MBeanConstructorInfo |
|
795 |
* returned by this MBean. |
|
796 |
* <br> |
|
797 |
* Subclasses may redefine this method in order to supply their |
|
798 |
* custom description. |
|
799 |
* The default implementation returns {@link |
|
800 |
* #getDescription(MBeanFeatureInfo) |
|
801 |
* getDescription((MBeanFeatureInfo) info)}. |
|
802 |
* @param info The default MBeanConstructorInfo derived by reflection. |
|
803 |
* @return the description for the given MBeanConstructorInfo. |
|
804 |
**/ |
|
805 |
protected String getDescription(MBeanConstructorInfo info) { |
|
806 |
return getDescription((MBeanFeatureInfo)info); |
|
807 |
} |
|
808 |
||
809 |
/** |
|
810 |
* Customization hook: |
|
811 |
* Get the description that will be used for the <var>sequence</var> |
|
812 |
* MBeanParameterInfo of the MBeanConstructorInfo returned by this MBean. |
|
813 |
* <br> |
|
814 |
* Subclasses may redefine this method in order to supply their |
|
815 |
* custom description. The default implementation returns |
|
816 |
* {@link MBeanParameterInfo#getDescription() param.getDescription()}. |
|
817 |
* |
|
818 |
* @param ctor The default MBeanConstructorInfo derived by reflection. |
|
819 |
* @param param The default MBeanParameterInfo derived by reflection. |
|
820 |
* @param sequence The sequence number of the parameter considered |
|
821 |
* ("0" for the first parameter, "1" for the second parameter, |
|
822 |
* etc...). |
|
823 |
* @return the description for the given MBeanParameterInfo. |
|
824 |
**/ |
|
825 |
protected String getDescription(MBeanConstructorInfo ctor, |
|
826 |
MBeanParameterInfo param, |
|
827 |
int sequence) { |
|
828 |
if (param == null) return null; |
|
829 |
return param.getDescription(); |
|
830 |
} |
|
831 |
||
832 |
/** |
|
833 |
* Customization hook: |
|
834 |
* Get the name that will be used for the <var>sequence</var> |
|
835 |
* MBeanParameterInfo of the MBeanConstructorInfo returned by this MBean. |
|
836 |
* <br> |
|
837 |
* Subclasses may redefine this method in order to supply their |
|
838 |
* custom parameter name. The default implementation returns |
|
839 |
* {@link MBeanParameterInfo#getName() param.getName()}. |
|
840 |
* |
|
841 |
* @param ctor The default MBeanConstructorInfo derived by reflection. |
|
842 |
* @param param The default MBeanParameterInfo derived by reflection. |
|
843 |
* @param sequence The sequence number of the parameter considered |
|
844 |
* ("0" for the first parameter, "1" for the second parameter, |
|
845 |
* etc...). |
|
846 |
* @return the name for the given MBeanParameterInfo. |
|
847 |
**/ |
|
848 |
protected String getParameterName(MBeanConstructorInfo ctor, |
|
849 |
MBeanParameterInfo param, |
|
850 |
int sequence) { |
|
851 |
if (param == null) return null; |
|
852 |
return param.getName(); |
|
853 |
} |
|
854 |
||
855 |
/** |
|
856 |
* Customization hook: |
|
857 |
* Get the description that will be used in the MBeanOperationInfo |
|
858 |
* returned by this MBean. |
|
859 |
* <br> |
|
860 |
* Subclasses may redefine this method in order to supply their |
|
861 |
* custom description. The default implementation returns |
|
862 |
* {@link #getDescription(MBeanFeatureInfo) |
|
863 |
* getDescription((MBeanFeatureInfo) info)}. |
|
864 |
* @param info The default MBeanOperationInfo derived by reflection. |
|
865 |
* @return the description for the given MBeanOperationInfo. |
|
866 |
**/ |
|
867 |
protected String getDescription(MBeanOperationInfo info) { |
|
868 |
return getDescription((MBeanFeatureInfo)info); |
|
869 |
} |
|
870 |
||
871 |
/** |
|
872 |
* Customization hook: |
|
873 |
* Get the <var>impact</var> flag of the operation that will be used in |
|
874 |
* the MBeanOperationInfo returned by this MBean. |
|
875 |
* <br> |
|
876 |
* Subclasses may redefine this method in order to supply their |
|
877 |
* custom impact flag. The default implementation returns |
|
878 |
* {@link MBeanOperationInfo#getImpact() info.getImpact()}. |
|
879 |
* @param info The default MBeanOperationInfo derived by reflection. |
|
880 |
* @return the impact flag for the given MBeanOperationInfo. |
|
881 |
**/ |
|
882 |
protected int getImpact(MBeanOperationInfo info) { |
|
883 |
if (info == null) return MBeanOperationInfo.UNKNOWN; |
|
884 |
return info.getImpact(); |
|
885 |
} |
|
886 |
||
887 |
/** |
|
888 |
* Customization hook: |
|
889 |
* Get the name that will be used for the <var>sequence</var> |
|
890 |
* MBeanParameterInfo of the MBeanOperationInfo returned by this MBean. |
|
891 |
* <br> |
|
892 |
* Subclasses may redefine this method in order to supply their |
|
893 |
* custom parameter name. The default implementation returns |
|
894 |
* {@link MBeanParameterInfo#getName() param.getName()}. |
|
895 |
* |
|
896 |
* @param op The default MBeanOperationInfo derived by reflection. |
|
897 |
* @param param The default MBeanParameterInfo derived by reflection. |
|
898 |
* @param sequence The sequence number of the parameter considered |
|
899 |
* ("0" for the first parameter, "1" for the second parameter, |
|
900 |
* etc...). |
|
901 |
* @return the name to use for the given MBeanParameterInfo. |
|
902 |
**/ |
|
903 |
protected String getParameterName(MBeanOperationInfo op, |
|
904 |
MBeanParameterInfo param, |
|
905 |
int sequence) { |
|
906 |
if (param == null) return null; |
|
907 |
return param.getName(); |
|
908 |
} |
|
909 |
||
910 |
/** |
|
911 |
* Customization hook: |
|
912 |
* Get the description that will be used for the <var>sequence</var> |
|
913 |
* MBeanParameterInfo of the MBeanOperationInfo returned by this MBean. |
|
914 |
* <br> |
|
915 |
* Subclasses may redefine this method in order to supply their |
|
916 |
* custom description. The default implementation returns |
|
917 |
* {@link MBeanParameterInfo#getDescription() param.getDescription()}. |
|
918 |
* |
|
919 |
* @param op The default MBeanOperationInfo derived by reflection. |
|
920 |
* @param param The default MBeanParameterInfo derived by reflection. |
|
921 |
* @param sequence The sequence number of the parameter considered |
|
922 |
* ("0" for the first parameter, "1" for the second parameter, |
|
923 |
* etc...). |
|
924 |
* @return the description for the given MBeanParameterInfo. |
|
925 |
**/ |
|
926 |
protected String getDescription(MBeanOperationInfo op, |
|
927 |
MBeanParameterInfo param, |
|
928 |
int sequence) { |
|
929 |
if (param == null) return null; |
|
930 |
return param.getDescription(); |
|
931 |
} |
|
932 |
||
933 |
/** |
|
934 |
* Customization hook: |
|
935 |
* Get the MBeanConstructorInfo[] that will be used in the MBeanInfo |
|
936 |
* returned by this MBean. |
|
937 |
* <br> |
|
938 |
* By default, this method returns <code>null</code> if the wrapped |
|
939 |
* implementation is not <var>this</var>. Indeed, if the wrapped |
|
940 |
* implementation is not this object itself, it will not be possible |
|
941 |
* to recreate a wrapped implementation by calling the implementation |
|
942 |
* constructors through <code>MBeanServer.createMBean(...)</code>.<br> |
|
943 |
* Otherwise, if the wrapped implementation is <var>this</var>, |
|
944 |
* <var>ctors</var> is returned. |
|
945 |
* <br> |
|
946 |
* Subclasses may redefine this method in order to modify this |
|
947 |
* behavior, if needed. |
|
948 |
* @param ctors The default MBeanConstructorInfo[] derived by reflection. |
|
949 |
* @param impl The wrapped implementation. If <code>null</code> is |
|
950 |
* passed, the wrapped implementation is ignored and |
|
951 |
* <var>ctors</var> is returned. |
|
952 |
* @return the MBeanConstructorInfo[] for the new MBeanInfo. |
|
953 |
**/ |
|
954 |
protected MBeanConstructorInfo[] |
|
955 |
getConstructors(MBeanConstructorInfo[] ctors, Object impl) { |
|
956 |
if (ctors == null) return null; |
|
957 |
if (impl != null && impl != this) return null; |
|
958 |
return ctors; |
|
959 |
} |
|
960 |
||
961 |
/** |
|
962 |
* Customization hook: |
|
963 |
* Get the MBeanNotificationInfo[] that will be used in the MBeanInfo |
|
964 |
* returned by this MBean. |
|
965 |
* <br> |
|
966 |
* Subclasses may redefine this method in order to supply their |
|
967 |
* custom notifications. |
|
968 |
* @param info The default MBeanInfo derived by reflection. |
|
969 |
* @return the MBeanNotificationInfo[] for the new MBeanInfo. |
|
970 |
**/ |
|
971 |
MBeanNotificationInfo[] getNotifications(MBeanInfo info) { |
|
687 | 972 |
return info.getNotifications(); |
2 | 973 |
} |
974 |
||
975 |
/** |
|
976 |
* <p>Get the Descriptor that will be used in the MBeanInfo |
|
977 |
* returned by this MBean.</p> |
|
978 |
* |
|
979 |
* <p>Subclasses may redefine this method in order to supply |
|
980 |
* their custom descriptor.</p> |
|
981 |
* |
|
982 |
* <p>The default implementation of this method returns a Descriptor |
|
983 |
* that contains at least the field {@code interfaceClassName}, with |
|
984 |
* value {@link #getMBeanInterface()}.getName(). It may also contain |
|
985 |
* the field {@code immutableInfo}, with a value that is the string |
|
986 |
* {@code "true"} if the implementation can determine that the |
|
987 |
* {@code MBeanInfo} returned by {@link #getMBeanInfo()} will always |
|
988 |
* be the same. It may contain other fields: fields defined by the |
|
989 |
* JMX specification must have appropriate values, and other fields |
|
990 |
* must follow the conventions for non-standard field names.</p> |
|
991 |
* |
|
992 |
* @param info The default MBeanInfo derived by reflection. |
|
993 |
* @return the Descriptor for the new MBeanInfo. |
|
994 |
*/ |
|
995 |
Descriptor getDescriptor(MBeanInfo info, boolean immutableInfo) { |
|
526
61ba2d5ea9da
6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents:
2
diff
changeset
|
996 |
ImmutableDescriptor desc; |
2 | 997 |
if (info == null || |
998 |
info.getDescriptor() == null || |
|
999 |
info.getDescriptor().getFieldNames().length == 0) { |
|
1000 |
final String interfaceClassNameS = |
|
1001 |
"interfaceClassName=" + getMBeanInterface().getName(); |
|
1002 |
final String immutableInfoS = |
|
1003 |
"immutableInfo=" + immutableInfo; |
|
1004 |
desc = new ImmutableDescriptor(interfaceClassNameS, immutableInfoS); |
|
1005 |
desc = descriptors.get(desc); |
|
1006 |
} else { |
|
1007 |
Descriptor d = info.getDescriptor(); |
|
1008 |
Map<String,Object> fields = new HashMap<String,Object>(); |
|
1009 |
for (String fieldName : d.getFieldNames()) { |
|
1010 |
if (fieldName.equals("immutableInfo")) { |
|
1011 |
// Replace immutableInfo as the underlying MBean/MXBean |
|
1012 |
// could already implement NotificationBroadcaster and |
|
1013 |
// return immutableInfo=true in its MBeanInfo. |
|
1014 |
fields.put(fieldName, Boolean.toString(immutableInfo)); |
|
1015 |
} else { |
|
1016 |
fields.put(fieldName, d.getFieldValue(fieldName)); |
|
1017 |
} |
|
1018 |
} |
|
1019 |
desc = new ImmutableDescriptor(fields); |
|
1020 |
} |
|
1021 |
return desc; |
|
1022 |
} |
|
1023 |
||
1024 |
/** |
|
1025 |
* Customization hook: |
|
1026 |
* Return the MBeanInfo cached for this object. |
|
1027 |
* |
|
1028 |
* <p>Subclasses may redefine this method in order to implement their |
|
1029 |
* own caching policy. The default implementation stores one |
|
1030 |
* {@link MBeanInfo} object per instance. |
|
1031 |
* |
|
1032 |
* @return The cached MBeanInfo, or null if no MBeanInfo is cached. |
|
1033 |
* |
|
1034 |
* @see #cacheMBeanInfo(MBeanInfo) |
|
1035 |
**/ |
|
1036 |
protected MBeanInfo getCachedMBeanInfo() { |
|
1037 |
return cachedMBeanInfo; |
|
1038 |
} |
|
1039 |
||
1040 |
/** |
|
1041 |
* Customization hook: |
|
1042 |
* cache the MBeanInfo built for this object. |
|
1043 |
* |
|
1044 |
* <p>Subclasses may redefine this method in order to implement |
|
1045 |
* their own caching policy. The default implementation stores |
|
1046 |
* <code>info</code> in this instance. A subclass can define |
|
1047 |
* other policies, such as not saving <code>info</code> (so it is |
|
1048 |
* reconstructed every time {@link #getMBeanInfo()} is called) or |
|
1049 |
* sharing a unique {@link MBeanInfo} object when several |
|
1050 |
* <code>StandardMBean</code> instances have equal {@link |
|
1051 |
* MBeanInfo} values. |
|
1052 |
* |
|
1053 |
* @param info the new <code>MBeanInfo</code> to cache. Any |
|
1054 |
* previously cached value is discarded. This parameter may be |
|
1055 |
* null, in which case there is no new cached value. |
|
1056 |
**/ |
|
1057 |
protected void cacheMBeanInfo(MBeanInfo info) { |
|
1058 |
cachedMBeanInfo = info; |
|
1059 |
} |
|
1060 |
||
1061 |
private static <T> boolean identicalArrays(T[] a, T[] b) { |
|
1062 |
if (a == b) |
|
1063 |
return true; |
|
1064 |
if (a == null || b == null || a.length != b.length) |
|
1065 |
return false; |
|
1066 |
for (int i = 0; i < a.length; i++) { |
|
1067 |
if (a[i] != b[i]) |
|
1068 |
return false; |
|
1069 |
} |
|
1070 |
return true; |
|
1071 |
} |
|
1072 |
||
1073 |
private static <T> boolean equal(T a, T b) { |
|
1074 |
if (a == b) |
|
1075 |
return true; |
|
1076 |
if (a == null || b == null) |
|
1077 |
return false; |
|
1078 |
return a.equals(b); |
|
1079 |
} |
|
1080 |
||
1081 |
private static MBeanParameterInfo |
|
1082 |
customize(MBeanParameterInfo pi, |
|
1083 |
String name, |
|
1084 |
String description) { |
|
1085 |
if (equal(name, pi.getName()) && |
|
1086 |
equal(description, pi.getDescription())) |
|
1087 |
return pi; |
|
1088 |
else if (pi instanceof OpenMBeanParameterInfo) { |
|
1089 |
OpenMBeanParameterInfo opi = (OpenMBeanParameterInfo) pi; |
|
1090 |
return new OpenMBeanParameterInfoSupport(name, |
|
1091 |
description, |
|
1092 |
opi.getOpenType(), |
|
1093 |
pi.getDescriptor()); |
|
1094 |
} else { |
|
1095 |
return new MBeanParameterInfo(name, |
|
1096 |
pi.getType(), |
|
1097 |
description, |
|
1098 |
pi.getDescriptor()); |
|
1099 |
} |
|
1100 |
} |
|
1101 |
||
1102 |
private static MBeanConstructorInfo |
|
1103 |
customize(MBeanConstructorInfo ci, |
|
1104 |
String description, |
|
1105 |
MBeanParameterInfo[] signature) { |
|
1106 |
if (equal(description, ci.getDescription()) && |
|
1107 |
identicalArrays(signature, ci.getSignature())) |
|
1108 |
return ci; |
|
1109 |
if (ci instanceof OpenMBeanConstructorInfo) { |
|
1110 |
OpenMBeanParameterInfo[] oparams = |
|
1111 |
paramsToOpenParams(signature); |
|
1112 |
return new OpenMBeanConstructorInfoSupport(ci.getName(), |
|
1113 |
description, |
|
1114 |
oparams, |
|
1115 |
ci.getDescriptor()); |
|
1116 |
} else { |
|
1117 |
return new MBeanConstructorInfo(ci.getName(), |
|
1118 |
description, |
|
1119 |
signature, |
|
1120 |
ci.getDescriptor()); |
|
1121 |
} |
|
1122 |
} |
|
1123 |
||
1124 |
private static MBeanOperationInfo |
|
1125 |
customize(MBeanOperationInfo oi, |
|
1126 |
String description, |
|
1127 |
MBeanParameterInfo[] signature, |
|
1128 |
int impact) { |
|
1129 |
if (equal(description, oi.getDescription()) && |
|
1130 |
identicalArrays(signature, oi.getSignature()) && |
|
1131 |
impact == oi.getImpact()) |
|
1132 |
return oi; |
|
1133 |
if (oi instanceof OpenMBeanOperationInfo) { |
|
1134 |
OpenMBeanOperationInfo ooi = (OpenMBeanOperationInfo) oi; |
|
1135 |
OpenMBeanParameterInfo[] oparams = |
|
1136 |
paramsToOpenParams(signature); |
|
1137 |
return new OpenMBeanOperationInfoSupport(oi.getName(), |
|
1138 |
description, |
|
1139 |
oparams, |
|
1140 |
ooi.getReturnOpenType(), |
|
1141 |
impact, |
|
1142 |
oi.getDescriptor()); |
|
1143 |
} else { |
|
1144 |
return new MBeanOperationInfo(oi.getName(), |
|
1145 |
description, |
|
1146 |
signature, |
|
1147 |
oi.getReturnType(), |
|
1148 |
impact, |
|
1149 |
oi.getDescriptor()); |
|
1150 |
} |
|
1151 |
} |
|
1152 |
||
1153 |
private static MBeanAttributeInfo |
|
1154 |
customize(MBeanAttributeInfo ai, |
|
1155 |
String description) { |
|
1156 |
if (equal(description, ai.getDescription())) |
|
1157 |
return ai; |
|
1158 |
if (ai instanceof OpenMBeanAttributeInfo) { |
|
1159 |
OpenMBeanAttributeInfo oai = (OpenMBeanAttributeInfo) ai; |
|
1160 |
return new OpenMBeanAttributeInfoSupport(ai.getName(), |
|
1161 |
description, |
|
1162 |
oai.getOpenType(), |
|
1163 |
ai.isReadable(), |
|
1164 |
ai.isWritable(), |
|
1165 |
ai.isIs(), |
|
1166 |
ai.getDescriptor()); |
|
1167 |
} else { |
|
1168 |
return new MBeanAttributeInfo(ai.getName(), |
|
1169 |
ai.getType(), |
|
1170 |
description, |
|
1171 |
ai.isReadable(), |
|
1172 |
ai.isWritable(), |
|
1173 |
ai.isIs(), |
|
1174 |
ai.getDescriptor()); |
|
1175 |
} |
|
1176 |
} |
|
1177 |
||
1178 |
private static OpenMBeanParameterInfo[] |
|
1179 |
paramsToOpenParams(MBeanParameterInfo[] params) { |
|
1180 |
if (params instanceof OpenMBeanParameterInfo[]) |
|
1181 |
return (OpenMBeanParameterInfo[]) params; |
|
1182 |
OpenMBeanParameterInfo[] oparams = |
|
1183 |
new OpenMBeanParameterInfoSupport[params.length]; |
|
1184 |
System.arraycopy(params, 0, oparams, 0, params.length); |
|
1185 |
return oparams; |
|
1186 |
} |
|
1187 |
||
1188 |
// ------------------------------------------------------------------ |
|
1189 |
// Build the custom MBeanConstructorInfo[] |
|
1190 |
// ------------------------------------------------------------------ |
|
1191 |
private MBeanConstructorInfo[] |
|
1192 |
getConstructors(MBeanInfo info, Object impl) { |
|
1193 |
final MBeanConstructorInfo[] ctors = |
|
1194 |
getConstructors(info.getConstructors(), impl); |
|
1195 |
if (ctors == null) |
|
1196 |
return null; |
|
1197 |
final int ctorlen = ctors.length; |
|
1198 |
final MBeanConstructorInfo[] nctors = new MBeanConstructorInfo[ctorlen]; |
|
1199 |
for (int i=0; i<ctorlen; i++) { |
|
1200 |
final MBeanConstructorInfo c = ctors[i]; |
|
1201 |
final MBeanParameterInfo[] params = c.getSignature(); |
|
1202 |
final MBeanParameterInfo[] nps; |
|
1203 |
if (params != null) { |
|
1204 |
final int plen = params.length; |
|
1205 |
nps = new MBeanParameterInfo[plen]; |
|
1206 |
for (int ii=0;ii<plen;ii++) { |
|
1207 |
MBeanParameterInfo p = params[ii]; |
|
1208 |
nps[ii] = customize(p, |
|
1209 |
getParameterName(c,p,ii), |
|
1210 |
getDescription(c,p,ii)); |
|
1211 |
} |
|
1212 |
} else { |
|
1213 |
nps = null; |
|
1214 |
} |
|
1215 |
nctors[i] = |
|
1216 |
customize(c, getDescription(c), nps); |
|
1217 |
} |
|
1218 |
return nctors; |
|
1219 |
} |
|
1220 |
||
1221 |
// ------------------------------------------------------------------ |
|
1222 |
// Build the custom MBeanOperationInfo[] |
|
1223 |
// ------------------------------------------------------------------ |
|
1224 |
private MBeanOperationInfo[] getOperations(MBeanInfo info) { |
|
1225 |
final MBeanOperationInfo[] ops = info.getOperations(); |
|
1226 |
if (ops == null) |
|
1227 |
return null; |
|
1228 |
final int oplen = ops.length; |
|
1229 |
final MBeanOperationInfo[] nops = new MBeanOperationInfo[oplen]; |
|
1230 |
for (int i=0; i<oplen; i++) { |
|
1231 |
final MBeanOperationInfo o = ops[i]; |
|
1232 |
final MBeanParameterInfo[] params = o.getSignature(); |
|
1233 |
final MBeanParameterInfo[] nps; |
|
1234 |
if (params != null) { |
|
1235 |
final int plen = params.length; |
|
1236 |
nps = new MBeanParameterInfo[plen]; |
|
1237 |
for (int ii=0;ii<plen;ii++) { |
|
1238 |
MBeanParameterInfo p = params[ii]; |
|
1239 |
nps[ii] = customize(p, |
|
1240 |
getParameterName(o,p,ii), |
|
1241 |
getDescription(o,p,ii)); |
|
1242 |
} |
|
1243 |
} else { |
|
1244 |
nps = null; |
|
1245 |
} |
|
1246 |
nops[i] = customize(o, getDescription(o), nps, getImpact(o)); |
|
1247 |
} |
|
1248 |
return nops; |
|
1249 |
} |
|
1250 |
||
1251 |
// ------------------------------------------------------------------ |
|
1252 |
// Build the custom MBeanAttributeInfo[] |
|
1253 |
// ------------------------------------------------------------------ |
|
1254 |
private MBeanAttributeInfo[] getAttributes(MBeanInfo info) { |
|
1255 |
final MBeanAttributeInfo[] atts = info.getAttributes(); |
|
1256 |
if (atts == null) |
|
1257 |
return null; // should not happen |
|
1258 |
final MBeanAttributeInfo[] natts; |
|
1259 |
final int attlen = atts.length; |
|
1260 |
natts = new MBeanAttributeInfo[attlen]; |
|
1261 |
for (int i=0; i<attlen; i++) { |
|
1262 |
final MBeanAttributeInfo a = atts[i]; |
|
1263 |
natts[i] = customize(a, getDescription(a)); |
|
1264 |
} |
|
1265 |
return natts; |
|
1266 |
} |
|
1267 |
||
1268 |
/** |
|
1269 |
* <p>Allows the MBean to perform any operations it needs before |
|
1270 |
* being registered in the MBean server. If the name of the MBean |
|
1271 |
* is not specified, the MBean can provide a name for its |
|
1272 |
* registration. If any exception is raised, the MBean will not be |
|
1273 |
* registered in the MBean server.</p> |
|
1274 |
* |
|
1275 |
* <p>The default implementation of this method returns the {@code name} |
|
1276 |
* parameter. It does nothing else for |
|
1277 |
* Standard MBeans. For MXBeans, it records the {@code MBeanServer} |
|
1278 |
* and {@code ObjectName} parameters so they can be used to translate |
|
1279 |
* inter-MXBean references.</p> |
|
1280 |
* |
|
1281 |
* <p>It is good practice for a subclass that overrides this method |
|
1282 |
* to call the overridden method via {@code super.preRegister(...)}. |
|
1283 |
* This is necessary if this object is an MXBean that is referenced |
|
1284 |
* by attributes or operations in other MXBeans.</p> |
|
1285 |
* |
|
1286 |
* @param server The MBean server in which the MBean will be registered. |
|
1287 |
* |
|
1288 |
* @param name The object name of the MBean. This name is null if |
|
1289 |
* the name parameter to one of the <code>createMBean</code> or |
|
1290 |
* <code>registerMBean</code> methods in the {@link MBeanServer} |
|
1291 |
* interface is null. In that case, this method must return a |
|
1292 |
* non-null ObjectName for the new MBean. |
|
1293 |
* |
|
1294 |
* @return The name under which the MBean is to be registered. |
|
1295 |
* This value must not be null. If the <code>name</code> |
|
1296 |
* parameter is not null, it will usually but not necessarily be |
|
1297 |
* the returned value. |
|
1298 |
* |
|
1299 |
* @throws IllegalArgumentException if this is an MXBean and |
|
1300 |
* {@code name} is null. |
|
1301 |
* |
|
1302 |
* @throws InstanceAlreadyExistsException if this is an MXBean and |
|
1303 |
* it has already been registered under another name (in this |
|
1304 |
* MBean Server or another). |
|
1305 |
* |
|
1306 |
* @throws Exception no other checked exceptions are thrown by |
|
1307 |
* this method but {@code Exception} is declared so that subclasses |
|
1308 |
* can override the method and throw their own exceptions. |
|
1309 |
* |
|
1310 |
* @since 1.6 |
|
1311 |
*/ |
|
1312 |
public ObjectName preRegister(MBeanServer server, ObjectName name) |
|
1313 |
throws Exception { |
|
1314 |
mbean.register(server, name); |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
687
diff
changeset
|
1315 |
MBeanInjector.inject(mbean.getWrappedObject(), server, name); |
2 | 1316 |
return name; |
1317 |
} |
|
1318 |
||
1319 |
/** |
|
1320 |
* <p>Allows the MBean to perform any operations needed after having been |
|
1321 |
* registered in the MBean server or after the registration has failed.</p> |
|
1322 |
* |
|
1323 |
* <p>The default implementation of this method does nothing for |
|
1324 |
* Standard MBeans. For MXBeans, it undoes any work done by |
|
1325 |
* {@link #preRegister preRegister} if registration fails.</p> |
|
1326 |
* |
|
1327 |
* <p>It is good practice for a subclass that overrides this method |
|
1328 |
* to call the overridden method via {@code super.postRegister(...)}. |
|
1329 |
* This is necessary if this object is an MXBean that is referenced |
|
1330 |
* by attributes or operations in other MXBeans.</p> |
|
1331 |
* |
|
1332 |
* @param registrationDone Indicates whether or not the MBean has |
|
1333 |
* been successfully registered in the MBean server. The value |
|
1334 |
* false means that the registration phase has failed. |
|
1335 |
* |
|
1336 |
* @since 1.6 |
|
1337 |
*/ |
|
1338 |
public void postRegister(Boolean registrationDone) { |
|
1339 |
if (!registrationDone) |
|
1340 |
mbean.unregister(); |
|
1341 |
} |
|
1342 |
||
1343 |
/** |
|
1344 |
* <p>Allows the MBean to perform any operations it needs before |
|
1345 |
* being unregistered by the MBean server.</p> |
|
1346 |
* |
|
1347 |
* <p>The default implementation of this method does nothing.</p> |
|
1348 |
* |
|
1349 |
* <p>It is good practice for a subclass that overrides this method |
|
1350 |
* to call the overridden method via {@code super.preDeegister(...)}.</p> |
|
1351 |
* |
|
1352 |
* @throws Exception no checked exceptions are throw by this method |
|
1353 |
* but {@code Exception} is declared so that subclasses can override |
|
1354 |
* this method and throw their own exceptions. |
|
1355 |
* |
|
1356 |
* @since 1.6 |
|
1357 |
*/ |
|
1358 |
public void preDeregister() throws Exception { |
|
1359 |
} |
|
1360 |
||
1361 |
/** |
|
1362 |
* <p>Allows the MBean to perform any operations needed after having been |
|
1363 |
* unregistered in the MBean server.</p> |
|
1364 |
* |
|
1365 |
* <p>The default implementation of this method does nothing for |
|
1366 |
* Standard MBeans. For MXBeans, it removes any information that |
|
1367 |
* was recorded by the {@link #preRegister preRegister} method.</p> |
|
1368 |
* |
|
1369 |
* <p>It is good practice for a subclass that overrides this method |
|
1370 |
* to call the overridden method via {@code super.postRegister(...)}. |
|
1371 |
* This is necessary if this object is an MXBean that is referenced |
|
1372 |
* by attributes or operations in other MXBeans.</p> |
|
1373 |
* |
|
1374 |
* @since 1.6 |
|
1375 |
*/ |
|
1376 |
public void postDeregister() { |
|
1377 |
mbean.unregister(); |
|
1378 |
} |
|
1379 |
||
1380 |
// |
|
1381 |
// MBeanInfo immutability |
|
1382 |
// |
|
1383 |
||
1384 |
/** |
|
1385 |
* Cached results of previous calls to immutableInfo. This is |
|
1386 |
* a WeakHashMap so that we don't prevent a class from being |
|
1387 |
* garbage collected just because we know whether its MBeanInfo |
|
1388 |
* is immutable. |
|
1389 |
*/ |
|
1510 | 1390 |
private static final Map<Class<?>, Boolean> mbeanInfoSafeMap = |
1391 |
new WeakHashMap<Class<?>, Boolean>(); |
|
2 | 1392 |
|
1393 |
/** |
|
1394 |
* Return true if {@code subclass} is known to preserve the immutability |
|
1395 |
* of the {@code MBeanInfo}. The {@code subclass} is considered to have |
|
1396 |
* an immutable {@code MBeanInfo} if it does not override any of the |
|
1397 |
* getMBeanInfo, getCachedMBeanInfo, cacheMBeanInfo and getNotificationInfo |
|
1398 |
* methods. |
|
1399 |
*/ |
|
1400 |
static boolean immutableInfo(Class<? extends StandardMBean> subclass) { |
|
1401 |
if (subclass == StandardMBean.class || |
|
1402 |
subclass == StandardEmitterMBean.class) |
|
1403 |
return true; |
|
1404 |
synchronized (mbeanInfoSafeMap) { |
|
1405 |
Boolean safe = mbeanInfoSafeMap.get(subclass); |
|
1406 |
if (safe == null) { |
|
1407 |
try { |
|
1408 |
MBeanInfoSafeAction action = |
|
1409 |
new MBeanInfoSafeAction(subclass); |
|
1410 |
safe = AccessController.doPrivileged(action); |
|
1411 |
} catch (Exception e) { // e.g. SecurityException |
|
1412 |
/* We don't know, so we assume it isn't. */ |
|
1413 |
safe = false; |
|
1414 |
} |
|
1415 |
mbeanInfoSafeMap.put(subclass, safe); |
|
1416 |
} |
|
1417 |
return safe; |
|
1418 |
} |
|
1419 |
} |
|
1420 |
||
1421 |
static boolean overrides(Class<?> subclass, Class<?> superclass, |
|
1422 |
String name, Class<?>... params) { |
|
1423 |
for (Class<?> c = subclass; c != superclass; c = c.getSuperclass()) { |
|
1424 |
try { |
|
1425 |
c.getDeclaredMethod(name, params); |
|
1426 |
return true; |
|
1427 |
} catch (NoSuchMethodException e) { |
|
1428 |
// OK: this class doesn't override it |
|
1429 |
} |
|
1430 |
} |
|
1431 |
return false; |
|
1432 |
} |
|
1433 |
||
1434 |
private static class MBeanInfoSafeAction |
|
1435 |
implements PrivilegedAction<Boolean> { |
|
1436 |
||
1510 | 1437 |
private final Class<?> subclass; |
2 | 1438 |
|
1510 | 1439 |
MBeanInfoSafeAction(Class<?> subclass) { |
2 | 1440 |
this.subclass = subclass; |
1441 |
} |
|
1442 |
||
1443 |
public Boolean run() { |
|
1444 |
// Check for "void cacheMBeanInfo(MBeanInfo)" method. |
|
1445 |
// |
|
1446 |
if (overrides(subclass, StandardMBean.class, |
|
1447 |
"cacheMBeanInfo", MBeanInfo.class)) |
|
1448 |
return false; |
|
1449 |
||
1450 |
// Check for "MBeanInfo getCachedMBeanInfo()" method. |
|
1451 |
// |
|
1452 |
if (overrides(subclass, StandardMBean.class, |
|
1510 | 1453 |
"getCachedMBeanInfo", (Class<?>[]) null)) |
2 | 1454 |
return false; |
1455 |
||
1456 |
// Check for "MBeanInfo getMBeanInfo()" method. |
|
1457 |
// |
|
1458 |
if (overrides(subclass, StandardMBean.class, |
|
1510 | 1459 |
"getMBeanInfo", (Class<?>[]) null)) |
2 | 1460 |
return false; |
1461 |
||
1462 |
// Check for "MBeanNotificationInfo[] getNotificationInfo()" |
|
1463 |
// method. |
|
1464 |
// |
|
1636
eb801ce73ac9
6776225: JMX.isNotificationSource wrong when DynamicWrapperMBean + SendNotification injection
emcmanus
parents:
1510
diff
changeset
|
1465 |
// This method is taken into account for the MBeanInfo |
2 | 1466 |
// immutability checks if and only if the given subclass is |
1467 |
// StandardEmitterMBean itself or can be assigned to |
|
1468 |
// StandardEmitterMBean. |
|
1469 |
// |
|
1470 |
if (StandardEmitterMBean.class.isAssignableFrom(subclass)) |
|
1471 |
if (overrides(subclass, StandardEmitterMBean.class, |
|
1510 | 1472 |
"getNotificationInfo", (Class<?>[]) null)) |
2 | 1473 |
return false; |
1474 |
return true; |
|
1475 |
} |
|
1476 |
} |
|
1477 |
} |