mcconnell    2003/11/26 06:13:28

  Modified:    kernel/impl/src/java/org/apache/avalon/merlin/kernel
                        KernelCriteria.java
               kernel/impl/src/java/org/apache/avalon/merlin/kernel/impl
                        DefaultCriteria.java DefaultFactory.java
               kernel/test/src/test/org/apache/avalon/merlin
                        MerlinEmbeddedTest.java
  Log:
  Synchronize with repo usage of the Map criteria.
  
  Revision  Changes    Path
  1.4       +31 -13    
avalon-sandbox/kernel/impl/src/java/org/apache/avalon/merlin/kernel/KernelCriteria.java
  
  Index: KernelCriteria.java
  ===================================================================
  RCS file: 
/home/cvs/avalon-sandbox/kernel/impl/src/java/org/apache/avalon/merlin/kernel/KernelCriteria.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- KernelCriteria.java       26 Nov 2003 00:50:12 -0000      1.3
  +++ KernelCriteria.java       26 Nov 2003 14:13:28 -0000      1.4
  @@ -52,6 +52,7 @@
   
   import java.net.URL;
   import java.io.File;
  +import java.util.Map;
   
   import org.apache.avalon.repository.criteria.Criteria;
   import org.apache.avalon.repository.criteria.Parameter;
  @@ -62,18 +63,19 @@
    * @author <a href="mailto:[EMAIL PROTECTED]">Stephen McConnell</a>
    * @version $Revision$ $Date$
    */
  -public interface KernelCriteria extends Criteria
  +public interface KernelCriteria extends Map
   {
       File USER_DIR = new File( System.getProperty( "user.dir" ) );
  +    File TEMP_DIR = new File( System.getProperty( "java.io.tmpdir" ) );
       File AVALON_HOME_DIR = new File( USER_DIR, ".avalon" );
       File MERLIN_HOME_DIR = new File( USER_DIR, ".merlin" );
   
      /**
       * Shared application repository root directory.
       */
  -    public static final Parameter AVALON_HOME = 
  +    public static final Parameter MERLIN_REPOSITORY = 
         new Parameter( 
  -        "avalon.home",
  +        "merlin.repository",
           File.class.getName(),
           AVALON_HOME_DIR );
   
  @@ -123,6 +125,15 @@
           USER_DIR );
   
      /**
  +    * The temp directory parameter descriptor.
  +    */
  +    public static final Parameter MERLIN_TEMP = 
  +      new Parameter( 
  +        "merlin.temp",
  +        File.class.getName(),
  +        TEMP_DIR );
  +
  +   /**
       * Base directory parameter descriptor.
       */
       public static final Parameter MERLIN_CONTEXT = 
  @@ -172,12 +183,13 @@
       */
       public static final Parameter[] PARAMS = 
         new Parameter[]{
  -           AVALON_HOME,
  +           MERLIN_REPOSITORY,
              MERLIN_HOME,
              MERLIN_SYSTEM,
              MERLIN_CONFIG,
              MERLIN_KERNEL,
              MERLIN_DIR,
  +           MERLIN_TEMP,
              MERLIN_CONTEXT,
              MERLIN_EXT,
              MERLIN_INFO,
  @@ -186,39 +198,45 @@
   
      /**
       * Return the root directory to the shared repository.
  -    * @return the avalon home root repository directory
  +    * @return the root common repository directory
       */
  -    File getAvalonHomeDirectory();
  +    File getRepositoryDirectory();
   
      /**
       * Return the root directory to the merlin installation
       * @return the merlin home directory
       */
  -    File getMerlinHomeDirectory();
  +    File getHomeDirectory();
   
      /**
       * Return the root directory to the merlin system repository
       * @return the merlin system repository directory
       */
  -    File getMerlinSystemDirectory();
  +    File getSystemDirectory();
   
      /**
       * Return the root directory to the merlin configurations
       * @return the merlin configuration directory
       */
  -    File getMerlinConfigDirectory();
  +    File getConfigDirectory();
   
      /**
       * Return the url to the kernel confiuration
       * @return the kernel configuration url
       */
  -    URL getMerlinKernelURL();
  +    URL getKernelURL();
   
      /**
       * Return the working client directory.
       * @return the working directory
       */
  -    File getMerlinWorkingDirectory();
  +    File getWorkingDirectory();
  +
  +   /**
  +    * Return the temporary client directory.
  +    * @return the temp directory
  +    */
  +    File getTempDirectory();
   
      /**
       * Return the context directory from which relative 
  @@ -227,7 +245,7 @@
       *
       * @return the working directory
       */
  -    File getMerlinContextDirectory();
  +    File getContextDirectory();
   
      /**
       * Return the anchor directory to be used when resolving 
  @@ -235,7 +253,7 @@
       *
       * @return the jar extension anchor directory
       */
  -    File getMerlinExtensionDirectory();
  +    File getExtensionDirectory();
   
      /**
       * Return info generation policy.  If TRUE the parameters 
  
  
  
  1.8       +38 -24    
avalon-sandbox/kernel/impl/src/java/org/apache/avalon/merlin/kernel/impl/DefaultCriteria.java
  
  Index: DefaultCriteria.java
  ===================================================================
  RCS file: 
/home/cvs/avalon-sandbox/kernel/impl/src/java/org/apache/avalon/merlin/kernel/impl/DefaultCriteria.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- DefaultCriteria.java      26 Nov 2003 00:50:12 -0000      1.7
  +++ DefaultCriteria.java      26 Nov 2003 14:13:28 -0000      1.8
  @@ -67,7 +67,6 @@
   import org.apache.avalon.repository.criteria.Criteria;
   import org.apache.avalon.repository.criteria.ValidationException;
   import org.apache.avalon.repository.criteria.Parameter;
  -import org.apache.avalon.repository.criteria.AbstractCriteria;
   
   import org.apache.avalon.defaults.Defaults;
   import org.apache.avalon.defaults.DefaultsFinder;
  @@ -85,7 +84,7 @@
    * @author <a href="mailto:[EMAIL PROTECTED]">Stephen McConnell</a>
    * @version $Revision$
    */
  -public class DefaultCriteria extends AbstractCriteria implements KernelCriteria
  +public class DefaultCriteria extends Criteria implements KernelCriteria
   {
       //--------------------------------------------------------------
       // static
  @@ -95,7 +94,7 @@
       private static final String MERLIN = "/merlin.properties";
   
       private static final String [] SINGLE_KEYS = {
  -           AVALON_HOME.getKey(),
  +           MERLIN_REPOSITORY.getKey(),
              MERLIN_HOME.getKey(),
              MERLIN_SYSTEM.getKey(),
              MERLIN_CONFIG.getKey(),
  @@ -168,7 +167,7 @@
               final String key = param.getKey();
               try
               {
  -                setValue( key, defaults.getProperty( key ));
  +                put( key, defaults.getProperty( key ));
               }
               catch( Exception re )
               {
  @@ -203,7 +202,7 @@
       */
       public String toString()
       {
  -        return "[merlin: " + getMap() + "]";
  +        return "[merlin: " + super.toString() + "]";
       }
   
       //--------------------------------------------------------------
  @@ -214,54 +213,63 @@
       * Return the root directory to the shared repository.
       * @return the avalon home root repository directory
       */
  -    public File getAvalonHomeDirectory()
  +    public File getRepositoryDirectory()
       {
  -        return (File) getValue( AVALON_HOME );
  +        return (File) get( MERLIN_REPOSITORY );
       }
   
      /**
       * Return the root directory to the merlin installation
       * @return the merlin home directory
       */
  -    public File getMerlinHomeDirectory()
  +    public File getHomeDirectory()
       {
  -        return (File) getValue( MERLIN_HOME );
  +        return (File) get( MERLIN_HOME );
       }
   
      /**
       * Return the root directory to the merlin system repository
       * @return the merlin system repository directory
       */
  -    public File getMerlinSystemDirectory()
  +    public File getSystemDirectory()
       {
  -        return (File) getValue( MERLIN_SYSTEM );
  +        return (File) get( MERLIN_SYSTEM );
       }
   
      /**
       * Return the root directory to the merlin configurations
       * @return the merlin configuration directory
       */
  -    public File getMerlinConfigDirectory()
  +    public File getConfigDirectory()
       {
  -        return (File) getValue( MERLIN_CONFIG );
  +        return (File) get( MERLIN_CONFIG );
       }
   
      /**
       * Return the url to the kernel confiuration
       * @return the kernel configuration url
       */
  -    public URL getMerlinKernelURL()
  +    public URL getKernelURL()
       {
  -        return (URL) getValue( MERLIN_KERNEL );
  +        return (URL) get( MERLIN_KERNEL );
       }
   
      /**
       * Return the working client directory.
       * @return the working directory
       */
  -    public File getMerlinWorkingDirectory()
  +    public File getWorkingDirectory()
       {
  -        return (File) getValue( MERLIN_DIR );
  +        return (File) get( MERLIN_DIR );
  +    }
  +
  +   /**
  +    * Return the temporary directory.
  +    * @return the temp directory
  +    */
  +    public File getTempDirectory()
  +    {
  +        return (File) get( MERLIN_TEMP );
       }
   
      /**
  @@ -271,9 +279,9 @@
       *
       * @return the working directory
       */
  -    public File getMerlinContextDirectory()
  +    public File getContextDirectory()
       {
  -        return (File) getValue( MERLIN_CONTEXT );
  +        return (File) get( MERLIN_CONTEXT );
       }
   
      /**
  @@ -282,9 +290,9 @@
       *
       * @return the jar extension anchor directory
       */
  -    public File getMerlinExtensionDirectory()
  +    public File getExtensionDirectory()
       {
  -        return (File) getValue( MERLIN_EXT );
  +        return (File) get( MERLIN_EXT );
       }
   
      /**
  @@ -295,7 +303,9 @@
       */
       public boolean isInfoEnabled()
       {
  -        return ((Boolean)getValue( MERLIN_INFO )).booleanValue();
  +        Boolean value = (Boolean) get( MERLIN_INFO );
  +        if( null != value ) return value.booleanValue();
  +        return false;
       }
   
      /**
  @@ -306,7 +316,9 @@
       */
       public boolean isDebugEnabled()
       {
  -        return ((Boolean)getValue( MERLIN_DEBUG )).booleanValue();
  +        Boolean value = (Boolean) get( MERLIN_DEBUG );
  +        if( null != value ) return value.booleanValue();
  +        return false;
       }
   
      /**
  @@ -318,7 +330,9 @@
       */
       public boolean isServerEnabled()
       {
  -        return ((Boolean)getValue( MERLIN_SERVER )).booleanValue();
  +        Boolean value = (Boolean) get( MERLIN_SERVER );
  +        if( null != value ) return value.booleanValue();
  +        return false;
       }
   
       //--------------------------------------------------------------
  
  
  
  1.6       +239 -7    
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.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- DefaultFactory.java       24 Nov 2003 21:26:36 -0000      1.5
  +++ DefaultFactory.java       26 Nov 2003 14:13:28 -0000      1.6
  @@ -3,16 +3,42 @@
   package org.apache.avalon.merlin.kernel.impl;
   
   import java.io.File;
  +import java.io.InputStream;
   import java.net.URL;
   import java.util.Map;
  +import java.util.ArrayList;
   
   import org.apache.avalon.excalibur.i18n.ResourceManager;
   import org.apache.avalon.excalibur.i18n.Resources;
  +
  +import org.apache.avalon.framework.logger.Logger;
  +import org.apache.avalon.framework.configuration.Configuration;
  +import org.apache.avalon.framework.configuration.ConfigurationException;
  +import org.apache.avalon.framework.configuration.DefaultConfiguration;
  +import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
  +
   import org.apache.avalon.repository.criteria.Parameter;
   import org.apache.avalon.repository.criteria.Criteria;
   import org.apache.avalon.repository.criteria.ValidationException;
   import org.apache.avalon.repository.criteria.Factory;
   
  +import org.apache.avalon.composition.logging.LoggingManager;
  +import org.apache.avalon.composition.logging.LoggingDescriptor;
  +import org.apache.avalon.composition.logging.TargetDescriptor;
  +import org.apache.avalon.composition.logging.TargetProvider;
  +import org.apache.avalon.composition.logging.impl.DefaultLoggingManager;
  +import org.apache.avalon.composition.logging.impl.FileTargetProvider;
  +import org.apache.avalon.composition.data.builder.XMLDeploymentProfileCreator;
  +import org.apache.avalon.composition.data.CategoriesDirective;
  +import org.apache.avalon.composition.data.ContainmentProfile;
  +import org.apache.avalon.composition.data.TargetDirective;
  +
  +import org.apache.avalon.merlin.kernel.KernelException;
  +import org.apache.avalon.merlin.kernel.KernelCriteria;
  +
  +import org.xml.sax.InputSource;
  +
  +
   /**
    * The DefaultFactory provides support for the establishment of a 
    * new merlin kernel.
  @@ -26,31 +52,237 @@
       private static Resources REZ =
           ResourceManager.getPackageResources( DefaultFactory.class );
   
  +    private static final String CATEGORY_NAME = "context";
  +
  +    private static final XMLDeploymentProfileCreator CREATOR = 
  +      new XMLDeploymentProfileCreator();
  +
  +    //--------------------------------------------------------------------------
  +    // state
  +    //--------------------------------------------------------------------------
  +
  +    private Logger m_logger;
  +
  +    private Logger m_kernelLogger;
  +
  +    private LoggingManager m_logging;
  + 
       //--------------------------------------------------------------------------
       // Factory
       //--------------------------------------------------------------------------
   
  -    public Criteria createDefaultCriteria()
  +    public Map createDefaultCriteria()
       {
           return new DefaultCriteria();
       }
   
  -    public Object create()
  +    public Object create() throws Exception
       {
           return create( createDefaultCriteria() );
       }
   
  -    public Object create( Criteria criteria )
  +    public Object create( Map map ) throws Exception
       {
  -        return create( criteria.getMap() );
  +        KernelCriteria criteria = null;
  +        if( map instanceof KernelCriteria) 
  +        {
  +            criteria = (KernelCriteria) map;
  +        }
  +        else
  +        {
  +            final String error = 
  +             "Suppied map was not created by this factory. ";
  +            throw new IllegalArgumentException( error );
  +        }
  +
  +        //
  +        // create the kernel configuration
  +        //
  +
  +        URL kernelURL = (URL) criteria.getKernelURL();
  +        Configuration kernelConfig = getKernelConfiguration( kernelURL );
  +
  +        //
  +        // create the logging subsystem
  +        //
  +
  +        Configuration loggingConfig = kernelConfig.getChild( "logging" );
  +        LoggingDescriptor loggingDescriptor = createLoggingDescriptor( 
loggingConfig );
  +
  +        m_logging = 
  +          new DefaultLoggingManager( 
  +            criteria.getWorkingDirectory(), 
  +            loggingDescriptor, 
  +            criteria.isDebugEnabled() );
  +
  +        m_kernelLogger = m_logging.getLoggerForCategory( 
loggingDescriptor.getName() );
  +        m_logger = m_kernelLogger.getChildLogger( CATEGORY_NAME );
  +        getLogger().debug( "logging system established" );
  +
  +        return null;
       }
   
  -    public Object create( Map map )
  +
  +    /**
  +     * Utility method to create a new logging descriptor from a
  +     * configuration instance.
  +     * @param config a configuration defining the logging descriptor
  +     * @return the logging descriptor
  +     * @exception ConfigurationException if the configuration is
  +     *   incomplete
  +     */
  +    private LoggingDescriptor createLoggingDescriptor(
  +        Configuration config )
  +        throws KernelException
       {
  +        final String name = config.getAttribute( "name", "kernel" );
  +        CategoriesDirective categories = null;
  +        try
  +        {
  +            categories = CREATOR.getCategoriesDirective( config, name );
  +        }
  +        catch( Throwable e )
  +        {
  +            final String error =
  +              "Invalid logging directive.";
  +            throw new KernelException( error, e );
  +        }
  +
           //
  -        // create the kernel with the supplied values
  +        // create any custom targets declared in the kernel directive
           //
   
  -        return null;
  +        ArrayList list = new ArrayList();
  +        Configuration[] configs = config.getChildren( "target" );
  +        for( int i = 0; i < configs.length; i++ )
  +        {
  +            Configuration c = configs[ i ];
  +            try
  +            {
  +                list.add( createTargetDescriptor( c ) );
  +            }
  +            catch( Throwable e )
  +            {
  +                final String error = 
  +                  "Invalid target descriptor.";
  +                throw new KernelException( error, e );
  +            }
  +        }
  +
  +        TargetDescriptor[] targets =
  +            (TargetDescriptor[])list.toArray(
  +                new TargetDescriptor[ 0 ] );
  +
  +        //
  +        // return the logging descriptor
  +        //
  +
  +        return new LoggingDescriptor(
  +          categories.getName(), 
  +          categories.getPriority(), 
  +          categories.getTarget(), 
  +          categories.getCategories(), 
  +          targets );
  +    }
  +
  +
  +   /**
  +    * Create the kernel configuration using a supplied url.  If the supplied
  +    * url is null the implementation will attempt to resolve a "/kernel.xml" 
  +    * resource within the deployment unit.
  +    *
  +    * @param url the location of the kernel confiuration
  +    * @return the kernel configuration
  +    * @exception if the configuration could not be resolved
  +    */
  +    private Configuration getKernelConfiguration( URL url ) throws Exception
  +    {
  +        if( null != url )
  +        {
  +            try
  +            {
  +                return new DefaultConfiguration( 
  +                  "kernel", 
  +                  DefaultFactory.class.getName() );
  +            }
  +            catch( Throwable e )
  +            {
  +                final String error = 
  +                  "Unable to build kernel configuration from the supplied url: " + 
url;
  +                throw new KernelException( error, e );
  +            }
  +        }
  +
  +        try
  +        {
  +            final InputStream stream =
  +            DefaultFactory.class.getClassLoader().getResourceAsStream( 
  +              "kernel.xml" );
  +            final InputSource source = new InputSource( stream );
  +            DefaultConfigurationBuilder builder = 
  +              new DefaultConfigurationBuilder();
  +            return builder.build( source );
  +        }
  +        catch( Throwable ee )
  +        {
  +            final String error = 
  +              "Internal error while attempting to build default kernel "
  +              + "configuration from the kernel spec: " + url;
  +            throw new KernelException( error, ee );
  +        }
  +    }
  +
  +    private Logger getLogger()
  +    {
  +        return m_logger;
  +    }
  +
  +    /**
  +     * Utility method to create a new target descriptor from a
  +     * configuration instance.
  +     * @param config a configuration defining the target descriptor
  +     * @return the logging target descriptor
  +     * @exception ConfigurationException if the configuration is
  +     *   incomplete
  +     */
  +    private TargetDescriptor createTargetDescriptor( Configuration config )
  +        throws ConfigurationException
  +    {
  +        final String name = config.getAttribute( "name" );
  +        if( config.getChildren().length == 0 )
  +        {
  +            throw new ConfigurationException(
  +                "missing target provider element in '"
  +                + config.getName() + "'." );
  +        }
  +
  +        final Configuration c = config.getChildren()[ 0 ];
  +        TargetProvider provider = null;
  +        if( c.getName().equals( "file" ) )
  +        {
  +            provider = createFileTargetProvider( c );
  +        }
  +        else
  +        {
  +            throw new ConfigurationException(
  +                "Unrecognized provider: " + c.getName() + " in " + config.getName() 
);
  +        }
  +        return new TargetDescriptor( name, provider );
  +    }
  +
  +    /**
  +     * Utility method to create a new file target descriptor from a
  +     * configuration instance.
  +     * @param config a configuration defining the file target descriptor
  +     * @return the file target descriptor
  +     * @exception ConfigurationException if the configuration is
  +     *   incomplete
  +     */
  +    private FileTargetProvider createFileTargetProvider( Configuration config )
  +        throws ConfigurationException
  +    {
  +        String file = config.getAttribute( "location" );
  +        return new FileTargetProvider( file );
       }
  +
   }
  
  
  
  1.4       +8 -7      
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.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- MerlinEmbeddedTest.java   26 Nov 2003 00:50:12 -0000      1.3
  +++ MerlinEmbeddedTest.java   26 Nov 2003 14:13:28 -0000      1.4
  @@ -52,6 +52,7 @@
   
   import java.io.File;
   import java.net.URL;
  +import java.util.Map;
   import javax.naming.directory.Attributes;
   import javax.naming.directory.Attribute;
   
  @@ -90,10 +91,10 @@
       {
           String[] bootstrap = getBootstrapRepositorySet();
           Factory factory = new InitialRepositoryFactory( bootstrap );
  -        Criteria criteria = factory.createDefaultCriteria();
  +        Map criteria = factory.createDefaultCriteria();
           String[] repositories = getWorkingRepositorySet();
  -        criteria.setValue( "avalon.repository.remote.url", repositories );
  -        m_repository = (Repository) factory.create( criteria.getMap() ) ;
  +        criteria.put( "avalon.repository.remote.url", repositories );
  +        m_repository = (Repository) factory.create( criteria ) ;
       }
   
       //----------------------------------------------------------------------
  @@ -117,20 +118,20 @@
           ClassLoader loader = m_repository.getClassLoader( parent, implementation );
           Class c = loader.loadClass( target.getFactoryClassname() );
           Factory factory = (Factory) c.newInstance();
  -        Criteria criteria = (Criteria) factory.createDefaultCriteria();
  +        Map criteria = (Map) factory.createDefaultCriteria();
           assertNotNull( "criteria", criteria );
   
           //
           // list the supplied default criteria
           //
   
  -        String[] keys = criteria.getKeys();
  +        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.getValue( key );
  +            Object arg = criteria.get( key );
               System.out.println( "   ${" + key + "} == " + arg );
           }
   
  
  
  

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

Reply via email to