KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > myvietnam > mvncore > configuration > ClassPropertiesConfiguration


1 package net.myvietnam.mvncore.configuration;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowledgement:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgement may appear in the software itself,
26  * if and wherever such third-party acknowledgements normally appear.
27  *
28  * 4. The names "The Jakarta Project", "Commons", and "Apache Software
29  * Foundation" must not be used to endorse or promote products derived
30  * from this software without prior written permission. For written
31  * permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache"
34  * nor may "Apache" appear in their names without prior written
35  * permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import java.io.FileNotFoundException JavaDoc;
58 import java.io.IOException JavaDoc;
59 import java.io.InputStream JavaDoc;
60
61 import org.apache.commons.lang.StringUtils;
62
63 /**
64  * Loads the configuration from the classpath utilizing a specified class to get
65  * the classloader from. The properties file will be attempted to be loaded
66  * first from the classes package directory and then from the class path in
67  * general.
68  * <p>
69  * This class does not support an empty constructor and saving of a
70  * synthesized properties file. Use PropertiesConfiguration for this.
71  *
72  * @see net.myvietnam.mvncore.configuration.BasePropertiesConfiguration
73  *
74  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
75  * @version $Id: ClassPropertiesConfiguration.java,v 1.3 2004/05/28 22:02:58 skoehler Exp $
76  */

77 public class ClassPropertiesConfiguration
78     extends BasePropertiesConfiguration
79     implements Configuration
80 {
81     /** Base class, which is used to load all relative class references */
82     private Class JavaDoc baseClass = null;
83
84     /** Class Loader which we will use to load the resources */
85     private ClassLoader JavaDoc classLoader = null;
86
87     /**
88      * Creates and loads an extended properties file from the Class
89      * Resources. Uses the class loader.
90      *
91      * @param baseClass The class providing the FileStream.
92      * @param resource The name of the Resource.
93      * @throws IOException Error while loading the properties file
94      */

95     public ClassPropertiesConfiguration(Class JavaDoc baseClass, String JavaDoc resource)
96         throws IOException JavaDoc
97     {
98         this.baseClass = baseClass;
99         // According to javadocs, getClassLoader() might return null
100
// if it represents the "bootstrap class loader"
101
// Use the System class loader in this case.
102
classLoader = (baseClass.getClassLoader() == null)
103             ? ClassLoader.getSystemClassLoader()
104             : baseClass.getClassLoader();
105
106         setIncludesAllowed(true);
107
108         load(getPropertyStream(resource));
109     }
110
111     /**
112      * Creates and loads an extended properties file from the Class
113      * Resources. Uses the class loader.
114      *
115      * @param baseClass The class providing the FileStream.
116      * @param resource The name of the Resource.
117      * @param defaults Configuration defaults to use if key not in file
118      * @throws IOException Error while loading the properties file
119      */

120     public ClassPropertiesConfiguration(Class JavaDoc baseClass, String JavaDoc resource,
121                                         Configuration defaults)
122         throws IOException JavaDoc
123     {
124         this(baseClass, resource);
125         this.defaults = defaults;
126     }
127
128     /**
129      * Creates and loads an extended properties file from the Class
130      * Resources. Uses the class loader.
131      *
132      * @param baseClass The class providing the FileStream.
133      * @param resource The name of the Resource.
134      * @param defaultFile Configuration defaults to use if key not in file
135      * @throws IOException Error while loading the properties file
136      */

137     public ClassPropertiesConfiguration(Class JavaDoc baseClass, String JavaDoc resource,
138                                         String JavaDoc defaultFile)
139         throws IOException JavaDoc
140     {
141         this(baseClass, resource);
142
143         if (StringUtils.isNotEmpty(defaultFile))
144         {
145             this.defaults =
146                 new ClassPropertiesConfiguration(baseClass, defaultFile);
147         }
148     }
149
150     /**
151      * Gets a resource relative to the supplied base class or
152      * from the class loader if it is not found from the supplied base class.
153      *
154      * @param resourceName The resource Name
155      * @return An Input Stream
156      * @throws IOException Error while loading the properties file
157      */

158     public InputStream JavaDoc getPropertyStream(String JavaDoc resourceName)
159         throws IOException JavaDoc
160     {
161         InputStream JavaDoc resource = null;
162         //For backwards compatibility with earlier versions,
163
//strip a leading "./" from the
164
if (resourceName.startsWith("./"))
165             {
166                 //classPath.append(resourceName.substring(2));
167
}
168
169         //First try to load from within the package of the provided class
170
resource = baseClass.getResourceAsStream(resourceName);
171
172         if (resource == null)
173         {
174           resource = classLoader.getResourceAsStream(resourceName);
175         }
176
177         if (resource == null)
178         {
179             throw new FileNotFoundException JavaDoc("Could not open Resource "
180                                             + resourceName);
181         }
182
183         return resource;
184     }
185 }
186
187
188
189
190
Popular Tags