36 import javax.management.MBeanRegistration; |
36 import javax.management.MBeanRegistration; |
37 import javax.management.MBeanServer; |
37 import javax.management.MBeanServer; |
38 import javax.management.NotCompliantMBeanException; |
38 import javax.management.NotCompliantMBeanException; |
39 import javax.management.ObjectName; |
39 import javax.management.ObjectName; |
40 import javax.management.ReflectionException; |
40 import javax.management.ReflectionException; |
|
41 import javax.management.openmbean.MXBeanMappingFactory; |
41 |
42 |
42 /** |
43 /** |
43 * Base class for MBeans. There is one instance of this class for |
44 * Base class for MBeans. There is one instance of this class for |
44 * every Standard MBean and every MXBean. We try to limit the amount |
45 * every Standard MBean and every MXBean. We try to limit the amount |
45 * of information per instance so we can handle very large numbers of |
46 * of information per instance so we can handle very large numbers of |
119 * |
120 * |
120 */ |
121 */ |
121 public abstract class MBeanSupport<M> |
122 public abstract class MBeanSupport<M> |
122 implements DynamicMBean2, MBeanRegistration { |
123 implements DynamicMBean2, MBeanRegistration { |
123 |
124 |
124 <T> MBeanSupport(T resource, Class<T> mbeanInterface) |
125 <T> MBeanSupport(T resource, Class<T> mbeanInterfaceType, |
|
126 MXBeanMappingFactory mappingFactory) |
125 throws NotCompliantMBeanException { |
127 throws NotCompliantMBeanException { |
126 if (mbeanInterface == null) |
128 if (mbeanInterfaceType == null) |
127 throw new NotCompliantMBeanException("Null MBean interface"); |
129 throw new NotCompliantMBeanException("Null MBean interface"); |
128 if (!mbeanInterface.isInstance(resource)) { |
130 if (!mbeanInterfaceType.isInstance(resource)) { |
129 final String msg = |
131 final String msg = |
130 "Resource class " + resource.getClass().getName() + |
132 "Resource class " + resource.getClass().getName() + |
131 " is not an instance of " + mbeanInterface.getName(); |
133 " is not an instance of " + mbeanInterfaceType.getName(); |
132 throw new NotCompliantMBeanException(msg); |
134 throw new NotCompliantMBeanException(msg); |
133 } |
135 } |
134 this.resource = resource; |
136 this.resource = resource; |
135 MBeanIntrospector<M> introspector = getMBeanIntrospector(); |
137 MBeanIntrospector<M> introspector = getMBeanIntrospector(mappingFactory); |
136 this.perInterface = introspector.getPerInterface(mbeanInterface); |
138 this.perInterface = introspector.getPerInterface(mbeanInterfaceType); |
137 this.mbeanInfo = introspector.getMBeanInfo(resource, perInterface); |
139 this.mbeanInfo = introspector.getMBeanInfo(resource, perInterface); |
138 } |
140 } |
139 |
141 |
140 /** Return the appropriate introspector for this type of MBean. */ |
142 /** Return the appropriate introspector for this type of MBean. */ |
141 abstract MBeanIntrospector<M> getMBeanIntrospector(); |
143 abstract MBeanIntrospector<M> |
|
144 getMBeanIntrospector(MXBeanMappingFactory mappingFactory); |
142 |
145 |
143 /** |
146 /** |
144 * Return a cookie for this MBean. This cookie will be passed to |
147 * Return a cookie for this MBean. This cookie will be passed to |
145 * MBean method invocations where it can supply additional information |
148 * MBean method invocations where it can supply additional information |
146 * to the invocation. For example, with MXBeans it can be used to |
149 * to the invocation. For example, with MXBeans it can be used to |
160 public abstract void unregister(); |
163 public abstract void unregister(); |
161 |
164 |
162 public final ObjectName preRegister(MBeanServer server, ObjectName name) |
165 public final ObjectName preRegister(MBeanServer server, ObjectName name) |
163 throws Exception { |
166 throws Exception { |
164 if (resource instanceof MBeanRegistration) |
167 if (resource instanceof MBeanRegistration) |
165 return ((MBeanRegistration) resource).preRegister(server, name); |
168 name = ((MBeanRegistration) resource).preRegister(server, name); |
166 else |
169 return name; |
167 return name; |
|
168 } |
170 } |
169 |
171 |
170 public final void preRegister2(MBeanServer server, ObjectName name) |
172 public final void preRegister2(MBeanServer server, ObjectName name) |
171 throws Exception { |
173 throws Exception { |
172 register(server, name); |
174 register(server, name); |