FileDocCategorySizeDatePackage
BaseModelMBean.javaAPI DocApache Tomcat 6.0.1443840Fri Jul 20 04:20:34 BST 2007org.apache.tomcat.util.modeler

BaseModelMBean

public class BaseModelMBean extends Object implements DynamicMBean, ModelMBeanNotificationBroadcaster, MBeanRegistration

Basic implementation of the DynamicMBean 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. Limitations:

  • Only managed resources of type objectReference are supportd.
  • Caching of attribute values and operation results is not supported. All calls to invoke() are immediately executed.
  • Persistence of MBean attributes and operations is not supported.
  • All classes referenced as attribute types, operation parameters, or operation return values must be one of the following:
    • One of the Java primitive types (boolean, byte, char, double, float, integer, long, short). Corresponding value will be wrapped in the appropriate wrapper class automatically.
    • Operations that return no value should declare a return type of void.
  • Attribute caching is not supported
author
Craig R. McClanahan
author
Costin Manolache

Fields Summary
private static org.apache.juli.logging.Log
log
protected ObjectName
oname
protected BaseNotificationBroadcaster
attributeBroadcaster
Notification broadcaster for attribute changes.
protected BaseNotificationBroadcaster
generalBroadcaster
Notification broadcaster for general notifications.
protected ManagedBean
managedBean
Metadata for the mbean instance.
protected Object
resource
The managed resource this MBean is associated with (if any).
static final Object[]
NO_ARGS_PARAM
static final Class[]
NO_ARGS_PARAM_SIG
protected String
resourceType
Constructors Summary
protected BaseModelMBean()
Construct a ModelMBean with default ModelMBeanInfo information.

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


    // ----------------------------------------------------------- Constructors

                                   
         
        super();
    
Methods Summary
public voidaddAttributeChangeNotificationListener(javax.management.NotificationListener listener, java.lang.String name, java.lang.Object handback)
Add an attribute change notification event listener to this MBean.

param
listener Listener that will receive event notifications
param
name Name of the attribute of interest, or null to indicate interest in all attributes
param
handback Handback object to be sent along with event notifications
exception
IllegalArgumentException if the listener parameter is null


        if (listener == null)
            throw new IllegalArgumentException("Listener is null");
        if (attributeBroadcaster == null)
            attributeBroadcaster = new BaseNotificationBroadcaster();

        if( log.isDebugEnabled() )
            log.debug("addAttributeNotificationListener " + listener);

        BaseAttributeFilter filter = new BaseAttributeFilter(name);
        attributeBroadcaster.addNotificationListener
            (listener, filter, handback);

    
public voidaddNotificationListener(javax.management.NotificationListener listener, javax.management.NotificationFilter filter, java.lang.Object handback)
Add a notification event listener to this MBean.

param
listener Listener that will receive event notifications
param
filter Filter object used to filter event notifications actually delivered, or null for no filtering
param
handback Handback object to be sent along with event notifications
exception
IllegalArgumentException if the listener parameter is null


        if (listener == null)
            throw new IllegalArgumentException("Listener is null");

        if( log.isDebugEnabled() ) log.debug("addNotificationListener " + listener);

        if (generalBroadcaster == null)
            generalBroadcaster = new BaseNotificationBroadcaster();
        generalBroadcaster.addNotificationListener
            (listener, filter, handback);

        // We'll send the attribute change notifications to all listeners ( who care )
        // The normal filtering can be used.
        // The problem is that there is no other way to add attribute change listeners
        // to a model mbean ( AFAIK ). I suppose the spec should be fixed.
        if (attributeBroadcaster == null)
            attributeBroadcaster = new BaseNotificationBroadcaster();

        if( log.isDebugEnabled() )
            log.debug("addAttributeNotificationListener " + listener);

        attributeBroadcaster.addNotificationListener
                (listener, filter, handback);
    
public java.lang.ObjectgetAttribute(java.lang.String name)
Obtain and return the value of a specific attribute of this MBean.

param
name Name of the requested attribute
exception
AttributeNotFoundException if this attribute is not supported by this MBean
exception
MBeanException if the initializer of an object throws an exception
exception
ReflectionException if a Java reflection exception occurs when invoking the getter


    // key: operation val: invoke method
    //private Hashtable invokeAttMap=new Hashtable();

                                                                 
       
          
             
        // Validate the input parameters
        if (name == null)
            throw new RuntimeOperationsException
                (new IllegalArgumentException("Attribute name is null"),
                 "Attribute name is null");

        if( (resource instanceof DynamicMBean) && 
             ! ( resource instanceof BaseModelMBean )) {
            return ((DynamicMBean)resource).getAttribute(name);
        }
        
        Method m=managedBean.getGetter(name, this, resource);
        Object result = null;
        try {
            Class declaring=m.getDeclaringClass();
            // workaround for catalina weird mbeans - the declaring class is BaseModelMBean.
            // but this is the catalina class.
            if( declaring.isAssignableFrom(this.getClass()) ) {
                result = m.invoke(this, NO_ARGS_PARAM );
            } else {
                result = m.invoke(resource, NO_ARGS_PARAM );
            }
        } catch (InvocationTargetException e) {
            Throwable t = e.getTargetException();
            if (t == null)
                t = e;
            if (t instanceof RuntimeException)
                throw new RuntimeOperationsException
                    ((RuntimeException) t, "Exception invoking method " + name);
            else if (t instanceof Error)
                throw new RuntimeErrorException
                    ((Error) t, "Error invoking method " + name);
            else
                throw new MBeanException
                    (e, "Exception invoking method " + name);
        } catch (Exception e) {
            throw new MBeanException
                (e, "Exception invoking method " + name);
        }

        // Return the results of this method invocation
        // FIXME - should we validate the return type?
        return (result);
    
static java.lang.ClassgetAttributeClass(java.lang.String signature)

        if (signature.equals(Boolean.TYPE.getName()))
            return Boolean.TYPE;
        else if (signature.equals(Byte.TYPE.getName()))
            return Byte.TYPE;
        else if (signature.equals(Character.TYPE.getName()))
            return Character.TYPE;
        else if (signature.equals(Double.TYPE.getName()))
            return Double.TYPE;
        else if (signature.equals(Float.TYPE.getName()))
            return Float.TYPE;
        else if (signature.equals(Integer.TYPE.getName()))
            return Integer.TYPE;
        else if (signature.equals(Long.TYPE.getName()))
            return Long.TYPE;
        else if (signature.equals(Short.TYPE.getName()))
            return Short.TYPE;
        else {
            try {
                ClassLoader cl=Thread.currentThread().getContextClassLoader();
                if( cl!=null )
                    return cl.loadClass(signature); 
            } catch( ClassNotFoundException e ) {
            }
            try {
                return Class.forName(signature);
            } catch (ClassNotFoundException e) {
                throw new ReflectionException
                    (e, "Cannot find Class for " + signature);
            }
        }
    
public javax.management.AttributeListgetAttributes(java.lang.String[] names)
Obtain and return the values of several attributes of this MBean.

param
names Names of the requested attributes


        // Validate the input parameters
        if (names == null)
            throw new RuntimeOperationsException
                (new IllegalArgumentException("Attribute names list is null"),
                 "Attribute names list is null");

        // Prepare our response, eating all exceptions
        AttributeList response = new AttributeList();
        for (int i = 0; i < names.length; i++) {
            try {
                response.add(new Attribute(names[i],getAttribute(names[i])));
            } catch (Exception e) {
                ; // Not having a particular attribute in the response
                ; // is the indication of a getter problem
            }
        }
        return (response);

    
public java.lang.StringgetClassName()

        return getModelerType();
    
public javax.management.ObjectNamegetJmxName()

        return oname;
    
public javax.management.MBeanInfogetMBeanInfo()
Return the MBeanInfo object for this MBean.

        return managedBean.getMBeanInfo();
    
public java.lang.ObjectgetManagedResource()
Get the instance handle of the object against which we execute all methods in this ModelMBean management interface.

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


        if (resource == null)
            throw new RuntimeOperationsException
                (new IllegalArgumentException("Managed resource is null"),
                 "Managed resource is null");

        return resource;

    
public java.lang.StringgetModelerType()
Set the type of the mbean. This is used as a key to locate the description in the Registry.

        return resourceType;
    
public javax.management.MBeanNotificationInfo[]getNotificationInfo()
Return an MBeanNotificationInfo object describing the notifications sent by this MBean.


        // Acquire the set of application notifications
        MBeanNotificationInfo current[] = getMBeanInfo().getNotifications();
        if (current == null)
            current = new MBeanNotificationInfo[0];
        MBeanNotificationInfo response[] =
            new MBeanNotificationInfo[current.length + 2];
 //       Descriptor descriptor = null;

        // Fill in entry for general notifications
//        descriptor = new DescriptorSupport
//            (new String[] { "name=GENERIC",
//                            "descriptorType=notification",
//                            "log=T",
//                            "severity=5",
//                            "displayName=jmx.modelmbean.generic" });
        response[0] = new MBeanNotificationInfo
            (new String[] { "jmx.modelmbean.generic" },
             "GENERIC",
             "Text message notification from the managed resource");
             //descriptor);

        // Fill in entry for attribute change notifications
//        descriptor = new DescriptorSupport
//            (new String[] { "name=ATTRIBUTE_CHANGE",
//                            "descriptorType=notification",
//                            "log=T",
//                            "severity=5",
//                            "displayName=jmx.attribute.change" });
        response[1] = new MBeanNotificationInfo
            (new String[] { "jmx.attribute.change" },
             "ATTRIBUTE_CHANGE",
             "Observed MBean attribute value has changed");
             //descriptor);

        // Copy remaining notifications as reported by the application
        System.arraycopy(current, 0, response, 2, current.length);
        return (response);

    
public java.lang.StringgetObjectName()

        if (oname != null) {
            return oname.toString();
        } else {
            return null;
        }
    
public java.lang.Objectinvoke(java.lang.String name, java.lang.Object[] params, java.lang.String[] signature)
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.

param
name Name of the operation to be invoked
param
params Array containing the method parameters of this operation
param
signature Array containing the class names representing the signature of this operation
exception
MBeanException if the initializer of an object throws an exception
exception
ReflectioNException if a Java reflection exception occurs when invoking a method

        if( (resource instanceof DynamicMBean) && 
             ! ( resource instanceof BaseModelMBean )) {
            return ((DynamicMBean)resource).invoke(name, params, signature);
        }
    
        // Validate the input parameters
        if (name == null)
            throw new RuntimeOperationsException
                (new IllegalArgumentException("Method name is null"),
                 "Method name is null");

        if( log.isDebugEnabled()) log.debug("Invoke " + name);
	MethodKey mkey = new MethodKey(name, signature);
        Method method= managedBean.getInvoke(name, params, signature, this, resource);
        
        // Invoke the selected method on the appropriate object
        Object result = null;
        try {
            if( method.getDeclaringClass().isAssignableFrom( this.getClass()) ) {
                result = method.invoke(this, params );
            } else {
                result = method.invoke(resource, params);
            }
        } catch (InvocationTargetException e) {
            Throwable t = e.getTargetException();
            log.error("Exception invoking method " + name , t );
            if (t == null)
                t = e;
            if (t instanceof RuntimeException)
                throw new RuntimeOperationsException
                    ((RuntimeException) t, "Exception invoking method " + name);
            else if (t instanceof Error)
                throw new RuntimeErrorException
                    ((Error) t, "Error invoking method " + name);
            else
                throw new MBeanException
                    ((Exception)t, "Exception invoking method " + name);
        } catch (Exception e) {
            log.error("Exception invoking method " + name , e );
            throw new MBeanException
                (e, "Exception invoking method " + name);
        }

        // Return the results of this method invocation
        // FIXME - should we validate the return type?
        return (result);

    
public voidpostDeregister()

        if( resource instanceof MBeanRegistration ) {
            ((MBeanRegistration)resource).postDeregister();
        }
    
public voidpostRegister(java.lang.Boolean registrationDone)

        if( resource instanceof MBeanRegistration ) {
            ((MBeanRegistration)resource).postRegister(registrationDone);
        }
    
public voidpreDeregister()

        if( resource instanceof MBeanRegistration ) {
            ((MBeanRegistration)resource).preDeregister();
        }
    
public javax.management.ObjectNamepreRegister(javax.management.MBeanServer server, javax.management.ObjectName name)
Is the specified ModelMBeanInfo instance valid?

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

param
info The ModelMBeanInfo object to check

        if( log.isDebugEnabled())
            log.debug("preRegister " + resource + " " + name );
        oname=name;
        if( resource instanceof MBeanRegistration ) {
            oname = ((MBeanRegistration)resource).preRegister(server, name );
        }
        return oname;
    
public voidremoveAttributeChangeNotificationListener(javax.management.NotificationListener listener, java.lang.String name)
Remove an attribute change notification event listener from this MBean.

param
listener The listener to be removed
param
name The attribute name for which no more events are required
exception
ListenerNotFoundException if this listener is not registered in the MBean


        if (listener == null)
            throw new IllegalArgumentException("Listener is null");
        if (attributeBroadcaster == null)
            attributeBroadcaster = new BaseNotificationBroadcaster();

        // FIXME - currently this removes *all* notifications for this listener
        attributeBroadcaster.removeNotificationListener(listener);

    
public voidremoveAttributeChangeNotificationListener(javax.management.NotificationListener listener, java.lang.String attributeName, java.lang.Object handback)
Remove an attribute change notification event listener from this MBean.

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


        removeAttributeChangeNotificationListener(listener, attributeName);

    
public voidremoveNotificationListener(javax.management.NotificationListener listener)
Remove a notification event listener from this MBean.

param
listener The listener to be removed (any and all registrations for this listener will be eliminated)
exception
ListenerNotFoundException if this listener is not registered in the MBean


        if (listener == null)
            throw new IllegalArgumentException("Listener is null");
        if (generalBroadcaster == null)
            generalBroadcaster = new BaseNotificationBroadcaster();
        generalBroadcaster.removeNotificationListener(listener);


    
public voidremoveNotificationListener(javax.management.NotificationListener listener, java.lang.Object handback)
Remove a notification event listener from this MBean.

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


        removeNotificationListener(listener);

    
public voidremoveNotificationListener(javax.management.NotificationListener listener, javax.management.NotificationFilter filter, java.lang.Object handback)
Remove a notification event listener from this MBean.

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


        removeNotificationListener(listener);

    
public voidsendAttributeChangeNotification(javax.management.AttributeChangeNotification notification)
Send an AttributeChangeNotification to all registered listeners.

param
notification The AttributeChangeNotification that will be passed
exception
MBeanException if an object initializer throws an exception
exception
RuntimeOperationsException wraps IllegalArgumentException when the specified notification is null or invalid


        if (notification == null)
            throw new RuntimeOperationsException
                (new IllegalArgumentException("Notification is null"),
                 "Notification is null");
        if (attributeBroadcaster == null)
            return; // This means there are no registered listeners
        if( log.isDebugEnabled() )
            log.debug( "AttributeChangeNotification " + notification );
        attributeBroadcaster.sendNotification(notification);

    
public voidsendAttributeChangeNotification(javax.management.Attribute oldValue, javax.management.Attribute newValue)
Send an AttributeChangeNotification to all registered listeners.

param
oldValue The original value of the Attribute
param
newValue The new value of the Attribute
exception
MBeanException if an object initializer throws an exception
exception
RuntimeOperationsException wraps IllegalArgumentException when the specified notification is null or invalid


        // Calculate the class name for the change notification
        String type = null;
        if (newValue.getValue() != null)
            type = newValue.getValue().getClass().getName();
        else if (oldValue.getValue() != null)
            type = oldValue.getValue().getClass().getName();
        else
            return;  // Old and new are both null == no change

        AttributeChangeNotification notification =
            new AttributeChangeNotification
            (this, 1, System.currentTimeMillis(),
             "Attribute value has changed",
             oldValue.getName(), type,
             oldValue.getValue(), newValue.getValue());
        sendAttributeChangeNotification(notification);

    
public voidsendNotification(javax.management.Notification notification)
Send a Notification to all registered listeners as a jmx.modelmbean.general notification.

param
notification The Notification that will be passed
exception
MBeanException if an object initializer throws an exception
exception
RuntimeOperationsException wraps IllegalArgumentException when the specified notification is null or invalid


        if (notification == null)
            throw new RuntimeOperationsException
                (new IllegalArgumentException("Notification is null"),
                 "Notification is null");
        if (generalBroadcaster == null)
            return; // This means there are no registered listeners
        generalBroadcaster.sendNotification(notification);

    
public voidsendNotification(java.lang.String message)
Send a Notification which contains the specified string as a jmx.modelmbean.generic notification.

param
message The message string to be passed
exception
MBeanException if an object initializer throws an exception
exception
RuntimeOperationsException wraps IllegalArgumentException when the specified notification is null or invalid


        if (message == null)
            throw new RuntimeOperationsException
                (new IllegalArgumentException("Message is null"),
                 "Message is null");
        Notification notification = new Notification
            ("jmx.modelmbean.generic", this, 1, message);
        sendNotification(notification);

    
public voidsetAttribute(javax.management.Attribute attribute)
Set the value of a specific attribute of this MBean.

param
attribute The identification of the attribute to be set and the new value
exception
AttributeNotFoundException if this attribute is not supported by this MBean
exception
MBeanException if the initializer of an object throws an exception
exception
ReflectionException if a Java reflection exception occurs when invoking the getter

        if( log.isDebugEnabled() )
            log.debug("Setting attribute " + this + " " + attribute );

        if( (resource instanceof DynamicMBean) && 
             ! ( resource instanceof BaseModelMBean )) {
            try {
                ((DynamicMBean)resource).setAttribute(attribute);
            } catch (InvalidAttributeValueException e) {
                throw new MBeanException(e);                
            }
            return;
        }
        
        // Validate the input parameters
        if (attribute == null)
            throw new RuntimeOperationsException
                (new IllegalArgumentException("Attribute is null"),
                 "Attribute is null");

        String name = attribute.getName();
        Object value = attribute.getValue();

        if (name == null)
            throw new RuntimeOperationsException
                (new IllegalArgumentException("Attribute name is null"),
                 "Attribute name is null");

        Object oldValue=null;
        //if( getAttMap.get(name) != null )
        //    oldValue=getAttribute( name );

        Method m=managedBean.getSetter(name,this,resource);

        try {
            if( m.getDeclaringClass().isAssignableFrom( this.getClass()) ) {
                m.invoke(this, new Object[] { value });
            } else {
                m.invoke(resource, new Object[] { value });
            }
        } catch (InvocationTargetException e) {
            Throwable t = e.getTargetException();
            if (t == null)
                t = e;
            if (t instanceof RuntimeException)
                throw new RuntimeOperationsException
                    ((RuntimeException) t, "Exception invoking method " + name);
            else if (t instanceof Error)
                throw new RuntimeErrorException
                    ((Error) t, "Error invoking method " + name);
            else
                throw new MBeanException
                    (e, "Exception invoking method " + name);
        } catch (Exception e) {
            log.error("Exception invoking method " + name , e );
            throw new MBeanException
                (e, "Exception invoking method " + name);
        }
        try {
            sendAttributeChangeNotification(new Attribute( name, oldValue),
                    attribute);
        } catch(Exception ex) {
            log.error("Error sending notification " + name, ex);
        }
        //attributes.put( name, value );
//        if( source != null ) {
//            // this mbean is asscoiated with a source - maybe we want to persist
//            source.updateField(oname, name, value);
//        }
    
public javax.management.AttributeListsetAttributes(javax.management.AttributeList attributes)
Set the values of several attributes of this MBean.

param
attributes THe names and values to be set
return
The list of attributes that were set and their new values

        AttributeList response = new AttributeList();

        // Validate the input parameters
        if (attributes == null)
            return response;
        
        // Prepare and return our response, eating all exceptions
        String names[] = new String[attributes.size()];
        int n = 0;
        Iterator items = attributes.iterator();
        while (items.hasNext()) {
            Attribute item = (Attribute) items.next();
            names[n++] = item.getName();
            try {
                setAttribute(item);
            } catch (Exception e) {
                ; // Ignore all exceptions
            }
        }

        return (getAttributes(names));

    
public voidsetManagedBean(ManagedBean managedBean)

        this.managedBean = managedBean;
    
public voidsetManagedResource(java.lang.Object resource, java.lang.String type)
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. The caller can provide the mbean instance or the object name to the resource, if needed.

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

        if (resource == null)
            throw new RuntimeOperationsException
                (new IllegalArgumentException("Managed resource is null"),
                 "Managed resource is null");

//        if (!"objectreference".equalsIgnoreCase(type))
//            throw new InvalidTargetObjectTypeException(type);

        this.resource = resource;
        this.resourceType = resource.getClass().getName();
        
//        // Make the resource aware of the model mbean.
//        try {
//            Method m=resource.getClass().getMethod("setModelMBean",
//                    new Class[] {ModelMBean.class});
//            if( m!= null ) {
//                m.invoke(resource, new Object[] {this});
//            }
//        } catch( NoSuchMethodException t ) {
//            // ignore
//        } catch( Throwable t ) {
//            log.error( "Can't set model mbean ", t );
//        }
    
public java.lang.StringtoString()

        if( resource==null ) 
            return "BaseModelMbean[" + resourceType + "]";
        return resource.toString();