javax.management

Class StandardMBean

public class StandardMBean extends Object implements DynamicMBean

Provides a dynamic management bean by using reflection on an interface and an implementing class. By default, a bean instance is paired up with its interface based on specific naming conventions (if the implementation is called X, the interface must be XMBean). Using this class removes the need to use a specific naming system to match up the two. Instead, an instance of this bean is created either via explicit construction or subclassing, and this provides access to the attributes, constructors and operations of the implementation via reflection. Various hooks are provided in order to allow customization of this process.

Since: 1.5

Constructor Summary
protected StandardMBean(Class<?> iface)
Constructs a new {@link StandardMBean} using the specified interface and this as the instance.
<T>StandardMBean(T impl, Class<T> iface)
Constructs a new {@link StandardMBean} using the specified interface and the supplied instance as the implementation.
Method Summary
protected voidcacheMBeanInfo(MBeanInfo info)
Caches the {@link MBeanInfo} instance for this object.
ObjectgetAttribute(String name)
Obtains the value of the specified attribute of the management bean.
AttributeListgetAttributes(String[] names)
Obtains the values of each of the specified attributes of the management bean.
protected MBeanInfogetCachedMBeanInfo()
Returns the cached {@link MBeanInfo} instance for this object.
protected StringgetClassName(MBeanInfo info)
Returns the class name that will be used in the {@link MBeanInfo} instance.
protected MBeanConstructorInfo[]getConstructors(MBeanConstructorInfo[] constructors, Object impl)
Returns information on the constructors that will be used in the {@link MBeanInfo} instance.
protected StringgetDescription(MBeanAttributeInfo info)
Returns the description of the attribute that will be used in the supplied {@link MBeanAttributeInfo} instance.
protected StringgetDescription(MBeanConstructorInfo info)
Returns the description of the constructor that will be used in the supplied {@link MBeanConstructorInfo} instance.
protected StringgetDescription(MBeanConstructorInfo info, MBeanParameterInfo param, int n)
Returns the description of the nth parameter of the constructor that will be used in the supplied {@link MBeanParameterInfo} instance.
protected StringgetDescription(MBeanFeatureInfo info)
Returns the description of the supplied feature that will be used in the supplied {@link MBeanFeatureInfo} instance.
protected StringgetDescription(MBeanInfo info)
Returns the description of the bean that will be used in the supplied {@link MBeanInfo} instance.
protected StringgetDescription(MBeanOperationInfo info)
Returns the description of the operation that will be used in the supplied {@link MBeanOperationInfo} instance.
protected StringgetDescription(MBeanOperationInfo info, MBeanParameterInfo param, int n)
Returns the description of the nth parameter of the operation that will be used in the supplied {@link MBeanParameterInfo} instance.
protected intgetImpact(MBeanOperationInfo info)
Returns the impact of the operation that will be used in the supplied {@link MBeanOperationInfo} instance.
ObjectgetImplementation()
Returns the instance that implements this bean.
Class<?>getImplementationClass()
Returns the class of the instance that implements this bean.
MBeanInfogetMBeanInfo()

Returns an information object which lists the attributes and actions associated with the management bean.

Class<?>getMBeanInterface()
Returns the interface for this management bean.
protected StringgetParameterName(MBeanConstructorInfo info, MBeanParameterInfo param, int n)
Returns the name of the nth parameter of the constructor that will be used in the supplied {@link MBeanParameterInfo} instance.
protected StringgetParameterName(MBeanOperationInfo info, MBeanParameterInfo param, int n)
Returns the name of the nth parameter of the operation that will be used in the supplied {@link MBeanParameterInfo} instance.
Objectinvoke(String name, Object[] params, String[] signature)
Invokes the specified action on the management bean using the supplied parameters.
voidsetAttribute(Attribute attribute)
Sets the value of the specified attribute of the management bean.
AttributeListsetAttributes(AttributeList attributes)
Sets the value of each of the specified attributes to that supplied by the {@link Attribute} object.
voidsetImplementation(Object impl)
Replaces the implementation of the interface used by this instance with the one specified.

Constructor Detail

StandardMBean

protected StandardMBean(Class<?> iface)
Constructs a new {@link StandardMBean} using the specified interface and this as the instance. This should be used to create an instance via subclassing.

Parameters: iface the interface this bean implements, or null if the interface should be determined using the naming convention (class X has interface XMBean).

Throws: NotCompliantMBeanException if this class doesn't implement the interface or a method appears in the interface that doesn't comply with the naming conventions.

StandardMBean

public <T> StandardMBean(T impl, Class<T> iface)
Constructs a new {@link StandardMBean} using the specified interface and the supplied instance as the implementation.

Parameters: impl the implementation. iface the interface the bean implements, or null if the interface should be determined using the naming convention (class X has interface XMBean).

Throws: IllegalArgumentException if impl is null. NotCompliantMBeanException if impl doesn't implement the interface or a method appears in the interface that doesn't comply with the naming conventions.

Method Detail

cacheMBeanInfo

protected void cacheMBeanInfo(MBeanInfo info)
Caches the {@link MBeanInfo} instance for this object. This is a customization hook, so that subclasses can choose the caching policy used. The default implementation caches the value in the instance itself. Subclasses may override this so as to not cache the data at all, or so as to use a cache shared between multiple beans.

Parameters: info the {@link MBeanInfo} instance to cache, or null if there is no new value to cache. When the value is not null, the cache should replace the current value with the value supplied here.

See Also: getCachedMBeanInfo

getAttribute

public Object getAttribute(String name)
Obtains the value of the specified attribute of the management bean. The management bean should perform a lookup for the named attribute, and return its value by calling the appropriate getter method, if possible.

Parameters: name the name of the attribute to retrieve.

Returns: the value of the specified attribute.

Throws: AttributeNotFoundException if the name does not correspond to an attribute of the bean. MBeanException if retrieving the attribute causes the bean to throw an exception (which becomes the cause of this exception). ReflectionException if an exception occurred in trying to use the reflection interface to lookup the attribute. The thrown exception is the cause of this exception.

See Also: StandardMBean

getAttributes

public AttributeList getAttributes(String[] names)
Obtains the values of each of the specified attributes of the management bean. The returned list includes those attributes that were retrieved and their corresponding values.

Parameters: names the names of the attributes to retrieve.

Returns: a list of the retrieved attributes.

See Also: setAttributes

getCachedMBeanInfo

protected MBeanInfo getCachedMBeanInfo()
Returns the cached {@link MBeanInfo} instance for this object. This is a customization hook, so that subclasses can choose the caching policy used. The default implementation caches the value in the instance itself, and returns this value on calls to this method.

Returns: the cached {@link MBeanInfo} instance, or null if no value is cached.

See Also: cacheMBeanInfo

getClassName

protected String getClassName(MBeanInfo info)
Returns the class name that will be used in the {@link MBeanInfo} instance. This is a customization hook, so that subclasses can provide a custom class name. By default, this returns the class name from the supplied {@link MBeanInfo} instance.

Parameters: info the {@link MBeanInfo} instance constructed via reflection.

Returns: the class name to use in the instance.

getConstructors

protected MBeanConstructorInfo[] getConstructors(MBeanConstructorInfo[] constructors, Object impl)
Returns information on the constructors that will be used in the {@link MBeanInfo} instance. This is a customization hook, so that subclasses can provide their own information on the bean's constructors, if necessary. By default, this method returns null unless the implementation supplied is either null or this. This default implementation prevents the use of {@link MBeanServer#createMBean} in cases where the bean is not created as a subclass of {@link StandardMBean}.

Parameters: constructors the constructor information created via reflection. impl the implementation, or null if this should be ignored.

Returns: the constructor information to use.

getDescription

protected String getDescription(MBeanAttributeInfo info)
Returns the description of the attribute that will be used in the supplied {@link MBeanAttributeInfo} instance. This is a customization hook, so that subclasses can provide a custom description. By default, this calls {@link #getDescription(MBeanFeatureInfo)} with the supplied {@link MBeanAttributeInfo} instance.

Parameters: info the {@link MBeanAttributeInfo} instance constructed via reflection.

Returns: the description to use in the instance.

getDescription

protected String getDescription(MBeanConstructorInfo info)
Returns the description of the constructor that will be used in the supplied {@link MBeanConstructorInfo} instance. This is a customization hook, so that subclasses can provide a custom description. By default, this calls {@link #getDescription(MBeanFeatureInfo)} with the supplied {@link MBeanConstructorInfo} instance.

Parameters: info the {@link MBeanConstructorInfo} instance constructed via reflection.

Returns: the description to use in the instance.

getDescription

protected String getDescription(MBeanConstructorInfo info, MBeanParameterInfo param, int n)
Returns the description of the nth parameter of the constructor that will be used in the supplied {@link MBeanParameterInfo} instance. This is a customization hook, so that subclasses can provide a custom description. By default, this calls param.getDescription().

Parameters: info the {@link MBeanConstructorInfo} instance constructed via reflection. param the {@link MBeanParameterInfo} instance constructed via reflection. n the number of the parameter, in order to link it to the information on the constructor.

Returns: the description to use in the instance.

getDescription

protected String getDescription(MBeanFeatureInfo info)
Returns the description of the supplied feature that will be used in the supplied {@link MBeanFeatureInfo} instance. This is a customization hook, so that subclasses can provide a custom description. By default, this calls info.getDescription(). This method is also called by default for the more specific description methods for attributes, constructors and operations.

Parameters: info the {@link MBeanFeatureInfo} instance constructed via reflection.

Returns: the description to use in the instance.

getDescription

protected String getDescription(MBeanInfo info)
Returns the description of the bean that will be used in the supplied {@link MBeanInfo} instance. This is a customization hook, so that subclasses can provide a custom description. By default, this calls info.getDescription().

Parameters: info the {@link MBeanInfo} instance constructed via reflection.

Returns: the description to use in the instance.

getDescription

protected String getDescription(MBeanOperationInfo info)
Returns the description of the operation that will be used in the supplied {@link MBeanOperationInfo} instance. This is a customization hook, so that subclasses can provide a custom description. By default, this calls {@link #getDescription(MBeanFeatureInfo)} with the supplied {@link MBeanOperationInfo} instance.

Parameters: info the {@link MBeanOperationInfo} instance constructed via reflection.

Returns: the description to use in the instance.

getDescription

protected String getDescription(MBeanOperationInfo info, MBeanParameterInfo param, int n)
Returns the description of the nth parameter of the operation that will be used in the supplied {@link MBeanParameterInfo} instance. This is a customization hook, so that subclasses can provide a custom description. By default, this calls param.getDescription().

Parameters: info the {@link MBeanOperationInfo} instance constructed via reflection. param the {@link MBeanParameterInfo} instance constructed via reflection. n the number of the parameter, in order to link it to the information on the operation.

Returns: the description to use in the instance.

getImpact

protected int getImpact(MBeanOperationInfo info)
Returns the impact of the operation that will be used in the supplied {@link MBeanOperationInfo} instance. This is a customization hook, so that subclasses can provide a custom impact flag. By default, this returns info.getImpact().

Parameters: info the {@link MBeanOperationInfo} instance constructed via reflection.

Returns: the impact flag to use in the instance.

getImplementation

public Object getImplementation()
Returns the instance that implements this bean.

Returns: the implementation.

getImplementationClass

public Class<?> getImplementationClass()
Returns the class of the instance that implements this bean.

Returns: the implementation class.

getMBeanInfo

public MBeanInfo getMBeanInfo()

Returns an information object which lists the attributes and actions associated with the management bean. This implementation proceeds as follows:

  1. {@link #getCachedMBeanInfo()} is called to obtain the cached instance. If this returns a non-null value, this value is returned.
  2. If there is no cached value, then the method proceeds to create one. During this process, the customization hooks detailed in this class are called to allow the values used to be overrided:
    • For each attribute, {@link #getDescription(MBeanAttributeInfo)} is called.
    • For each constructor, {@link #getDescription(MBeanConstructorInfo)} is called, along with {@link #getDescription(MBeanConstructorInfo, MBeanParameterInfo, int)} and {@link #getParameterName(MBeanConstructorInfo, MBeanParameterInfo, int)} for each parameter.
    • The constructors may be replaced as a whole by a call to {@link #getConstructors(MBeanConstructorInfo[], Object)}.
    • For each operation, {@link #getDescription(MBeanOperationInfo)} and {@link #getImpact(MBeanOperationInfo)} are called, along with {@link #getDescription(MBeanOperationInfo, MBeanParameterInfo, int)} and {@link #getParameterName(MBeanOperationInfo, MBeanParameterInfo, int)} for each parameter.
    • {@link #getClassName(MBeanInfo)} and {@link #getDescription(MBeanInfo)} are called to customise the basic information about the class.
  3. Finally, {@link #cacheMBeanInfo(MBeanInfo)} is called with the created instance before it is returned.

Returns: a description of the management bean, including all exposed attributes and actions.

getMBeanInterface

public final Class<?> getMBeanInterface()
Returns the interface for this management bean.

Returns: the management interface.

getParameterName

protected String getParameterName(MBeanConstructorInfo info, MBeanParameterInfo param, int n)
Returns the name of the nth parameter of the constructor that will be used in the supplied {@link MBeanParameterInfo} instance. This is a customization hook, so that subclasses can provide a custom name. By default, this calls param.getName().

Parameters: info the {@link MBeanConstructorInfo} instance constructed via reflection. param the {@link MBeanParameterInfo} instance constructed via reflection. n the number of the parameter, in order to link it to the information on the constructor.

Returns: the name to use in the instance.

getParameterName

protected String getParameterName(MBeanOperationInfo info, MBeanParameterInfo param, int n)
Returns the name of the nth parameter of the operation that will be used in the supplied {@link MBeanParameterInfo} instance. This is a customization hook, so that subclasses can provide a custom name. By default, this calls param.getName().

Parameters: info the {@link MBeanOperationInfo} instance constructed via reflection. param the {@link MBeanParameterInfo} instance constructed via reflection. n the number of the parameter, in order to link it to the information on the operation.

Returns: the name to use in the instance.

invoke

public Object invoke(String name, Object[] params, String[] signature)
Invokes the specified action on the management bean using the supplied parameters. The signature of the action is specified by a {@link String} array, which lists the classes corresponding to each parameter. The class loader used to load these classes is the same as that used for loading the management bean itself.

Parameters: name the name of the action to invoke. params the parameters used to call the action. signature the signature of the action.

Returns: the return value of the action.

Throws: MBeanException if the action throws an exception. The thrown exception is the cause of this exception. ReflectionException if an exception occurred in trying to use the reflection interface to invoke the action. The thrown exception is the cause of this exception.

setAttribute

public void setAttribute(Attribute attribute)
Sets the value of the specified attribute of the management bean. The management bean should perform a lookup for the named attribute, and sets its value using the associated setter method, if possible.

Parameters: attribute the attribute to set.

Throws: AttributeNotFoundException if the attribute does not correspond to an attribute of the bean. InvalidAttributeValueException if the value is invalid for this particular attribute of the bean. MBeanException if setting the attribute causes the bean to throw an exception (which becomes the cause of this exception). ReflectionException if an exception occurred in trying to use the reflection interface to lookup the attribute. The thrown exception is the cause of this exception.

See Also: getAttribute

setAttributes

public AttributeList setAttributes(AttributeList attributes)
Sets the value of each of the specified attributes to that supplied by the {@link Attribute} object. The returned list contains the attributes that were set and their new values.

Parameters: attributes the attributes to set.

Returns: a list of the changed attributes.

See Also: StandardMBean

setImplementation

public void setImplementation(Object impl)
Replaces the implementation of the interface used by this instance with the one specified. The new implementation must be non-null and implement the interface specified on construction of this instance.

Throws: IllegalArgumentException if impl is null. NotCompliantMBeanException if impl doesn't implement the interface or a method appears in the interface that doesn't comply with the naming conventions.