henning     2002/12/14 03:37:28

  Added:       configuration/src/java/org/apache/commons/configuration
                        ClassPropertiesConfiguration.java
  Log:
  This is a new PropertyConfiguration class which is intended to
  be used when loading properties as resources from the class path.
  It supports the "include = " statement for loading associated files
  from the classpath.
  
  Revision  Changes    Path
  1.1                  
jakarta-commons-sandbox/configuration/src/java/org/apache/commons/configuration/ClassPropertiesConfiguration.java
  
  Index: ClassPropertiesConfiguration.java
  ===================================================================
  package org.apache.commons.configuration;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.IOException;
  import java.io.InputStream;
  
  import org.apache.commons.lang.StringUtils;
  
  /**
   * Loads the configuration from a class resource. The included
   * properties files are relative to the class or absolute class
   * references. All files are loaded from the class loader.
   * <p>
   * This class does not support an empty constructor and saving of a
   * synthesized properties file. Use PropertiesConfiguration for this.
   *
   * @see org.apache.commons.configuration.BasePropertiesConfiguration
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Henning P. Schmiedehausen</a>
   * @version $Id: ClassPropertiesConfiguration.java,v 1.1 2002/12/14 11:37:28 henning 
Exp $
   */
  public class ClassPropertiesConfiguration
      extends BasePropertiesConfiguration
      implements Configuration
  {
      /** Base class, which is used to load all relative class references */
      protected Class baseClass = null;
  
      /** Class Loader which we will use to load the resources */
      protected ClassLoader classLoader = null;
  
      /**
       * Creates and loads an extended properties file from the Class
       * Resources. Uses the class loader.
       *
       * @param baseClass The class providing the FileStream.
       * @param resource The name of the Resource.
       * @throws IOException Error while loading the properties file
       */
      public ClassPropertiesConfiguration(Class baseClass, String resource)
          throws IOException
      {
          this.baseClass = baseClass;
          // According to javadocs, getClassLoader() might return null
          // if it represents the "bootstrap class loader"
          // Use the System class loader in this case.
          classLoader = (baseClass.getClassLoader() == null) 
              ? ClassLoader.getSystemClassLoader()
              : baseClass.getClassLoader();
          
          setIncludesAllowed(true);
  
          load(getPropertyStream(resource));
      }
  
      /**
       * Creates and loads an extended properties file from the Class
       * Resources. Uses the class loader.
       *
       * @param baseClass The class providing the FileStream.
       * @param resource The name of the Resource.
       * @param defaults Configuration defaults to use if key not in file
       * @throws IOException Error while loading the properties file
       */
      public ClassPropertiesConfiguration(Class baseClass, String resource, 
                                          Configuration defaults)
          throws IOException
      {
          this(baseClass, resource);
          this.defaults = defaults;
      }
  
      /**
       * Creates and loads an extended properties file from the Class
       * Resources. Uses the class loader.
       *
       * @param baseClass The class providing the FileStream.
       * @param resource The name of the Resource.
       * @param defaultFile Configuration defaults to use if key not in file
       * @throws IOException Error while loading the properties file
       */
      public ClassPropertiesConfiguration(Class baseClass, String resource,
                                          String defaultFile)
          throws IOException
      {
          this(baseClass, resource);
  
          if (StringUtils.isNotEmpty(defaultFile))
          {
              this.defaults =
                  new ClassPropertiesConfiguration(baseClass, defaultFile);
          }
      }
  
      /**
       * Gets a resource relative to the supplied base class or
       * from the class loader if its an absolute reference (starting
       * with a "/").
       *
       * @param resourceName The resource Name
       * @return An Input Stream
       * @throws IOException Error while loading the properties file
       */
      public InputStream getPropertyStream(String resourceName)
          throws IOException
      {
          InputStream resource = null;
  
          // JavaDocs say, that Resource Names always start with "/"
          if (resourceName.startsWith("/"))
          {
              resource = classLoader.getResourceAsStream(resourceName);
          }
          else
          {
              // Relative reference
              StringBuffer classPath = new StringBuffer();
              // Javadocs say "/" separated...
              classPath.append(baseClass.getPackage()
                               .getName().replace('.', '/'));
              classPath.append("/");
                               
              /*
               * We have a relative path, and we have
               * two possible forms here. If we have the
               * "./" form then just strip that off first
               * before continuing.
               */
              
              if (resourceName.startsWith("./"))
              {
                  classPath.append(resourceName.substring(2));
              }
              else
              {
                  classPath.append(resourceName);
              }
  
              resource = classLoader.getResourceAsStream(classPath.toString());
          }
  
          if (resource == null)
          {
              throw new FileNotFoundException("Could not open Resource "
                                              + resourceName);
          }
  
          return resource;
      }
  }
  
       
  
      
  
  
  

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

Reply via email to