avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mcconn...@apache.org
Subject cvs commit: avalon-sandbox/kernel/test/src/test/org/apache/avalon/merlin MerlinEmbeddedTest.java
Date Wed, 26 Nov 2003 21:46:52 GMT
mcconnell    2003/11/26 13:46:52

  Modified:    kernel/impl project.xml
               kernel/impl/src/java/org/apache/avalon/merlin/kernel/impl
                        DefaultFactory.java
               kernel/test/src/test/org/apache/avalon/merlin
                        MerlinEmbeddedTest.java
  Log:
  Update to incorporate constructor based supply of the intial factory by the deployment system.
  
  Revision  Changes    Path
  1.9       +17 -0     avalon-sandbox/kernel/impl/project.xml
  
  Index: project.xml
  ===================================================================
  RCS file: /home/cvs/avalon-sandbox/kernel/impl/project.xml,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- project.xml	26 Nov 2003 00:50:11 -0000	1.8
  +++ project.xml	26 Nov 2003 21:46:52 -0000	1.9
  @@ -69,6 +69,23 @@
   
       <dependency>
         <groupId>avalon-repository</groupId>
  +      <artifactId>avalon-repository-main</artifactId>
  +      <version>1.2-dev</version>
  +      <properties>
  +        <avalon.classloader>api</avalon.classloader>
  +      </properties>
  +    </dependency>
  +
  +    <!--
  +    <dependency> 
  +      <groupId>avalon-repository</groupId>
  +      <artifactId>avalon-repository-impl</artifactId>
  +      <version>1.2-dev</version>
  +    </dependency>
  +    -->
  +
  +    <dependency>
  +      <groupId>avalon-repository</groupId>
         <artifactId>avalon-repository-api</artifactId>
         <version>1.2-dev</version>
         <properties>
  
  
  
  1.8       +136 -2    avalon-sandbox/kernel/impl/src/java/org/apache/avalon/merlin/kernel/impl/DefaultFactory.java
  
  Index: DefaultFactory.java
  ===================================================================
  RCS file: /home/cvs/avalon-sandbox/kernel/impl/src/java/org/apache/avalon/merlin/kernel/impl/DefaultFactory.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- DefaultFactory.java	26 Nov 2003 14:34:55 -0000	1.7
  +++ DefaultFactory.java	26 Nov 2003 21:46:52 -0000	1.8
  @@ -17,6 +17,7 @@
   import org.apache.avalon.framework.configuration.DefaultConfiguration;
   import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
   
  +import org.apache.avalon.repository.Repository;
   import org.apache.avalon.repository.criteria.Parameter;
   import org.apache.avalon.repository.criteria.Criteria;
   import org.apache.avalon.repository.criteria.ValidationException;
  @@ -36,6 +37,8 @@
   import org.apache.avalon.merlin.kernel.KernelException;
   import org.apache.avalon.merlin.kernel.KernelCriteria;
   
  +import org.apache.excalibur.configuration.ConfigurationUtil;
  +
   import org.xml.sax.InputSource;
   
   
  @@ -66,8 +69,21 @@
       private Logger m_kernelLogger;
   
       private LoggingManager m_logging;
  +
  +    private Repository m_repository;
  +
  +    private Factory m_factory;
    
       //--------------------------------------------------------------------------
  +    // constructor
  +    //--------------------------------------------------------------------------
  +
  +    public DefaultFactory( Factory factory )
  +    {
  +        m_factory = factory;
  +    }
  +
  +    //--------------------------------------------------------------------------
       // Factory
       //--------------------------------------------------------------------------
   
  @@ -83,6 +99,9 @@
   
       public Object create( Map map ) throws Exception
       {
  +        if( null == map ) 
  +          throw new NullPointerException( "map" );
  +
           KernelCriteria criteria = null;
           if( map instanceof KernelCriteria) 
           {
  @@ -101,6 +120,8 @@
   
           URL kernelURL = (URL) criteria.getKernelURL();
           Configuration kernelConfig = getKernelConfiguration( kernelURL );
  +        String listing = ConfigurationUtil.list( kernelConfig );
  +        System.out.println( listing );
   
           //
           // create the logging subsystem
  @@ -117,12 +138,125 @@
   
           m_kernelLogger = m_logging.getLoggerForCategory( loggingDescriptor.getName() );
           m_logger = m_kernelLogger.getChildLogger( CATEGORY_NAME );
  -
           getLogger().info( "logging system established" );
   
  +        //
  +        // create the common repository
  +        //
  +
  +        Configuration repositoryConfig = kernelConfig.getChild( "repository" );
  +        File cache = criteria.getRepositoryDirectory();
  +        m_repository = createRepository( m_factory, cache, repositoryConfig );
  +        getLogger().debug( "repository established: " + m_repository.getLocation() );
  +
  +
           return null;
       }
   
  +    private Repository createRepository( Factory factory, File root, Configuration config
) 
  +      throws KernelException
  +    {
  +
  +        //
  +        // the supplied root argument is the root cache resolved relative
  +        // to system properties and environment variables.  This value is 
  +        // overriden if a cache is declared in the kernel repository 
  +        // configuration
  +        //
  +
  +        File cache = getCacheDirectory( root, config.getChild( "cache" ) );
  +        String[] hosts = getHosts( config.getChild( "hosts" ) );
  +
  +        try
  +        {
  +            Map criteria = factory.createDefaultCriteria();
  +            criteria.put( "avalon.repository.remote.url", hosts );
  +
  +            Configuration proxyConfig = config.getChild( "proxy", false );
  +            if( null != proxyConfig )
  +            {
  +                final String host = 
  +                  proxyConfig.getChild( "host" ).getValue( null );
  +                criteria.put( "avalon.repository.proxy.host", hosts );
  +
  +                final int port = 
  +                  proxyConfig.getChild( "port" ).getValueAsInteger( 0 );
  +                criteria.put( "avalon.repository.proxy.port", new Integer( port ) );
  +
  +                Configuration credentials = 
  +                  proxyConfig.getChild( "credentials", false );
  +                if( credentials != null )
  +                {
  +                    final String username = 
  +                      credentials.getChild( "username" ).getValue( null );
  +                    if( username == null )
  +                    {
  +                        final String error =
  +                          "Credentials configuration does not contain the required 'username'
element."
  +                          + ConfigurationUtil.list( credentials );
  +                        throw new KernelException( error );                
  +                    }
  +                    else
  +                    {
  +                        criteria.put( "avalon.repository.proxy.username", username );
  +                    }
  +
  +                    final String password = 
  +                        credentials.getChild( "password" ).getValue( null );
  +                    if( password == null )
  +                    {
  +                        final String error =
  +                          "Credentials configuration does not contain the required 'password'
element."
  +                          + ConfigurationUtil.list( credentials );
  +                        throw new KernelException( error );                
  +                    }
  +                    else
  +                    {
  +                        criteria.put( "avalon.repository.proxy.password", password );
  +                    }
  +                }
  +            }
  +
  +            return (Repository) factory.create( criteria );
  +        }
  +        catch ( Throwable e )
  +        {
  +            final String error = 
  +              "Internal error while attempting to create the common repository "
  +              + " using the supplied cache: [" + cache + "].";
  +            throw new KernelException( error, e );
  +        }
  +    }
  +
  +    private File getCacheDirectory( File cache, Configuration config )
  +    {
  +        String directive = config.getValue( null );
  +        if( null == directive ) return cache;
  +        return new File( directive );  
  +    }
  +
  +    private String[] getHosts( Configuration config ) throws KernelException
  +    {
  +        ArrayList list = new ArrayList();
  +        Configuration[] hosts = config.getChildren( "host" );
  +        for( int i=0; i<hosts.length; i++ )
  +        {
  +            Configuration host = hosts[i];
  +            String path = host.getAttribute( "path", null );
  +            if( path == null )
  +            {      
  +                final String error = 
  +                  "Missing host element value: " 
  +                + ConfigurationUtil.list( host );
  +                throw new KernelException( error );
  +            }
  +            else
  +            {
  +                list.add( path );
  +            }
  +        }
  +        return (String[]) list.toArray( new String[0] );
  +    }
   
       /**
        * Utility method to create a new logging descriptor from a
  @@ -198,7 +332,7 @@
       */
       private Configuration getKernelConfiguration( URL url ) throws Exception
       {
  -        if( null != url )
  +        if( null == url )
           {
               try
               {
  
  
  
  1.5       +53 -38    avalon-sandbox/kernel/test/src/test/org/apache/avalon/merlin/MerlinEmbeddedTest.java
  
  Index: MerlinEmbeddedTest.java
  ===================================================================
  RCS file: /home/cvs/avalon-sandbox/kernel/test/src/test/org/apache/avalon/merlin/MerlinEmbeddedTest.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- MerlinEmbeddedTest.java	26 Nov 2003 14:13:28 -0000	1.4
  +++ MerlinEmbeddedTest.java	26 Nov 2003 21:46:52 -0000	1.5
  @@ -53,6 +53,7 @@
   import java.io.File;
   import java.net.URL;
   import java.util.Map;
  +import java.lang.reflect.Constructor;
   import javax.naming.directory.Attributes;
   import javax.naming.directory.Attribute;
   
  @@ -67,6 +68,8 @@
   import org.apache.avalon.repository.meta.ApplicationDescriptor ;
   import org.apache.avalon.repository.main.InitialRepositoryFactory ;
   
  +import org.apache.avalon.util.exception.ExceptionHelper;
  +
   /**
    * 
    * 
  @@ -76,6 +79,7 @@
    */
   public class MerlinEmbeddedTest extends TestCase
   {
  +    private Factory m_factory;
       private Repository m_repository;
   
       /**
  @@ -90,11 +94,11 @@
       protected void setUp() throws Exception
       {
           String[] bootstrap = getBootstrapRepositorySet();
  -        Factory factory = new InitialRepositoryFactory( bootstrap );
  -        Map criteria = factory.createDefaultCriteria();
  +        m_factory = new InitialRepositoryFactory( bootstrap );
  +        Map criteria = m_factory.createDefaultCriteria();
           String[] repositories = getWorkingRepositorySet();
           criteria.put( "avalon.repository.remote.url", repositories );
  -        m_repository = (Repository) factory.create( criteria ) ;
  +        m_repository = (Repository) m_factory.create( criteria ) ;
       }
   
       //----------------------------------------------------------------------
  @@ -103,43 +107,54 @@
   
       public void testEmbeddingScenario() throws Exception
       {
  -        Artifact spec = ClassicArtifact.createClassicArtifact( "merlin", "merlin-kernel-impl"
);
  -        Attributes attributes = m_repository.getArtifactAttributes( spec );
  -        ApplicationDescriptor target = new ApplicationDescriptor( attributes );
  -        Artifact implementation = target.getImplementation();
  -        String classname = target.getFactoryClassname();
  -
  -        //
  -        // create a classloader using the artifact referenced by the target
  -        // using the context classloader as the parent
  -        //
  -
  -        ClassLoader parent = Thread.currentThread().getContextClassLoader();
  -        ClassLoader loader = m_repository.getClassLoader( parent, implementation );
  -        Class c = loader.loadClass( target.getFactoryClassname() );
  -        Factory factory = (Factory) c.newInstance();
  -        Map criteria = (Map) factory.createDefaultCriteria();
  -        assertNotNull( "criteria", criteria );
  -
  -        //
  -        // list the supplied default criteria
  -        //
  -
  -        String[] keys = (String[]) criteria.keySet().toArray( new String[0] ) ;
  +        try
  +        {
  +            Artifact spec = ClassicArtifact.createClassicArtifact( "merlin", "merlin-kernel-impl"
);
  +            Attributes attributes = m_repository.getArtifactAttributes( spec );
  +            ApplicationDescriptor target = new ApplicationDescriptor( attributes );
  +            Artifact implementation = target.getImplementation();
  +            String classname = target.getFactoryClassname();
  +
  +            //
  +            // create a classloader using the artifact referenced by the target
  +            // using the context classloader as the parent
  +            //
  +
  +            ClassLoader parent = Thread.currentThread().getContextClassLoader();
  +            ClassLoader loader = m_repository.getClassLoader( parent, implementation );
  +            Class c = loader.loadClass( target.getFactoryClassname() );
  +            Constructor constructor = c.getConstructor( new Class[]{ Factory.class } );
  +
  +            Factory factory = (Factory) constructor.newInstance( new Object[]{ m_factory
} );
  +            Map criteria = (Map) factory.createDefaultCriteria();
  +            assertNotNull( "criteria", criteria );
  +
  +            //
  +            // list the supplied default criteria
  +            //
  +
  +            String[] keys = (String[]) criteria.keySet().toArray( new String[0] ) ;
  +
  +            System.out.println( "\n----------- LISTING (in unit test) ------------\n" );
  +            for( int i=0; i<keys.length; i++ )
  +            {
  +                final String key = keys[i];
  +                Object arg = criteria.get( key );
  +                System.out.println( "   ${" + key + "} == " + arg );
  +            }
  +
  +            //
  +            // instantiate the application
  +            //
   
  -        System.out.println( "\n----------- LISTING (in unit test) ------------\n" );
  -        for( int i=0; i<keys.length; i++ )
  +            Object app = factory.create( criteria );
  +        }
  +        catch( Throwable e )
           {
  -            final String key = keys[i];
  -            Object arg = criteria.get( key );
  -            System.out.println( "   ${" + key + "} == " + arg );
  +            final String error = ExceptionHelper.packException( e );
  +            System.out.println( error );
  +            throw new Exception( error );
           }
  -
  -        //
  -        // instantiate the application
  -        //
  -
  -        Object app = factory.create( criteria );
       }
   
       //----------------------------------------------------------------------
  
  
  

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


Mime
View raw message