001: /*
002: * SystemProperties.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.util;
023:
024: import java.awt.Color;
025: import java.io.IOException;
026: import java.util.HashMap;
027: import java.util.Map;
028: import java.util.Properties;
029:
030: /* ----------------------------------------------------------
031: * CVS NOTE: Changes to the CVS repository prior to the
032: * release of version 3.0.0beta1 has meant a
033: * resetting of CVS revision numbers.
034: * ----------------------------------------------------------
035: */
036:
037: /**
038: * A convenience class to store and return system properties
039: * not limited to the <code>String</code> datatype provided
040: * by <code>java.util.Properties</code>.<br>
041: * It provides the set and get methods for integer, string,
042: * boolean and colour datatypes [and objects] where applicable.
043: *
044: * @author Takis Diakoumis
045: * @version $Revision: 1.4 $
046: * @date $Date: 2006/05/14 06:56:07 $
047: */
048: public class SystemProperties {
049:
050: /** properties cache */
051: private static Map<String, Properties> map;
052:
053: private SystemProperties() {
054: }
055:
056: /**
057: * Loads and stores in the cache the properties file at the
058: * specified file system path. The properties are stored in the
059: * cache under the specified name and accessed using that name.
060: *
061: * @param name - the cache name to store the props
062: * @param path - the file system to the properties file
063: */
064: public static void loadProperties(String name, String path) {
065: try {
066: Properties properties = FileUtils.loadProperties(path);
067: if (properties != null) {
068:
069: if (map == null) {
070: map = new HashMap<String, Properties>();
071: }
072:
073: map.put(name, properties);
074: }
075: } catch (IOException e) {
076: throw new RuntimeException(e);
077: }
078: }
079:
080: /**
081: * Loads and stores in the cache the properties file at the
082: * specified file system path. The properties are stored in the
083: * cache under the specified name and accessed using that name.
084: *
085: * @param name - the cache name to store the props
086: * @param path - the file system to the properties file
087: */
088: public static void loadProperties(String name, String path,
089: Properties defaults) {
090: try {
091: Properties properties = FileUtils.loadProperties(path,
092: defaults);
093: if (properties != null) {
094:
095: if (map == null) {
096: map = new HashMap<String, Properties>();
097: }
098:
099: map.put(name, properties);
100: }
101: } catch (IOException e) {
102: throw new RuntimeException(e);
103: }
104: }
105:
106: /**
107: * Loads and stores in the cache the properties file at the
108: * specified package resource path. The properties are stored
109: * in the cache under the specified name and accessed using that name.
110: *
111: * @param name - the cache name to store the props
112: * @param path - the package resource path to the properties file
113: */
114: public static void loadPropertiesResource(String name, String path) {
115: try {
116: Properties properties = FileUtils
117: .loadPropertiesResource(path);
118: if (properties != null) {
119:
120: if (map == null) {
121: map = new HashMap<String, Properties>();
122: }
123:
124: map.put(name, properties);
125: }
126: } catch (IOException e) {
127: throw new RuntimeException(e);
128: }
129: }
130:
131: /**
132: * Returns the properties object with the specified name in the cache.
133: *
134: * @param name - the name assigned to the properties bundle
135: * @return the properties object or null if doesn't exist
136: */
137: public static Properties getProperties(String name) {
138: if (map.containsKey(name)) {
139: return map.get(name);
140: }
141: return null;
142: }
143:
144: public static final boolean containsKey(String propertiesName,
145: String key) {
146: Properties properties = getProperties(propertiesName);
147: if (properties != null) {
148: return properties.containsKey(key);
149: }
150: return false;
151: }
152:
153: /**
154: * Sets a property with a <code>String</code> value<br>
155: *
156: * @param key - the property key
157: * @param value - the property value
158: */
159: public static final void setStringProperty(String propertiesName,
160: String key, String value) {
161: Properties properties = getProperties(propertiesName);
162: if (properties != null) {
163: properties.setProperty(key, value);
164: }
165: }
166:
167: /**
168: * Sets a property using the <code>Properties</code>
169: * method <code>setProperty(key, value)</code><br>
170: *
171: * @param key - the property key
172: * @param value - the property value
173: */
174: public static final void setProperty(String propertiesName,
175: String key, String value) {
176: Properties properties = getProperties(propertiesName);
177: if (properties != null) {
178: properties.setProperty(key, value);
179: }
180: }
181:
182: /**
183: * Sets a property with an <code>int</code> value<br>
184: *
185: * @param key - the property key
186: * @param value - the property value
187: */
188: public static final void setIntProperty(String propertiesName,
189: String key, int value) {
190: Properties properties = getProperties(propertiesName);
191: if (properties != null) {
192: properties.setProperty(key, Integer.toString(value));
193: }
194: }
195:
196: /**
197: * Sets a property with a <code>boolean</code> value<br>
198: *
199: * @param key - the property key
200: * @param value - the property value
201: */
202: public static final void setBooleanProperty(String propertiesName,
203: String key, boolean value) {
204: Properties properties = getProperties(propertiesName);
205: if (properties != null) {
206: properties.setProperty(key, value ? "true" : "false");
207: }
208: }
209:
210: /**
211: * Sets a property with a <code>Color</code> value<br>
212: *
213: * @param key - the property key
214: * @param value - the property value
215: */
216: public static final void setColourProperty(String propertiesName,
217: String key, Color value) {
218: Properties properties = getProperties(propertiesName);
219: if (properties != null) {
220: properties.setProperty(key, Integer
221: .toString(value.getRGB()));
222: }
223: }
224:
225: /**
226: * Retrieves a property of type <code>int</code><br>
227: *
228: * @param key - the property key
229: * @return - the <code>int</code> value
230: */
231: public static final int getIntProperty(String propertiesName,
232: String key) {
233: Properties properties = getProperties(propertiesName);
234: if (properties != null) {
235: return Integer.parseInt(properties.getProperty(key));
236: }
237: return -1;
238: }
239:
240: /**
241: * Retrieves a property of type <code>boolean</code><br>
242: *
243: * @param key - the property key
244: * @return the <code>boolean</code> value
245: */
246: public static final boolean getBooleanProperty(
247: String propertiesName, String key) {
248: Properties properties = getProperties(propertiesName);
249: if (properties != null) {
250: return Boolean.valueOf(properties.getProperty(key))
251: .booleanValue();
252: }
253: return false;
254: }
255:
256: /**
257: * Retrieves a property of object type <code>Color</code><br>
258: *
259: * @param key - the property key
260: * @return the <code>Color</code> value
261: */
262: public static final Color getColourProperty(String propertiesName,
263: String key) {
264: Properties properties = getProperties(propertiesName);
265: if (properties != null) {
266: return new Color(Integer.parseInt(properties
267: .getProperty(key)));
268: }
269: return null;
270: }
271:
272: /**
273: * Retrieves a property using the <code>Properties</code>
274: * method <code>getProperty(key)</code><br>
275: *
276: * @param key - the property key
277: * @return the property value
278: */
279: public static final String getProperty(String propertiesName,
280: String key) {
281: Properties properties = getProperties(propertiesName);
282: if (properties != null) {
283: return properties.getProperty(key);
284:
285: }
286: return null;
287: }
288:
289: /**
290: * Retrieves a property of object type <code>String</code><br>
291: *
292: * @param key - the property key
293: * @return the <code>String</code> value
294: */
295: public static final String getStringProperty(String propertiesName,
296: String key) {
297: return getProperty(propertiesName, key);
298: }
299:
300: /**
301: * Returns the property with the specified name, formatting it with
302: * the <code>java.text.MessageFormat.format()</code> method.
303: *
304: * @param key - the property key
305: * @param args The positional parameters
306: */
307: public static final String getProperty(String propertiesName,
308: String name, Object[] args) {
309:
310: if (name == null) {
311: return null;
312: }
313:
314: Properties properties = getProperties(propertiesName);
315: if (properties == null) {
316: return null;
317: }
318:
319: if (args == null) {
320: return properties.getProperty(name, name);
321: } else {
322: return java.text.MessageFormat.format(properties
323: .getProperty(name, name), args);
324: }
325:
326: }
327:
328: }
|