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, 03 Dec 2003 04:38:02 GMT
mcconnell    2003/12/02 20:38:02

  Modified:    kernel/impl/src/java/org/apache/avalon/merlin/impl
                        DefaultCriteria.java
               kernel/test merlin.properties
               kernel/test/src/test/org/apache/avalon/merlin
                        MerlinEmbeddedTest.java
  Log:
  Add local and home properties resolution.
  
  Revision  Changes    Path
  1.4       +155 -70   avalon-sandbox/kernel/impl/src/java/org/apache/avalon/merlin/impl/DefaultCriteria.java
  
  Index: DefaultCriteria.java
  ===================================================================
  RCS file: /home/cvs/avalon-sandbox/kernel/impl/src/java/org/apache/avalon/merlin/impl/DefaultCriteria.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DefaultCriteria.java	2 Dec 2003 23:20:02 -0000	1.3
  +++ DefaultCriteria.java	3 Dec 2003 04:38:02 -0000	1.4
  @@ -51,6 +51,7 @@
   package org.apache.avalon.merlin.impl;
   
   import java.io.File;
  +import java.io.FileInputStream;
   import java.io.InputStream;
   import java.io.IOException;
   import java.net.URL;
  @@ -89,13 +90,19 @@
       // static
       //--------------------------------------------------------------
   
  -    private static final String AVALON = "/avalon.properties";
  -    private static final String MERLIN = "/merlin.properties";
  +    private static final String AVALON = "avalon.properties";
  +    private static final String MERLIN = "merlin.properties";
   
  -    private static final File USER_DIR = new File( System.getProperty( "user.dir" ) );
  -    private static final File TEMP_DIR = new File( System.getProperty( "java.io.tmpdir"
) );
  -    private static final File AVALON_HOME_DIR = new File( USER_DIR, ".avalon" );
  -    private static final File MERLIN_HOME_DIR = new File( USER_DIR, ".merlin" );
  +    private static final File USER_DIR = 
  +      getBaseDirectory();
  +    private static final File USER_HOME = 
  +      new File( System.getProperty( "user.home" ) );
  +    private static final File TEMP_DIR = 
  +      new File( System.getProperty( "java.io.tmpdir" ) );
  +    private static final File AVALON_HOME_DIR = 
  +      new File( USER_HOME, ".avalon" );
  +    private static final File MERLIN_HOME_DIR = 
  +      new File( USER_HOME, ".merlin" );
   
      /**
       * Shared application repository root directory.
  @@ -194,8 +201,8 @@
           new Parameter( 
             MERLIN_CONFIG,
             File.class, new File( MERLIN_HOME_DIR, "config" ) ),
  -        new URLTokenizer( 
  -          MERLIN_DEPLOYMENT, ",", new URL[0] ),
  +        new DeploymentURLTokenizer( 
  +          MERLIN_DEPLOYMENT, ",", new URL[0], getBaseDirectory() ),
           new Parameter( 
             MERLIN_KERNEL, URL.class, null ),
           new Parameter( 
  @@ -232,13 +239,19 @@
       {
           super( PARAMS );
   
  -        Properties avalon = getStaticProperties( AVALON );
  -        Properties merlin = getStaticProperties( MERLIN );
  +        Properties avalonStatic = getStaticProperties( AVALON );
  +        Properties merlinStatic = getStaticProperties( MERLIN );
  +
           Properties env = getEnvinronment();
           Properties system = System.getProperties();
  -
  -        printProperties( avalon, "avalon" );
  -        printProperties( merlin, "merlin" );
  +        Properties avalonHome = getLocalProperties( USER_HOME, AVALON );
  +        Properties merlinHome = getLocalProperties( USER_HOME, MERLIN );
  +        Properties avalonWork = getLocalProperties( USER_DIR, AVALON );
  +        Properties merlinWork = getLocalProperties( USER_DIR, MERLIN );
  +
  +        printProperties( avalonStatic, "avalon" );
  +        printProperties( merlinStatic, "merlin" );
  +        printProperties( merlinWork, "merlin-work" );
           printProperties( env, "environment" );
   
           //
  @@ -246,19 +259,29 @@
           // macro expand the values.
           //
   
  +        final Properties[] parameters = 
  +          new Properties[] { 
  +            avalonStatic, 
  +            merlinStatic, 
  +            env, 
  +            avalonHome, 
  +            avalonWork, 
  +            merlinHome, 
  +            merlinWork };
  +
           final DefaultsFinder[] finders = 
             new DefaultsFinder[]{
               new SimpleDefaultsFinder( 
  -              new Properties[] { merlin, avalon, env }, 
  +              parameters, 
                 false ), 
               new SystemDefaultsFinder() 
             };
           
           Defaults defaults = new Defaults( SINGLE_KEYS, MULTI_VALUE_KEYS, finders );
  -        printProperties( defaults, "defaults (unresolved)" );
  -        Defaults.macroExpand( defaults, new Properties[]{ system, avalon, env } );
  -        Defaults.macroExpand( defaults, new Properties[]{ system, avalon, env } );
           printProperties( defaults, "defaults (resolved)" );
  +        Defaults.macroExpand( defaults, new Properties[]{ system, avalonStatic, env } );
  +        Defaults.macroExpand( defaults, new Properties[]{ system, avalonStatic, env } );
  +        printProperties( defaults, "defaults (expanded)" );
           System.out.print( "" );
   
           //
  @@ -457,14 +480,17 @@
   
       private void printProperties( Properties properties, String label )
       {
  -        System.out.print( "\n------------ " + label.toUpperCase()
  -          + "--------------------------------".substring( label.length() ) + "\n\n" );
  +        System.out.print( "\n------------ " 
  +          + label.toUpperCase()
  +          + "--------------------------------".substring( label.length() ) 
  +          + "\n\n" );
           if( null == properties ) return;
           Enumeration names = properties.propertyNames();
           while( names.hasMoreElements() )
           {
               String name = (String) names.nextElement();
  -            System.out.println( "   ${" + name + "} == " + properties.getProperty( name
) );
  +            System.out.println( "   ${" + name + "} == " 
  +            + properties.getProperty( name ) );
           }
       }
   
  @@ -473,10 +499,18 @@
           try
           {
               Properties properties = new Properties();
  -            setProperty( properties, "avalon.home", Env.getEnvVariable( "AVALON_HOME" )
);
  -            setProperty( properties, "maven.home", Env.getEnvVariable( "MAVEN_HOME" ) );
  -            setProperty( properties, "maven.home.local", Env.getEnvVariable( "MAVEN_HOME_LOCAL"
) );
  -            setProperty( properties, "merlin.home", Env.getEnvVariable( "MERLIN_HOME" )
);
  +            setProperty( 
  +              properties, "avalon.home", 
  +              Env.getEnvVariable( "AVALON_HOME" ) );
  +            setProperty( 
  +              properties, "merlin.home", 
  +              Env.getEnvVariable( "MERLIN_HOME" ) );
  +            setProperty( 
  +              properties, "maven.home", 
  +              Env.getEnvVariable( "MAVEN_HOME" ) );
  +            setProperty( 
  +              properties, "maven.home.local", 
  +              Env.getEnvVariable( "MAVEN_HOME_LOCAL" ) );
               return properties;
           }
           catch( Throwable e )
  @@ -487,11 +521,33 @@
           }
       }
   
  -    private void setProperty( Properties properties, String key, String value )
  +    private void setProperty( 
  +      Properties properties, String key, String value )
       {
           if( null != value ) properties.setProperty( key, value );
       }
   
  +    private Properties getLocalProperties( 
  +      File dir, String filename ) 
  +    {
  +        Properties properties = new Properties();
  +        if( null == dir ) return properties;
  +        File file = new File( dir, filename );
  +        if( !file.exists() ) return properties;
  +        try
  +        {
  +            properties.load( new FileInputStream( file ) );
  +            return properties;
  +        }
  +        catch( Throwable e )
  +        {
  +            final String error = 
  +              "Unexpected exception while attempting to read properties from: " 
  +              + file;
  +            throw new KernelRuntimeException( error, e );
  +        }
  +    }
  +
      /**
       * Read in the static defined properties that contribute to 
       * the default context value establishment.
  @@ -500,77 +556,106 @@
       * @exception KernelRuntimeException if a error occurs while attempt to 
       *    load the property resource
       */
  -    private Properties getStaticProperties( String path ) throws KernelRuntimeException
  +    private Properties getStaticProperties( String path ) 
  +      throws KernelRuntimeException
       {
           try
           {
  -            return Defaults.getStaticProperties( DefaultCriteria.class, path );
  +            final String resource = "/" + path;
  +            return Defaults.getStaticProperties( 
  +              DefaultCriteria.class, resource );
           }
           catch ( IOException e )
           {
               throw new KernelRuntimeException( 
  -             "Failed to load implementation defaults resource: "
  +             "Failed to load implementation defaults resource: /"
                + path, e );
           }
       }
   
  -    private static class URLTokenizer extends Parameter
  +    private static class DeploymentURLTokenizer extends Parameter
       {
           private final Class m_base = URL[].class;
           private final String m_delimiter;
  +        private final File m_basedir;
   
           /**
            * Transform a string to a url array.
            * @param object the value to transform
            * @return the transformed object
            */
  -         public URLTokenizer( 
  -           final String key, final String delimiter, URL[] defaults ) 
  +         public DeploymentURLTokenizer( 
  +           final String key, final String delimiter, 
  +           URL[] defaults, File base ) 
            {
                super( key, URL[].class, defaults );
                m_delimiter = delimiter;
  +             m_basedir = base;
            }
   
            public Object resolve( Object value ) 
              throws FactoryException
            {
  -             if( value == null ) return null;
  -             if( value instanceof URL[] )
  -             {
  -                 return value;
  -             }
  -             else if( value instanceof String )
  -             {
  -                 ArrayList list = new ArrayList();
  -                 String s = (String) value;
  -                 StringTokenizer tokenizer = 
  -                   new StringTokenizer( s, m_delimiter );
  -                 while( tokenizer.hasMoreTokens() )
  -                 {
  -                     String v = tokenizer.nextToken();
  -                     try
  -                     {
  -                         list.add( new URL( v ) );
  -                     }
  -                     catch( Throwable e )
  -                     {
  -                         final String error = 
  -                           "Unable to transform the token: ["
  -                           + v
  -                           + " due to an unexpected error.";
  -                         throw new FactoryException( error, e );
  -                     }
  -                 }
  -                 return list.toArray( new URL[0] );
  -             }
  -             else
  -             {
  -                 final String error = 
  -                   "Don't know how to transform an instance of [" 
  -                   + value.getClass().getName() 
  -                   + " to a URL[].";
  -                 throw new FactoryException( error );
  -             }
  -         }
  +            if( value == null ) return null;
  +            if( value instanceof URL[] )
  +            {
  +                return value;
  +            }
  +            else if( value instanceof String )
  +            {
  +                ArrayList list = new ArrayList();
  +                String s = (String) value;
  +                StringTokenizer tokenizer = 
  +                  new StringTokenizer( s, m_delimiter );
  +                while( tokenizer.hasMoreTokens() )
  +                {
  +                    String v = tokenizer.nextToken();
  +                    try
  +                    {
  +                        URL url = resolveURL( m_basedir, v );
  +                        list.add( url );
  +                    }
  +                    catch( Throwable e )
  +                    {
  +                        final String error = 
  +                          "Unable to transform the token: ["
  +                          + v
  +                          + " due to an unexpected error.";
  +                        throw new FactoryException( error, e );
  +                    }
  +                }
  +                return list.toArray( new URL[0] );
  +            }
  +            else
  +            {
  +                final String error = 
  +                  "Don't know how to transform an instance of [" 
  +                  + value.getClass().getName() 
  +                  + " to a URL[].";
  +                throw new FactoryException( error );
  +            }
  +        }
  +
  +        private URL resolveURL( File base, String value ) throws Exception
  +        {
  +            try
  +            {
  +                return new URL( value );
  +            }
  +            catch( Exception e )
  +            {
  +                return new File( base, value ).toURL();
  +            }
  +        }
  +    }
  +
  +    private static File getBaseDirectory()
  +    {
  +        String base = System.getProperty( "basedir" );
  +        if( null != base )
  +        {
  +            return new File( base );
  +        }
  +        return new File( System.getProperty( "user.dir" ) );
       }
   }
  
  
  
  1.2       +2 -0      avalon-sandbox/kernel/test/merlin.properties
  
  Index: merlin.properties
  ===================================================================
  RCS file: /home/cvs/avalon-sandbox/kernel/test/merlin.properties,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- merlin.properties	30 Nov 2003 05:57:17 -0000	1.1
  +++ merlin.properties	3 Dec 2003 04:38:02 -0000	1.2
  @@ -1,2 +1,4 @@
   
  +merlin.info = true
   merlin.debug = true
  +merlin.deployment = conf/test.block,conf/test-2.block
  
  
  
  1.10      +45 -4     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.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- MerlinEmbeddedTest.java	2 Dec 2003 23:20:02 -0000	1.9
  +++ MerlinEmbeddedTest.java	3 Dec 2003 04:38:02 -0000	1.10
  @@ -51,7 +51,11 @@
   package org.apache.avalon.merlin;
   
   import java.io.File;
  +import java.io.FileInputStream;
  +import java.io.IOException;
   import java.util.Map;
  +import java.util.Properties;
  +import java.util.Enumeration;
   
   import junit.framework.TestCase;
   
  @@ -107,9 +111,7 @@
               Builder builder = new DefaultBuilder( context, artifact );
               Factory factory = builder.getFactory();
               Map criteria = factory.createDefaultCriteria();
  -            criteria.put( "merlin.debug", new Boolean( true ) );
  -
  -            System.out.println( "\n----------- INSTANTIATION ------------\n" );
  +            applyLocalProperties( criteria );
   
               Object app = factory.create( criteria );
               assertNotNull( "app", app );
  @@ -126,6 +128,35 @@
       // utilities
       //----------------------------------------------------------------------
   
  +    private void applyLocalProperties( Map criteria ) throws IOException
  +    {
  +        File base = getBaseDirectory();
  +        Properties properties = 
  +          getLocalProperties( base, "merlin.properties" );
  +        Enumeration keys = properties.keys();
  +        while( keys.hasMoreElements() )
  +        {
  +            final String key = (String) keys.nextElement();
  +            if( key.startsWith( "merlin." ) )
  +            {
  +                String value = properties.getProperty( key );
  +                criteria.put( key, value );
  +            }
  +        }
  +    }
  +
  +    private Properties getLocalProperties( 
  +      File dir, String filename ) throws IOException
  +    {
  +        Properties properties = new Properties();
  +        if( null == dir ) return properties;
  +        File file = new File( dir, filename );
  +        if( !file.exists() ) return properties;
  +        properties.load( new FileInputStream( file ) );
  +        return properties;
  +    }
  +
  +
       private static File getMavenRepositoryDirectory()
       {
           return new File( getMavenHomeDirectory(), "repository" );
  @@ -163,6 +194,16 @@
                 ExceptionHelper.packException( error, e, true );
               throw new RuntimeException( message );
           }
  +    }
  +
  +    private File getBaseDirectory()
  +    {
  +        String base = System.getProperty( "basedir" );
  +        if( null != base )
  +        {
  +            return new File( base );
  +        }
  +        return new File( System.getProperty( "user.dir" ) );
       }
   
       private static String[] getDefaultHosts()
  
  
  

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


Mime
View raw message