001: /*
002: * Copyright 2006 Le Duc Bao, Ralf Joachim
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package org.castor.ddlgen;
017:
018: import java.io.IOException;
019: import java.net.MalformedURLException;
020: import java.net.URL;
021: import java.util.Enumeration;
022: import java.util.Properties;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026:
027: /**
028: * Handle the configuration for DDL generator including load configuration files,
029: * manage configuration values.
030: *
031: * @author <a href="mailto:leducbao AT gmail DOT com">Le Duc Bao</a>
032: * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
033: * @version $Revision: 5951 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
034: * @since 1.1
035: */
036: public class Configuration {
037: //--------------------------------------------------------------------------
038:
039: /** The <a href="http://jakarta.apache.org/commons/logging/">Jakarta
040: * Commons Logging</a> instance used for all logging. */
041: private static final Log LOG = LogFactory
042: .getLog(Configuration.class);
043:
044: /** String representation of boolean <code>true</code>. */
045: public static final String TRUE = "true";
046:
047: /** String representation of boolean <code>false</code>. */
048: public static final String FALSE = "false";
049:
050: //--------------------------------------------------------------------------
051:
052: /**
053: * handle configuration.
054: */
055: private final Properties _conf = new Properties();
056:
057: //--------------------------------------------------------------------------
058:
059: /**
060: * Constructor for Configuration.
061: */
062: public Configuration() {
063: super ();
064:
065: addProperties(System.getProperties());
066: }
067:
068: //--------------------------------------------------------------------------
069:
070: /**
071: * get boolean value associated with key in the configuration files.
072: *
073: * @param key key
074: * @return return value associated with key. If not exists, throw an
075: * exception
076: * @throws WrongFormatException format error
077: * @throws KeyNotFoundException key error
078: */
079: public final boolean getBoolValue(final String key)
080: throws WrongFormatException, KeyNotFoundException {
081: String value = this .getStringValue(key);
082:
083: if (value == null) {
084: throw new KeyNotFoundException("can not found key " + key);
085: }
086:
087: if (TRUE.equals(value)) {
088: return true;
089: }
090:
091: if (FALSE.equals(value)) {
092: return false;
093: }
094:
095: throw new WrongFormatException(
096: "require boolean (true/false), receive " + value
097: + " for key=" + key);
098: }
099:
100: /**
101: * get boolean value associated with key in the configuration files.
102: *
103: * @param key key
104: * @param defaultValue default value
105: * @return return value associated with key. If not exists, return the default value
106: */
107: public final boolean getBoolValue(final String key,
108: final boolean defaultValue) {
109: String value = null;
110: try {
111: value = this .getStringValue(key);
112: } catch (KeyNotFoundException e) {
113: return defaultValue;
114: }
115:
116: if (value == null) {
117: return defaultValue;
118: }
119:
120: if (TRUE.equals(value)) {
121: return true;
122: }
123:
124: if (FALSE.equals(value)) {
125: return false;
126: }
127:
128: return defaultValue;
129: }
130:
131: /**
132: * Get property with given name as Integer value. If property is not
133: * available or can not be interpreted as integer null will be returned.
134: *
135: * @param name Name of the property.
136: * @return The configured Integer property or null if property is not
137: * available or can not be interpreted as integer.
138: */
139: public final Integer getInteger(final String name) {
140: String value = _conf.getProperty(name);
141: if (value == null) {
142: return null;
143: }
144: try {
145: return Integer.valueOf(value);
146: } catch (NumberFormatException ex) {
147: return null;
148: }
149: }
150:
151: /**
152: * get String value associated with key in the configuration files.
153: *
154: * @param key key
155: * @return return value associated with key. If not exists, throw an
156: * exception
157: * @throws KeyNotFoundException key error
158: */
159: public final String getStringValue(final String key)
160: throws KeyNotFoundException {
161: String value = (String) _conf.get(key);
162: if (value == null || "".equals(value)) {
163: throw new KeyNotFoundException(
164: "Can not find value correspondence to " + key);
165: }
166: return value;
167: }
168:
169: /**
170: * get String value associated with key in the configuration files.
171: *
172: * @param key key
173: * @param defaultValue default value
174: * @return return value associated with key. If not exists, return default
175: * value
176: */
177: public final String getStringValue(final String key,
178: final String defaultValue) {
179: String value = (String) _conf.get(key);
180: if (value == null || "".equals(value)) {
181: value = defaultValue;
182: }
183: return value;
184: }
185:
186: /**
187: * add properties (key, value) for configuration, the existed item will
188: * be overwrited.
189: * @param props properties
190: */
191: public final void addProperties(final Properties props) {
192: if (props != null) {
193: Object key;
194: Object value;
195: for (Enumeration e = props.keys(); e.hasMoreElements();) {
196: key = e.nextElement();
197: value = props.get(key);
198: _conf.put(key, value);
199: }
200: }
201: }
202:
203: /**
204: * add properties (key, value) for configuration, the existed item will
205: * be overwrited.
206: *
207: * @param filename a properties file
208: * @throws GeneratorException generator error
209: */
210: public final void addProperties(final String filename)
211: throws GeneratorException {
212: Properties props = new Properties();
213: URL url = null;
214: try {
215: try {
216: url = new URL(filename);
217: } catch (MalformedURLException ex) {
218: url = getClass().getClassLoader().getResource(filename);
219: }
220: if (url != null) {
221: props.load(url.openStream());
222: addProperties(props);
223: } else {
224: String msg = "Could not obtain the configuration file '"
225: + filename + "' from the Castor JAR.";
226: LOG.error(msg);
227: throw new GeneratorException(msg);
228: }
229: } catch (IOException ex) {
230: String msg = "Could not read the configuration file '"
231: + url.toExternalForm() + "' from the Castor JAR.";
232: LOG.error(msg, ex);
233: throw new GeneratorException(msg, ex);
234: }
235: }
236:
237: /**
238: * set property value, this will overwrite the loaded value.
239: *
240: * @param key key
241: * @param value value
242: */
243: public final void setProperty(final String key, final String value) {
244: _conf.put(key, value);
245: }
246:
247: //--------------------------------------------------------------------------
248: }
|