1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util.spring;
17
18 import java.util.Properties;
19
20 import org.kuali.common.util.Assert;
21 import org.kuali.common.util.LocationUtils;
22 import org.kuali.common.util.ModeUtils;
23 import org.kuali.common.util.PropertyUtils;
24 import org.kuali.common.util.property.PropertiesContext;
25 import org.springframework.beans.factory.FactoryBean;
26
27
28
29
30 public class PropertiesLoaderFactoryBean extends PropertiesContext implements FactoryBean<Properties> {
31
32 protected Properties global = PropertyUtils.getGlobalProperties();
33 boolean singleton = true;
34
35 @Override
36 public Properties getObject() throws Exception {
37 Assert.notNull(helper, "helper is null");
38 Assert.notNull(locations, "locations are null");
39 Assert.notNull(encoding, "encoding is null");
40 Assert.notNull(missingLocationsMode, "missingLocationsMode is null");
41 Properties global = PropertyUtils.getGlobalProperties();
42 this.properties = PropertyUtils.toEmpty(properties);
43 Properties result = new Properties();
44 for (String location : locations) {
45 Properties resolverProperties = PropertyUtils.combine(properties, result, global);
46 String resolvedLocation = helper.replacePlaceholders(location, resolverProperties);
47 if (LocationUtils.exists(resolvedLocation)) {
48 Properties properties = PropertyUtils.load(location, encoding);
49 result.putAll(properties);
50 } else {
51 ModeUtils.validate(missingLocationsMode, "Skipping non-existent location [" + resolvedLocation + "]");
52 }
53 }
54 return result;
55 }
56
57 @Override
58 public Class<Properties> getObjectType() {
59 return Properties.class;
60 }
61
62 @Override
63 public boolean isSingleton() {
64 return singleton;
65 }
66
67 public void setSingleton(boolean singleton) {
68 this.singleton = singleton;
69 }
70 }