1 7 8 package javax.management; 9 10 import java.lang.reflect.Method ; 11 import java.util.Arrays ; 12 import java.util.Map ; 13 import java.util.WeakHashMap ; 14 import java.security.AccessController ; 15 import java.security.PrivilegedAction ; 16 17 63 public class MBeanInfo implements Cloneable , java.io.Serializable { 64 65 66 static final long serialVersionUID = -6451021435135161911L; 67 68 71 private final String description; 72 73 76 private final String className; 77 78 81 private final MBeanAttributeInfo [] attributes; 82 83 86 private final MBeanOperationInfo [] operations; 87 88 91 private final MBeanConstructorInfo [] constructors; 92 93 96 private final MBeanNotificationInfo [] notifications; 97 98 private transient int hashCode; 99 100 111 private final transient boolean immutable; 112 113 136 public MBeanInfo(String className, 137 String description, 138 MBeanAttributeInfo [] attributes, 139 MBeanConstructorInfo [] constructors, 140 MBeanOperationInfo [] operations, 141 MBeanNotificationInfo [] notifications) 142 throws IllegalArgumentException { 143 144 this.className = className; 145 146 this.description = description; 147 148 if (attributes == null) 149 attributes = MBeanAttributeInfo.NO_ATTRIBUTES; 150 this.attributes = attributes; 151 152 if (operations == null) 153 operations = MBeanOperationInfo.NO_OPERATIONS; 154 this.operations = operations; 155 156 if (constructors == null) 157 constructors = MBeanConstructorInfo.NO_CONSTRUCTORS; 158 this.constructors = constructors; 159 160 if (notifications == null) 161 notifications = MBeanNotificationInfo.NO_NOTIFICATIONS; 162 this.notifications = notifications; 163 164 this.immutable = isImmutableClass(this.getClass(), MBeanInfo .class); 165 } 166 167 177 public Object clone () { 178 try { 179 return super.clone() ; 180 } catch (CloneNotSupportedException e) { 181 return null; 183 } 184 } 185 186 187 193 public String getClassName() { 194 return className; 195 } 196 197 202 public String getDescription() { 203 return description; 204 } 205 206 217 public MBeanAttributeInfo [] getAttributes() { 218 MBeanAttributeInfo [] as = nonNullAttributes(); 219 if (as.length == 0) 220 return as; 221 else 222 return (MBeanAttributeInfo []) as.clone(); 223 } 224 225 private MBeanAttributeInfo [] fastGetAttributes() { 226 if (immutable) 227 return nonNullAttributes(); 228 else 229 return getAttributes(); 230 } 231 232 243 private MBeanAttributeInfo [] nonNullAttributes() { 244 return (attributes == null) ? 245 MBeanAttributeInfo.NO_ATTRIBUTES : attributes; 246 } 247 248 259 public MBeanOperationInfo [] getOperations() { 260 MBeanOperationInfo [] os = nonNullOperations(); 261 if (os.length == 0) 262 return os; 263 else 264 return (MBeanOperationInfo []) os.clone(); 265 } 266 267 private MBeanOperationInfo [] fastGetOperations() { 268 if (immutable) 269 return nonNullOperations(); 270 else 271 return getOperations(); 272 } 273 274 private MBeanOperationInfo [] nonNullOperations() { 275 return (operations == null) ? 276 MBeanOperationInfo.NO_OPERATIONS : operations; 277 } 278 279 298 public MBeanConstructorInfo [] getConstructors() { 299 MBeanConstructorInfo [] cs = nonNullConstructors(); 300 if (cs.length == 0) 301 return cs; 302 else 303 return (MBeanConstructorInfo []) cs.clone(); 304 } 305 306 private MBeanConstructorInfo [] fastGetConstructors() { 307 if (immutable) 308 return nonNullConstructors(); 309 else 310 return getConstructors(); 311 } 312 313 private MBeanConstructorInfo [] nonNullConstructors() { 314 return (constructors == null) ? 315 MBeanConstructorInfo.NO_CONSTRUCTORS : constructors; 316 } 317 318 329 public MBeanNotificationInfo [] getNotifications() { 330 MBeanNotificationInfo [] ns = nonNullNotifications(); 331 if (ns.length == 0) 332 return ns; 333 else 334 return (MBeanNotificationInfo []) ns.clone(); 335 } 336 337 private MBeanNotificationInfo [] fastGetNotifications() { 338 if (immutable) 339 return nonNullNotifications(); 340 else 341 return getNotifications(); 342 } 343 344 private MBeanNotificationInfo [] nonNullNotifications() { 345 return (notifications == null) ? 346 MBeanNotificationInfo.NO_NOTIFICATIONS : notifications; 347 } 348 349 367 public boolean equals(Object o) { 368 if (o == this) 369 return true; 370 if (!(o instanceof MBeanInfo )) 371 return false; 372 MBeanInfo p = (MBeanInfo ) o; 373 if (!p.getClassName().equals(getClassName()) || 374 !p.getDescription().equals(getDescription())) 375 return false; 376 return 377 (Arrays.equals(p.fastGetAttributes(), fastGetAttributes()) && 378 Arrays.equals(p.fastGetOperations(), fastGetOperations()) && 379 Arrays.equals(p.fastGetConstructors(), fastGetConstructors()) && 380 Arrays.equals(p.fastGetNotifications(), fastGetNotifications())); 381 } 382 383 public int hashCode() { 384 390 if (hashCode != 0) 391 return hashCode; 392 393 hashCode = 394 getClassName().hashCode() ^ 395 arrayHashCode(fastGetAttributes()) ^ 396 arrayHashCode(fastGetOperations()) ^ 397 arrayHashCode(fastGetConstructors()) ^ 398 arrayHashCode(fastGetNotifications()); 399 400 return hashCode; 401 } 402 403 private static int arrayHashCode(Object [] array) { 404 int hash = 0; 405 for (int i = 0; i < array.length; i++) 406 hash ^= array[i].hashCode(); 407 return hash; 408 } 409 410 416 private static final Map immutability = new WeakHashMap (); 417 418 428 static boolean isImmutableClass(Class subclass, Class immutableClass) { 429 if (subclass == immutableClass) 430 return true; 431 synchronized (immutability) { 432 Boolean immutable = (Boolean ) immutability.get(subclass); 433 if (immutable == null) { 434 try { 435 PrivilegedAction immutabilityAction = 436 new ImmutabilityAction(subclass, immutableClass); 437 immutable = (Boolean ) 438 AccessController.doPrivileged(immutabilityAction); 439 } catch (Exception e) { 441 immutable = Boolean.FALSE; 442 } 443 immutability.put(subclass, immutable); 444 } 445 return immutable.booleanValue(); 446 } 447 } 448 449 456 private static class ImmutabilityAction implements PrivilegedAction { 457 private final Class subclass; 458 private final Class immutableClass; 459 460 ImmutabilityAction(Class subclass, Class immutableClass) { 461 this.subclass = subclass; 462 this.immutableClass = immutableClass; 463 } 464 465 public Object run() { 466 Method [] methods = immutableClass.getMethods(); 467 for (int i = 0; i < methods.length; i++) { 468 Method method = methods[i]; 469 String methodName = method.getName(); 470 if (methodName.startsWith("get") 471 || methodName.startsWith("is")) { 472 Class [] paramTypes = method.getParameterTypes(); 473 try { 474 Method submethod = 475 subclass.getMethod(methodName, paramTypes); 476 if (!submethod.equals(method)) 477 return Boolean.FALSE; 478 } catch (NoSuchMethodException e) { 479 return Boolean.FALSE; 480 } 481 } 482 } 483 return Boolean.TRUE; 484 } 485 } 486 } 487 | Popular Tags |