/*
 * Copyright 2001-2004 The 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.commons.configuration;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Loads the configuration from the classpath utilizing a specified class to get
 * the classloader from. The properties file will be attempted to be loaded
 * first from the classes package directory and then from the class path in
 * general.
 * <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:hps@intermeta.de">Henning P. Schmiedehausen</a>
 * @version $Id: ClassPropertiesConfiguration.java,v 1.9 2004/06/24 14:01:03 ebourg Exp $
 */
public class ClassPropertiesConfiguration extends BasePropertiesConfiguration implements Configuration
{
    /** Base class, which is used to load all relative class references */
    private Class baseClass;

    /** Class Loader which we will use to load the resources */
    private ClassLoader classLoader;

    /**
     * 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 ConfigurationException Error while loading the properties file
     */
    public ClassPropertiesConfiguration(Class baseClass, String resource) throws ConfigurationException
    {
        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);
        try
        {
            load(getPropertyStream(resource));
        }
        catch (IOException ioe)
        {
            throw new ConfigurationException("Could not load input stream from resource " + resource, ioe);
        }
    }

    /**
     * Gets a resource relative to the supplied base class or
     * from the class loader if it is not found from the supplied base class.
     *
     * @param resourceName The resource Name
     * @return An Input Stream
     * @throws IOException Error while loading the properties file
     */
    protected InputStream getPropertyStream(String resourceName) throws IOException
    {
        InputStream resource = null;

        //First try to load from within the package of the provided class
        resource = baseClass.getResourceAsStream(resourceName);

        if (resource == null)
        {
          resource = classLoader.getResourceAsStream(resourceName);
        }

        if (resource == null)
        {
            throw new FileNotFoundException("Could not open Resource " + resourceName);
        }

        return resource;
    }
}

@@ -751,16 +751,95 @@
 tream;%0A%0A
+import java.security.AccessController;%0Aimport java.security.PrivilegedAction;%0A%0A
 /**%0A * L
@@ -1433,35 +1433,37 @@
 v 1.
-9
+10
  2004/0
-6/24 14:01:03 ebour
+7/13 09:20:37 hennin
 g Ex
@@ -1779,16 +1779,23 @@
 ssLoader
+ = null
 ;%0A%0A    /
@@ -2254,24 +2254,25 @@
 eClass;%0A
+%0A
         
 // Accor
@@ -2267,68 +2267,30 @@
     
-// According to javadocs, getClassLoader() might return
+if (baseClass !=
  null
+)
 %0A   
@@ -2298,110 +2298,14 @@
     
-// if it represents the %22bootstrap class loader%22%0A        // Use the System class loader in this case.%0A
+%7B%0A    
     
@@ -2322,17 +2322,16 @@
 oader = 
-(
 baseClas
@@ -2352,108 +2352,19 @@
 er()
- == null)%0A            ? ClassLoader.getSystemClassLoader()%0A            : baseClass.getClassLoader();
+;%0A        %7D
 %0A%0A  
@@ -3145,24 +3145,69 @@
 vided class%0A
+        if (baseClass != null)%0A        %7B%0A    
         reso
@@ -3257,16 +3257,26 @@
 ceName);
+%0A        %7D
 %0A%0A      
@@ -3311,32 +3311,34 @@
     %7B%0A          
+  
 resource = class
@@ -3336,20 +3336,8 @@
 e = 
-classLoader.
 getR
@@ -3344,32 +3344,45 @@
 esourceAsStream(
+classLoader, 
 resourceName);%0A 
@@ -3560,11 +3560,672 @@
 e;%0A    %7D
+%0A%0A    private InputStream getResourceAsStream(final ClassLoader loader,%0A            final String name)%0A    %7B%0A        return (InputStream) AccessController.doPrivileged(%0A                new PrivilegedAction()%0A                %7B%0A                    public Object run()%0A                    %7B%0A                        if (loader != null)%0A                        %7B%0A                            return loader.getResourceAsStream(name);%0A                        %7D%0A                        else%0A                        %7B%0A                            return ClassLoader.getSystemResourceAsStream(name);%0A                        %7D%0A                    %7D%0A                %7D);%0A    %7D
 %0A%7D%0A

Make the loading of the properties file from the class loader a bit more robust. Allow passing "null" into the C'tor, thus directly accessing the classpath and bypassing the check for a defined class.