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