costin      2002/06/06 15:17:00

  Modified:    util/java/org/apache/tomcat/util/mx DynamicMBeanProxy.java
  Log:
  Few more improvements.
  
  - if name is not specified, construct one from the last part of the class
  name, plus an int if multiple instances are present. ( can be improved ).
  We should add some common 'patterns' to extract an id from a bean -
  getName, getId, etc ( using introspection ).
  
  - use findMBeanServer() instead of create - so we use the same server
  as tomcat. ( assuming modeler is loaded first - I think the modeler should
  do the same, first look for existing server - tomcat may run in jboss ! )
  
  - reduce debug info
  
  - Try to use the real class name - I don't know how it is used ( but
  it shouldn't ), but it's better to see it instead of the DynamicMBeanProxy.
  
  Revision  Changes    Path
  1.2       +69 -11    
jakarta-tomcat-connectors/util/java/org/apache/tomcat/util/mx/DynamicMBeanProxy.java
  
  Index: DynamicMBeanProxy.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-connectors/util/java/org/apache/tomcat/util/mx/DynamicMBeanProxy.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DynamicMBeanProxy.java    5 Jun 2002 21:19:48 -0000       1.1
  +++ DynamicMBeanProxy.java    6 Jun 2002 22:17:00 -0000       1.2
  @@ -77,7 +77,8 @@
    */
   public class DynamicMBeanProxy implements DynamicMBean {
       Object real;
  -
  +    String name;
  +    
       Method methods[];
   
       Hashtable attMap=new Hashtable();
  @@ -91,6 +92,10 @@
       // key: operation val: invoke method
       Hashtable invokeAttMap=new Hashtable();
   
  +    static MBeanServer mserver=null;
  +
  +    static Hashtable instances=new Hashtable();
  +    
       /** Create a Dynamic proxy, using introspection to manage a
        *  real tomcat component.
        */
  @@ -98,6 +103,60 @@
           
       }
   
  +    public void setName(String name ) {
  +        this.name=name;
  +    }
  +
  +    public String getName() {
  +        if( name!=null ) return name;
  +
  +        if( real==null ) return null;
  +
  +        name=real.getClass().getName();
  +        name=name.substring( name.lastIndexOf( ".") + 1 );
  +        Integer iInt=(Integer)instances.get(name );
  +        if( iInt!= null ) {
  +            int i=iInt.intValue();
  +            i++;
  +            instances.put( name, new Integer( i ));
  +            name=name + "_" + i;
  +        } else {
  +            instances.put( name, new Integer( 0 ));
  +        }
  +        return name;
  +    }
  +    
  +    public void registerMBean( String domain ) {
  +        try {
  +            // XXX use aliases, suffix only, proxy.getName(), etc
  +            ObjectName oname=new ObjectName( domain + ": name=" +  getName());
  +            
  +            getMBeanServer().registerMBean( this, oname );
  +        } catch( Throwable t ) {
  +            log.error( "Error creating mbean ", t );
  +        }
  +    }
  +
  +    public static MBeanServer getMBeanServer() {
  +        if( mserver==null ) {
  +            if( MBeanServerFactory.findMBeanServer(null).size() > 0 ) {
  +                
mserver=(MBeanServer)MBeanServerFactory.findMBeanServer(null).get(0);
  +            } else {
  +                mserver=MBeanServerFactory.createMBeanServer();
  +            }
  +        }
  +        
  +        return mserver;
  +    }
  +
  +    private boolean supportedType( Class ret ) {
  +        return ret == String.class ||
  +            ret == Integer.class ||
  +            ret == Integer.TYPE ||
  +            ret == java.io.File.class ||
  +            ret == Boolean.class;
  +    }
  +    
       /** Set the managed object.
        *
        * @todo Read an XML ( or .properties ) file containing descriptions,
  @@ -119,9 +178,7 @@
                   if( ! Modifier.isPublic( methods[j].getModifiers() ) )
                       continue;
                   Class ret=methods[j].getReturnType();
  -                if( ret != String.class &&
  -                    ret != Integer.class &&
  -                    ret != Boolean.class ) {
  +                if( ! supportedType( ret ) ) {
                       continue;
                   }
                   name=unCapitalize( name.substring(3));
  @@ -140,9 +197,7 @@
                   if( ! Modifier.isPublic( methods[j].getModifiers() ) )
                       continue;
                   Class ret=params[0];
  -                if( ret != String.class &&
  -                    ret != Integer.class &&
  -                    ret != Boolean.class ) {
  +                if( ! supportedType( ret ) ) {
                       continue;
                   }
                   name=unCapitalize( name.substring(3));
  @@ -197,9 +252,11 @@
                   }
               }
               
  -            log.info(real.getClass().getName() +  "getMBeanInfo ");
  -            return new MBeanInfo( this.getClass().getName(),
  -                                  "Management bean for " + 
real.getClass().getName(),
  +            if( log.isDebugEnabled() )
  +                log.debug(real.getClass().getName() +  " getMBeanInfo()");
  +            
  +            return new MBeanInfo( real.getClass().getName(), /* ??? */
  +                                  "MBean for " + getName(),
                                     attributes,
                                     new MBeanConstructorInfo[0],
                                     operations,
  @@ -219,7 +276,8 @@
           if( m==null ) throw new AttributeNotFoundException(attribute);
   
           try {
  -            log.info(real.getClass().getName() +  "getAttribute " + attribute);
  +            if( log.isDebugEnabled() )
  +                log.debug(real.getClass().getName() +  " getAttribute " + 
attribute);
               return m.invoke(real, NO_ARGS_PARAM );
           } catch( IllegalAccessException ex ) {
               ex.printStackTrace();
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to