1 /**
2 * Copyright 2005-2011 The Kuali Foundation
3 *
4 * Licensed under the Educational Community License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/ecl2.php
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.kuali.rice.ken.util;
17
18 import java.io.IOException;
19 import java.util.Properties;
20
21 import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
22 import org.springframework.core.io.FileSystemResourceLoader;
23 import org.springframework.core.io.Resource;
24 import org.springframework.util.DefaultPropertiesPersister;
25
26 /**
27 * PropertyPlaceholderConfigurer subclass that overrides any statically defined properties and properties locations,
28 * with configuration settings from a properties file specified in a System property.
29 * @see PropertyPlaceholderConfigurer
30 * @author Kuali Rice Team (rice.collab@kuali.org)
31 */
32 public class NotificationPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
33 private static final String CFG_LOCATION_PROPERTY = "notification.config";
34
35 /**
36 * @see org.springframework.core.io.support.PropertiesLoaderSupport#mergeProperties()
37 */
38 @Override
39 protected Properties mergeProperties() throws IOException {
40 Properties properties = super.mergeProperties();
41 return transformProperties(properties);
42 }
43
44 /**
45 * This method consolidates config properties.
46 * @param properties
47 * @return Properties
48 * @throws IOException
49 */
50 protected Properties transformProperties(Properties properties) throws IOException {
51 String cfgLocation = System.getProperty(CFG_LOCATION_PROPERTY);
52
53 if (cfgLocation != null) {
54 Resource resource = new FileSystemResourceLoader().getResource(cfgLocation);
55 if (resource != null && resource.exists()) {
56 new DefaultPropertiesPersister().load(properties, resource.getInputStream());
57 }
58 }
59
60 return properties;
61 }
62
63 /* I would have liked to just dynamically add a new Resource to the list of resource locations and
64 * delegate entirely to the superclass hierarchy for loading, but unfortunately the locations member
65 * is inaccessible, so we have to load the properties into the existing properties object ourselves,
66 * in the process overridding any existing entries
67 */
68 // public void afterPropertiesSet() throws Exception {
69 // String cfg = System.getProperty(CFG_LOCATION_PROPERTY);
70 // if (cfg == null) return;
71 //
72 // Resource resource = new FileSystemResourceLoader().getResource(cfg);
73 // // add resource to locations
74 // }
75 }