KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.myvietnam.mvncore.configuration;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 1999-2003 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.IOException JavaDoc;
58 import java.io.InputStream JavaDoc;
59 import java.net.MalformedURLException JavaDoc;
60 import java.net.URL JavaDoc;
61
62 import org.apache.commons.logging.Log;
63 import org.apache.commons.logging.LogFactory;
64
65 import org.apache.commons.lang.StringUtils;
66
67 /**
68  * This is the "classic" Properties loader which loads the values from
69  * a single or multiple files (which can be chained with "include =".
70  * All given path references are either absolute or relative to the
71  * file name supplied in the Constructor.
72  * <p>
73  * In this class, empty PropertyConfigurations can be built, properties
74  * added and later saved. include statements are (obviously) not supported
75  * if you don't construct a PropertyConfiguration from a file.
76  * <p>
77  * If you want to use the getResourceAsStream() trick to load your
78  * resources without an absolute path, please take a look at the
79  * ClassPropertiesConfiguration which is intended to be used for this.
80  *
81  * @author <a HREF="mailto:stefano@apache.org">Stefano Mazzocchi</a>
82  * @author <a HREF="mailto:jon@latchkey.com">Jon S. Stevens</a>
83  * @author <a HREF="mailto:daveb@miceda-data">Dave Bryson</a>
84  * @author <a HREF="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
85  * @author <a HREF="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
86  * @author <a HREF="mailto:kjohnson@transparent.com">Kent Johnson</a>
87  * @author <a HREF="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
88  * @author <a HREF="mailto:ipriha@surfeu.fi">Ilkka Priha</a>
89  * @author <a HREF="mailto:jvanzyl@apache.org">Jason van Zyl</a>
90  * @author <a HREF="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
91  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
92  * @author <a HREF="mailto:epugh@upstate.com">Eric Pugh</a>
93  * @author <a HREF="mailto:oliver.heger@t-online.de">Oliver Heger</a>
94  * @version $Id: PropertiesConfiguration.java,v 1.1 2003/12/09 08:25:30 huumai Exp $
95  */

96 public class PropertiesConfiguration
97         extends BasePropertiesConfiguration
98         implements Configuration
99 {
100     /** Static logger */
101     Log log = LogFactory.getLog(PropertiesConfiguration.class);
102
103     /** File separator. */
104     protected String JavaDoc fileSeparator = System.getProperty("file.separator");
105
106     /**
107      * The name of the file to be loaded. This is used in conjuction with
108      * the load method. */

109     protected String JavaDoc fileName = null;
110
111     /**
112      * Creates an empty PropertyConfiguration object which can be
113      * used to synthesize a new Properties file by adding values and
114      * then saving(). An object constructed by this C'tor can not be
115      * tickled into loading included files because it cannot supply a
116      * base for relative includes.
117      */

118     public PropertiesConfiguration()
119     {
120         setIncludesAllowed(false);
121     }
122
123     /**
124      * Creates an empty PropertyConfiguration object with
125      * a Super-Object which is queries for every key.
126      *
127      * @param defaults Configuration defaults to use if key not in file
128      * @throws IOException Error while loading the properties file
129      */

130     public PropertiesConfiguration(Configuration defaults) throws IOException JavaDoc
131     {
132         this();
133         this.defaults = defaults;
134     }
135
136     /**
137      * Creates and loads the extended properties from the specified file.
138      * The specified file can contain "include = " properties which then
139      * are loaded and merged into the properties.
140      *
141      * @param fileName The name of the Properties File to load.
142      * @throws IOException Error while loading the properties file
143      */

144     public PropertiesConfiguration(String JavaDoc fileName) throws IOException JavaDoc
145     {
146
147         load(fileName);
148     }
149
150     /**
151      * Load the properties from the fileName set by setFileName
152      *
153      * @throws IOException
154      */

155     public void load() throws IOException JavaDoc
156     {
157         load(getFileName());
158     }
159
160     /**
161      * Load the properties from the given fileName
162      *
163      * @param fileName A properties file to load
164      * @throws IOException
165      */

166     public void load(String JavaDoc fileName) throws IOException JavaDoc
167     {
168         load(getPropertyStream(fileName));
169     }
170
171     /**
172      * Creates and loads the extended properties from the specified file.
173      *
174      * @param file The name of the Properties File to load.
175      * @param defaults Configuration defaults to use if key not in file
176      * @throws IOException Error while loading the properties file
177      */

178     public PropertiesConfiguration(String JavaDoc file, Configuration defaults) throws IOException JavaDoc
179     {
180         this(file);
181         this.defaults = defaults;
182     }
183
184     /**
185      * Creates and loads the extended properties from the specified file.
186      *
187      * @param file The name of the Properties File to load.
188      * @param defaultFile The name of a properties file whose values
189      * should be used if a key is not in the file.
190      * @throws IOException Error while loading the properties file
191      */

192     public PropertiesConfiguration(String JavaDoc file, String JavaDoc defaultFile) throws IOException JavaDoc
193     {
194         this(file);
195         if (StringUtils.isNotEmpty(defaultFile))
196         {
197             this.defaults = new PropertiesConfiguration(defaultFile);
198         }
199     }
200
201     /**
202      * Gets a resource relative to the supplied base path. If the passed in
203      * resource name is absolute, it is used directly.
204      *
205      * @param resourceName The resource Name
206      * @return An Input Stream
207      * @throws IOException Error while loading the properties file
208      */

209     public InputStream JavaDoc getPropertyStream(String JavaDoc resourceName) throws IOException JavaDoc
210     {
211         InputStream JavaDoc resource = null;
212         URL JavaDoc url = null;
213
214         try
215         {
216             url = ConfigurationUtils.getURL(getBasePath(), resourceName);
217         } /* try */
218         catch(MalformedURLException JavaDoc uex)
219         {
220             throw new IOException JavaDoc("Cannot obtain URL for resource "
221             + resourceName);
222         } /* catch */
223
224         resource = url.openStream();
225
226         setBasePath(url.toString());
227         setIncludesAllowed(true);
228
229         return resource;
230     }
231
232     /**
233      * Returns the fileName.
234      * @return String
235      */

236     public String JavaDoc getFileName()
237     {
238         return fileName;
239     }
240
241     /**
242      * Sets the fileName.
243      * @param fileName The fileName to set
244      */

245     public void setFileName(String JavaDoc fileName)
246     {
247         this.fileName = fileName;
248     }
249
250     /**
251      * Extend the setBasePath method to turn includes
252      * on and off based on the existence of a base path.
253      *
254      * @param basePath The new basePath to set.
255      */

256     public void setBasePath(String JavaDoc basePath)
257     {
258         super.setBasePath(basePath);
259         setIncludesAllowed(StringUtils.isNotEmpty(basePath));
260     }
261 }
262
Popular Tags