FileDocCategorySizeDatePackage
ManagedBean.javaAPI DocApache Tomcat 6.0.1421743Fri Jul 20 04:20:32 BST 2007org.apache.tomcat.util.modeler

ManagedBean

public class ManagedBean extends Object implements Serializable

Internal configuration information for a managed bean (MBean) descriptor.

author
Craig R. McClanahan
version
$Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $

Fields Summary
private static final String
BASE_MBEAN
static final Object[]
NO_ARGS_PARAM
static final Class[]
NO_ARGS_PARAM_SIG
transient MBeanInfo
info
The ModelMBeanInfo object that corresponds to this ManagedBean instance.
private Map
attributes
private Map
operations
protected String
className
protected String
description
protected String
domain
protected String
group
protected String
name
protected NotificationInfo[]
notifications
protected String
type
Constructors Summary
public ManagedBean()
Constructor. Will add default attributes.


                  
      
        AttributeInfo ai=new AttributeInfo();
        ai.setName("modelerType");
        ai.setDescription("Type of the modeled resource. Can be set only once");
        ai.setType("java.lang.String");
        ai.setWriteable(false);
        addAttribute(ai);
    
Methods Summary
public voidaddAttribute(AttributeInfo attribute)
Add a new attribute to the set of attributes for this MBean.

param
attribute The new attribute descriptor

        attributes.put(attribute.getName(), attribute);
    
public voidaddNotification(NotificationInfo notification)
Add a new notification to the set of notifications for this MBean.

param
notification The new notification descriptor


        synchronized (notifications) {
            NotificationInfo results[] =
                new NotificationInfo[notifications.length + 1];
            System.arraycopy(notifications, 0, results, 0,
                             notifications.length);
            results[notifications.length] = notification;
            notifications = results;
            this.info = null;
        }

    
public voidaddOperation(OperationInfo operation)
Add a new operation to the set of operations for this MBean.

param
operation The new operation descriptor

        operations.put(operation.getName(), operation);
    
public javax.management.DynamicMBeancreateMBean()
Create and return a ModelMBean that has been preconfigured with the ModelMBeanInfo information for this managed bean, but is not associated with any particular managed resource. The returned ModelMBean will NOT have been registered with our MBeanServer.

exception
InstanceNotFoundException if the managed resource object cannot be found
exception
InvalidTargetObjectTypeException if our MBean cannot handle object references (should never happen)
exception
MBeanException if a problem occurs instantiating the ModelMBean instance
exception
RuntimeOperationsException if a JMX runtime error occurs


        return (createMBean(null));

    
public javax.management.DynamicMBeancreateMBean(java.lang.Object instance)
Create and return a ModelMBean that has been preconfigured with the ModelMBeanInfo information for this managed bean, and is associated with the specified managed object instance. The returned ModelMBean will NOT have been registered with our MBeanServer.

param
instance Instanced of the managed object, or null for no associated instance
exception
InstanceNotFoundException if the managed resource object cannot be found
exception
InvalidTargetObjectTypeException if our MBean cannot handle object references (should never happen)
exception
MBeanException if a problem occurs instantiating the ModelMBean instance
exception
RuntimeOperationsException if a JMX runtime error occurs


        BaseModelMBean mbean = null;

        // Load the ModelMBean implementation class
        if(getClassName().equals(BASE_MBEAN)) {
            // Skip introspection
            mbean = new BaseModelMBean();
        } else {
            Class clazz = null;
            Exception ex = null;
            try {
                clazz = Class.forName(getClassName());
            } catch (Exception e) {
            }
          
            if( clazz==null ) {  
                try {
                    ClassLoader cl= Thread.currentThread().getContextClassLoader();
                    if ( cl != null)
                        clazz= cl.loadClass(getClassName());
                } catch (Exception e) {
                    ex=e;
                }
            }
    
            if( clazz==null) { 
                throw new MBeanException
                    (ex, "Cannot load ModelMBean class " + getClassName());
            }
            try {
                // Stupid - this will set the default minfo first....
                mbean = (BaseModelMBean) clazz.newInstance();
            } catch (RuntimeOperationsException e) {
                throw e;
            } catch (Exception e) {
                throw new MBeanException
                    (e, "Cannot instantiate ModelMBean of class " +
                     getClassName());
            }
        }
        
        mbean.setManagedBean(this);
        
        // Set the managed resource (if any)
        try {
            if (instance != null)
                mbean.setManagedResource(instance, "ObjectReference");
        } catch (InstanceNotFoundException e) {
            throw e;
        }
        return (mbean);

    
public AttributeInfo[]getAttributes()
The collection of attributes for this MBean.

        AttributeInfo result[] = new AttributeInfo[attributes.size()];
        attributes.values().toArray(result);
        return result;
    
public java.lang.StringgetClassName()
The fully qualified name of the Java class of the MBean described by this descriptor. If not specified, the standard JMX class (javax.management.modelmbean.RequiredModeLMBean) will be utilized.

        return (this.className);
    
public java.lang.StringgetDescription()
The human-readable description of this MBean.

        return (this.description);
    
public java.lang.StringgetDomain()
The (optional) ObjectName domain in which this MBean should be registered in the MBeanServer.

        return (this.domain);
    
java.lang.reflect.MethodgetGetter(java.lang.String aname, BaseModelMBean mbean, java.lang.Object resource)

        // TODO: do we need caching ? JMX is for management, it's not supposed to require lots of performance.
        Method m=null; // (Method)getAttMap.get( name );

        if( m==null ) {
            AttributeInfo attrInfo = (AttributeInfo)attributes.get(aname);
            // Look up the actual operation to be used
            if (attrInfo == null)
                throw new AttributeNotFoundException(" Cannot find attribute " + aname + " for " + resource);
            
            String getMethod = attrInfo.getGetMethod();
            if (getMethod == null)
                throw new AttributeNotFoundException("Cannot find attribute " + aname + " get method name");

            Object object = null;
            NoSuchMethodException exception = null;
            try {
                object = mbean;
                m = object.getClass().getMethod(getMethod, NO_ARGS_PARAM_SIG);
            } catch (NoSuchMethodException e) {
                exception = e;;
            }
            if( m== null && resource != null ) {
                try {
                    object = resource;
                    m = object.getClass().getMethod(getMethod, NO_ARGS_PARAM_SIG);
                    exception=null;
                } catch (NoSuchMethodException e) {
                    exception = e;
                }
            }
            if( exception != null )
                throw new ReflectionException(exception,
                                              "Cannot find getter method " + getMethod);
            //getAttMap.put( name, m );
        }

        return m;
    
public java.lang.StringgetGroup()
The (optional) group to which this MBean belongs.

        return (this.group);
    
public java.lang.reflect.MethodgetInvoke(java.lang.String aname, java.lang.Object[] params, java.lang.String[] signature, BaseModelMBean bean, java.lang.Object resource)

        Method method = null;
        if (method == null) {
            if (params == null)
                params = new Object[0];
            if (signature == null)
                signature = new String[0];
            if (params.length != signature.length)
                throw new RuntimeOperationsException(
                        new IllegalArgumentException(
                                "Inconsistent arguments and signature"),
                        "Inconsistent arguments and signature");

            // Acquire the ModelMBeanOperationInfo information for
            // the requested operation
            OperationInfo opInfo = (OperationInfo)operations.get(aname);
            if (opInfo == null)
                throw new MBeanException(new ServiceNotFoundException(
                        "Cannot find operation " + aname),
                        "Cannot find operation " + aname);

            // Prepare the signature required by Java reflection APIs
            // FIXME - should we use the signature from opInfo?
            Class types[] = new Class[signature.length];
            for (int i = 0; i < signature.length; i++) {
                types[i] = BaseModelMBean.getAttributeClass(signature[i]);
            }

            // Locate the method to be invoked, either in this MBean itself
            // or in the corresponding managed resource
            // FIXME - Accessible methods in superinterfaces?
            Object object = null;
            Exception exception = null;
            try {
                object = this;
                method = object.getClass().getMethod(aname, types);
            } catch (NoSuchMethodException e) {
                exception = e;
                ;
            }
            try {
                if ((method == null) && (resource != null)) {
                    object = resource;
                    method = object.getClass().getMethod(aname, types);
                }
            } catch (NoSuchMethodException e) {
                exception = e;
            }
            if (method == null) {
                throw new ReflectionException(exception, "Cannot find method "
                        + aname + " with this signature");
            }
            // invokeAttMap.put(mkey, method);
        }
        return method;
    
javax.management.MBeanInfogetMBeanInfo()
Create and return a ModelMBeanInfo object that describes this entire managed bean.


        // Return our cached information (if any)
        if (info != null)
            return (info);

        // Create subordinate information descriptors as required
        AttributeInfo attrs[] = getAttributes();
        MBeanAttributeInfo attributes[] =
            new MBeanAttributeInfo[attrs.length];
        for (int i = 0; i < attrs.length; i++)
            attributes[i] = attrs[i].createAttributeInfo();

        OperationInfo opers[] = getOperations();
        MBeanOperationInfo operations[] =
            new MBeanOperationInfo[opers.length];
        for (int i = 0; i < opers.length; i++)
            operations[i] = opers[i].createOperationInfo();


//        ConstructorInfo consts[] = getConstructors();
//        ModelMBeanConstructorInfo constructors[] =
//            new ModelMBeanConstructorInfo[consts.length];
//        for (int i = 0; i < consts.length; i++)
//            constructors[i] = consts[i].createConstructorInfo();
        
        NotificationInfo notifs[] = getNotifications();
        MBeanNotificationInfo notifications[] =
            new MBeanNotificationInfo[notifs.length];
        for (int i = 0; i < notifs.length; i++)
            notifications[i] = notifs[i].createNotificationInfo();

        
        // Construct and return a new ModelMBeanInfo object
        info = new MBeanInfo(getClassName(), 
                             getDescription(),
                             attributes, 
                             new MBeanConstructorInfo[] {}, 
                             operations, 
                             notifications);
//        try {
//            Descriptor descriptor = info.getMBeanDescriptor();
//            Iterator fields = getFields().iterator();
//            while (fields.hasNext()) {
//                FieldInfo field = (FieldInfo) fields.next();
//                descriptor.setField(field.getName(), field.getValue());
//            }
//            info.setMBeanDescriptor(descriptor);
//        } catch (MBeanException e) {
//            ;
//        }

        return (info);

    
public java.lang.StringgetName()
The name of this managed bean, which must be unique among all MBeans managed by a particular MBeans server.

        return (this.name);
    
public NotificationInfo[]getNotifications()
The collection of notifications for this MBean.

        return (this.notifications);
    
public OperationInfo[]getOperations()
The collection of operations for this MBean.

        OperationInfo[] result = new OperationInfo[operations.size()];
        operations.values().toArray(result);
        return result;
    
public java.lang.reflect.MethodgetSetter(java.lang.String aname, BaseModelMBean bean, java.lang.Object resource)

        // Cache may be needed for getters, but it is a really bad idea for setters, this is far 
        // less frequent.
        Method m=null;//(Method)setAttMap.get( name );

        if( m==null ) {
            AttributeInfo attrInfo = (AttributeInfo)attributes.get(aname);
            if (attrInfo == null)
                throw new AttributeNotFoundException(" Cannot find attribute " + aname);

            // Look up the actual operation to be used
            String setMethod = attrInfo.getSetMethod();
            if (setMethod == null)
                throw new AttributeNotFoundException("Cannot find attribute " + aname + " set method name");

            String argType=attrInfo.getType();

            Class signature[] = new Class[] { BaseModelMBean.getAttributeClass( argType ) };

            Object object = null;
            NoSuchMethodException exception = null;
            try {
                object = this;
                m = object.getClass().getMethod(setMethod, signature);
            } catch (NoSuchMethodException e) {
                exception = e;;
            }
            if( m== null && resource != null ) {
                try {
                    object = resource;
                    m = object.getClass().getMethod(setMethod, signature);
                    exception=null;
                } catch (NoSuchMethodException e) {
                    exception = e;
                }
            }
            if( exception != null )
                throw new ReflectionException(exception,
                                              "Cannot find setter method " + setMethod +
                        " " + resource);
            //setAttMap.put( name, m );
        }

        return m;
    
public java.lang.StringgetType()
The fully qualified name of the Java class of the resource implementation class described by the managed bean described by this descriptor.

        return (this.type);
    
public voidsetClassName(java.lang.String className)

        this.className = className;
        this.info = null;
    
public voidsetDescription(java.lang.String description)

        this.description = description;
        this.info = null;
    
public voidsetDomain(java.lang.String domain)

        this.domain = domain;
    
public voidsetGroup(java.lang.String group)

        this.group = group;
    
public voidsetName(java.lang.String name)

        this.name = name;
        this.info = null;
    
public voidsetType(java.lang.String type)

        this.type = type;
        this.info = null;
    
public java.lang.StringtoString()
Return a string representation of this managed bean.


        StringBuffer sb = new StringBuffer("ManagedBean[");
        sb.append("name=");
        sb.append(name);
        sb.append(", className=");
        sb.append(className);
        sb.append(", description=");
        sb.append(description);
        if (group != null) {
            sb.append(", group=");
            sb.append(group);
        }
        sb.append(", type=");
        sb.append(type);
        sb.append("]");
        return (sb.toString());