mx4j
Class AbstractDynamicMBean

java.lang.Object
  extended bymx4j.AbstractDynamicMBean
All Implemented Interfaces:
DynamicMBean

public abstract class AbstractDynamicMBean
extends java.lang.Object
implements DynamicMBean

Utility class that allow the user to easily write DynamicMBeans.
By extending this class, the developer does not have to implement the methods of the DynamicMBean interface, but has instead to provide only the metadata (by overriding few methods) and the implementation (by implementing the methods) of the MBean itself.
The methods to override that provides metadata information are usually the following:

For example, the following MBean only has one manageable attribute:
 public class SimpleDynamic extends AbstractDynamicMBean
 {
    protected MBeanAttributeInfo[] createMBeanAttributeInfo()
    {
       return new MBeanAttributeInfo[]
       {
          new MBeanAttributeInfo("Name", String.class.getName(), "The name", true, true, false)
       };
    }

    protected String getMBeanDescription()
    {
       return "A simple DynamicMBean";
    }

    public String getName() { ... }

    public void setName(String name) { ... }
 }
 
It is responsibility of the developer to specify the metadata and implement the methods specified by the metadata, that will be invoked via reflection by the AbstractDynamicMBean class. For this reason, the methods belonging to the MBean implementation (in the case above getName() and setName(...)) must be public.

Version:
$Revision: 1.3 $
Author:
Simone Bordet

Constructor Summary
protected AbstractDynamicMBean()
          Only subclasses can create a new instance of an AbstractDynamicMBean.
 
Method Summary
protected  MBeanAttributeInfo[] createMBeanAttributeInfo()
          To be overridden to return metadata information about manageable attributes.
protected  MBeanConstructorInfo[] createMBeanConstructorInfo()
          To be overridden to return metadata information about manageable constructors.
protected  MBeanInfo createMBeanInfo()
          Creates the MBeanInfo for this instance, calling in succession factory methods that the user can override.
protected  MBeanNotificationInfo[] createMBeanNotificationInfo()
          To be overridden to return metadata information about manageable notifications.
protected  MBeanOperationInfo[] createMBeanOperationInfo()
          To be overridden to return metadata information about manageable operations.
protected  java.lang.reflect.Method findMethod(java.lang.Class cls, java.lang.String name, java.lang.Class[] params)
          Returns the (public) method with the given name and signature on the given class.
 java.lang.Object getAttribute(java.lang.String attribute)
          Returns the value of the manageable attribute, as specified by the DynamicMBean interface.
 AttributeList getAttributes(java.lang.String[] attributes)
          Returns the manageable attributes, as specified by the DynamicMBean interface.
protected  java.lang.String getMBeanClassName()
          To be overridden to return metadata information about the class name of this MBean; by default returns this class' name.
protected  java.lang.String getMBeanDescription()
          To be overridden to return metadata information about the description of this MBean.
 MBeanInfo getMBeanInfo()
          Returns the MBeaInfo, as specified by the DynamicMBean interface; the default implementation caches the value returned by createMBeanInfo() (that is thus called only once).
protected  java.lang.Object getResource()
          Returns the resource object on which invoke attribute's getters, attribute's setters and operation's methods
protected  java.lang.Object invoke(java.lang.String name, java.lang.Class[] params, java.lang.Object[] args)
          Looks up the method to call on the object set as resource (see setResource(java.lang.Object)) and invokes it.
 java.lang.Object invoke(java.lang.String method, java.lang.Object[] arguments, java.lang.String[] params)
          Returns the value of the manageable operation as specified by the DynamicMBean interface
protected  java.lang.Object invokeMethod(java.lang.reflect.Method method, java.lang.Object resource, java.lang.Object[] args)
          Invokes the given method on the given resource object with the given arguments.
 void setAttribute(Attribute attribute)
          Sets the value of the manageable attribute, as specified by the DynamicMBean interface.
 AttributeList setAttributes(AttributeList attributes)
          Sets the manageable attributes, as specified by the DynamicMBean interface.
protected  void setMBeanInfo(MBeanInfo info)
          Sets the MBeanInfo object cached by this instance.
 void setResource(java.lang.Object resource)
          Specifies the resource object on which invoke attribute's getters, attribute's setters and operation's methods.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractDynamicMBean

protected AbstractDynamicMBean()
Only subclasses can create a new instance of an AbstractDynamicMBean.

See Also:
createMBeanConstructorInfo()
Method Detail

getAttribute

public java.lang.Object getAttribute(java.lang.String attribute)
                              throws AttributeNotFoundException,
                                     MBeanException,
                                     ReflectionException
Returns the value of the manageable attribute, as specified by the DynamicMBean interface.

Specified by:
getAttribute in interface DynamicMBean
Parameters:
attribute - The attribute name for the value being retrieved.
Returns:
Object The value of the attribute.
Throws:
ReflectionException - Wraps a java.lang.Exception while trying to invoke the getter.
AttributeNotFoundException - If the given attribute isn't found.
MBeanException - Wraps a java.lang.Exception
See Also:
createMBeanAttributeInfo()

getAttributes

public AttributeList getAttributes(java.lang.String[] attributes)
Returns the manageable attributes, as specified by the DynamicMBean interface.

Specified by:
getAttributes in interface DynamicMBean
Parameters:
attributes - The array of attributes being retrieved
Returns:
AttributeList The list of Attribute

getMBeanInfo

public MBeanInfo getMBeanInfo()
Returns the MBeaInfo, as specified by the DynamicMBean interface; the default implementation caches the value returned by createMBeanInfo() (that is thus called only once).

Specified by:
getMBeanInfo in interface DynamicMBean
Returns:
MBeanInfo an instance of MBeanInfo
See Also:
createMBeanInfo(), setMBeanInfo(javax.management.MBeanInfo)

invoke

public java.lang.Object invoke(java.lang.String method,
                               java.lang.Object[] arguments,
                               java.lang.String[] params)
                        throws MBeanException,
                               ReflectionException
Returns the value of the manageable operation as specified by the DynamicMBean interface

Specified by:
invoke in interface DynamicMBean
Parameters:
method - The name of the method to be invoked.
arguments - The array of arguments to be set.
params - Contains the signature of the method to be invoked.
Returns:
Object The object returned by the invocaton.
Throws:
ReflectionException
MBeanException - Wraps a java.lang.Exception
See Also:
createMBeanOperationInfo()

setAttribute

public void setAttribute(Attribute attribute)
                  throws AttributeNotFoundException,
                         InvalidAttributeValueException,
                         MBeanException,
                         ReflectionException
Sets the value of the manageable attribute, as specified by the DynamicMBean interface.

Specified by:
setAttribute in interface DynamicMBean
Parameters:
attribute - The Attribute being set.
Throws:
MBeanException - Wraps a java.lang.Exception which is thrown by the setter.
AttributeNotFoundException
InvalidAttributeValueException
ReflectionException
See Also:
createMBeanAttributeInfo()

setAttributes

public AttributeList setAttributes(AttributeList attributes)
Sets the manageable attributes, as specified by the DynamicMBean interface.

Specified by:
setAttributes in interface DynamicMBean
Parameters:
attributes - The attribute being set.
Returns:
AttributeList the list of Attribute successfully set.

invoke

protected java.lang.Object invoke(java.lang.String name,
                                  java.lang.Class[] params,
                                  java.lang.Object[] args)
                           throws InvalidAttributeValueException,
                                  MBeanException,
                                  ReflectionException
Looks up the method to call on the object set as resource (see setResource(java.lang.Object)) and invokes it. If the resource object is not specified, defaults to 'this'. The default implementation requires that the methods that implement attribute and operation behavior on the resource object are public, but it is possible to override this behavior, and call also private methods.

Throws:
InvalidAttributeValueException
MBeanException
ReflectionException
See Also:
findMethod(java.lang.Class, java.lang.String, java.lang.Class[]), invokeMethod(java.lang.reflect.Method, java.lang.Object, java.lang.Object[])

findMethod

protected java.lang.reflect.Method findMethod(java.lang.Class cls,
                                              java.lang.String name,
                                              java.lang.Class[] params)
                                       throws java.lang.NoSuchMethodException
Returns the (public) method with the given name and signature on the given class.
Override to return non-public methods, or to map methods to other classes, or to map methods with different signatures

Throws:
java.lang.NoSuchMethodException
See Also:
invoke(String, Class[], Object[]), invokeMethod(java.lang.reflect.Method, java.lang.Object, java.lang.Object[])

invokeMethod

protected java.lang.Object invokeMethod(java.lang.reflect.Method method,
                                        java.lang.Object resource,
                                        java.lang.Object[] args)
                                 throws java.lang.IllegalAccessException,
                                        java.lang.IllegalArgumentException,
                                        java.lang.reflect.InvocationTargetException
Invokes the given method on the given resource object with the given arguments.
Override to map methods to other objects, or to map methods with different arguments

Throws:
java.lang.IllegalAccessException
java.lang.IllegalArgumentException
java.lang.reflect.InvocationTargetException
See Also:
invoke(String, Class[], Object[]), findMethod(java.lang.Class, java.lang.String, java.lang.Class[])

getResource

protected java.lang.Object getResource()
Returns the resource object on which invoke attribute's getters, attribute's setters and operation's methods

See Also:
setResource(java.lang.Object)

setResource

public void setResource(java.lang.Object resource)
Specifies the resource object on which invoke attribute's getters, attribute's setters and operation's methods.

See Also:
getResource()

setMBeanInfo

protected void setMBeanInfo(MBeanInfo info)
Sets the MBeanInfo object cached by this instance.
The given MBeanInfo is not cloned.

See Also:
getMBeanInfo()

createMBeanInfo

protected MBeanInfo createMBeanInfo()
Creates the MBeanInfo for this instance, calling in succession factory methods that the user can override. Information to create MBeanInfo are taken calling the following methods:


createMBeanAttributeInfo

protected MBeanAttributeInfo[] createMBeanAttributeInfo()
To be overridden to return metadata information about manageable attributes.


createMBeanConstructorInfo

protected MBeanConstructorInfo[] createMBeanConstructorInfo()
To be overridden to return metadata information about manageable constructors.


createMBeanOperationInfo

protected MBeanOperationInfo[] createMBeanOperationInfo()
To be overridden to return metadata information about manageable operations.


createMBeanNotificationInfo

protected MBeanNotificationInfo[] createMBeanNotificationInfo()
To be overridden to return metadata information about manageable notifications.


getMBeanClassName

protected java.lang.String getMBeanClassName()
To be overridden to return metadata information about the class name of this MBean; by default returns this class' name.


getMBeanDescription

protected java.lang.String getMBeanDescription()
To be overridden to return metadata information about the description of this MBean.



Copyright © 2001-2002 MX4J Team. All Rights Reserved.