You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by co...@apache.org on 2002/09/07 06:04:54 UTC

cvs commit: jakarta-avalon-phoenix/src/java/org/apache/avalon/phoenix/components/manager MBeanScripter.java ExtendedMX4JSystemManager.java

colus       2002/09/06 21:04:54

  Modified:    src/java/org/apache/avalon/phoenix/components/manager
                        ExtendedMX4JSystemManager.java
  Added:       src/java/org/apache/avalon/phoenix/components/manager
                        MBeanScripter.java
  Log:
  Extract MBeanScripter from ExtendedMX4JSystemManager.
  
  Revision  Changes    Path
  1.4       +25 -114   jakarta-avalon-phoenix/src/java/org/apache/avalon/phoenix/components/manager/ExtendedMX4JSystemManager.java
  
  Index: ExtendedMX4JSystemManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-phoenix/src/java/org/apache/avalon/phoenix/components/manager/ExtendedMX4JSystemManager.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ExtendedMX4JSystemManager.java	6 Sep 2002 17:31:53 -0000	1.3
  +++ ExtendedMX4JSystemManager.java	7 Sep 2002 04:04:54 -0000	1.4
  @@ -7,27 +7,20 @@
    */
   package org.apache.avalon.phoenix.components.manager;
   
  -import java.io.File;
   import java.util.Iterator;
   import java.util.HashMap;
   import java.util.Map;
  -import javax.management.Attribute;
   import javax.management.MBeanServer;
   import javax.management.MBeanServerFactory;
  -import javax.management.ObjectName;
   import org.apache.avalon.framework.configuration.Configurable;
   import org.apache.avalon.framework.configuration.Configuration;
   import org.apache.avalon.framework.configuration.ConfigurationException;
  -import org.apache.avalon.framework.context.Context;
  -import org.apache.avalon.framework.context.ContextException;
  -import org.apache.avalon.framework.context.Contextualizable;
   import org.apache.avalon.excalibur.i18n.Resources;
   import org.apache.avalon.excalibur.i18n.ResourceManager;
  -import org.apache.excalibur.converter.Converter;
  -import org.apache.excalibur.converter.lib.SimpleMasterConverter;
   
   /**
    * This component is responsible for managing phoenix instance.
  + * Support Flexible jmx helper mbean configuration.
    *
    * @author <a href="mail@leosimons.com">Leo Simons</a>
    * @author <a href="mailto:peter at apache.org">Peter Donald</a>
  @@ -36,7 +29,7 @@
    */
   public class ExtendedMX4JSystemManager
       extends AbstractJMXManager
  -    implements Contextualizable, Configurable
  +    implements Configurable
   {
       private static final Resources REZ =
           ResourceManager.getPackageResources( ExtendedMX4JSystemManager.class );
  @@ -44,16 +37,8 @@
       private static final String DEFAULT_NAMING_FACTORY =
           "com.sun.jndi.rmi.registry.RegistryContextFactory";
   
  -    private File m_homeDir;
       private Configuration m_configuration;
  -    private Map m_jmxMBeans;
  -    private Map m_jmxMBeanConfs;
  -
  -    public void contextualize( Context context )
  -        throws ContextException
  -    {
  -        m_homeDir = (File)context.get( "phoenix.home" );
  -    }
  +    private Map m_mBeanScripters;
   
       public void configure( final Configuration configuration )
           throws ConfigurationException
  @@ -78,134 +63,60 @@
       {
           super.initialize();
   
  -        m_jmxMBeans = new HashMap();
  -        m_jmxMBeanConfs = new HashMap();
  -        final Configuration[] mBeanConfs = m_configuration.getChildren( "mbean" );
  -        for ( int i = 0; i < mBeanConfs.length; i++ )
  +        m_mBeanScripters = new HashMap();
  +        final Configuration[] scripters =
  +            m_configuration.getChildren( "mbean" );
  +        for ( int i = 0; i < scripters.length; i++ )
           {
  -            createMBean( mBeanConfs[ i ] );
  +            createMBeanScripter( scripters[ i ] );
           }
       }
   
       public void dispose()
       {
  -        final MBeanServer mBeanServer = getMBeanServer();
  -
  -        final Iterator mBeanNames = m_jmxMBeans.keySet().iterator();
  -        while ( mBeanNames.hasNext() )
  +        final Iterator scripterNames = m_mBeanScripters.keySet().iterator();
  +        while ( scripterNames.hasNext() )
           {
  -            destroyMBean( (String)mBeanNames.next() );
  +            destroyMBeanScripter( (String)scripterNames.next() );
           }
  +        m_mBeanScripters.clear();
  +        m_mBeanScripters = null;
   
           super.dispose();
       }
   
  -    private void createMBean( final Configuration mBeanConf )
  +    private void createMBeanScripter( final Configuration scripterConf )
           throws Exception
       {
  -        final MBeanServer mBeanServer = getMBeanServer();
  -        final Converter valueConverter = new SimpleMasterConverter();
  -
  -        final ObjectName objectName = new ObjectName( mBeanConf.getAttribute( "name" ) );
  -
  +        final MBeanScripter scripter =
  +            new MBeanScripter( getMBeanServer(), scripterConf );
           try
           {
  -            mBeanServer.createMBean( mBeanConf.getAttribute( "class" ), objectName, null );
  -
  -            //set attributes
  -            final Configuration[] attributes = mBeanConf.getChildren( "attribute" );
  -            for ( int i = 0; i < attributes.length; i++ )
  -            {
  -                final Configuration attribute = attributes[ i ];
  -                final String name = attribute.getAttribute( "name" );
  -                final String type = attribute.getAttribute( "type" );
  -                Object value = attribute.getValue( null );
  -                if ( null != value )
  -                {
  -                    final Class valueClass = Class.forName( type );
  -                    value = valueConverter.convert( valueClass, value, null );
  -                }
  -                mBeanServer.setAttribute( objectName, new Attribute( name, value ) );
  -            }
  -
  -            //set dependent attributes
  -            final Configuration[] uses = mBeanConf.getChildren( "use" );
  -            for ( int i = 0; i < uses.length; i++ )
  -            {
  -                final Configuration use = uses[ i ];
  -                final String name = use.getAttribute( "name" );
  -                final String value = use.getValue();
  -                mBeanServer.setAttribute( objectName, new Attribute( name, new ObjectName( value ) ) );
  -            }
  -
  -            //invoke startup operations
  -            final Configuration[] invokes = mBeanConf.getChild( "startup", true ).getChildren( "invoke" );
  -            invokeOperations( mBeanServer, objectName, invokes );
  +            scripter.startup();
   
  -            m_jmxMBeans.put( objectName.getCanonicalName(), objectName );
  -            m_jmxMBeanConfs.put( objectName.getCanonicalName(), mBeanConf );
  +            m_mBeanScripters.put( scripter.getName(), scripter );
           }
           catch ( final Exception e )
           {
  -            final String message = REZ.getString( "jmxmanager.error.jmxmbean.initialize", objectName );
  +            final String message = REZ.getString( "jmxmanager.error.jmxmbean.initialize", scripter.getName() );
               getLogger().error( message , e );
  +            throw e;
           }
       }
   
  -    private void destroyMBean( final String name )
  +    private void destroyMBeanScripter( final String name )
       {
  -        final MBeanServer mBeanServer = getMBeanServer();
  -
  -        final ObjectName objectName = (ObjectName)m_jmxMBeans.get( name );
  -        final Configuration mBeanConf = (Configuration)m_jmxMBeanConfs.get( name );
  +        final MBeanScripter scripter =
  +            (MBeanScripter)m_mBeanScripters.get( name );
           try
           {
  -            final Configuration[] invokes = mBeanConf.getChild( "shutdown", true ).getChildren( "invoke" );
  -            invokeOperations( mBeanServer, objectName, invokes );
  +            scripter.shutdown();
           }
           catch ( final Exception e )
           {
  -            final String message = REZ.getString( "jmxmanager.error.jmxmbean.dispose", objectName );
  +            final String message = REZ.getString( "jmxmanager.error.jmxmbean.dispose", scripter.getName() );
               getLogger().error( message , e );
           }
  -    }
  -
  -    private void invokeOperations( final MBeanServer mBeanServer,
  -                                   final ObjectName objectName,
  -                                   final Configuration[] invokes )
  -        throws Exception
  -    {
  -        for ( int i = 0; i < invokes.length; i++ )
  -        {
  -            final Configuration invoke = invokes[ i ];
  -            invokeOperation( mBeanServer, objectName, invoke );
  -        }
  -    }
  -
  -    private void invokeOperation( final MBeanServer mBeanServer,
  -                                  final ObjectName objectName,
  -                                  final Configuration invoke )
  -        throws Exception
  -    {
  -        final Converter valueConverter = new SimpleMasterConverter();
  -
  -        final String operationName = invoke.getAttribute( "name" );
  -        final Configuration[] paramConfs = invoke.getChildren( "parameter" );
  -        final String[] types = new String[ paramConfs.length ];
  -        final Object[] values = new Object[ paramConfs.length ];
  -        for ( int i = 0; i < paramConfs.length; i++ )
  -        {
  -            final String type = paramConfs[ i ].getAttribute( "type" );
  -            Object value = paramConfs[ i ].getValue( null );
  -            if ( null != value )
  -            {
  -                final Class valueClass = Class.forName( type );
  -                value = valueConverter.convert( valueClass, value, null );
  -            }
  -            types[ i ] = type;
  -            values[ i ] = value;
  -        }
  -        mBeanServer.invoke( objectName, operationName, values, types );
       }
   
       protected MBeanServer createMBeanServer()
  
  
  
  1.1                  jakarta-avalon-phoenix/src/java/org/apache/avalon/phoenix/components/manager/MBeanScripter.java
  
  Index: MBeanScripter.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.phoenix.components.manager;
  
  import javax.management.Attribute;
  import javax.management.MBeanServer;
  import javax.management.MalformedObjectNameException;
  import javax.management.ObjectName;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.excalibur.converter.Converter;
  import org.apache.excalibur.converter.lib.SimpleMasterConverter;
  
  /**
   * Support JMX MBean lifecycle.
   *
   * @author <a href="mailto:colus@apache.org">Eung-ju Park</a>
   */
  public class MBeanScripter
  {
      private final static Converter c_valueConverter =
          new SimpleMasterConverter();
  
      private MBeanServer m_mBeanServer;
      private Configuration m_configuration;
      private ObjectName m_objectName;
  
      public MBeanScripter( final MBeanServer mBeanServer,
                            final Configuration configuration )
          throws ConfigurationException, MalformedObjectNameException
      {
          m_mBeanServer = mBeanServer;
          m_configuration = configuration;
          m_objectName = new ObjectName( m_configuration.getAttribute( "name" ) );
      }
  
      public String getName()
      {
          return m_objectName.getCanonicalName();
      }
  
      public ObjectName getObjectName()
      {
          return m_objectName;
      }
  
      public void startup()
          throws Exception
      {
          m_mBeanServer.createMBean( m_configuration.getAttribute( "class" ),
                                     getObjectName(), null );
  
          setAttributes();
          setUses();
          invokeStartupOperations();
      }
  
      public void shutdown()
          throws Exception
      {
          invokeShutdownOperations();
      }
  
      private void setAttributes()
          throws Exception
      {
          final Configuration[] attributes = m_configuration.getChildren( "attribute" );
          for ( int i = 0; i < attributes.length; i++ )
          {
              setAttribute( attributes[ i ] );
          }
      }
  
      private void setAttribute( final Configuration attribute )
          throws Exception
      {
          final String name = attribute.getAttribute( "name" );
          final String type = attribute.getAttribute( "type" );
          Object value = attribute.getValue( null );
          if ( null != value )
          {
              final Class valueClass = Class.forName( type );
              value = c_valueConverter.convert( valueClass, value, null );
          }
          m_mBeanServer.setAttribute( getObjectName(), new Attribute( name, value ) );
      }
  
      private void setUses()
          throws Exception
      {
          final Configuration[] uses = m_configuration.getChildren( "use" );
          for ( int i = 0; i < uses.length; i++ )
          {
              setUse( uses[ i ] );
          }
      }
  
      private void setUse( final Configuration use )
          throws Exception
      {
          final String name = use.getAttribute( "name" );
          final String value = use.getValue();
          m_mBeanServer.setAttribute( getObjectName(), new Attribute( name, new ObjectName( value ) ) );
      }
  
      private void invokeStartupOperations()
          throws Exception
      {
          final Configuration[] invokes = m_configuration.getChild( "startup", true ).getChildren( "invoke" );
          invokeOperations( invokes );
      }
  
      private void invokeShutdownOperations()
          throws Exception
      {
          final Configuration[] invokes = m_configuration.getChild( "startup", true ).getChildren( "invoke" );
          invokeOperations( invokes );
      }
  
      private void invokeOperations( final Configuration[] invokes )
          throws Exception
      {
          for ( int i = 0; i < invokes.length; i++ )
          {
              final Configuration invoke = invokes[ i ];
              invokeOperation( invoke );
          }
      }
  
      private void invokeOperation( final Configuration invoke )
          throws Exception
      {
          final String operationName = invoke.getAttribute( "name" );
          final Configuration[] paramConfs = invoke.getChildren( "parameter" );
          final String[] types = new String[ paramConfs.length ];
          final Object[] values = new Object[ paramConfs.length ];
          for ( int i = 0; i < paramConfs.length; i++ )
          {
              final String type = paramConfs[ i ].getAttribute( "type" );
              Object value = paramConfs[ i ].getValue( null );
              if ( null != value )
              {
                  final Class valueClass = Class.forName( type );
                  value = c_valueConverter.convert( valueClass, value, null );
              }
              types[ i ] = type;
              values[ i ] = value;
          }
          m_mBeanServer.invoke( getObjectName(), operationName, values, types );
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>