001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of 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,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.config;
018:
019: import java.util.Collection;
020: import java.util.HashMap;
021: import java.util.Map;
022: import java.util.Properties;
023: import java.util.concurrent.ConcurrentHashMap;
024:
025: import org.compass.core.util.ClassUtils;
026:
027: /**
028: * A set of settings that are used to configure the Compass instance.
029: *
030: * @author kimchy
031: */
032: public class CompassSettings {
033:
034: private Properties settings;
035:
036: private final Map<String, HashMap<String, CompassSettings>> groups = new ConcurrentHashMap<String, HashMap<String, CompassSettings>>();
037:
038: private Map<Object, Object> registry = new ConcurrentHashMap<Object, Object>();
039:
040: private ClassLoader classLoader;
041:
042: public CompassSettings() {
043: this (new Properties());
044: }
045:
046: public CompassSettings(ClassLoader classLoader) {
047: this (new Properties());
048: this .classLoader = classLoader;
049: }
050:
051: public CompassSettings(Properties settings) {
052: this .settings = settings;
053: }
054:
055: public void addSettings(Properties settings) {
056: this .settings.putAll(settings);
057: }
058:
059: public void addSettings(CompassSettings settings) {
060: this .settings.putAll(settings.getProperties());
061: }
062:
063: public CompassSettings copy() {
064: CompassSettings copySettings = new CompassSettings(
065: (Properties) settings.clone());
066: copySettings.registry = new ConcurrentHashMap<Object, Object>(
067: registry);
068: copySettings.classLoader = classLoader;
069: return copySettings;
070: }
071:
072: public CompassSettings clear() {
073: this .settings.clear();
074: return this ;
075: }
076:
077: void setClassLoader(ClassLoader classLoader) {
078: this .classLoader = classLoader;
079: }
080:
081: /**
082: * Returns the class loader. If none is defined, return the thread context class loader.
083: */
084: public ClassLoader getClassLoader() {
085: if (classLoader == null) {
086: return Thread.currentThread().getContextClassLoader();
087: }
088: return classLoader;
089: }
090:
091: /**
092: * Returns the direct class loader configured for this settings. <code>null</code>
093: * if none is defined.
094: */
095: public ClassLoader getDirectClassLoader() {
096: return this .classLoader;
097: }
098:
099: public Properties getProperties() {
100: return settings;
101: }
102:
103: public Collection keySet() {
104: return settings.keySet();
105: }
106:
107: public String getSetting(String setting) {
108: return settings.getProperty(setting);
109: }
110:
111: public String getSetting(String setting, String defaultValue) {
112: return settings.getProperty(setting, defaultValue);
113: }
114:
115: public Map<String, CompassSettings> getSettingGroups(
116: String settingPrefix) {
117: if (settingPrefix.charAt(settingPrefix.length() - 1) != '.') {
118: settingPrefix = settingPrefix + ".";
119: }
120: Map<String, CompassSettings> group = groups.get(settingPrefix);
121: if (group != null) {
122: return group;
123: }
124: // we don't really care that it might happen twice
125: HashMap<String, CompassSettings> map = new HashMap<String, CompassSettings>();
126: for (Object o : settings.keySet()) {
127: String setting = (String) o;
128: if (setting.startsWith(settingPrefix)) {
129: String nameValue = setting.substring(settingPrefix
130: .length());
131: int dotIndex = nameValue.indexOf('.');
132: if (dotIndex == -1) {
133: throw new ConfigurationException(
134: "Failed to get setting group for ["
135: + settingPrefix
136: + "] setting prefix and setting ["
137: + setting
138: + "] because of a missing '.'");
139: }
140: String name = nameValue.substring(0, dotIndex);
141: String value = nameValue.substring(dotIndex + 1);
142: CompassSettings groupSettings = map.get(name);
143: if (groupSettings == null) {
144: groupSettings = new CompassSettings();
145: groupSettings.setClassLoader(getClassLoader());
146: map.put(name, groupSettings);
147: }
148: groupSettings.setSetting(value, getSetting(setting));
149: }
150: }
151: groups.put(settingPrefix, map);
152: return map;
153: }
154:
155: public float getSettingAsFloat(String setting, float defaultValue) {
156: String sValue = getSetting(setting);
157: if (sValue == null) {
158: return defaultValue;
159: }
160: return Float.parseFloat(sValue);
161: }
162:
163: public double getSettingAsDouble(String setting, double defaultValue) {
164: String sValue = getSetting(setting);
165: if (sValue == null) {
166: return defaultValue;
167: }
168: return Double.parseDouble(sValue);
169: }
170:
171: public int getSettingAsInt(String setting, int defaultValue) {
172: String sValue = getSetting(setting);
173: if (sValue == null) {
174: return defaultValue;
175: }
176: return Integer.parseInt(sValue);
177: }
178:
179: public long getSettingAsLong(String setting, long defaultValue) {
180: String sValue = getSetting(setting);
181: if (sValue == null) {
182: return defaultValue;
183: }
184: return Long.parseLong(sValue);
185: }
186:
187: public boolean getSettingAsBoolean(String setting,
188: boolean defaultValue) {
189: String sValue = getSetting(setting);
190: if (sValue == null) {
191: return defaultValue;
192: }
193: return Boolean.valueOf(sValue);
194: }
195:
196: public Class getSettingAsClass(String setting, Class clazz)
197: throws ClassNotFoundException {
198: String sValue = getSetting(setting);
199: if (sValue == null) {
200: return clazz;
201: }
202: return ClassUtils.forName(sValue, getClassLoader());
203: }
204:
205: public Class getSettingAsClass(String setting, Class clazz,
206: ClassLoader classLoader) throws ClassNotFoundException {
207: String sValue = getSetting(setting);
208: if (sValue == null) {
209: return clazz;
210: }
211: return ClassUtils.forName(sValue, classLoader);
212: }
213:
214: public CompassSettings setSetting(String setting, String value) {
215: if (value == null) {
216: return this ;
217: }
218: this .settings.setProperty(setting, value);
219: return this ;
220: }
221:
222: public CompassSettings setBooleanSetting(String setting,
223: boolean value) {
224: setSetting(setting, String.valueOf(value));
225: return this ;
226: }
227:
228: public CompassSettings setFloatSetting(String setting, float value) {
229: setSetting(setting, String.valueOf(value));
230: return this ;
231: }
232:
233: public CompassSettings setDoubleSetting(String setting, double value) {
234: setSetting(setting, String.valueOf(value));
235: return this ;
236: }
237:
238: public CompassSettings setIntSetting(String setting, int value) {
239: setSetting(setting, String.valueOf(value));
240: return this ;
241: }
242:
243: public CompassSettings setLongSetting(String setting, long value) {
244: setSetting(setting, String.valueOf(value));
245: return this ;
246: }
247:
248: public CompassSettings setClassSetting(String setting, Class clazz) {
249: setSetting(setting, clazz.getName());
250: return this ;
251: }
252:
253: /**
254: * Sets a group of settings, sharing the same setting prefix. The provided
255: * settings are appended to the settingPrefix, and the matching values are
256: * set.
257: * <p/>
258: * The constructed setting is: settingPrefix + "." + groupName + "." + settings[i].
259: *
260: * @param settingPrefix The prefix used for all settings
261: * @param groupName The name of the setting group
262: * @param settings The settings name appended to settingsPrefix + "." + groupName + "."
263: * @param values The values of the settings matched against the settings parameters
264: * @return This settings instance for method chaining
265: */
266: public CompassSettings setGroupSettings(String settingPrefix,
267: String groupName, String[] settings, String[] values) {
268: if (settings.length != values.length) {
269: throw new IllegalArgumentException(
270: "The settings length must match the value length");
271: }
272: for (int i = 0; i < settings.length; i++) {
273: if (values[i] == null) {
274: continue;
275: }
276: setSetting(settingPrefix + "." + groupName + "."
277: + settings[i], values[i]);
278: }
279: return this ;
280: }
281:
282: /**
283: * ADANCE: An internal compass global registry
284: */
285: public Object getRegistry(Object key) {
286: return registry.get(key);
287: }
288:
289: /**
290: * ADVANCE: An internal compass global registry
291: */
292: public void setRegistry(Object key, Object value) {
293: registry.put(key, value);
294: }
295:
296: /**
297: * ADVANCE: An internal compass global registry
298: */
299: public Object removeRegistry(Object key) {
300: return registry.remove(key);
301: }
302:
303: public String toString() {
304: return settings.toString();
305: }
306: }
|