mcconnell    2004/02/15 10:04:11

  Modified:    util/env/src/java/org/apache/avalon/util/env
                        EnvAccessException.java
  Added:       util/defaults/src/java/org/apache/avalon/util/defaults
                        DefaultsBuilder.java
  Log:
  Update the env exception to inherit from IOException and add a initial cut on a 
defaults builder utility.
  
  Revision  Changes    Path
  1.3       +3 -2      
avalon/util/env/src/java/org/apache/avalon/util/env/EnvAccessException.java
  
  Index: EnvAccessException.java
  ===================================================================
  RCS file: 
/home/cvs/avalon/util/env/src/java/org/apache/avalon/util/env/EnvAccessException.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- EnvAccessException.java   24 Jan 2004 23:18:55 -0000      1.2
  +++ EnvAccessException.java   15 Feb 2004 18:04:11 -0000      1.3
  @@ -17,6 +17,7 @@
   
   package org.apache.avalon.util.env ;
   
  +import java.io.IOException;
   
   /**
    * A simple wrapper exception around exceptions that could occur while accessing
  @@ -26,7 +27,7 @@
    * @author <a href="mailto:[EMAIL PROTECTED]">Stephen McConnell</a>
    * @version $Revision$
    */
  -public class EnvAccessException extends Exception
  +public class EnvAccessException extends IOException
   {
       /** the environment variable name if available */
       public final String m_variable ;
  
  
  
  1.1                  
avalon/util/defaults/src/java/org/apache/avalon/util/defaults/DefaultsBuilder.java
  
  Index: DefaultsBuilder.java
  ===================================================================
  /* 
   * Copyright 2004 Apache Software Foundation
   * Licensed  under the  Apache License,  Version 2.0  (the "License");
   * you may not use  this file  except in  compliance with the License.
   * You may obtain a copy of the License at 
   * 
   *   http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed  under the  License is distributed on an "AS IS" BASIS,
   * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
   * implied.
   * 
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.avalon.util.defaults;
  
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.InputStream;
  import java.io.IOException;
  import java.net.URL;
  import java.util.Enumeration;
  import java.util.Hashtable;
  import java.util.Properties;
  import java.util.ArrayList;
  import java.util.StringTokenizer;
  import java.util.Map;
  
  import org.apache.avalon.util.env.Env;
  import org.apache.avalon.util.env.EnvAccessException;
  
  
  /**
   * A utility class that provides support for the establishment
   * of a set of installation properties.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]">Stephen McConnell</a>
   * @version $Revision: 1.1 $
   */
  public class DefaultsBuilder
  {
      //--------------------------------------------------------------
      // static
      //--------------------------------------------------------------
  
     /**
      * Return a directory taking into account a supplied env symbol, 
      * a property key and a fallback directory.
      *
      * If the supplied key references a known system property
      * value of '[key].home' then that value will be used to establish the home 
      * directory. Otherwise, if the supplied env symbol cannot be resolved
      * to a value, a directory corresponding to ${user.home}/.[key]
      * will be returned.
      *
      * @param key an application key such as 'merlin'
      * @param symbol an environment variable name such a MERLIN_HOME
      * @return the derived directory
      */
      public static File getHomeDirectory( 
        String key, String symbol ) throws IOException
      {
          final String homeKey = key + ".home";
          final String home = 
            System.getProperty( 
              homeKey, 
              Env.getEnvVariable( symbol ) );
  
          if( null != home )
          {
              return new File( home ).getCanonicalFile();
          }
          else
          {
              final File user = 
                new File( System.getProperty( "user.home" ) );
              final String path = "." + key;
              return new File( user, path ).getCanonicalFile();
          }
      }
  
     /**
      * Create a installation properties object.  The implementation
      * will create a new properties object and read in a properties 
      * file if it exists relative to the filename [home]/[key].properties.  
      * Before returning the properties object the home directory will be 
      * assigned as the value of a property name [key].home if the supplied
      * flag argument is TRUE.
      *
      * @param home the home directory
      * @param key the application key 
      * @param flag if TRUE set the property '[key].home' to the home directory 
      * @return the application properties object
      */
      public static Properties getHomeProperties( 
        File home, String key, boolean flag ) throws IOException
      {
          Properties properties = getProperties( home, key );
          if( flag )
          {
              final String name = key + ".home";
              final String path = home.getCanonicalPath();
              properties.setProperty( name, path );
          }
          return properties;
      }
  
     /**
      * Create a user properties object.  The implementation
      * will create a new properties object and read in a properties 
      * file if it exists relative to the filename ${user.home}/[key].properties.  
      *
      * @param key the application key 
      * @return the user properties object
      */
      public static Properties getUserProperties( 
        String key ) throws IOException
      {
          final File user = new File( System.getProperty( "user.home" ) );
          return getProperties( user, key );
      }
  
     /**
      * Create a dir properties object.  The implementation
      * will create a new properties object and read in a properties 
      * file if it exists relative to [dir]/[key].properties.  
      *
      * @param dir the base directory 
      * @param key the application key 
      * @return the user properties object
      */
      public static Properties getProperties( 
        File dir, String key ) throws IOException
      {
          final String filename = key + ".properties";
          final File file = new File( dir, filename );
          return getProperties( file );
      }
  
     /**
      * Create a properties object from the supplied file.  If 
      * the file does not exists an empty property object will be 
      * returned.
      *
      * @param file the properties file
      * @return the properties object
      */
      public static Properties getProperties( File file ) throws IOException
      {
          if( null == file )
          {
              throw new NullPointerException( "file" );
          }
  
          Properties properties = new Properties();
          if( file.exists() )
          {
              properties.load( 
                new FileInputStream( file ) );
          }   
          return properties;
      }
  }
  
  
  

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

Reply via email to