mcconnell    2003/12/14 12:07:45

  Modified:    merlin/kernel/cli/src/java/org/apache/avalon/merlin/cli
                        Main.java
               merlin/kernel/impl/src/java/org/apache/avalon/merlin/impl
                        DefaultCriteria.java
               merlin/kernel/plugin/src/java/org/apache/avalon/merlin/tools
                        MerlinBean.java
               merlin/kernel/unit/src/java/org/apache/avalon/merlin/unit
                        AbstractMerlinTestCase.java
               repository/main/src/java/org/apache/avalon/repository/main
                        DefaultBuilder.java DefaultInitialContext.java
  Log:
  Add additional system level in property resolution.
  
  Revision  Changes    Path
  1.9       +2 -1      
avalon/merlin/kernel/cli/src/java/org/apache/avalon/merlin/cli/Main.java
  
  Index: Main.java
  ===================================================================
  RCS file: 
/home/cvs/avalon/merlin/kernel/cli/src/java/org/apache/avalon/merlin/cli/Main.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- Main.java 14 Dec 2003 11:58:26 -0000      1.8
  +++ Main.java 14 Dec 2003 20:07:45 -0000      1.9
  @@ -467,6 +467,7 @@
           {
               return DefaultBuilder.createImplementationArtifact( 
                   Main.class.getClassLoader(), 
  +                getMerlinHome(),
                   getBaseDirectory(), 
                   MERLIN_PROPERTIES, 
                   IMPLEMENTATION_KEY );
  
  
  
  1.11      +95 -59    
avalon/merlin/kernel/impl/src/java/org/apache/avalon/merlin/impl/DefaultCriteria.java
  
  Index: DefaultCriteria.java
  ===================================================================
  RCS file: 
/home/cvs/avalon/merlin/kernel/impl/src/java/org/apache/avalon/merlin/impl/DefaultCriteria.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- DefaultCriteria.java      14 Dec 2003 14:09:59 -0000      1.10
  +++ DefaultCriteria.java      14 Dec 2003 20:07:45 -0000      1.11
  @@ -94,10 +94,9 @@
       //--------------------------------------------------------------
   
       private static final String AVALON_PROPERTIES = "avalon.properties";
  -    private static final String MERLIN = "merlin.properties";
  +    private static final String MERLIN_PROPERTIES = "merlin.properties";
   
  -    private static final File USER_DIR = 
  -      getBaseDirectory();
  +    private static final File USER_DIR = getBaseDirectory();
   
       private static final File USER_HOME = 
         new File( System.getProperty( "user.home" ) );
  @@ -111,16 +110,34 @@
       private static final File MERLIN_HOME_DIR = 
         getMerlinHomeDirectory();
   
  +   /**
  +    * Return the avalon home directory using the ${avalon.home}, AVALON_HOME,
  +    * and fallback ${user.home}/.avalon as the search order.
  +    * @return the avalon home directory
  +    */
       private static File getAvalonHomeDirectory()
       {
           return getEnvironment( "AVALON_HOME", "avalon.home", ".avalon" );
       }
   
  +   /**
  +    * Return the merlin home directory using the ${merlin.home}, MERLIN_HOME,
  +    * and fallback ${user.home}/.merlin as the search order.
  +    * @return the merlin home directory
  +    */
       private static File getMerlinHomeDirectory()
       {
           return getEnvironment( "MERLIN_HOME", "merlin.home", ".merlin" );
       }
   
  +   /**
  +    * Return a directory taking into account a supplied env symbol, 
  +    * a property key and a property filename.  Use the supplied key
  +    * to locate a system property with falback to the supplied 
  +    * sysbol, with fallback to the supplied path relative to ${user.home}.
  +    * 
  +    * @return the derived directory
  +    */
       private static File getEnvironment( String symbol, String key, String path )
       {
           try
  @@ -150,57 +167,52 @@
   
      /**
       * The factory parameters template.
  +    * @return the set of parameters constraining the criteria
       */
       private static Parameter[] buildParameters( InitialContext context )
       { 
           return new Parameter[]{
  -        new Parameter( 
  -          MERLIN_REPOSITORY,
  -          File.class, new File( AVALON_HOME_DIR, "repository" ) ),
  -        new Parameter( 
  -          MERLIN_HOME,
  -          File.class, MERLIN_HOME_DIR ),
  -        new Parameter( 
  -          MERLIN_SYSTEM,
  -          File.class, new File( MERLIN_HOME_DIR, "system" ) ),
  -        new Parameter( 
  -          MERLIN_CONFIG,
  -          File.class, new File( MERLIN_HOME_DIR, "config" ) ),
  -        new PackedParameter( 
  -          MERLIN_INSTALL, ",", new String[0] ),
  -        new PackedParameter( 
  -          MERLIN_DEPLOYMENT, ",", new String[0] ),
  -        new Parameter( 
  -          MERLIN_KERNEL, URL.class, null ),
  -        new Parameter( 
  -          MERLIN_OVERRIDE, String.class, null ),
  -        new Parameter( 
  -          MERLIN_DIR, File.class, context.getInitialWorkingDirectory() ),
  -        new Parameter( 
  -          MERLIN_TEMP, File.class, TEMP_DIR ),
  -        new Parameter( 
  -          MERLIN_CONTEXT, File.class, null ),
  -        new Parameter( 
  -          MERLIN_ANCHOR, File.class, null ),
  -        new Parameter( 
  -          MERLIN_INFO, Boolean.class, new Boolean( false ) ),
  -        new Parameter( 
  -          MERLIN_DEBUG, Boolean.class, new Boolean( false ) ),
  -        new Parameter( 
  -          MERLIN_SERVER, Boolean.class, new Boolean( false ) ),
  -        new Parameter( 
  -          MERLIN_AUTOSTART, Boolean.class, new Boolean( true ) ),
  -        new Parameter( 
  -          MERLIN_LANG, String.class, null )
  -      };
  +            new Parameter( 
  +              MERLIN_REPOSITORY,
  +              File.class, new File( AVALON_HOME_DIR, "repository" ) ),
  +            new Parameter( 
  +              MERLIN_HOME,
  +              File.class, MERLIN_HOME_DIR ),
  +            new Parameter( 
  +              MERLIN_SYSTEM,
  +              File.class, new File( MERLIN_HOME_DIR, "system" ) ),
  +            new Parameter( 
  +              MERLIN_CONFIG,
  +              File.class, new File( MERLIN_HOME_DIR, "config" ) ),
  +            new PackedParameter( 
  +              MERLIN_INSTALL, ",", new String[0] ),
  +            new PackedParameter( 
  +              MERLIN_DEPLOYMENT, ",", new String[0] ),
  +            new Parameter( 
  +              MERLIN_KERNEL, URL.class, null ),
  +            new Parameter( 
  +              MERLIN_OVERRIDE, String.class, null ),
  +            new Parameter( 
  +              MERLIN_DIR, File.class, context.getInitialWorkingDirectory() ),
  +            new Parameter( 
  +              MERLIN_TEMP, File.class, TEMP_DIR ),
  +            new Parameter( 
  +              MERLIN_CONTEXT, File.class, null ),
  +            new Parameter( 
  +              MERLIN_ANCHOR, File.class, null ),
  +            new Parameter( 
  +              MERLIN_INFO, Boolean.class, new Boolean( false ) ),
  +            new Parameter( 
  +              MERLIN_DEBUG, Boolean.class, new Boolean( false ) ),
  +            new Parameter( 
  +              MERLIN_SERVER, Boolean.class, new Boolean( false ) ),
  +            new Parameter( 
  +              MERLIN_AUTOSTART, Boolean.class, new Boolean( true ) ),
  +            new Parameter( 
  +              MERLIN_LANG, String.class, null )
  +         };
       }
   
  -    //private static final String [] SINGLE_KEYS = 
  -    //  Parameter.getKeys( PARAMS );
  -
  -    //private static final String[] MULTI_VALUE_KEYS = 
  -    //  new String[0];
  -
       //--------------------------------------------------------------
       // immutable state
       //--------------------------------------------------------------
  @@ -225,7 +237,7 @@
           //
   
           Properties avalonStatic = getStaticProperties( AVALON_PROPERTIES );
  -        Properties merlinStatic = getStaticProperties( MERLIN );
  +        Properties merlinStatic = getStaticProperties( MERLIN_PROPERTIES );
   
           //
           // then comes environment variables
  @@ -240,11 +252,22 @@
           Properties system = System.getProperties();
   
           //
  +        // get the application properties
  +        //
  +
  +        Properties avalonSystem = 
  +          getLocalProperties( getAvalonHomeDirectory(), AVALON_PROPERTIES );
  +        Properties merlinSystem = 
  +          getLocalProperties( getMerlinHomeDirectory(), MERLIN_PROPERTIES );
  +
  +        //
           // ${user.home} overrides environment
           //
   
  -        Properties avalonHome = getLocalProperties( USER_HOME, AVALON_PROPERTIES );
  -        Properties merlinHome = getLocalProperties( USER_HOME, MERLIN );
  +        Properties avalonHome = 
  +          getLocalProperties( USER_HOME, AVALON_PROPERTIES );
  +        Properties merlinHome = 
  +          getLocalProperties( USER_HOME, MERLIN_PROPERTIES );
   
           //
           // and ${merlin.dir} overrides ${user.home}
  @@ -254,7 +277,7 @@
           Properties avalonWork = 
             getLocalProperties( work, AVALON_PROPERTIES );
           Properties merlinWork = 
  -          getLocalProperties( work, MERLIN );
  +          getLocalProperties( work, MERLIN_PROPERTIES );
   
           //
           // Create the finder (discovery policy), construct the defaults, and
  @@ -265,6 +288,8 @@
             new Properties[] { 
               avalonStatic, 
               merlinStatic, 
  +            avalonSystem, 
  +            merlinSystem, 
               env, 
               avalonHome, 
               avalonWork, 
  @@ -307,7 +332,7 @@
           {
               Parameter param = params[i];
               final String key = param.getKey();
  -            if( !key.equals( "merlin.dir" ) )
  +            if( !key.equals( "merlin.dir" ) && !key.equals( "merlin.implementation" 
))
               {
                   try
                   {
  @@ -330,11 +355,22 @@
               Throwable[] throwables = 
                 (Throwable[]) errors.toArray( new Throwable[0] );
               
  -            final String report = 
  -              "One or more errors occured while attempting to resolve defaults.";
  -            String message = 
  -              ExceptionHelper.packException( report, throwables, false );
  -            System.err.println( message );
  +            if( errors.size() > 1 )
  +            {
  +                final String report = 
  +                  "Multiple errors (ignored) while resolving defaults.";
  +                String message = 
  +                  ExceptionHelper.packException( report, throwables, false );
  +                System.err.println( message );
  +            }
  +            else
  +            {
  +                final String report = 
  +                  "One error (ignored) occured while resolving defaults.";
  +                String message = 
  +                  ExceptionHelper.packException( report, throwables[0], false );
  +                System.err.println( message );
  +            }
           }
       }
   
  
  
  
  1.10      +38 -1     
avalon/merlin/kernel/plugin/src/java/org/apache/avalon/merlin/tools/MerlinBean.java
  
  Index: MerlinBean.java
  ===================================================================
  RCS file: 
/home/cvs/avalon/merlin/kernel/plugin/src/java/org/apache/avalon/merlin/tools/MerlinBean.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- MerlinBean.java   14 Dec 2003 10:29:47 -0000      1.9
  +++ MerlinBean.java   14 Dec 2003 20:07:45 -0000      1.10
  @@ -153,6 +153,7 @@
               Artifact artifact = 
                 DefaultBuilder.createImplementationArtifact( 
                   classloader, 
  +                getMerlinHome(),
                   getBaseDirectory(), 
                   MERLIN_PROPERTIES, 
                   IMPLEMENTATION_KEY );
  @@ -244,5 +245,41 @@
           }
           return (String[]) list.toArray( new String[0] );
       }
  +
  +   /**
  +    * Return the merlin home directory.
  +    * @return the merlin install directory
  +    */
  +    private static File getMerlinHome()
  +    {
  +        return new File( getMerlinHomePath() );
  +    }
  +
  +   /**
  +    * Return the merlin home directory path.
  +    * @return the merlin install directory path
  +    */
  +    private static String getMerlinHomePath()
  +    {
  +        try
  +        {
  +            String merlin = 
  +              System.getProperty( 
  +                "merlin.home", 
  +                Env.getEnvVariable( "MERLIN_HOME" ) );
  +            if( null != merlin ) return merlin;
  +            return System.getProperty( "user.home" ) 
  +              + File.separator + ".merlin";
  +        }
  +        catch( Throwable e )
  +        {
  +            final String error = 
  +              "Internal error while attempting to access MERLIN_HOME environment.";
  +            final String message = 
  +              ExceptionHelper.packException( error, e, true );
  +            throw new RuntimeException( message );
  +        }
  +    }
  +
   }
   
  
  
  
  1.22      +37 -1     
avalon/merlin/kernel/unit/src/java/org/apache/avalon/merlin/unit/AbstractMerlinTestCase.java
  
  Index: AbstractMerlinTestCase.java
  ===================================================================
  RCS file: 
/home/cvs/avalon/merlin/kernel/unit/src/java/org/apache/avalon/merlin/unit/AbstractMerlinTestCase.java,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- AbstractMerlinTestCase.java       14 Dec 2003 09:56:08 -0000      1.21
  +++ AbstractMerlinTestCase.java       14 Dec 2003 20:07:45 -0000      1.22
  @@ -132,6 +132,7 @@
               Artifact artifact = 
                 DefaultBuilder.createImplementationArtifact( 
                   classloader, 
  +                getMerlinHome(),
                   getBaseDirectory(), 
                   MERLIN_PROPERTIES, 
                   IMPLEMENTATION_KEY );
  @@ -323,6 +324,41 @@
               return new File( base );
           }
           return new File( System.getProperty( "user.dir" ) );
  +    }
  +
  +   /**
  +    * Return the merlin home directory.
  +    * @return the merlin install directory
  +    */
  +    private static File getMerlinHome()
  +    {
  +        return new File( getMerlinHomePath() );
  +    }
  +
  +   /**
  +    * Return the merlin home directory path.
  +    * @return the merlin install directory path
  +    */
  +    private static String getMerlinHomePath()
  +    {
  +        try
  +        {
  +            String merlin = 
  +              System.getProperty( 
  +                "merlin.home", 
  +                Env.getEnvVariable( "MERLIN_HOME" ) );
  +            if( null != merlin ) return merlin;
  +            return System.getProperty( "user.home" ) 
  +              + File.separator + ".merlin";
  +        }
  +        catch( Throwable e )
  +        {
  +            final String error = 
  +              "Internal error while attempting to access MERLIN_HOME environment.";
  +            final String message = 
  +              ExceptionHelper.packException( error, e, true );
  +            throw new RuntimeException( message );
  +        }
       }
   
   }
  
  
  
  1.10      +80 -24    
avalon/repository/main/src/java/org/apache/avalon/repository/main/DefaultBuilder.java
  
  Index: DefaultBuilder.java
  ===================================================================
  RCS file: 
/home/cvs/avalon/repository/main/src/java/org/apache/avalon/repository/main/DefaultBuilder.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- DefaultBuilder.java       14 Dec 2003 09:50:10 -0000      1.9
  +++ DefaultBuilder.java       14 Dec 2003 20:07:45 -0000      1.10
  @@ -90,53 +90,106 @@
   public class DefaultBuilder extends AbstractBuilder implements Builder
   {
       //-----------------------------------------------------------
  -    // static
  +    // public static
       //-----------------------------------------------------------
   
  -    private static final File USER = 
  -      new File( System.getProperty( "user.home" ) );
  +   /**
  +    * Resolve the default implementation taking into account 
  +    * local and home properties, and application defaults.
  +    * @param classloader the embedding classloader
  +    * @param base the base directory
  +    * @param resource a properties filename
  +    * @param key a property key containing an artifact specification
  +    * @return the artifact reference
  +    */
  +    public static Artifact createImplementationArtifact( 
  +      ClassLoader classloader, File base, 
  +      String resource, String key ) throws Exception
  +    {
  +        return createImplementationArtifact( 
  +          classloader, null, base, resource, key );
  +    }
   
      /**
       * Resolve the default implementation taking into account 
       * local and home properties, and application defaults.
       * @param classloader the embedding classloader
  +    * @param system the application system home directory
       * @param base the base directory
       * @param resource a properties filename
       * @param key a property key containing an artifact specification
       * @return the artifact reference
       */
       public static Artifact createImplementationArtifact( 
  -      ClassLoader classloader, File base, String resource, String key ) throws 
Exception
  +      ClassLoader classloader, File system, File base, 
  +      String resource, String key ) throws Exception
       {
  +        //
  +        // check for the implementation property in the 
  +        // working directory
  +        //
  +
           String spec = 
  -          getLocalProperties( USER, resource ).getProperty( key );
  +          getLocalProperties( base, resource ).getProperty( key );
  +       
  +        //
  +        // check for the implementation property in the 
  +        // user's home directory
  +        //
  +
           if( null == spec )
           {
               spec = 
  -              getLocalProperties( base, resource ).getProperty( key );
  -            if( null == spec )
  +              getLocalProperties( USER, resource ).getProperty( key );
  +        }
  +
  +        //
  +        // check for the implementation property in the 
  +        // applications home directory
  +        //
  +
  +        if( null == spec )
  +        {
  +            spec = 
  +              getLocalProperties( system, resource ).getProperty( key );
  +        }
  +
  +        //
  +        // check for the implementation property in the 
  +        // classloader
  +        //
  +
  +        if( null == spec )
  +        {
  +            Properties properties = new Properties();
  +            InputStream input = classloader.getResourceAsStream( resource );
  +            if( input == null ) 
  +            {
  +                final String error = 
  +                  "Missing resource: [" + resource + "]";
  +                throw new IllegalStateException( error );
  +            }
  +            properties.load( input );
  +            spec = properties.getProperty( key );
  +            if( spec == null ) 
               {
  -                Properties properties = new Properties();
  -                InputStream input = classloader.getResourceAsStream( resource );
  -                if( input == null ) 
  -                {
  -                    final String error = 
  -                      "Missing resource: [" + resource + "]";
  -                    throw new Error( error );
  -                }
  -                properties.load( input );
  -                spec = properties.getProperty( key );
  -                if( spec == null ) 
  -                {
  -                    final String error = 
  -                      "Missing property: [" + key + "] in resource: [" + resource + 
"]";
  -                    throw new Error( error );
  -                }
  +                final String error = 
  +                  "Missing property: [" + key + "] in resource: [" + resource + "]";
  +                throw new IllegalStateException( error );
               }
           }
  +
  +        //
  +        // return the artifact referencing the implementation to be loaded
  +        //
  +
           return Artifact.createArtifact( spec );
       }
   
  +    //-----------------------------------------------------------
  +    // private static
  +    //-----------------------------------------------------------
  +
       private static Properties getLocalProperties( 
         File dir, String filename ) throws IOException
       {
  @@ -147,6 +200,9 @@
           properties.load( new FileInputStream( file ) );
           return properties;
       }
  +
  +    private static final File USER = 
  +      new File( System.getProperty( "user.home" ) );
   
       //-----------------------------------------------------------
       // immutable state
  
  
  
  1.12      +24 -14    
avalon/repository/main/src/java/org/apache/avalon/repository/main/DefaultInitialContext.java
  
  Index: DefaultInitialContext.java
  ===================================================================
  RCS file: 
/home/cvs/avalon/repository/main/src/java/org/apache/avalon/repository/main/DefaultInitialContext.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- DefaultInitialContext.java        14 Dec 2003 17:01:46 -0000      1.11
  +++ DefaultInitialContext.java        14 Dec 2003 20:07:45 -0000      1.12
  @@ -111,7 +111,7 @@
       * properties.  Seaches will be conducted on the current directory and 
       * the user's home directory.
       */
  -    public static final String AVALON = "avalon.properties";
  +    public static final String AVALON_PROPERTIES = "avalon.properties";
   
      /**
       * Return the Avalon system common directory.  This directory is 
  @@ -269,10 +269,14 @@
         throws RepositoryException
       {
           m_base = setupBaseDirectory( base );
  -        Properties avalonHome = getLocalProperties( USER_HOME, AVALON );
  -        Properties avalonWork = getLocalProperties( m_base, AVALON );
  -        m_cache = setupCache( cache, avalonHome, avalonWork );
  -        m_hosts = setupHosts( hosts, avalonHome, avalonWork );
  +        Properties avalonSystem = 
  +          getLocalProperties( getAvalonHome(), AVALON_PROPERTIES );
  +        Properties avalonHome = 
  +          getLocalProperties( USER_HOME, AVALON_PROPERTIES );
  +        Properties avalonWork = 
  +          getLocalProperties( m_base, AVALON_PROPERTIES );
  +        m_cache = setupCache( cache, avalonSystem, avalonHome, avalonWork );
  +        m_hosts = setupHosts( hosts, avalonSystem, avalonHome, avalonWork );
   
           Artifact implementation = setupImplementation( artifact );
           ClassLoader parent = setupClassLoader( loader );
  @@ -413,16 +417,18 @@
           return DefaultInitialContext.class.getClassLoader();
       }
   
  -    private File setupCache( File file, Properties home, Properties work )
  +    private File setupCache( 
  +      File file, Properties system, Properties home, Properties work )
       {
           if( null != file ) return file;
  -        return setupDefaultCache( home, work );
  +        return setupDefaultCache( system, home, work );
       }
   
  -    private String[] setupHosts( String[] hosts, Properties home, Properties work )
  +    private String[] setupHosts( 
  +      String[] hosts, Properties system, Properties home, Properties work )
       {
           if( null != hosts ) return RepositoryUtils.getCleanPaths( hosts );
  -        return setupDefaultHosts( home, work );
  +        return setupDefaultHosts( system, home, work );
       }
   
       private Artifact setupImplementation( Artifact artifact )
  @@ -482,18 +488,22 @@
           return getBaseDirectory();
       }
   
  -    private String[] setupDefaultHosts( Properties home, Properties work )
  +    private String[] setupDefaultHosts(
  +      Properties system, Properties home, Properties work )
       {
  -        String homeValue = home.getProperty( HOSTS_KEY );
  +        String systemValue = system.getProperty( HOSTS_KEY );
  +        String homeValue = home.getProperty( HOSTS_KEY, systemValue );
           String workValue = work.getProperty( HOSTS_KEY, homeValue );
           String value = System.getProperty( HOSTS_KEY , workValue );
           if( null == value ) return DEFAULT_INITIAL_HOSTS;
           return expandHosts( value );
       }
   
  -    private static File setupDefaultCache( Properties home, Properties work )
  +    private static File setupDefaultCache( 
  +      Properties system, Properties home, Properties work )
       {
  -        String homeValue = home.getProperty( CACHE_KEY );
  +        String systemValue = system.getProperty( CACHE_KEY );
  +        String homeValue = home.getProperty( CACHE_KEY, systemValue );
           String workValue = work.getProperty( CACHE_KEY, homeValue );
           String value = System.getProperty( CACHE_KEY , workValue );
           if( null != value ) return new File( value  );
  
  
  

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

Reply via email to