commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cos...@apache.org
Subject cvs commit: jakarta-commons/modeler/src/java/org/apache/commons/modeler BaseModelMBean.java
Date Mon, 07 Apr 2003 07:12:05 GMT
costin      2003/04/07 00:12:03

  Modified:    modeler/src/java/org/apache/commons/modeler
                        BaseModelMBean.java
  Log:
  Few changes:
  - added callbacks to the source on attribute change
  - if the resource has a setModelMBean method, it'll be called with the current model.
  This can be used by the resource to send notifications ( since BaseModelMBean has
  all the support for registration and sending ) or to find the Registry without using
  evil static methods
  - various fixes to avoid depending on an actual resource
  - save the object name - it'll be needed to pass up to the source
  - constructor that takes the source
  - split setModeledType in 2 - one part will set the metadata, the other
  will create the resource ( need to use BaseModelMBean in standard MLET
  files or other config files that expect regular mbeans )
  
  Revision  Changes    Path
  1.17      +152 -35   jakarta-commons/modeler/src/java/org/apache/commons/modeler/BaseModelMBean.java
  
  Index: BaseModelMBean.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/modeler/src/java/org/apache/commons/modeler/BaseModelMBean.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- BaseModelMBean.java	15 Mar 2003 00:34:10 -0000	1.16
  +++ BaseModelMBean.java	7 Apr 2003 07:12:02 -0000	1.17
  @@ -67,11 +67,14 @@
   
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  +import org.apache.commons.modeler.modules.MbeansSource;
  +import org.apache.commons.modeler.modules.ModelerSource;
   
   import java.lang.reflect.InvocationTargetException;
   import java.lang.reflect.Method;
   import java.util.Iterator;
   import java.util.Hashtable;
  +import java.util.HashMap;
   
   import javax.management.modelmbean.DescriptorSupport;
   import javax.management.modelmbean.InvalidTargetObjectTypeException;
  @@ -91,8 +94,8 @@
    *
    * <p>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 or "modeledType" attribute will be used
  - * to construct an instance of the real object that we wrap.
  + * passed to the constructor will be used to construct an instance of the
  + * real object that we wrap.
    *
    * Limitations:
    * <ul>
  @@ -111,6 +114,7 @@
    *     <li>Operations that return no value should declare a return type of
    *         <code>void</code>.</li>
    *     </ul>
  + * <li>Attribute caching is not supported</li>
    * </ul>
    *
    * @author Craig R. McClanahan
  @@ -165,6 +169,8 @@
        * 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.
  +     *
        * @param type Class name or the type key used in the descriptor.
        * @throws MBeanException
        * @throws RuntimeOperationsException
  @@ -173,10 +179,23 @@
           throws MBeanException, RuntimeOperationsException
       {
           try {
  +            // This constructor is used from <mlet>, it should create
  +            // the resource
  +            setModeledType(type);
  +        } catch( Throwable ex ) {
  +            log.error( "Error creating mbean ", ex);
  +        }
  +    }
  +
  +    public BaseModelMBean( String type, ModelerSource source )
  +        throws MBeanException, RuntimeOperationsException
  +    {
  +        try {
               setModeledType(type);
           } catch( Throwable ex ) {
               log.error( "Error creating mbean ", ex);
           }
  +        this.source=source;
       }
   
       // ----------------------------------------------------- Instance Variables
  @@ -187,12 +206,16 @@
        */
       protected BaseNotificationBroadcaster attributeBroadcaster = null;
   
  +    /** Registry we are associated with
  +     */
  +    protected Registry registry=null;
   
       /**
        * Notification broadcaster for general notifications.
        */
       protected BaseNotificationBroadcaster generalBroadcaster = null;
   
  +    protected ObjectName oname=null;
   
       /**
        * The <code>ModelMBeanInfo</code> object that controls our activity.
  @@ -204,19 +227,28 @@
        * The managed resource this MBean is associated with (if any).
        */
       protected Object resource = null;
  +    protected String resourceType = null;
   
  +    /** Source object used to read this mbean. Can be used to
  +     * persist the mbean
  +     */
  +    protected ModelerSource source=null;
  +
  +    /** Attribute values. XXX That can be stored in the value Field
  +     */
  +    protected HashMap attributes=new HashMap();
   
       // --------------------------------------------------- DynamicMBean Methods
       static final Object[] NO_ARGS_PARAM=new Object[0];
       static final Class[] NO_ARGS_PARAM_SIG=new Class[0];
       // key: attribute val: getter method
  -    Hashtable getAttMap=new Hashtable();
  +    private Hashtable getAttMap=new Hashtable();
   
       // key: attribute val: setter method
  -    Hashtable setAttMap=new Hashtable();
  +    private Hashtable setAttMap=new Hashtable();
   
       // key: operation val: invoke method
  -    Hashtable invokeAttMap=new Hashtable();
  +    private Hashtable invokeAttMap=new Hashtable();
   
       /**
        * Obtain and return the value of a specific attribute of this MBean.
  @@ -523,16 +555,19 @@
   
           String name = attribute.getName();
           Object value = attribute.getValue();
  +
           if (name == null)
               throw new RuntimeOperationsException
                   (new IllegalArgumentException("Attribute name is null"),
                    "Attribute name is null");
  -        // Special type
  -        if( "modeledType".equals( name )) {
  -            log.info("Change modeled type " + value);
  -            setModeledType((String)value);
  -            return;
  -        }
  +
  +        ModelMBeanAttributeInfo attrInfo=info.getAttribute(name);
  +        if (attrInfo == null)
  +            throw new AttributeNotFoundException("Cannot find attribute " + name);
  +
  +        Descriptor attrDesc=attrInfo.getDescriptor();
  +        if (attrDesc == null)
  +            throw new AttributeNotFoundException("Cannot find attribute " + name + " descriptor");
   
           try {
               // XXX Is it before or after ?
  @@ -550,14 +585,6 @@
   
           if( m==null ) {
               // Look up the actual operation to be used
  -            ModelMBeanAttributeInfo attrInfo = info.getAttribute(name);
  -            if (attrInfo == null)
  -                throw new AttributeNotFoundException("Cannot find attribute " + name);
  -
  -            Descriptor attrDesc = attrInfo.getDescriptor();
  -            if (attrDesc == null)
  -                throw new AttributeNotFoundException("Cannot find attribute " + name +
" descriptor");
  -
               String setMethod = (String) attrDesc.getFieldValue("setMethod");
               if (setMethod == null)
                   throw new AttributeNotFoundException("Cannot find attribute " + name +
" set method name");
  @@ -617,8 +644,18 @@
               throw new MBeanException
                   (e, "Exception invoking method " + name);
           }
  +
  +        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 String toString() {
  +        if( resource==null ) return "BaseModelMbean[" + resourceType + "]";
  +        return resource.toString();
  +    }
   
       /**
        * Set the values of several attributes of this MBean.
  @@ -687,6 +724,11 @@
        * 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.
  +     *
        * @param resource The resource object to be managed
        * @param type The type of reference for the managed resource
        *  ("ObjectReference", "Handle", "IOR", "EJBHandle", or
  @@ -703,8 +745,8 @@
        */
       public void setManagedResource(Object resource, String type)
           throws InstanceNotFoundException, InvalidTargetObjectTypeException,
  -        MBeanException, RuntimeOperationsException {
  -
  +        MBeanException, RuntimeOperationsException
  +    {
           if (resource == null)
               throw new RuntimeOperationsException
                   (new IllegalArgumentException("Managed resource is null"),
  @@ -714,7 +756,20 @@
               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 );
  +        }
       }
   
   
  @@ -724,6 +779,9 @@
        * descriptors have been customized, the <code>ModelMBean</code> should
        * be registered with the associated <code>MBeanServer</code>.
        *
  +     * Currently the model can be set after registration. This behavior is
  +     * deprecated and won't be supported in future versions.
  +     *
        * @param info The ModelMBeanInfo object to be used by this ModelMBean
        *
        * @exception MBeanException If an exception occurs recording this
  @@ -1169,15 +1227,34 @@
        * @param type the type of classname of the modeled object
        */
       public void setModeledType( String type ) {
  +        initModelInfo(type);
  +        createResource();
  +    }
  +    /** Set the type of the mbean. This is used as a key to locate
  +     * the description in the Registry.
  +     *
  +     * @param type the type of classname of the modeled object
  +     */
  +    protected void initModelInfo( String type ) {
           try {
               if( log.isDebugEnabled())
                   log.debug("setModeledType " + type);
   
  -            Registry reg=Registry.getRegistry();
  +            log.debug( "Set model Info " + type);
  +            if(type==null) {
  +                return;
  +            }
  +            resourceType=type;
               //Thread.currentThread().setContextClassLoader(BaseModelMBean.class.getClassLoader());
  -            Class c=Class.forName( type);
  -            resource = c.newInstance();
  -            ManagedBean descriptor=reg.findManagedBean(c, type);
  +            Class c=null;
  +            try {
  +                c=Class.forName( type);
  +            } catch( Throwable t ) {
  +                log.error( "Error creating class " + t);
  +            }
  +
  +            // The class c doesn't need to exist
  +            ManagedBean descriptor=getRegistry().findManagedBean(c, type);
   
               this.setModelMBeanInfo(descriptor.createMBeanInfo());
           } catch( Throwable ex) {
  @@ -1186,11 +1263,50 @@
           }
       }
   
  -    public String getClassName() {
  -        if( resource==null ) {
  -            return null;
  +    /** Set the type of the mbean. This is used as a key to locate
  +     * the description in the Registry.
  +     *
  +     * @param type the type of classname of the modeled object
  +     */
  +    protected void createResource() {
  +        try {
  +            //Thread.currentThread().setContextClassLoader(BaseModelMBean.class.getClassLoader());
  +            Class c=null;
  +            try {
  +                c=Class.forName( resourceType );
  +                resource = c.newInstance();
  +            } catch( Throwable t ) {
  +                log.error( "Error creating class " + t);
  +            }
  +        } catch( Throwable ex) {
  +            log.error( "TCL: " + Thread.currentThread().getContextClassLoader(),
  +                    ex);
           }
  -        return resource.getClass().getName();
  +    }
  +
  +
  +    public String getModelerType() {
  +        return resourceType;
  +    }
  +
  +    public String getClassName() {
  +        return getModelerType();
  +    }
  +
  +    public ObjectName getObjectName() {
  +        return oname;
  +    }
  +
  +    public void setRegistry(Registry registry) {
  +        this.registry = registry;
  +    }
  +
  +    public Registry getRegistry() {
  +        // XXX Need a better solution - to avoid the static
  +        if( registry == null )
  +            registry=Registry.getRegistry();
  +
  +        return registry;
       }
   
       // ------------------------------------------------------ Protected Methods
  @@ -1230,10 +1346,11 @@
       {
           if( log.isDebugEnabled())
               log.debug("preRegister " + resource + " " + name );
  +        oname=name;
           if( resource instanceof MBeanRegistration ) {
  -            return ((MBeanRegistration)resource).preRegister(server, name );
  +            oname= ((MBeanRegistration)resource).preRegister(server, name );
           }
  -        return name;
  +        return oname;
       }
   
       public void postRegister(Boolean registrationDone) {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message