org.apache.commons.modeler
Class BaseModelMBean

java.lang.Object
  extended by org.apache.commons.modeler.BaseModelMBean
All Implemented Interfaces:
DynamicMBean, MBeanRegistration, ModelMBean, ModelMBeanNotificationBroadcaster, NotificationBroadcaster, PersistentMBean
Direct Known Subclasses:
JndiJmx

public class BaseModelMBean
extends Object
implements ModelMBean, MBeanRegistration

Basic implementation of the ModelMBean interface, which supports the minimal requirements of the interface contract.

This can be used directly to wrap an existing java bean, or inside an mlet or anywhere an MBean would be used. The String parameter passed to the constructor will be used to construct an instance of the real object that we wrap. Limitations:

Version:
$Revision: 480402 $ $Date: 2006-11-29 05:43:23 +0100 (Wed, 29 Nov 2006) $
Author:
Craig R. McClanahan, Costin Manolache

Field Summary
protected  BaseNotificationBroadcaster attributeBroadcaster
          Notification broadcaster for attribute changes.
protected  HashMap attributes
          Attribute values.
protected  BaseNotificationBroadcaster generalBroadcaster
          Notification broadcaster for general notifications.
protected  ModelMBeanInfo info
          The ModelMBeanInfo object that controls our activity.
protected  ObjectName oname
           
protected  Registry registry
          Registry we are associated with
protected  Object resource
          The managed resource this MBean is associated with (if any).
protected  String resourceType
           
protected  ModelerSource source
          Source object used to read this mbean.
 
Constructor Summary
BaseModelMBean()
          Construct a ModelMBean with default ModelMBeanInfo information.
BaseModelMBean(ModelMBeanInfo info)
          Construct a ModelMBean associated with the specified ModelMBeanInfo information.
BaseModelMBean(String type)
          Construct a ModelMBean of a specified type.
BaseModelMBean(String type, ModelerSource source)
           
 
Method Summary
 void addAttributeChangeNotificationListener(NotificationListener listener, String name, Object handback)
          Add an attribute change notification event listener to this MBean.
 void addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback)
          Add a notification event listener to this MBean.
protected  ModelMBeanInfo createDefaultModelMBeanInfo()
          Create and return a default ModelMBeanInfo object.
protected  void createResource()
          Set the type of the mbean.
 Object getAttribute(String name)
          Obtain and return the value of a specific attribute of this MBean.
 AttributeList getAttributes(String[] names)
          Obtain and return the values of several attributes of this MBean.
 String getClassName()
           
 ObjectName getJmxName()
           
 Object getManagedResource()
          Get the instance handle of the object against which we execute all methods in this ModelMBean management interface.
 MBeanInfo getMBeanInfo()
          Return the MBeanInfo object for this MBean.
 String getModelerType()
           
 MBeanNotificationInfo[] getNotificationInfo()
          Return an MBeanNotificationInfo object describing the notifications sent by this MBean.
 String getObjectName()
           
 Registry getRegistry()
           
protected  void initModelInfo(String type)
          Set the type of the mbean.
 Object invoke(String name, Object[] params, String[] signature)
          Invoke a particular method on this MBean, and return any returned value.
protected  boolean isModelMBeanInfoValid(ModelMBeanInfo info)
          Is the specified ModelMBeanInfo instance valid?
 void load()
          Instantiates this MBean instance from data found in the persistent store.
 void postDeregister()
           
 void postRegister(Boolean registrationDone)
           
 void preDeregister()
           
 ObjectName preRegister(MBeanServer server, ObjectName name)
           
 void removeAttributeChangeNotificationListener(NotificationListener listener, String name)
          Remove an attribute change notification event listener from this MBean.
 void removeAttributeChangeNotificationListener(NotificationListener listener, String attributeName, Object handback)
          Remove an attribute change notification event listener from this MBean.
 void removeNotificationListener(NotificationListener listener)
          Remove a notification event listener from this MBean.
 void removeNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback)
          Remove a notification event listener from this MBean.
 void removeNotificationListener(NotificationListener listener, Object handback)
          Remove a notification event listener from this MBean.
 void sendAttributeChangeNotification(Attribute oldValue, Attribute newValue)
          Send an AttributeChangeNotification to all registered listeners.
 void sendAttributeChangeNotification(AttributeChangeNotification notification)
          Send an AttributeChangeNotification to all registered listeners.
 void sendNotification(Notification notification)
          Send a Notification to all registered listeners as a jmx.modelmbean.general notification.
 void sendNotification(String message)
          Send a Notification which contains the specified string as a jmx.modelmbean.generic notification.
 void setAttribute(Attribute attribute)
          Set the value of a specific attribute of this MBean.
 AttributeList setAttributes(AttributeList attributes)
          Set the values of several attributes of this MBean.
 void setManagedResource(Object resource, String type)
          Set the instance handle of the object against which we will execute all methods in this ModelMBean management interface.
 void setModeledType(String type)
          Set the type of the mbean.
 void setModelMBeanInfo(ModelMBeanInfo info)
          Initialize the ModelMBeanInfo associated with this ModelMBean.
 void setRegistry(Registry registry)
           
 void store()
          Capture the current state of this MBean instance and write it out to the persistent store.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

attributeBroadcaster

protected BaseNotificationBroadcaster attributeBroadcaster
Notification broadcaster for attribute changes.


registry

protected Registry registry
Registry we are associated with


generalBroadcaster

protected BaseNotificationBroadcaster generalBroadcaster
Notification broadcaster for general notifications.


oname

protected ObjectName oname

info

protected ModelMBeanInfo info
The ModelMBeanInfo object that controls our activity.


resource

protected Object resource
The managed resource this MBean is associated with (if any).


resourceType

protected String resourceType

source

protected ModelerSource source
Source object used to read this mbean. Can be used to persist the mbean


attributes

protected HashMap attributes
Attribute values. XXX That can be stored in the value Field

Constructor Detail

BaseModelMBean

public BaseModelMBean()
               throws MBeanException,
                      RuntimeOperationsException
Construct a ModelMBean with default ModelMBeanInfo information.

Throws:
MBeanException - if the initializer of an object throws an exception
RuntimeOperationsException - if an IllegalArgumentException occurs

BaseModelMBean

public BaseModelMBean(ModelMBeanInfo info)
               throws MBeanException,
                      RuntimeOperationsException
Construct a ModelMBean associated with the specified ModelMBeanInfo information.

Parameters:
info - ModelMBeanInfo for this MBean
Throws:
MBeanException - if the initializer of an object throws an exception
RuntimeOperationsException - if an IllegalArgumentException occurs

BaseModelMBean

public BaseModelMBean(String type)
               throws MBeanException,
                      RuntimeOperationsException
Construct a ModelMBean of a specified type. The type can be a class name or the key used in one of the descriptors. If no descriptor is available, we'll first try to locate one in the same package with the class, then use introspection. The mbean resource will be created.

Parameters:
type - Class name or the type key used in the descriptor.
Throws:
MBeanException
RuntimeOperationsException

BaseModelMBean

public BaseModelMBean(String type,
                      ModelerSource source)
               throws MBeanException,
                      RuntimeOperationsException
Throws:
MBeanException
RuntimeOperationsException
Method Detail

getAttribute

public Object getAttribute(String name)
                    throws AttributeNotFoundException,
                           MBeanException,
                           ReflectionException
Obtain and return the value of a specific attribute of this MBean.

Specified by:
getAttribute in interface DynamicMBean
Parameters:
name - Name of the requested attribute
Throws:
AttributeNotFoundException - if this attribute is not supported by this MBean
MBeanException - if the initializer of an object throws an exception
ReflectionException - if a Java reflection exception occurs when invoking the getter

getAttributes

public AttributeList getAttributes(String[] names)
Obtain and return the values of several attributes of this MBean.

Specified by:
getAttributes in interface DynamicMBean
Parameters:
names - Names of the requested attributes

getMBeanInfo

public MBeanInfo getMBeanInfo()
Return the MBeanInfo object for this MBean.

Specified by:
getMBeanInfo in interface DynamicMBean

invoke

public Object invoke(String name,
                     Object[] params,
                     String[] signature)
              throws MBeanException,
                     ReflectionException
Invoke a particular method on this MBean, and return any returned value.

IMPLEMENTATION NOTE - This implementation will attempt to invoke this method on the MBean itself, or (if not available) on the managed resource object associated with this MBean.

Specified by:
invoke in interface DynamicMBean
Parameters:
name - Name of the operation to be invoked
params - Array containing the method parameters of this operation
signature - Array containing the class names representing the signature of this operation
Throws:
MBeanException - if the initializer of an object throws an exception
ReflectioNException - if a Java reflection exception occurs when invoking a method
ReflectionException

setAttribute

public void setAttribute(Attribute attribute)
                  throws AttributeNotFoundException,
                         MBeanException,
                         ReflectionException
Set the value of a specific attribute of this MBean.

Specified by:
setAttribute in interface DynamicMBean
Parameters:
attribute - The identification of the attribute to be set and the new value
Throws:
AttributeNotFoundException - if this attribute is not supported by this MBean
MBeanException - if the initializer of an object throws an exception
ReflectionException - if a Java reflection exception occurs when invoking the getter

toString

public String toString()
Overrides:
toString in class Object

setAttributes

public AttributeList setAttributes(AttributeList attributes)
Set the values of several attributes of this MBean.

Specified by:
setAttributes in interface DynamicMBean
Parameters:
attributes - THe names and values to be set
Returns:
The list of attributes that were set and their new values

getManagedResource

public Object getManagedResource()
                          throws InstanceNotFoundException,
                                 InvalidTargetObjectTypeException,
                                 MBeanException,
                                 RuntimeOperationsException
Get the instance handle of the object against which we execute all methods in this ModelMBean management interface.

Throws:
InstanceNotFoundException - if the managed resource object cannot be found
MBeanException - if the initializer of the object throws an exception
RuntimeOperationsException - if the managed resource or the resource type is null or invalid
InvalidTargetObjectTypeException

setManagedResource

public void setManagedResource(Object resource,
                               String type)
                        throws InstanceNotFoundException,
                               InvalidTargetObjectTypeException,
                               MBeanException,
                               RuntimeOperationsException
Set the instance handle of the object against which we will execute all methods in this ModelMBean management interface. This method will detect and call "setModelMbean" method. A resource can implement this method to get a reference to the model mbean. The reference can be used to send notification and access the registry.

Specified by:
setManagedResource in interface ModelMBean
Parameters:
resource - The resource object to be managed
type - The type of reference for the managed resource ("ObjectReference", "Handle", "IOR", "EJBHandle", or "RMIReference")
Throws:
InstanceNotFoundException - if the managed resource object cannot be found
InvalidTargetObjectTypeException - if this ModelMBean is asked to handle a reference type it cannot deal with
MBeanException - if the initializer of the object throws an exception
RuntimeOperationsException - if the managed resource or the resource type is null or invalid

setModelMBeanInfo

public void setModelMBeanInfo(ModelMBeanInfo info)
                       throws MBeanException,
                              RuntimeOperationsException
Initialize the ModelMBeanInfo associated with this ModelMBean. After the information and associated descriptors have been customized, the ModelMBean should be registered with the associated MBeanServer. Currently the model can be set after registration. This behavior is deprecated and won't be supported in future versions.

Specified by:
setModelMBeanInfo in interface ModelMBean
Parameters:
info - The ModelMBeanInfo object to be used by this ModelMBean
Throws:
MBeanException - If an exception occurs recording this ModelMBeanInfo information
RuntimeOperations - if the specified parameter is null or invalid
RuntimeOperationsException

addAttributeChangeNotificationListener

public void addAttributeChangeNotificationListener(NotificationListener listener,
                                                   String name,
                                                   Object handback)
                                            throws IllegalArgumentException
Add an attribute change notification event listener to this MBean.

Specified by:
addAttributeChangeNotificationListener in interface ModelMBeanNotificationBroadcaster
Parameters:
listener - Listener that will receive event notifications
name - Name of the attribute of interest, or null to indicate interest in all attributes
handback - Handback object to be sent along with event notifications
Throws:
IllegalArgumentException - if the listener parameter is null

removeAttributeChangeNotificationListener

public void removeAttributeChangeNotificationListener(NotificationListener listener,
                                                      String name)
                                               throws ListenerNotFoundException
Remove an attribute change notification event listener from this MBean.

Specified by:
removeAttributeChangeNotificationListener in interface ModelMBeanNotificationBroadcaster
Parameters:
listener - The listener to be removed
name - The attribute name for which no more events are required
Throws:
ListenerNotFoundException - if this listener is not registered in the MBean

removeAttributeChangeNotificationListener

public void removeAttributeChangeNotificationListener(NotificationListener listener,
                                                      String attributeName,
                                                      Object handback)
                                               throws ListenerNotFoundException
Remove an attribute change notification event listener from this MBean.

Parameters:
listener - The listener to be removed
attributeName - The attribute name for which no more events are required
handback - Handback object to be sent along with event notifications
Throws:
ListenerNotFoundException - if this listener is not registered in the MBean

sendAttributeChangeNotification

public void sendAttributeChangeNotification(AttributeChangeNotification notification)
                                     throws MBeanException,
                                            RuntimeOperationsException
Send an AttributeChangeNotification to all registered listeners.

Specified by:
sendAttributeChangeNotification in interface ModelMBeanNotificationBroadcaster
Parameters:
notification - The AttributeChangeNotification that will be passed
Throws:
MBeanException - if an object initializer throws an exception
RuntimeOperationsException - wraps IllegalArgumentException when the specified notification is null or invalid

sendAttributeChangeNotification

public void sendAttributeChangeNotification(Attribute oldValue,
                                            Attribute newValue)
                                     throws MBeanException,
                                            RuntimeOperationsException
Send an AttributeChangeNotification to all registered listeners.

Specified by:
sendAttributeChangeNotification in interface ModelMBeanNotificationBroadcaster
Parameters:
oldValue - The original value of the Attribute
newValue - The new value of the Attribute
Throws:
MBeanException - if an object initializer throws an exception
RuntimeOperationsException - wraps IllegalArgumentException when the specified notification is null or invalid

sendNotification

public void sendNotification(Notification notification)
                      throws MBeanException,
                             RuntimeOperationsException
Send a Notification to all registered listeners as a jmx.modelmbean.general notification.

Specified by:
sendNotification in interface ModelMBeanNotificationBroadcaster
Parameters:
notification - The Notification that will be passed
Throws:
MBeanException - if an object initializer throws an exception
RuntimeOperationsException - wraps IllegalArgumentException when the specified notification is null or invalid

sendNotification

public void sendNotification(String message)
                      throws MBeanException,
                             RuntimeOperationsException
Send a Notification which contains the specified string as a jmx.modelmbean.generic notification.

Specified by:
sendNotification in interface ModelMBeanNotificationBroadcaster
Parameters:
message - The message string to be passed
Throws:
MBeanException - if an object initializer throws an exception
RuntimeOperationsException - wraps IllegalArgumentException when the specified notification is null or invalid

addNotificationListener

public void addNotificationListener(NotificationListener listener,
                                    NotificationFilter filter,
                                    Object handback)
                             throws IllegalArgumentException
Add a notification event listener to this MBean.

Specified by:
addNotificationListener in interface NotificationBroadcaster
Parameters:
listener - Listener that will receive event notifications
filter - Filter object used to filter event notifications actually delivered, or null for no filtering
handback - Handback object to be sent along with event notifications
Throws:
IllegalArgumentException - if the listener parameter is null

getNotificationInfo

public MBeanNotificationInfo[] getNotificationInfo()
Return an MBeanNotificationInfo object describing the notifications sent by this MBean.

Specified by:
getNotificationInfo in interface NotificationBroadcaster

removeNotificationListener

public void removeNotificationListener(NotificationListener listener)
                                throws ListenerNotFoundException
Remove a notification event listener from this MBean.

Specified by:
removeNotificationListener in interface NotificationBroadcaster
Parameters:
listener - The listener to be removed (any and all registrations for this listener will be eliminated)
Throws:
ListenerNotFoundException - if this listener is not registered in the MBean

removeNotificationListener

public void removeNotificationListener(NotificationListener listener,
                                       Object handback)
                                throws ListenerNotFoundException
Remove a notification event listener from this MBean.

Parameters:
listener - The listener to be removed (any and all registrations for this listener will be eliminated)
handback - Handback object to be sent along with event notifications
Throws:
ListenerNotFoundException - if this listener is not registered in the MBean

removeNotificationListener

public void removeNotificationListener(NotificationListener listener,
                                       NotificationFilter filter,
                                       Object handback)
                                throws ListenerNotFoundException
Remove a notification event listener from this MBean.

Parameters:
listener - The listener to be removed (any and all registrations for this listener will be eliminated)
filter - Filter object used to filter event notifications actually delivered, or null for no filtering
handback - Handback object to be sent along with event notifications
Throws:
ListenerNotFoundException - if this listener is not registered in the MBean

load

public void load()
          throws InstanceNotFoundException,
                 MBeanException,
                 RuntimeOperationsException
Instantiates this MBean instance from data found in the persistent store. The data loaded could include attribute and operation values. This method should be called during construction or initialization of the instance, and before the MBean is registered with the MBeanServer.

IMPLEMENTATION NOTE - This implementation does not support persistence.

Specified by:
load in interface PersistentMBean
Throws:
InstanceNotFoundException - if the managed resource object cannot be found
MBeanException - if the initializer of the object throws an exception
RuntimeOperationsException - if an exception is reported by the persistence mechanism

store

public void store()
           throws InstanceNotFoundException,
                  MBeanException,
                  RuntimeOperationsException
Capture the current state of this MBean instance and write it out to the persistent store. The state stored could include attribute and operation values. If one of these methods of persistence is not supported, a "service not found" exception will be thrown.

IMPLEMENTATION NOTE - This implementation does not support persistence.

Specified by:
store in interface PersistentMBean
Throws:
InstanceNotFoundException - if the managed resource object cannot be found
MBeanException - if the initializer of the object throws an exception, or persistence is not supported
RuntimeOperationsException - if an exception is reported by the persistence mechanism

setModeledType

public void setModeledType(String type)
Set the type of the mbean. This is used as a key to locate the description in the Registry.

Parameters:
type - the type of classname of the modeled object

initModelInfo

protected void initModelInfo(String type)
Set the type of the mbean. This is used as a key to locate the description in the Registry.

Parameters:
type - the type of classname of the modeled object

createResource

protected void createResource()
Set the type of the mbean. This is used as a key to locate the description in the Registry.


getModelerType

public String getModelerType()

getClassName

public String getClassName()

getJmxName

public ObjectName getJmxName()

getObjectName

public String getObjectName()

setRegistry

public void setRegistry(Registry registry)

getRegistry

public Registry getRegistry()

createDefaultModelMBeanInfo

protected ModelMBeanInfo createDefaultModelMBeanInfo()
Create and return a default ModelMBeanInfo object.


isModelMBeanInfoValid

protected boolean isModelMBeanInfoValid(ModelMBeanInfo info)
Is the specified ModelMBeanInfo instance valid?

IMPLEMENTATION NOTE - This implementation does not check anything, but this method can be overridden as required.

Parameters:
info - The ModelMBeanInfo object to check

preRegister

public ObjectName preRegister(MBeanServer server,
                              ObjectName name)
                       throws Exception
Specified by:
preRegister in interface MBeanRegistration
Throws:
Exception

postRegister

public void postRegister(Boolean registrationDone)
Specified by:
postRegister in interface MBeanRegistration

preDeregister

public void preDeregister()
                   throws Exception
Specified by:
preDeregister in interface MBeanRegistration
Throws:
Exception

postDeregister

public void postDeregister()
Specified by:
postDeregister in interface MBeanRegistration


Copyright © 2002-2011 Apache Software Foundation. All Rights Reserved.