33 import javax.management.InstanceAlreadyExistsException; |
33 import javax.management.InstanceAlreadyExistsException; |
34 import javax.management.JMX; |
34 import javax.management.JMX; |
35 import javax.management.MBeanServer; |
35 import javax.management.MBeanServer; |
36 import javax.management.NotCompliantMBeanException; |
36 import javax.management.NotCompliantMBeanException; |
37 import javax.management.ObjectName; |
37 import javax.management.ObjectName; |
38 import javax.management.openmbean.MXBeanMappingFactory; |
|
39 |
38 |
40 /** |
39 /** |
41 * Base class for MXBeans. |
40 * Base class for MXBeans. |
42 * |
41 * |
43 * @since 1.6 |
42 * @since 1.6 |
60 |
59 |
61 @throws IllegalArgumentException if {@code resource} is null or |
60 @throws IllegalArgumentException if {@code resource} is null or |
62 if it does not implement the class {@code mxbeanInterface} or if |
61 if it does not implement the class {@code mxbeanInterface} or if |
63 that class is not a valid MXBean interface. |
62 that class is not a valid MXBean interface. |
64 */ |
63 */ |
65 public <T> MXBeanSupport(T resource, Class<T> mxbeanInterface, |
64 public <T> MXBeanSupport(T resource, Class<T> mxbeanInterface) |
66 MXBeanMappingFactory mappingFactory) |
|
67 throws NotCompliantMBeanException { |
65 throws NotCompliantMBeanException { |
68 super(resource, mxbeanInterface, mappingFactory); |
66 super(resource, mxbeanInterface); |
69 } |
67 } |
70 |
68 |
71 @Override |
69 @Override |
72 MBeanIntrospector<ConvertingMethod> |
70 MBeanIntrospector<ConvertingMethod> getMBeanIntrospector() { |
73 getMBeanIntrospector(MXBeanMappingFactory mappingFactory) { |
71 return MXBeanIntrospector.getInstance(); |
74 return MXBeanIntrospector.getInstance(mappingFactory); |
|
75 } |
72 } |
76 |
73 |
77 @Override |
74 @Override |
78 Object getCookie() { |
75 Object getCookie() { |
79 return mxbeanLookup; |
76 return mxbeanLookup; |
157 if (name == null) |
154 if (name == null) |
158 throw new IllegalArgumentException("Null object name"); |
155 throw new IllegalArgumentException("Null object name"); |
159 // eventually we could have some logic to supply a default name |
156 // eventually we could have some logic to supply a default name |
160 |
157 |
161 synchronized (lock) { |
158 synchronized (lock) { |
162 this.mxbeanLookup = MXBeanLookup.Plain.lookupFor(server); |
159 this.mxbeanLookup = MXBeanLookup.lookupFor(server); |
163 this.mxbeanLookup.addReference(name, getWrappedObject()); |
160 this.mxbeanLookup.addReference(name, getResource()); |
164 this.objectName = name; |
161 this.objectName = name; |
165 } |
162 } |
166 } |
163 } |
167 |
164 |
168 @Override |
165 @Override |
169 public void unregister() { |
166 public void unregister() { |
170 synchronized (lock) { |
167 synchronized (lock) { |
171 if (mxbeanLookup != null) { |
168 if (mxbeanLookup != null) { |
172 if (mxbeanLookup.removeReference(objectName, getWrappedObject())) |
169 if (mxbeanLookup.removeReference(objectName, getResource())) |
173 objectName = null; |
170 objectName = null; |
174 } |
171 } |
175 // XXX: need to revisit the whole register/unregister logic in |
|
176 // the face of wrapping. The mxbeanLookup!=null test is a hack. |
|
177 // If you wrap an MXBean in a MyWrapperMBean and register it, |
|
178 // the lookup table should contain the wrapped object. But that |
|
179 // implies that MyWrapperMBean calls register, which today it |
|
180 // can't within the public API. |
|
181 } |
172 } |
182 } |
173 } |
183 private final Object lock = new Object(); // for mxbeanLookup and objectName |
174 private final Object lock = new Object(); // for mxbeanLookup and objectName |
184 |
175 |
185 private MXBeanLookup.Plain mxbeanLookup; |
176 private MXBeanLookup mxbeanLookup; |
186 private ObjectName objectName; |
177 private ObjectName objectName; |
187 } |
178 } |