001: package net.myvietnam.mvncore.configuration;
002:
003: /* ====================================================================
004: * The Apache Software License, Version 1.1
005: *
006: * Copyright (c) 1999-2003 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.IOException;
058: import java.io.InputStream;
059: import java.net.MalformedURLException;
060: import java.net.URL;
061:
062: import org.apache.commons.logging.Log;
063: import org.apache.commons.logging.LogFactory;
064:
065: import org.apache.commons.lang.StringUtils;
066:
067: /**
068: * This is the "classic" Properties loader which loads the values from
069: * a single or multiple files (which can be chained with "include =".
070: * All given path references are either absolute or relative to the
071: * file name supplied in the Constructor.
072: * <p>
073: * In this class, empty PropertyConfigurations can be built, properties
074: * added and later saved. include statements are (obviously) not supported
075: * if you don't construct a PropertyConfiguration from a file.
076: * <p>
077: * If you want to use the getResourceAsStream() trick to load your
078: * resources without an absolute path, please take a look at the
079: * ClassPropertiesConfiguration which is intended to be used for this.
080: *
081: * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
082: * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
083: * @author <a href="mailto:daveb@miceda-data">Dave Bryson</a>
084: * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
085: * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
086: * @author <a href="mailto:kjohnson@transparent.com">Kent Johnson</a>
087: * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
088: * @author <a href="mailto:ipriha@surfeu.fi">Ilkka Priha</a>
089: * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
090: * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
091: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
092: * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
093: * @author <a href="mailto:oliver.heger@t-online.de">Oliver Heger</a>
094: * @version $Id: PropertiesConfiguration.java,v 1.2 2007/10/16 06:54:48 lexuanttkhtn Exp $
095: */
096: public class PropertiesConfiguration extends
097: BasePropertiesConfiguration implements Configuration {
098:
099: /** Static logger */
100: Log log = LogFactory.getLog(PropertiesConfiguration.class);
101:
102: /** File separator. */
103: protected String fileSeparator = System
104: .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 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: setIncludesAllowed(false);
120: }
121:
122: /**
123: * Creates an empty PropertyConfiguration object with
124: * a Super-Object which is queries for every key.
125: *
126: * @param defaults Configuration defaults to use if key not in file
127: * @throws IOException Error while loading the properties file
128: */
129: public PropertiesConfiguration(Configuration defaults)
130: throws IOException {
131: this ();
132: this .defaults = defaults;
133: }
134:
135: /**
136: * Creates and loads the extended properties from the specified file.
137: * The specified file can contain "include = " properties which then
138: * are loaded and merged into the properties.
139: *
140: * @param fileName The name of the Properties File to load.
141: * @throws IOException Error while loading the properties file
142: */
143: public PropertiesConfiguration(String fileName) throws IOException {
144:
145: load(fileName);
146: }
147:
148: /**
149: * Load the properties from the fileName set by setFileName
150: *
151: * @throws IOException
152: */
153: public void load() throws IOException {
154: load(getFileName());
155: }
156:
157: /**
158: * Load the properties from the given fileName
159: *
160: * @param fileName A properties file to load
161: * @throws IOException
162: */
163: public void load(String fileName) throws IOException {
164: load(getPropertyStream(fileName));
165: }
166:
167: /**
168: * Creates and loads the extended properties from the specified file.
169: *
170: * @param file The name of the Properties File to load.
171: * @param defaults Configuration defaults to use if key not in file
172: * @throws IOException Error while loading the properties file
173: */
174: public PropertiesConfiguration(String file, Configuration defaults)
175: throws IOException {
176: this (file);
177: this .defaults = defaults;
178: }
179:
180: /**
181: * Creates and loads the extended properties from the specified file.
182: *
183: * @param file The name of the Properties File to load.
184: * @param defaultFile The name of a properties file whose values
185: * should be used if a key is not in the file.
186: * @throws IOException Error while loading the properties file
187: */
188: public PropertiesConfiguration(String file, String defaultFile)
189: throws IOException {
190: this (file);
191: if (StringUtils.isNotEmpty(defaultFile)) {
192: this .defaults = new PropertiesConfiguration(defaultFile);
193: }
194: }
195:
196: /**
197: * Gets a resource relative to the supplied base path. If the passed in
198: * resource name is absolute, it is used directly.
199: *
200: * @param resourceName The resource Name
201: * @return An Input Stream
202: * @throws IOException Error while loading the properties file
203: */
204: public InputStream getPropertyStream(String resourceName)
205: throws IOException {
206: InputStream resource = null;
207: URL url = null;
208:
209: try {
210: url = ConfigurationUtils
211: .getURL(getBasePath(), resourceName);
212: } /* try */
213: catch (MalformedURLException uex) {
214: throw new IOException("Cannot obtain URL for resource "
215: + resourceName);
216: } /* catch */
217:
218: resource = url.openStream();
219:
220: setBasePath(url.toString());
221: setIncludesAllowed(true);
222:
223: return resource;
224: }
225:
226: /**
227: * Returns the fileName.
228: * @return String
229: */
230: public String getFileName() {
231: return fileName;
232: }
233:
234: /**
235: * Sets the fileName.
236: * @param fileName The fileName to set
237: */
238: public void setFileName(String fileName) {
239: this .fileName = fileName;
240: }
241:
242: /**
243: * Extend the setBasePath method to turn includes
244: * on and off based on the existence of a base path.
245: *
246: * @param basePath The new basePath to set.
247: */
248: public void setBasePath(String basePath) {
249: super.setBasePath(basePath);
250: setIncludesAllowed(StringUtils.isNotEmpty(basePath));
251: }
252: }
|