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.Map;
020: import java.util.Properties;
021:
022: /**
023: * Runtime settings for Compass applies on the Session level. Reading settings
024: * is first perfomed against the runtime settings set, and then the global settings.
025: * Writing settings will only apply on the runtime settings and not affect the
026: * global settings.
027: *
028: * @author kimchy
029: */
030: public class RuntimeCompassSettings extends CompassSettings {
031:
032: private CompassSettings globalSettings;
033:
034: private CompassSettings runtimeSettings;
035:
036: public RuntimeCompassSettings(CompassSettings globalSettings) {
037: this .globalSettings = globalSettings;
038: this .runtimeSettings = new CompassSettings();
039: }
040:
041: public void addSettings(Properties settings) {
042: runtimeSettings.addSettings(settings);
043: }
044:
045: public void addSettings(CompassSettings settings) {
046: runtimeSettings.addSettings(settings);
047: }
048:
049: public CompassSettings copy() {
050: RuntimeCompassSettings copy = new RuntimeCompassSettings(
051: globalSettings);
052: copy.runtimeSettings = runtimeSettings.copy();
053: return copy;
054: }
055:
056: public String getSetting(String setting) {
057: String value = runtimeSettings.getSetting(setting);
058: if (value != null) {
059: return value;
060: }
061: return globalSettings.getSetting(setting);
062: }
063:
064: public String getSetting(String setting, String defaultValue) {
065: String value = runtimeSettings.getSetting(setting);
066: if (value != null) {
067: return value;
068: }
069: return globalSettings.getSetting(setting, defaultValue);
070: }
071:
072: public Map<String, CompassSettings> getSettingGroups(
073: String settingPrefix) {
074: Map group = runtimeSettings.getSettingGroups(settingPrefix);
075: if (group.size() != 0) {
076: return group;
077: }
078: return globalSettings.getSettingGroups(settingPrefix);
079: }
080:
081: public float getSettingAsFloat(String setting, float defaultValue) {
082: String value = runtimeSettings.getSetting(setting);
083: if (value != null) {
084: return runtimeSettings.getSettingAsFloat(setting,
085: defaultValue);
086: }
087: return globalSettings.getSettingAsFloat(setting, defaultValue);
088: }
089:
090: public int getSettingAsInt(String setting, int defaultValue) {
091: String value = runtimeSettings.getSetting(setting);
092: if (value != null) {
093: return runtimeSettings.getSettingAsInt(setting,
094: defaultValue);
095: }
096: return globalSettings.getSettingAsInt(setting, defaultValue);
097: }
098:
099: public long getSettingAsLong(String setting, long defaultValue) {
100: String value = runtimeSettings.getSetting(setting);
101: if (value != null) {
102: return runtimeSettings.getSettingAsLong(setting,
103: defaultValue);
104: }
105: return globalSettings.getSettingAsLong(setting, defaultValue);
106: }
107:
108: public boolean getSettingAsBoolean(String setting,
109: boolean defaultValue) {
110: String value = runtimeSettings.getSetting(setting);
111: if (value != null) {
112: return runtimeSettings.getSettingAsBoolean(setting,
113: defaultValue);
114: }
115: return globalSettings
116: .getSettingAsBoolean(setting, defaultValue);
117: }
118:
119: public Class getSettingAsClass(String setting, Class clazz)
120: throws ClassNotFoundException {
121: String value = runtimeSettings.getSetting(setting);
122: if (value != null) {
123: return runtimeSettings.getSettingAsClass(setting, clazz);
124: }
125: return globalSettings.getSettingAsClass(setting, clazz);
126: }
127:
128: public Class getSettingAsClass(String setting, Class clazz,
129: ClassLoader classLoader) throws ClassNotFoundException {
130: String value = runtimeSettings.getSetting(setting);
131: if (value != null) {
132: return runtimeSettings.getSettingAsClass(setting, clazz,
133: classLoader);
134: }
135: return globalSettings.getSettingAsClass(setting, clazz,
136: classLoader);
137: }
138:
139: public CompassSettings setSetting(String setting, String value) {
140: runtimeSettings.setSetting(setting, value);
141: return this ;
142: }
143:
144: public CompassSettings setBooleanSetting(String setting,
145: boolean value) {
146: runtimeSettings.setBooleanSetting(setting, value);
147: return this ;
148: }
149:
150: public CompassSettings setFloatSetting(String setting, float value) {
151: runtimeSettings.setFloatSetting(setting, value);
152: return this ;
153: }
154:
155: public CompassSettings setIntSetting(String setting, int value) {
156: runtimeSettings.setIntSetting(setting, value);
157: return this ;
158: }
159:
160: public CompassSettings setLongSetting(String setting, long value) {
161: runtimeSettings.setLongSetting(setting, value);
162: return this ;
163: }
164:
165: public CompassSettings setClassSetting(String setting, Class clazz) {
166: runtimeSettings.setClassSetting(setting, clazz);
167: return this ;
168: }
169:
170: public CompassSettings setGroupSettings(String settingPrefix,
171: String groupName, String[] settings, String[] values) {
172: runtimeSettings.setGroupSettings(settingPrefix, groupName,
173: settings, values);
174: return this ;
175: }
176:
177: public Object getRegistry(Object key) {
178: return globalSettings.getRegistry(key);
179: }
180:
181: public void setRegistry(Object key, Object value) {
182: globalSettings.setRegistry(key, value);
183: }
184:
185: public Object removeRegistry(Object key) {
186: return globalSettings.removeRegistry(key);
187: }
188:
189: public String toString() {
190: StringBuffer sb = new StringBuffer();
191: sb.append("Global [").append(globalSettings).append("]");
192: sb.append("Runtime [").append(runtimeSettings).append("]");
193: return sb.toString();
194: }
195: }
|