1 7 8 package javax.management; 9 10 import com.sun.jmx.mbeanserver.StandardMBeanMetaDataImpl; 11 import com.sun.jmx.trace.Trace; 12 import java.io.PrintWriter ; 13 import java.io.StringWriter ; 14 import java.lang.reflect.UndeclaredThrowableException ; 15 16 87 public class StandardMBean implements DynamicMBean { 88 89 private final static String dbgTag = "StandardMBean"; 90 91 94 private Class mbeanInterface; 95 96 99 private Object implementation; 100 101 104 private final StandardMBeanMetaDataImpl meta; 105 106 109 private MBeanInfo cachedMBeanInfo; 110 111 133 private StandardMBean(Object implementation, Class mbeanInterface, 134 boolean nullImplementationAllowed) 135 throws NotCompliantMBeanException { 136 if (implementation == null) { 137 if (nullImplementationAllowed) implementation = this; 138 else throw new IllegalArgumentException ("implementation is null"); 139 } 140 this.meta = new StandardMBeanMetaDataImpl(this); 141 setImplementation(implementation,mbeanInterface); 142 } 143 144 163 public StandardMBean(Object implementation,Class mbeanInterface) 164 throws NotCompliantMBeanException { 165 this(implementation,mbeanInterface,false); 166 } 167 168 183 protected StandardMBean(Class mbeanInterface) 184 throws NotCompliantMBeanException { 185 this(null,mbeanInterface,true); 186 } 187 188 206 public synchronized void setImplementation(Object implementation) 207 throws NotCompliantMBeanException { 208 setImplementation(implementation, getMBeanInterface()); 209 } 210 211 227 private synchronized void setImplementation(Object implementation, 228 Class mbeanInterface) 229 throws NotCompliantMBeanException { 230 if (implementation == null) 231 throw new IllegalArgumentException ("implementation is null"); 232 233 this.meta.testCompliance(implementation.getClass(),mbeanInterface); 235 236 cacheMBeanInfo(null); 238 this.implementation = implementation; 239 this.mbeanInterface = mbeanInterface; 240 if (this.mbeanInterface == null) 241 this.mbeanInterface = 242 meta.getStandardMBeanInterface(implementation.getClass()); 243 } 244 245 251 public synchronized Object getImplementation() { 252 return implementation; 253 } 254 255 259 public final synchronized Class getMBeanInterface() { 260 return mbeanInterface; 261 } 262 263 267 public synchronized Class getImplementationClass() { 268 if (implementation == null) return null; 269 return implementation.getClass(); 270 } 271 272 public Object getAttribute(String attribute) 276 throws AttributeNotFoundException , 277 MBeanException , ReflectionException { 278 return meta.getAttribute(getImplementation(),attribute); 279 } 280 281 public void setAttribute(Attribute attribute) 285 throws AttributeNotFoundException , 286 InvalidAttributeValueException , MBeanException , 287 ReflectionException { 288 meta.setAttribute(getImplementation(),attribute); 289 } 290 291 public AttributeList getAttributes(String [] attributes) { 295 try { 296 return meta.getAttributes(getImplementation(),attributes); 297 } catch (ReflectionException x) { 298 final RuntimeException r = 299 new UndeclaredThrowableException (x,x.getMessage()); 300 throw new RuntimeOperationsException (r,x.getMessage()); 301 } 302 } 303 304 public AttributeList setAttributes(AttributeList attributes) { 308 try { 309 return meta.setAttributes(getImplementation(),attributes); 310 } catch (ReflectionException x) { 311 final RuntimeException r = 312 new UndeclaredThrowableException (x,x.getMessage()); 313 throw new RuntimeOperationsException (r,x.getMessage()); 314 } 315 } 316 317 public Object invoke(String actionName, Object params[], 321 String signature[]) 322 throws MBeanException , ReflectionException { 323 return meta.invoke(getImplementation(),actionName,params,signature); 324 } 325 326 348 public MBeanInfo getMBeanInfo() { 349 try { 350 final MBeanInfo cached = getCachedMBeanInfo(); 351 if (cached != null) return (MBeanInfo )cached; 352 } catch (RuntimeException x) { 353 debug("getMBeanInfo","failed to get cached MBeanInfo: "+x); 354 debugX("getMBeanInfo",x); 355 } 356 357 if (isTraceOn()) { 358 trace("getMBeanInfo", "Building MBeanInfo for "+ 359 getImplementationClass().getName()); 360 } 361 362 final MBeanInfo bi; 363 final Object impl; 364 try { 365 synchronized (this) { 366 impl = getImplementation(); 367 bi = buildStandardMBeanInfo(); 368 } 369 } catch (NotCompliantMBeanException x) { 370 final RuntimeException r = 371 new UndeclaredThrowableException (x,x.getMessage()); 372 throw new RuntimeOperationsException (r,x.getMessage()); 373 } 374 375 final String cname = getClassName(bi); 376 final String text = getDescription(bi); 377 final MBeanConstructorInfo [] ctors = getConstructors(bi,impl); 378 final MBeanAttributeInfo [] attrs = getAttributes(bi); 379 final MBeanOperationInfo [] ops = getOperations(bi); 380 final MBeanNotificationInfo [] ntfs = getNotifications(bi); 381 final MBeanInfo nmbi = 382 new MBeanInfo (cname,text,attrs,ctors,ops,ntfs); 383 384 try { cacheMBeanInfo(nmbi); } catch (RuntimeException x) { 385 debug("cacheMBeanInfo","failed to cache MBeanInfo: "+x); 386 debugX("cacheMBeanInfo",x); 387 } 388 389 return nmbi; 390 } 391 392 403 protected String getClassName(MBeanInfo info) { 404 if (info == null) return getImplementationClass().getName(); 405 return info.getClassName(); 406 } 407 408 419 protected String getDescription(MBeanInfo info) { 420 if (info == null) return null; 421 return info.getDescription(); 422 } 423 424 442 protected String getDescription(MBeanFeatureInfo info) { 443 if (info == null) return null; 444 return info.getDescription(); 445 } 446 447 459 protected String getDescription(MBeanAttributeInfo info) { 460 return getDescription((MBeanFeatureInfo )info); 461 } 462 463 476 protected String getDescription(MBeanConstructorInfo info) { 477 return getDescription((MBeanFeatureInfo )info); 478 } 479 480 496 protected String getDescription(MBeanConstructorInfo ctor, 497 MBeanParameterInfo param, 498 int sequence) { 499 if (param == null) return null; 500 return param.getDescription(); 501 } 502 503 519 protected String getParameterName(MBeanConstructorInfo ctor, 520 MBeanParameterInfo param, 521 int sequence) { 522 if (param == null) return null; 523 return param.getName(); 524 } 525 526 538 protected String getDescription(MBeanOperationInfo info) { 539 return getDescription((MBeanFeatureInfo )info); 540 } 541 542 553 protected int getImpact(MBeanOperationInfo info) { 554 if (info == null) return MBeanOperationInfo.UNKNOWN; 555 return info.getImpact(); 556 } 557 558 574 protected String getParameterName(MBeanOperationInfo op, 575 MBeanParameterInfo param, 576 int sequence) { 577 if (param == null) return null; 578 return param.getName(); 579 } 580 581 597 protected String getDescription(MBeanOperationInfo op, 598 MBeanParameterInfo param, 599 int sequence) { 600 if (param == null) return null; 601 return param.getDescription(); 602 } 603 604 625 protected MBeanConstructorInfo [] 626 getConstructors(MBeanConstructorInfo [] ctors, Object impl) { 627 if (ctors == null) return null; 628 if (impl != null && impl != this) return null; 629 return ctors; 630 } 631 632 642 private MBeanNotificationInfo [] 645 getNotifications(MBeanInfo info) { 646 if (info == null) return null; 647 return info.getNotifications(); 648 } 649 650 662 protected synchronized MBeanInfo getCachedMBeanInfo() { 663 return cachedMBeanInfo; 664 } 665 666 683 protected synchronized void cacheMBeanInfo(MBeanInfo info) { 684 cachedMBeanInfo = info; 685 } 686 687 private synchronized MBeanInfo buildStandardMBeanInfo() 691 throws NotCompliantMBeanException { 692 return meta.buildMBeanInfo(getImplementationClass(), 693 getMBeanInterface()); 694 } 695 696 private MBeanConstructorInfo [] 700 getConstructors(MBeanInfo info,Object impl) { 701 final MBeanConstructorInfo [] ctors = 702 getConstructors(info.getConstructors(),impl); 703 final MBeanConstructorInfo [] nctors; 704 if (ctors != null) { 705 final int ctorlen = ctors.length; 706 nctors = new MBeanConstructorInfo [ctorlen]; 707 for (int i=0; i<ctorlen; i++) { 708 final MBeanConstructorInfo c = ctors[i]; 709 final MBeanParameterInfo [] params = c.getSignature(); 710 final MBeanParameterInfo [] nps; 711 if (params != null) { 712 final int plen = params.length; 713 nps = new MBeanParameterInfo [plen]; 714 for (int ii=0;ii<plen;ii++) { 715 MBeanParameterInfo p = params[ii]; 716 final String name = getParameterName(c,p,ii); 717 final String text = getDescription(c,p,ii); 718 nps[ii] = new MBeanParameterInfo (name, 719 p.getType(), 720 text); 721 } 722 } else { 723 nps = null; 724 } 725 nctors[i] = new MBeanConstructorInfo (c.getName(), 726 getDescription(c), 727 nps); 728 } 729 } else { 730 nctors = null; 731 } 732 return nctors; 733 } 734 735 private MBeanOperationInfo [] getOperations(MBeanInfo info) { 739 final MBeanOperationInfo [] ops = info.getOperations(); 740 final MBeanOperationInfo [] nops; 741 if (ops != null) { 742 final int oplen = ops.length; 743 nops = new MBeanOperationInfo [oplen]; 744 for (int i=0; i<oplen; i++) { 745 final MBeanOperationInfo o = ops[i]; 746 final MBeanParameterInfo [] params = o.getSignature(); 747 final MBeanParameterInfo [] nps; 748 if (params != null) { 749 final int plen = params.length; 750 nps = new MBeanParameterInfo [plen]; 751 for (int ii=0;ii<plen;ii++) { 752 MBeanParameterInfo p = params[ii]; 753 final String name = getParameterName(o,p,ii); 754 final String text = getDescription(o,p,ii); 755 nps[ii] = new MBeanParameterInfo (name, 756 p.getType(), 757 text); 758 } 759 } else { 760 nps = null; 761 } 762 nops[i] = new MBeanOperationInfo (o.getName(), 763 getDescription(o), 764 nps, 765 o.getReturnType(), 766 getImpact(o)); 767 } 768 } else { 769 nops = null; 770 } 771 return nops; 772 } 773 774 private MBeanAttributeInfo [] getAttributes(MBeanInfo info) { 778 final MBeanAttributeInfo [] atts = info.getAttributes(); 779 final MBeanAttributeInfo [] natts; 780 if (atts != null) { 781 final int attlen = atts.length; 782 natts = new MBeanAttributeInfo [attlen]; 783 for (int i=0; i<attlen; i++) { 784 final MBeanAttributeInfo a = atts[i]; 785 natts[i] = new MBeanAttributeInfo (a.getName(), 786 a.getType(), 787 getDescription(a), 788 a.isReadable(), 789 a.isWritable(), 790 a.isIs()); 791 } 792 } else { 793 natts = null; 794 } 795 return natts; 796 } 797 798 800 private static boolean isTraceOn() { 801 return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MISC); 802 } 803 804 private static void trace(String clz, String func, String info) { 805 Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MISC, clz, func, info); 806 } 807 808 private static void trace(String func, String info) { 809 trace(dbgTag, func, info); 810 } 811 812 private static boolean isDebugOn() { 813 return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_MISC); 814 } 815 816 private static void debug(String clz, String func, String info) { 817 Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_MISC, clz, func, info); 818 } 819 820 private static void debug(String func, String info) { 821 debug(dbgTag, func, info); 822 } 823 824 private static void debugX(String func,Throwable e) { 825 if (isDebugOn()) { 826 final StringWriter s = new StringWriter (); 827 e.printStackTrace(new PrintWriter (s)); 828 final String stack = s.toString(); 829 830 debug(dbgTag,func,"Exception caught in "+ func+"(): "+e); 831 debug(dbgTag,func,stack); 832 833 } 837 } 838 839 } 840 | Popular Tags |