001: package net.myvietnam.mvncore.configuration;
002:
003: /* ====================================================================
004: * The Apache Software License, Version 1.1
005: *
006: * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
007: * reserved.
008: *
009: * Redistribution and use in source and binary forms, with or without
010: * modification, are permitted provided that the following conditions
011: * are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowledgement:
023: * "This product includes software developed by the
024: * Apache Software Foundation (http://www.apache.org/)."
025: * Alternately, this acknowledgement may appear in the software itself,
026: * if and wherever such third-party acknowledgements normally appear.
027: *
028: * 4. The names "The Jakarta Project", "Commons", and "Apache Software
029: * Foundation" must not be used to endorse or promote products derived
030: * from this software without prior written permission. For written
031: * permission, please contact apache@apache.org.
032: *
033: * 5. Products derived from this software may not be called "Apache"
034: * nor may "Apache" appear in their names without prior written
035: * permission of the Apache Software Foundation.
036: *
037: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
038: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
039: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
040: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
041: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
042: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
043: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
044: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
045: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
046: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
047: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
048: * SUCH DAMAGE.
049: * ====================================================================
050: *
051: * This software consists of voluntary contributions made by many
052: * individuals on behalf of the Apache Software Foundation. For more
053: * information on the Apache Software Foundation, please see
054: * <http://www.apache.org/>.
055: */
056:
057: import java.io.FileNotFoundException;
058: import java.io.IOException;
059: import java.io.InputStream;
060:
061: import org.apache.commons.lang.StringUtils;
062:
063: /**
064: * Loads the configuration from the classpath utilizing a specified class to get
065: * the classloader from. The properties file will be attempted to be loaded
066: * first from the classes package directory and then from the class path in
067: * general.
068: * <p>
069: * This class does not support an empty constructor and saving of a
070: * synthesized properties file. Use PropertiesConfiguration for this.
071: *
072: * @see net.myvietnam.mvncore.configuration.BasePropertiesConfiguration
073: *
074: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
075: * @version $Id: ClassPropertiesConfiguration.java,v 1.3 2004/05/28 22:02:58 skoehler Exp $
076: */
077: public class ClassPropertiesConfiguration extends
078: BasePropertiesConfiguration implements Configuration {
079: /** Base class, which is used to load all relative class references */
080: private Class baseClass = null;
081:
082: /** Class Loader which we will use to load the resources */
083: private ClassLoader classLoader = null;
084:
085: /**
086: * Creates and loads an extended properties file from the Class
087: * Resources. Uses the class loader.
088: *
089: * @param baseClass The class providing the FileStream.
090: * @param resource The name of the Resource.
091: * @throws IOException Error while loading the properties file
092: */
093: public ClassPropertiesConfiguration(Class baseClass, String resource)
094: throws IOException {
095: this .baseClass = baseClass;
096: // According to javadocs, getClassLoader() might return null
097: // if it represents the "bootstrap class loader"
098: // Use the System class loader in this case.
099: classLoader = (baseClass.getClassLoader() == null) ? ClassLoader
100: .getSystemClassLoader()
101: : baseClass.getClassLoader();
102:
103: setIncludesAllowed(true);
104:
105: load(getPropertyStream(resource));
106: }
107:
108: /**
109: * Creates and loads an extended properties file from the Class
110: * Resources. Uses the class loader.
111: *
112: * @param baseClass The class providing the FileStream.
113: * @param resource The name of the Resource.
114: * @param defaults Configuration defaults to use if key not in file
115: * @throws IOException Error while loading the properties file
116: */
117: public ClassPropertiesConfiguration(Class baseClass,
118: String resource, Configuration defaults) throws IOException {
119: this (baseClass, resource);
120: this .defaults = defaults;
121: }
122:
123: /**
124: * Creates and loads an extended properties file from the Class
125: * Resources. Uses the class loader.
126: *
127: * @param baseClass The class providing the FileStream.
128: * @param resource The name of the Resource.
129: * @param defaultFile Configuration defaults to use if key not in file
130: * @throws IOException Error while loading the properties file
131: */
132: public ClassPropertiesConfiguration(Class baseClass,
133: String resource, String defaultFile) throws IOException {
134: this (baseClass, resource);
135:
136: if (StringUtils.isNotEmpty(defaultFile)) {
137: this .defaults = new ClassPropertiesConfiguration(baseClass,
138: defaultFile);
139: }
140: }
141:
142: /**
143: * Gets a resource relative to the supplied base class or
144: * from the class loader if it is not found from the supplied base class.
145: *
146: * @param resourceName The resource Name
147: * @return An Input Stream
148: * @throws IOException Error while loading the properties file
149: */
150: public InputStream getPropertyStream(String resourceName)
151: throws IOException {
152: InputStream resource = null;
153: //For backwards compatibility with earlier versions,
154: //strip a leading "./" from the
155: if (resourceName.startsWith("./")) {
156: //classPath.append(resourceName.substring(2));
157: }
158:
159: //First try to load from within the package of the provided class
160: resource = baseClass.getResourceAsStream(resourceName);
161:
162: if (resource == null) {
163: resource = classLoader.getResourceAsStream(resourceName);
164: }
165:
166: if (resource == null) {
167: throw new FileNotFoundException("Could not open Resource "
168: + resourceName);
169: }
170:
171: return resource;
172: }
173: }
|