001: /**
002: * Objective Database Abstraction Layer (ODAL)
003: * Copyright (c) 2004, The ODAL Development Group
004: * All rights reserved.
005: * For definition of the ODAL Development Group please refer to LICENCE.txt file
006: *
007: * Distributable under LGPL license.
008: * See terms of license at gnu.org.
009: */package com.completex.objective.util;
010:
011: import com.completex.objective.components.sdl.reader.SdlReader;
012: import com.completex.objective.components.sdl.reader.impl.SdlReaderImpl;
013:
014: import java.util.*;
015: import java.io.*;
016:
017: /**
018: * @author Gennady Krizhevsky
019: */
020: public class PropertyMap extends Properties {
021:
022: private SdlReader sdlReader = new SdlReaderImpl();
023:
024: public PropertyMap() {
025: }
026:
027: /**
028: * Constructs a new PropertyMap with the same mappings as the given Properties
029: *
030: * @param prop the properties whose mappings are to be placed in this property map
031: * @throws NullPointerException if the specified prop Properties is null
032: */
033: public PropertyMap(Map prop) {
034: this ();
035: putAll(prop);
036: }
037:
038: public synchronized void loadSdl(String configPath)
039: throws IOException {
040: loadSdl(new BufferedReader(new FileReader(configPath)));
041: }
042:
043: public synchronized void loadSdl(Reader reader) throws IOException {
044: Map map = (Map) sdlReader.read(reader);
045: putAll(map);
046: }
047:
048: public synchronized void putAll(Map t) {
049: Iterator i = t.entrySet().iterator();
050: while (i.hasNext()) {
051: Map.Entry e = (Map.Entry) i.next();
052: if (e.getValue() != null) {
053: put(e.getKey(), e.getValue());
054: }
055: }
056: }
057:
058: public Object get(String key, boolean required) {
059: Object value = get(key);
060: return TypeUtil.resolveValue(key, value, null, required);
061: }
062:
063: public Object get(String key, Object defaultValue) {
064: return get(key, defaultValue, false);
065: }
066:
067: public Object get(String key, Object defaultValue, boolean required) {
068: Object value = get(key);
069: return TypeUtil
070: .resolveValue(key, value, defaultValue, required);
071: }
072:
073: public StringBuffer getPropertyStringBuffer(String key) {
074: return getPropertyStringBuffer(key, false);
075: }
076:
077: public StringBuffer getPropertyStringBuffer(String key,
078: boolean required) {
079: String prop = getProperty(key, required);
080: return prop == null ? null : new StringBuffer(prop);
081: }
082:
083: public String getProperty(String key, String defaultValue,
084: boolean required) {
085: return (String) get(key, defaultValue, required);
086: }
087:
088: public String getProperty(String key, boolean required) {
089: return (String) get(key, required);
090: }
091:
092: public String getPropertyTrimmed(String key) {
093: return getPropertyTrimmed(key, false);
094: }
095:
096: public String getPropertyTrimmed(String key, String defaultValue,
097: boolean required) {
098: String value = (String) get(key, defaultValue, required);
099: return value == null ? null : value.trim();
100: }
101:
102: public String getPropertyTrimmed(String key, boolean required) {
103: return getPropertyTrimmed(key, null, required);
104: }
105:
106: public Boolean getBooleanObj(String key) {
107: return getBooleanObj(key, false);
108: }
109:
110: public Boolean getBooleanObj(String key, boolean required) {
111: return TypeUtil.getBooleanObj(key, get(key), required);
112: }
113:
114: public Boolean getBooleanObj(String key, Boolean defaultValue,
115: boolean required) {
116: return TypeUtil.getBooleanObj(key, get(key), required,
117: defaultValue);
118: }
119:
120: public boolean getBoolean(String key) {
121: return getBoolean(key, false);
122: }
123:
124: public boolean getBoolean(String key, boolean required) {
125: return getBoolean(key, null, required);
126: }
127:
128: public boolean getBoolean(String key, Boolean defaultValue,
129: boolean required) {
130: return TypeUtil.getBoolean(key, get(key), defaultValue,
131: required);
132: }
133:
134: public Long getLongObj(String key) {
135: return getLongObj(key, false);
136: }
137:
138: public Long getLongObj(String key, boolean required) {
139: return TypeUtil.getLongObj(key, get(key), required);
140: }
141:
142: public Long getLongObj(String key, Long defaultValue) {
143: return TypeUtil.getLongObj(key, get(key), defaultValue);
144: }
145:
146: public long getLong(String key) {
147: return getLong(key, false);
148: }
149:
150: public long getLong(String key, boolean required) {
151: return TypeUtil.getLong(key, get(key), required);
152: }
153:
154: public long getLong(String key, int defaultValue) {
155: return TypeUtil.getLong(key, get(key), defaultValue);
156: }
157:
158: public Integer getIntObj(String key) {
159: return getIntObj(key, false);
160: }
161:
162: public Integer getIntObj(String key, boolean required) {
163: return TypeUtil.getIntObj(key, get(key), required);
164: }
165:
166: public Integer getIntObj(String key, Integer defaultValue) {
167: return TypeUtil.getIntObj(key, get(key), defaultValue);
168: }
169:
170: public int getInt(String key) {
171: return getInt(key, false);
172: }
173:
174: public int getInt(String key, boolean required) {
175: return TypeUtil.getInt(key, get(key), required);
176: }
177:
178: public int getInt(String key, int defaultValue) {
179: return TypeUtil.getInt(key, get(key), defaultValue);
180: }
181:
182: public PropertyMap getPropertyMap(String key) {
183: Map map = (Map) get(key, false);
184: return toPropertyMap(map);
185: }
186:
187: public PropertyMap getPropertyMap(String key, boolean required) {
188: Map map = (Map) get(key, required);
189: return toPropertyMap(map);
190: }
191:
192: public Map getMap(String key) {
193: return getMap(key, false);
194: }
195:
196: public Map getMap(String key, boolean required) {
197: return (Map) get(key, required);
198: }
199:
200: public List getList(String key) {
201: return getList(key, false);
202: }
203:
204: public List getList(String key, boolean required) {
205: return (List) get(key, required);
206: }
207:
208: public static PropertyMap toPropertyMap(Map map) {
209: return toPropertyMap(map, false);
210: }
211:
212: public static PropertyMap toPropertyMap(Map map, boolean required) {
213: if (map == null && required) {
214: throw new IllegalArgumentException(
215: "Map parameter is require but is null");
216: }
217: if (map instanceof PropertyMap) {
218: return ((PropertyMap) map);
219: } else {
220: return map == null ? null : new PropertyMap(map);
221: }
222: }
223:
224: /**
225: * Resolves template token values (${token}) to a proper ones in accord with env Properties
226: * passed.
227: *
228: * @param map initial map possibly containing template token values
229: * @param env Value resolution object
230: * @return Map with resolved values (the same instance as a parameter one)
231: */
232: public static Map resolveParameters(Map map, Properties env) {
233: resolveMapParameters(map, env);
234: return map;
235: }
236:
237: private static void resolveMapParameters(Map map, Properties env) {
238: LinkedHashMap temp = new LinkedHashMap(map);
239: for (Iterator it = temp.keySet().iterator(); it.hasNext();) {
240: Object key = it.next();
241: Object value = temp.get(key);
242: Object resolvedValue = resolveValueParameter(value, env);
243: if (resolvedValue instanceof String) {
244: map.put(key, resolvedValue);
245: }
246: }
247: }
248:
249: private static void resolveListParameter(List list, Properties env) {
250: ArrayList temp = new ArrayList(list);
251: for (int i = 0; i < temp.size(); i++) {
252: Object value = list.get(i);
253: Object resolvedValue = resolveValueParameter(value, env);
254: if (resolvedValue instanceof String) {
255: list.set(i, resolvedValue);
256: }
257: }
258: }
259:
260: private static Object resolveStringParameter(String value,
261: Properties env) {
262: return StringUtil.resolveParameters(value, env);
263: }
264:
265: private static Object resolveValueParameter(Object value,
266: Properties env) {
267: Object resovedValue = null;
268: if (value instanceof String) {
269: resovedValue = resolveStringParameter((String) value, env);
270: } else if (value instanceof List) {
271: resolveListParameter((List) value, env);
272: } else if (value instanceof Map) {
273: resolveMapParameters((Map) value, env);
274: }
275: return resovedValue;
276: }
277:
278: /**
279: * Creates and loads new PropertyMap from java property file
280: *
281: * @param envPath
282: * @return
283: * @throws IOException
284: */
285: public static PropertyMap createAndLoad(String envPath)
286: throws IOException {
287: PropertyMap envPropertyMap = null;
288: if (envPath != null) {
289: envPropertyMap = new PropertyMap();
290: envPropertyMap.load(new FileInputStream(envPath));
291: }
292: return envPropertyMap;
293: }
294:
295: /**
296: * Creates and loads new PropertyMap from SDL file
297: *
298: * @param envPath
299: * @return
300: * @throws IOException
301: */
302: public static PropertyMap createAndLoadSdl(String envPath)
303: throws IOException {
304: PropertyMap envPropertyMap = null;
305: if (envPath != null) {
306: envPropertyMap = new PropertyMap();
307: envPropertyMap.loadSdl(envPath);
308: }
309: return envPropertyMap;
310: }
311:
312: public static void main(String[] args) {
313: HashMap map = new HashMap();
314: map.put("key-a", "value-a");
315: map.put("key-boolean", "true");
316:
317: PropertyMap propertyMap = PropertyMap.toPropertyMap(map);
318:
319: boolean b = propertyMap.getBoolean("key-boolean", true);
320: System.out.println("b = " + b);
321:
322: PropertyMap cloned = (PropertyMap) propertyMap.clone();
323: System.out.println("cloned: " + cloned);
324: }
325:
326: }
|