avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mcconn...@apache.org
Subject cvs commit: jakarta-avalon-excalibur/all/src/scratchpad/org/apache/avalon/excalibur/service UnitInfo.java
Date Fri, 03 May 2002 04:58:56 GMT
mcconnell    02/05/02 21:58:56

  Modified:    all/src/scratchpad/org/apache/avalon/excalibur/service
                        UnitInfo.java
  Log:
  no message
  
  Revision  Changes    Path
  1.6       +116 -53   jakarta-avalon-excalibur/all/src/scratchpad/org/apache/avalon/excalibur/service/UnitInfo.java
  
  Index: UnitInfo.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/all/src/scratchpad/org/apache/avalon/excalibur/service/UnitInfo.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- UnitInfo.java	2 May 2002 19:23:50 -0000	1.5
  +++ UnitInfo.java	3 May 2002 04:58:56 -0000	1.6
  @@ -7,19 +7,26 @@
   
   package org.apache.avalon.excalibur.service;
   
  +import java.io.File;
   import java.io.InputStream;
  +import java.io.IOException;
   import java.util.Vector;
  -import org.apache.avalon.excalibur.configuration.CascadingConfiguration;
  +
   import org.apache.excalibur.mpool.Pool;
  +import org.apache.avalon.excalibur.configuration.CascadingConfiguration;
  +import org.apache.avalon.excalibur.context.ContextUtility;
   import org.apache.avalon.framework.CascadingException;
  +import org.apache.avalon.framework.configuration.DefaultConfiguration;
   import org.apache.avalon.framework.configuration.Configuration;
   import org.apache.avalon.framework.configuration.ConfigurationException;
   import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
  +import org.apache.avalon.framework.context.DefaultContext;
  +import org.apache.avalon.framework.context.Context;
   
   /**
  - * Meta information about a <code>Serviceable</code> component.
  + * Meta information about a <code>Serviceable</code> component.  
    */
  -class UnitInfo
  +public class UnitInfo
   {
   
       public static final int SINGLETON_LIFETIME_POLICY = 0;
  @@ -28,6 +35,7 @@
   
       private Class m_block;
       private Configuration m_config;
  +    private Context m_context;
       private ServiceInfo[] m_services;
       private DependencyInfo[] m_dependencies;
       private int m_policy = SINGLETON_LIFETIME_POLICY;
  @@ -40,31 +48,31 @@
        * the xinfo configuration:
        * <pre>
        *   &lt;blockinfo&gt;
  -     *
  +     *   
        *     &lt;block>
        *       &lt;version>1.0&lt;/version&gt;
        *     &lt;/block&gt;
  -     *
  +     *   
        *     &lt;!--
  -     *     services that are offered by this block
  +     *     services that are offered by this block 
        *     --&gt;
  -     *
  +     *   
        *     &lt;services&gt;
        *         &lt;service name="hello.Hello" version="1.0"/&gt;
        *     &lt;/services&gt;
  -     *
  +     *   
        *     &lt;!--
        *     Implementation policy may one of the following enumerations:
        *     (a) SINGLETON, service is available for the lifetime of the manager
        *     (b) POOLED, the implementation implements Pool
        *     (c) TRANSIENT, manager is a factory of transient service instances
        *     --&gt;
  -     *
  +     *   
        *     &lt;implementation policy="SINGLETON" /&gt;
  -     *
  +     *   
        *   &lt;/blockinfo&gt;
        * </pre>
  -     *
  +     * 
        * @param block the implementation class
        * @param xinfo the xinfo meta-information in the form of a Configuration
        * @param runtime the runtime profile
  @@ -72,21 +80,31 @@
        */
       public UnitInfo( Class block, Configuration xinfo ) throws Exception
       {
  -        m_block = block;
  +        this( block, xinfo, new DefaultConfiguration("configuration",null) );
  +    }
   
  +    public UnitInfo( Class block, Configuration xinfo, Configuration conf ) throws Exception
  +    {
  +        this( block, xinfo, conf, new File( System.getProperty("user.dir") ) );
  +    }
  +
  +    public UnitInfo( Class block, Configuration xinfo, Configuration conf, File root )
throws Exception
  +    {
  +        m_block = block;
  +        
           //
           // create the list of service provided by block implementation
           //
   
           try
           {
  -            Configuration[] services = xinfo.getChild( "services" ).getChildren( "service"
);
  +            Configuration[] services = xinfo.getChild("services").getChildren("service");
               Vector vector = new Vector();
  -            for( int i = 0; i < services.length; i++ )
  +            for( int i=0; i<services.length; i++ )
               {
  -                vector.add( new ServiceInfo( services[ i ] ) );
  +                vector.add( new ServiceInfo( services[i] ));
               }
  -            m_services = (ServiceInfo[])vector.toArray( new ServiceInfo[ vector.size()
] );
  +            m_services = (ServiceInfo[]) vector.toArray( new ServiceInfo[ vector.size()
] );
           }
           catch( Throwable e )
           {
  @@ -99,13 +117,13 @@
   
           try
           {
  -            Configuration[] dependencies = xinfo.getChild( "dependencies" ).getChildren(
"dependency" );
  +            Configuration[] dependencies = xinfo.getChild("dependencies").getChildren("dependency");
               Vector vector = new Vector();
  -            for( int i = 0; i < dependencies.length; i++ )
  +            for( int i=0; i<dependencies.length; i++ )
               {
  -                vector.add( new DependencyInfo( dependencies[ i ] ) );
  +                vector.add( new DependencyInfo( dependencies[i] ));
               }
  -            m_dependencies = (DependencyInfo[])vector.toArray( new DependencyInfo[ vector.size()
] );
  +            m_dependencies = (DependencyInfo[]) vector.toArray( new DependencyInfo[ vector.size()
] );
           }
           catch( Throwable e )
           {
  @@ -116,7 +134,7 @@
           // resolve the implementation policy
           //
   
  -        String policy = xinfo.getChild( "implementation" ).getAttribute( "policy", "OTHER"
);
  +        String policy = xinfo.getChild("implementation").getAttribute("policy","OTHER");
           if( policy.equalsIgnoreCase( "SINGLETON" ) )
           {
               m_policy = SINGLETON_LIFETIME_POLICY;
  @@ -141,32 +159,47 @@
           // get the default configuration
           //
   
  -        m_config = xinfo.getChild( "configuration" );
  -        String base = m_config.getAttribute( "extends", null );
  +        Configuration configuration = xinfo.getChild("configuration");
  +        String base = configuration.getAttribute("extends", null );
           if( base != null )
           {
  -            Configuration c = loadConfiguration( base );
  -            m_config = new CascadingConfiguration( m_config, c );
  +            Configuration base_config = loadConfiguration( base );
  +            configuration = new CascadingConfiguration( configuration, base_config );
           }
  +        m_config = new CascadingConfiguration( conf, configuration );
   
           //
           // get the default name
           //
   
  -        m_name = xinfo.getChild( "block" ).getAttribute( "name", getClassName() );
  +        m_name = xinfo.getChild("block").getAttribute("name", getClassName() );
   
  +        //
  +        // get the default context
  +        //
  +
  +        Context context = new ServiceContext( new String[0], root, getName() );
  +        Configuration context_config = xinfo.getChild("context",false);
  +        if( context_config != null )
  +        {
  +            m_context = ContextUtility.createContextFromConfiguration( context, context_config
);
  +        }
  +        else
  +        {
  +            m_context = context;
  +        }
       }
   
       /**
  -     * Creation of a new <code>UnitInfo</code> based on an existing
  +     * Creation of a new <code>UnitInfo</code> based on an existing 
        * info and supplimentary configuration.  The implementation will
        * assigns the supplied configuration as the primary configuration
        * backed by the info current configuration.
        * @param info, the primary <code>UnitInfo</code>
  -     * @param config, a configuration to assign as the primary
  +     * @param config, a configuration to assign as the primary 
        *   configuration for the created info
  -     * @return UnitInfo an equivalent <code>UnitInfo</code> with a
  -     *   <code>CascadingConfiguration</code> in which the supplied
  +     * @return UnitInfo an equivalent <code>UnitInfo</code> with a 
  +     *   <code>CascadingConfiguration</code> in which the supplied 
        *   configuration is primary, backed by the supplied unit configuration
        */
       public UnitInfo( UnitInfo info, Configuration config )
  @@ -177,6 +210,7 @@
           m_dependencies = info.getDependencies();
           m_policy = info.getPolicy();
           m_name = info.getName();
  +        m_context = info.getContext();
       }
   
       public UnitInfo( String path ) throws Exception
  @@ -184,6 +218,16 @@
           this( loadClass( path ), loadConfiguration( path + ".xinfo" ) );
       }
   
  +    public UnitInfo( String path, File root ) throws Exception
  +    {
  +        this( path, new DefaultConfiguration("configuration",null), root );
  +    }
  +
  +    public UnitInfo( String path, Configuration config, File root ) throws Exception
  +    {
  +        this( loadClass( path ), loadConfiguration( path + ".xinfo" ), config, root );
  +    }
  +
       /**
        * Returns the name of the class implemenmting the service.
        * @return the class name
  @@ -203,7 +247,7 @@
       }
   
       /**
  -     * Returns a <code>ServiceInfo</code> from the <code>UnitInfo</code>
matching
  +     * Returns a <code>ServiceInfo</code> from the <code>UnitInfo</code>
matching 
        * the supplied service descriptor.
        * @param info a <code>ServiceInfo</code> to locate with the <code>UnitInfo</code>
instance
        * @return a <code>ServiceInfo</code> instance matching the supplied service
descriptor
  @@ -211,16 +255,16 @@
       public ServiceInfo getService( ServiceInfo info )
       {
           ServiceInfo[] services = getServices();
  -        for( int i = 0; i < services.length; i++ )
  +        for( int i=0; i<services.length; i++ )
           {
  -            if( services[ i ].equals( info ) ) return services[ i ];
  +            if( services[i].equals( info )) return services[i];
           }
           return null;
       }
   
       /**
  -     * Returns a <code>TRUE</code> if the supplied <code>ServiceInfo</code>
can be
  -     * provided by the implementation.
  +     * Returns a <code>TRUE</code> if the supplied <code>ServiceInfo</code>
can be 
  +     * provided by the implementation. 
        * @param service the requested service
        * @return boolean TRUE if the service is available otherwise FALSE
        */
  @@ -259,7 +303,7 @@
       }
   
       /**
  -     * Returns the default configuration.
  +      * Returns the default configuration.
        * @return Configuration the default configuration
        */
       public Configuration getConfiguration()
  @@ -268,6 +312,15 @@
       }
   
       /**
  +     * Returns the default service context.
  +     * @return Context the default service context
  +     */
  +    public Context getContext()
  +    {
  +        return m_context;
  +    }
  +
  +    /**
        * Returns the block name.
        * @return String the block name
        */
  @@ -286,48 +339,58 @@
           buffer.append( "  name: " + getName() );
           buffer.append( "\n  class: " + getClassName() );
           ServiceInfo[] services = getServices();
  -        for( int i = 0; i < services.length; i++ )
  +        for( int i=0; i<services.length; i++ )
           {
  -            buffer.append( "\n  service: " + services[ i ].getInterface().getName()
  -                           + ", version: " + services[ i ].getVersion() );
  +            buffer.append( "\n  service: " + services[i].getInterface().getName() 
  +              + ", version: " + services[i].getVersion() );
           }
           DependencyInfo[] dependencies = getDependencies();
  -        for( int i = 0; i < dependencies.length; i++ )
  +        for( int i=0; i<dependencies.length; i++ )
           {
  -            buffer.append( "\n  dependency: "
  -                           + "role: " + dependencies[ i ].getRole()
  -                           + ", service: " + dependencies[ i ].getService().getInterface().getName()
  -                           + ", version: " + dependencies[ i ].getService().getVersion()
);
  +            buffer.append( "\n  dependency: " 
  +              + "role: " + dependencies[i].getRole() 
  +              + ", service: " + dependencies[i].getService().getInterface().getName() 
  +              + ", version: " + dependencies[i].getService().getVersion() );
           }
  +        buffer.append("\n  configuration\n");
  +        buffer.append( CascadingConfiguration.list( this.getConfiguration() ));
  +
           return buffer.toString();
       }
   
       /**
  -     * Returns a configuration resource form a jar file.
  +     * Returns a configuration resource form a jar file. 
        * @param path the package path to the resource e.g. net/osm/xinfo.xml
        * @exception ConfigurationException if there is a problem
        */
  -    private static Configuration loadConfiguration( String path )
  -        throws ConfigurationException
  +    private static Configuration loadConfiguration( String path ) 
  +    throws ConfigurationException 
       {
           try
           {
  -            DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
  +            DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder( );
               InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(
path );
               if( is != null ) return builder.build( is );
               throw new ConfigurationException( "Could not locate configuration from path:
" + path );
           }
           catch( Throwable e )
           {
  -            final String error =
  -                "Unexpected exception while attempting to load .xinfo configuration from
path: ";
  -            throw new ConfigurationException( error + path, e );
  -        }
  +            final String error = 
  +              "Unexpected exception while attempting to load .xinfo configuration from
path: ";
  +            throw new ConfigurationException( error + path, e ); 
  +        } 
       }
   
  +    /**
  +     * Loads a class given a path in the form <code>org/xyz/MyClass</code>.
  +     * @param path the resource path
  +     * @return Class the class loaded from the path
  +     */
       private static Class loadClass( final String path ) throws Exception
       {
  -        return Thread.currentThread().getContextClassLoader().loadClass( path.replace(
'/', '.' ) );
  +        return Thread.currentThread().getContextClassLoader().loadClass( path.replace('/','.'));
       }
   }
  +
  +
   
  
  
  

--
To unsubscribe, e-mail:   <mailto:avalon-cvs-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-cvs-help@jakarta.apache.org>


Mime
View raw message