1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.core.impl.services;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.log4j.Logger;
22 import org.kuali.rice.core.api.config.property.ConfigContext;
23 import org.kuali.rice.core.api.config.property.ConfigurationService;
24 import org.kuali.rice.core.api.exception.RiceRuntimeException;
25 import org.kuali.rice.core.api.util.Truth;
26
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.net.URL;
30 import java.util.Collections;
31 import java.util.Iterator;
32 import java.util.Map;
33 import java.util.Properties;
34 import java.util.ResourceBundle;
35
36
37
38
39
40
41
42 public class ConfigurationServiceImpl implements ConfigurationService {
43
44
45
46 private static final String MESSAGE_RESOURCES = "rice.struts.message.resources";
47
48 private final PropertyHolder propertyHolder = new PropertyHolder();
49
50
51
52
53 public ConfigurationServiceImpl() {
54 this.propertyHolder.getHeldProperties().putAll(ConfigContext.getCurrentContextConfig().getProperties());
55
56
57 String propertyConfig = (String) ConfigContext.getCurrentContextConfig().getProperties().get(MESSAGE_RESOURCES);
58 propertyConfig = removeSpacesAround(propertyConfig);
59
60 String[] bundleNames = StringUtils.split(propertyConfig, ",");
61 for (String bundleName : bundleNames) {
62 ResourceBundle bundle = ResourceBundle.getBundle(bundleName);
63
64 for (String key : bundle.keySet()) {
65 String message = bundle.getString(key);
66 this.propertyHolder.getHeldProperties().put(key, message);
67 }
68 }
69 }
70
71
72
73
74 @Override
75 public String getPropertyValueAsString(String key) {
76 if (key == null) {
77 throw new IllegalArgumentException("invalid (null) key");
78 }
79
80 return this.propertyHolder.getProperty(key);
81 }
82
83
84
85
86 @Override
87 public boolean getPropertyValueAsBoolean(String key) {
88 if (key == null) {
89 throw new IllegalArgumentException("invalid (null) key");
90 }
91
92 String property = this.propertyHolder.getProperty(key);
93 Boolean b = Truth.strToBooleanIgnoreCase(property);
94 if (b == null) {
95 return false;
96 }
97
98 return b;
99 }
100
101
102
103
104 @Override
105 public Map<String, String> getAllProperties() {
106 return (Map) Collections.unmodifiableMap(propertyHolder.getHeldProperties());
107 }
108
109
110
111
112
113
114
115
116
117
118
119 private String removeSpacesAround(String csv) {
120 if (csv == null) {
121 return null;
122 }
123
124 final StringBuilder result = new StringBuilder();
125 for (final String value : csv.split(",")) {
126 if (!"".equals(value.trim())) {
127 result.append(value.trim());
128 result.append(",");
129 }
130 }
131
132
133 int i = result.lastIndexOf(",");
134 if (i != -1) {
135 result.deleteCharAt(i);
136 }
137
138 return result.toString();
139 }
140
141
142
143
144 protected static interface PropertySource {
145
146
147
148
149 public Properties loadProperties();
150 }
151
152
153
154
155 protected static class PropertyHolder {
156 private static Logger LOG = Logger.getLogger(PropertyHolder.class);
157
158 Properties heldProperties;
159
160
161
162
163 public PropertyHolder() {
164 this.heldProperties = new Properties();
165 }
166
167
168
169
170 public boolean isEmpty() {
171 return this.heldProperties.isEmpty();
172 }
173
174
175
176
177
178
179 public boolean containsKey(String key) {
180 validateKey(key);
181
182 return this.heldProperties.containsKey(key);
183 }
184
185
186
187
188
189
190 public String getProperty(String key) {
191 validateKey(key);
192
193 return this.heldProperties.getProperty(key);
194 }
195
196
197
198
199
200
201
202
203
204 public void setProperty(String key, String value) {
205 setProperty(null, key, value);
206 }
207
208
209
210
211
212
213
214
215
216
217 public void setProperty(PropertySource source, String key, String value) {
218 validateKey(key);
219 validateValue(value);
220
221 if (containsKey(key)) {
222 if (source != null && source instanceof FilePropertySource && ((FilePropertySource) source)
223 .isAllowOverrides()) {
224 LOG.info("Duplicate Key: Override is enabled [key="
225 + key
226 + ", new value="
227 + value
228 + ", old value="
229 + this.heldProperties.getProperty(key)
230 + "]");
231 } else {
232 throw new RiceRuntimeException("duplicate key '" + key + "'");
233 }
234 }
235 this.heldProperties.setProperty(key, value);
236 }
237
238
239
240
241
242
243
244 public void clearProperty(String key) {
245 validateKey(key);
246
247 this.heldProperties.remove(key);
248 }
249
250
251
252
253
254
255
256
257 public void loadProperties(PropertySource source) {
258 if (source == null) {
259 throw new IllegalArgumentException("invalid (null) source");
260 }
261
262 Properties newProperties = source.loadProperties();
263
264 for (Iterator i = newProperties.keySet().iterator(); i.hasNext(); ) {
265 String key = (String) i.next();
266 setProperty(source, key, newProperties.getProperty(key));
267 }
268 }
269
270
271
272
273 public void clearProperties() {
274 this.heldProperties.clear();
275 }
276
277
278
279
280 public Iterator getKeys() {
281 return this.heldProperties.keySet().iterator();
282 }
283
284
285
286
287
288 private void validateKey(String key) {
289 if (key == null) {
290 throw new IllegalArgumentException("invalid (null) key");
291 }
292 }
293
294
295
296
297 private void validateValue(String value) {
298 if (value == null) {
299 throw new IllegalArgumentException("invalid (null) value");
300 }
301 }
302
303 public Properties getHeldProperties() {
304 return heldProperties;
305 }
306
307 public void setHeldProperties(Properties heldProperties) {
308 this.heldProperties = heldProperties;
309 }
310 }
311
312
313
314
315 protected static class FilePropertySource implements PropertySource {
316 private static Log log = LogFactory.getLog(FilePropertySource.class);
317
318 private String fileName;
319 private boolean allowOverrides;
320
321 public void setFileName(String fileName) {
322 this.fileName = fileName;
323 }
324
325 public String getFileName() {
326 return this.fileName;
327 }
328
329 public boolean isAllowOverrides() {
330 return this.allowOverrides;
331 }
332
333 public void setAllowOverrides(boolean allowOverrides) {
334 this.allowOverrides = allowOverrides;
335 }
336
337
338
339
340
341
342
343
344 public Properties loadProperties() {
345 if (StringUtils.isBlank(getFileName())) {
346 throw new IllegalStateException("invalid (blank) fileName");
347 }
348
349 Properties properties = new Properties();
350
351 ClassLoader loader = Thread.currentThread().getContextClassLoader();
352 URL url = loader.getResource(getFileName());
353 if (url == null) {
354 throw new RiceRuntimeException("unable to locate properties file '" + getFileName() + "'");
355 }
356
357 InputStream in = null;
358
359 try {
360 in = url.openStream();
361 properties.load(in);
362 } catch (IOException e) {
363 throw new RiceRuntimeException("error loading from properties file '" + getFileName() + "'", e);
364 } finally {
365 if (in != null) {
366 try {
367 in.close();
368 } catch (IOException e) {
369 log.error("caught exception closing InputStream: " + e);
370 }
371
372 }
373 }
374
375 return properties;
376 }
377 }
378 }