1 7 8 package com.sun.jmx.mbeanserver; 9 10 import static com.sun.jmx.mbeanserver.Util.*; 11 import java.lang.annotation.Annotation ; 12 import java.lang.ref.WeakReference ; 13 import java.lang.reflect.Constructor ; 14 import java.lang.reflect.GenericArrayType ; 15 import java.lang.reflect.InvocationTargetException ; 16 import java.lang.reflect.Method ; 17 import java.lang.reflect.Type ; 18 import java.util.Arrays ; 19 import java.util.Collections ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.Set ; 24 import java.util.WeakHashMap ; 25 import javax.management.Attribute ; 26 import javax.management.AttributeList ; 27 import javax.management.AttributeNotFoundException ; 28 import javax.management.Descriptor ; 29 import javax.management.DynamicMBean ; 30 import javax.management.ImmutableDescriptor ; 31 import javax.management.InstanceAlreadyExistsException ; 32 import javax.management.InvalidAttributeValueException ; 33 import javax.management.JMX ; 34 import javax.management.MBeanAttributeInfo ; 35 import javax.management.MBeanConstructorInfo ; 36 import javax.management.MBeanException ; 37 import javax.management.MBeanInfo ; 38 import javax.management.MBeanNotificationInfo ; 39 import javax.management.MBeanOperationInfo ; 40 import javax.management.MBeanParameterInfo ; 41 import javax.management.MBeanRegistration ; 42 import javax.management.MBeanServer ; 43 import javax.management.NotCompliantMBeanException ; 44 import javax.management.NotificationBroadcaster ; 45 import javax.management.ObjectName ; 46 import javax.management.ReflectionException ; 47 import javax.management.openmbean.OpenMBeanAttributeInfoSupport ; 48 import javax.management.openmbean.OpenMBeanOperationInfoSupport ; 49 import javax.management.openmbean.OpenMBeanParameterInfo ; 50 import javax.management.openmbean.OpenMBeanParameterInfoSupport ; 51 import javax.management.openmbean.OpenType ; 52 53 64 132 public abstract class MBeanSupport<M> 133 implements DynamicMBean2, MBeanRegistration { 134 135 <T> MBeanSupport(T resource, Class <T> mbeanInterface) 136 throws NotCompliantMBeanException { 137 if (mbeanInterface == null) 138 throw new NotCompliantMBeanException ("Null MBean interface"); 139 if (!mbeanInterface.isInstance(resource)) { 140 final String msg = 141 "Resource class " + resource.getClass().getName() + 142 " is not an instance of " + mbeanInterface.getName(); 143 throw new NotCompliantMBeanException (msg); 144 } 145 this.resource = resource; 146 MBeanIntrospector introspector = getMBeanIntrospector(); 147 this.perInterface = introspector.getPerInterface(mbeanInterface); 148 this.mbeanInfo = introspector.getMBeanInfo(resource, perInterface); 149 } 150 151 152 abstract MBeanIntrospector<M> getMBeanIntrospector(); 153 154 160 abstract Object getCookie(); 161 162 public final boolean isMXBean() { 163 return perInterface.isMXBean(); 164 } 165 166 public abstract void register(MBeanServer mbs, ObjectName name) 170 throws Exception ; 171 public abstract void unregister(); 172 173 public final ObjectName preRegister(MBeanServer server, ObjectName name) 174 throws Exception { 175 if (resource instanceof MBeanRegistration ) 176 return ((MBeanRegistration ) resource).preRegister(server, name); 177 else 178 return name; 179 } 180 181 public final void preRegister2(MBeanServer server, ObjectName name) 182 throws Exception { 183 register(server, name); 184 } 185 186 public final void registerFailed() { 187 unregister(); 188 } 189 190 public final void postRegister(Boolean registrationDone) { 191 if (resource instanceof MBeanRegistration ) 192 ((MBeanRegistration ) resource).postRegister(registrationDone); 193 } 194 195 public final void preDeregister() throws Exception { 196 if (resource instanceof MBeanRegistration ) 197 ((MBeanRegistration ) resource).preDeregister(); 198 } 199 200 public final void postDeregister() { 201 try { 205 unregister(); 206 } finally { 207 if (resource instanceof MBeanRegistration ) 208 ((MBeanRegistration ) resource).postDeregister(); 209 } 210 } 211 212 public final Object getAttribute(String attribute) 213 throws AttributeNotFoundException , 214 MBeanException , 215 ReflectionException { 216 return perInterface.getAttribute(resource, attribute, getCookie()); 217 } 218 219 public final AttributeList getAttributes(String [] attributes) { 220 final AttributeList result = new AttributeList (attributes.length); 221 for (String attrName : attributes) { 222 try { 223 final Object attrValue = getAttribute(attrName); 224 result.add(new Attribute (attrName, attrValue)); 225 } catch (Exception e) { 226 } 229 } 230 return result; 231 } 232 233 public final void setAttribute(Attribute attribute) 234 throws AttributeNotFoundException , 235 InvalidAttributeValueException , 236 MBeanException , 237 ReflectionException { 238 final String name = attribute.getName(); 239 final Object value = attribute.getValue(); 240 perInterface.setAttribute(resource, name, value, getCookie()); 241 } 242 243 public final AttributeList setAttributes(AttributeList attributes) { 244 final AttributeList result = new AttributeList (attributes.size()); 245 for (Object attrObj : attributes) { 246 Attribute attr = (Attribute ) attrObj; 248 try { 249 setAttribute(attr); 250 result.add(new Attribute (attr.getName(), attr.getValue())); 251 } catch (Exception e) { 252 } 255 } 256 return result; 257 } 258 259 public final Object invoke(String operation, Object [] params, 260 String [] signature) 261 throws MBeanException , ReflectionException { 262 return perInterface.invoke(resource, operation, params, signature, 263 getCookie()); 264 } 265 266 public MBeanInfo getMBeanInfo() { 268 return mbeanInfo; 269 } 270 271 public final String getClassName() { 272 return resource.getClass().getName(); 273 } 274 275 public final Object getResource() { 276 return resource; 277 } 278 279 public final Class <?> getMBeanInterface() { 280 return perInterface.getMBeanInterface(); 281 } 282 283 private final MBeanInfo mbeanInfo; 284 private final Object resource; 285 private final PerInterface<M> perInterface; 286 } 287 | Popular Tags |