jdk/src/share/classes/javax/management/StandardMBean.java
changeset 834 dc74d4ddc28e
parent 715 f16baef3a20e
parent 833 bfa2bef7517c
child 1510 e747d3193ef2
equal deleted inserted replaced
832:5484c7a35278 834:dc74d4ddc28e
    25 
    25 
    26 package javax.management;
    26 package javax.management;
    27 
    27 
    28 import com.sun.jmx.mbeanserver.DescriptorCache;
    28 import com.sun.jmx.mbeanserver.DescriptorCache;
    29 import com.sun.jmx.mbeanserver.Introspector;
    29 import com.sun.jmx.mbeanserver.Introspector;
       
    30 import com.sun.jmx.mbeanserver.MBeanInjector;
    30 import com.sun.jmx.mbeanserver.MBeanSupport;
    31 import com.sun.jmx.mbeanserver.MBeanSupport;
    31 import com.sun.jmx.mbeanserver.MXBeanSupport;
    32 import com.sun.jmx.mbeanserver.MXBeanSupport;
    32 import com.sun.jmx.mbeanserver.StandardMBeanSupport;
    33 import com.sun.jmx.mbeanserver.StandardMBeanSupport;
    33 import com.sun.jmx.mbeanserver.Util;
    34 import com.sun.jmx.mbeanserver.Util;
    34 import java.security.AccessController;
    35 import java.security.AccessController;
   123  * examples above, the {@code false} parameter to the constructor or
   124  * examples above, the {@code false} parameter to the constructor or
   124  * {@code super(...)} invocation is instead {@code true}.</p>
   125  * {@code super(...)} invocation is instead {@code true}.</p>
   125  *
   126  *
   126  * @since 1.5
   127  * @since 1.5
   127  */
   128  */
   128 public class StandardMBean implements DynamicMBean, MBeanRegistration {
   129 public class StandardMBean implements DynamicWrapperMBean, MBeanRegistration {
       
   130 
       
   131     /**
       
   132      * <p>Options controlling the behavior of {@code StandardMBean} instances.</p>
       
   133      */
       
   134     public static class Options extends JMX.MBeanOptions {
       
   135         private static final long serialVersionUID = 5107355471177517164L;
       
   136 
       
   137         private boolean wrappedVisible;
       
   138 
       
   139         /**
       
   140          * <p>Construct an {@code Options} object where all options have
       
   141          * their default values.</p>
       
   142          */
       
   143         public Options() {}
       
   144 
       
   145         @Override
       
   146         public Options clone() {
       
   147             return (Options) super.clone();
       
   148         }
       
   149 
       
   150         /**
       
   151          * <p>Defines whether the {@link StandardMBean#getWrappedObject()
       
   152          * getWrappedObject} method returns the wrapped object.</p>
       
   153          *
       
   154          * <p>If this option is true, then {@code getWrappedObject()} will return
       
   155          * the same object as {@link StandardMBean#getImplementation()
       
   156          * getImplementation}.  Otherwise, it will return the
       
   157          * StandardMBean instance itself.  The setting of this option
       
   158          * affects the behavior of {@link MBeanServer#getClassLoaderFor
       
   159          * MBeanServer.getClassLoaderFor} and {@link MBeanServer#isInstanceOf
       
   160          * MBeanServer.isInstanceOf}.  The default value is false for
       
   161          * compatibility reasons, but true is a better value for most new code.</p>
       
   162          *
       
   163          * @return true if this StandardMBean's {@link
       
   164          * StandardMBean#getWrappedObject getWrappedObject} returns the wrapped
       
   165          * object.
       
   166          */
       
   167         public boolean isWrappedObjectVisible() {
       
   168             return this.wrappedVisible;
       
   169         }
       
   170 
       
   171         /**
       
   172          * <p>Set the {@link #isWrappedObjectVisible WrappedObjectVisible} option
       
   173          * to the given value.</p>
       
   174          * @param visible the new value.
       
   175          */
       
   176         public void setWrappedObjectVisible(boolean visible) {
       
   177             this.wrappedVisible = visible;
       
   178         }
       
   179 
       
   180         // Canonical objects for each of (MXBean,!MXBean) x (WVisible,!WVisible)
       
   181         private static final Options[] CANONICALS = {
       
   182             new Options(), new Options(), new Options(), new Options(),
       
   183         };
       
   184         static {
       
   185             CANONICALS[1].setMXBeanMappingFactory(MXBeanMappingFactory.DEFAULT);
       
   186             CANONICALS[2].setWrappedObjectVisible(true);
       
   187             CANONICALS[3].setMXBeanMappingFactory(MXBeanMappingFactory.DEFAULT);
       
   188             CANONICALS[3].setWrappedObjectVisible(true);
       
   189         }
       
   190         @Override
       
   191         MBeanOptions[] canonicals() {
       
   192             return CANONICALS;
       
   193         }
       
   194 
       
   195         @Override
       
   196         boolean same(MBeanOptions opts) {
       
   197             return (super.same(opts) && opts instanceof Options &&
       
   198                     ((Options) opts).wrappedVisible == wrappedVisible);
       
   199         }
       
   200     }
   129 
   201 
   130     private final static DescriptorCache descriptors =
   202     private final static DescriptorCache descriptors =
   131         DescriptorCache.getInstance(JMX.proof);
   203         DescriptorCache.getInstance(JMX.proof);
   132 
   204 
   133     /**
   205     /**
   345      *        MBean's implementation. If <code>null</code>, then this
   417      *        MBean's implementation. If <code>null</code>, then this
   346      *        object will use standard JMX design pattern to determine
   418      *        object will use standard JMX design pattern to determine
   347      *        the management interface associated with the given
   419      *        the management interface associated with the given
   348      *        implementation.
   420      *        implementation.
   349      * @param options MBeanOptions that control the operation of the resulting
   421      * @param options MBeanOptions that control the operation of the resulting
   350      *        MBean, as documented in the {@link MBeanOptions} class.
   422      *        MBean.
   351      * @param <T> Allows the compiler to check
   423      * @param <T> Allows the compiler to check
   352      * that {@code implementation} does indeed implement the class
   424      * that {@code implementation} does indeed implement the class
   353      * described by {@code mbeanInterface}.  The compiler can only
   425      * described by {@code mbeanInterface}.  The compiler can only
   354      * check this if {@code mbeanInterface} is a class literal such
   426      * check this if {@code mbeanInterface} is a class literal such
   355      * as {@code MyMBean.class}.
   427      * as {@code MyMBean.class}.
   379      * This constructor is reserved to subclasses.</p>
   451      * This constructor is reserved to subclasses.</p>
   380      *
   452      *
   381      * @param mbeanInterface The Management Interface exported by this
   453      * @param mbeanInterface The Management Interface exported by this
   382      *        MBean.
   454      *        MBean.
   383      * @param options MBeanOptions that control the operation of the resulting
   455      * @param options MBeanOptions that control the operation of the resulting
   384      *        MBean, as documented in the {@link MBeanOptions} class.
   456      *        MBean.
   385      *
   457      *
   386      * @exception IllegalArgumentException if the <var>mbeanInterface</var>
   458      * @exception IllegalArgumentException if the <var>mbeanInterface</var>
   387      *    does not follow JMX design patterns for Management Interfaces, or
   459      *    does not follow JMX design patterns for Management Interfaces, or
   388      *    if <var>this</var> does not implement the specified interface.
   460      *    if <var>this</var> does not implement the specified interface.
   389      **/
   461      **/
   439      * @return The implementation of this Standard MBean (or MXBean).
   511      * @return The implementation of this Standard MBean (or MXBean).
   440      *
   512      *
   441      * @see #setImplementation
   513      * @see #setImplementation
   442      **/
   514      **/
   443     public Object getImplementation() {
   515     public Object getImplementation() {
   444         return mbean.getResource();
   516         return mbean.getWrappedObject();
       
   517     }
       
   518 
       
   519     /**
       
   520      * <p>Get the wrapped implementation object or return this object.</p>
       
   521      *
       
   522      * <p>For compatibility reasons, this method only returns the wrapped
       
   523      * implementation object if the {@link Options#isWrappedObjectVisible
       
   524      * WrappedObjectVisible} option was specified when this StandardMBean
       
   525      * was created.  Otherwise it returns {@code this}.</p>
       
   526      *
       
   527      * <p>If you want the MBeanServer's {@link MBeanServer#getClassLoaderFor
       
   528      * getClassLoaderFor} and {@link MBeanServer#isInstanceOf
       
   529      * isInstanceOf} methods to refer to the wrapped implementation and
       
   530      * not this StandardMBean object, then you must set the
       
   531      * {@code WrappedObjectVisible} option, for example using:</p>
       
   532      *
       
   533      * <pre>
       
   534      * StandardMBean.Options opts = new StandardMBean.Options();
       
   535      * opts.setWrappedObjectVisible(true);
       
   536      * StandardMBean mbean = new StandardMBean(impl, MyMBean.class, opts);
       
   537      * </pre>
       
   538      *
       
   539      * @return The wrapped implementation object, or this StandardMBean
       
   540      * instance.
       
   541      */
       
   542     public Object getWrappedObject() {
       
   543         if (options instanceof Options &&
       
   544                 ((Options) options).isWrappedObjectVisible())
       
   545             return getImplementation();
       
   546         else
       
   547             return this;
       
   548     }
       
   549 
       
   550     /**
       
   551      * <p>Get the ClassLoader of the wrapped implementation object or of this
       
   552      * object.</p>
       
   553      *
       
   554      * <p>For compatibility reasons, this method only returns the ClassLoader
       
   555      * of the wrapped implementation object if the {@link
       
   556      * Options#isWrappedObjectVisible WrappedObjectVisible} option was
       
   557      * specified when this StandardMBean was created. Otherwise it returns
       
   558      * {@code this.getClass().getClassLoader()}.</p>
       
   559      *
       
   560      * <p>If you want the MBeanServer's {@link MBeanServer#getClassLoaderFor
       
   561      * getClassLoaderFor} and {@link MBeanServer#isInstanceOf
       
   562      * isInstanceOf} methods to refer to the wrapped implementation and
       
   563      * not this StandardMBean object, then you must set the
       
   564      * {@code WrappedObjectVisible} option, for example using:</p>
       
   565      *
       
   566      * <pre>
       
   567      * StandardMBean.Options opts = new StandardMBean.Options();
       
   568      * opts.setWrappedObjectVisible(true);
       
   569      * StandardMBean mbean = new StandardMBean(impl, MyMBean.class, opts);
       
   570      * </pre>
       
   571      *
       
   572      * @return The ClassLoader of the wrapped Cimplementation object, or of
       
   573      * this StandardMBean instance.
       
   574      */
       
   575     public ClassLoader getWrappedClassLoader() {
       
   576         return getWrappedObject().getClass().getClassLoader();
   445     }
   577     }
   446 
   578 
   447     /**
   579     /**
   448      * Get the Management Interface of this Standard MBean (or MXBean).
   580      * Get the Management Interface of this Standard MBean (or MXBean).
   449      * @return The management interface of this Standard MBean (or MXBean).
   581      * @return The management interface of this Standard MBean (or MXBean).
   455     /**
   587     /**
   456      * Get the class of the implementation of this Standard MBean (or MXBean).
   588      * Get the class of the implementation of this Standard MBean (or MXBean).
   457      * @return The class of the implementation of this Standard MBean (or MXBean).
   589      * @return The class of the implementation of this Standard MBean (or MXBean).
   458      **/
   590      **/
   459     public Class<?> getImplementationClass() {
   591     public Class<?> getImplementationClass() {
   460         return mbean.getResource().getClass();
   592         return mbean.getWrappedObject().getClass();
   461     }
   593     }
   462 
   594 
   463     /**
   595     /**
   464      * Return the MBeanOptions that were specified or implied for this StandardMBean
   596      * Return the MBeanOptions that were specified or implied for this StandardMBean
   465      * instance.  If an MBeanOptions object was supplied when this StandardMBean
   597      * instance.  If an MBeanOptions object was supplied when this StandardMBean
   557                     getImplementationClass().getName());
   689                     getImplementationClass().getName());
   558         }
   690         }
   559 
   691 
   560         MBeanSupport msupport = mbean;
   692         MBeanSupport msupport = mbean;
   561         final MBeanInfo bi = msupport.getMBeanInfo();
   693         final MBeanInfo bi = msupport.getMBeanInfo();
   562         final Object impl = msupport.getResource();
   694         final Object impl = msupport.getWrappedObject();
   563 
   695 
   564         final boolean immutableInfo = immutableInfo(this.getClass());
   696         final boolean immutableInfo = immutableInfo(this.getClass());
   565 
   697 
   566         final String                  cname = getClassName(bi);
   698         final String                  cname = getClassName(bi);
   567         final String                  text  = getDescription(bi);
   699         final String                  text  = getDescription(bi);
  1182      * @since 1.6
  1314      * @since 1.6
  1183      */
  1315      */
  1184     public ObjectName preRegister(MBeanServer server, ObjectName name)
  1316     public ObjectName preRegister(MBeanServer server, ObjectName name)
  1185             throws Exception {
  1317             throws Exception {
  1186         mbean.register(server, name);
  1318         mbean.register(server, name);
       
  1319         MBeanInjector.inject(mbean.getWrappedObject(), server, name);
  1187         return name;
  1320         return name;
  1188     }
  1321     }
  1189 
  1322 
  1190     /**
  1323     /**
  1191      * <p>Allows the MBean to perform any operations needed after having been
  1324      * <p>Allows the MBean to perform any operations needed after having been