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.ArrayList;
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Properties;
23
24 import org.kuali.common.util.Assert;
25 import org.kuali.common.util.FormatUtils;
26 import org.kuali.common.util.PropertyUtils;
27 import org.kuali.common.util.property.ProjectProperties;
28 import org.kuali.common.util.property.ProjectPropertiesComparator;
29 import org.kuali.common.util.property.PropertiesLoaderContext;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.FactoryBean;
33 import org.springframework.util.CollectionUtils;
34
35
36
37
38 public class ProjectPropertiesLoaderFactoryBean implements FactoryBean<Properties> {
39
40 private static final Logger logger = LoggerFactory.getLogger(ProjectPropertiesLoaderFactoryBean.class);
41
42 List<String> locations;
43 boolean singleton = true;
44 ProjectPropertiesComparator comparator;
45
46 @Override
47 public Properties getObject() throws Exception {
48 Assert.isFalse(CollectionUtils.isEmpty(locations), "locations is empty");
49 long start = System.currentTimeMillis();
50 List<ProjectProperties> pps = new ArrayList<ProjectProperties>();
51
52 for (String location : locations) {
53 Map<String, ProjectProperties> beans = SpringUtils.getAllBeans(location, ProjectProperties.class);
54 logger.info("Located {} sets of project properties", beans.size());
55
56 List<ProjectProperties> list = new ArrayList<ProjectProperties>();
57
58 for (ProjectProperties bean : beans.values()) {
59 list.add(bean);
60 }
61
62 Collections.sort(list);
63 pps.addAll(list);
64 }
65
66
67 Properties properties = new Properties();
68 for (ProjectProperties pp : pps) {
69 for (PropertiesLoaderContext ctx : pp.getLoaderContexts()) {
70 Properties combined = PropertyUtils.combine(properties, ctx.getProperties());
71 ctx.setProperties(combined);
72 Properties loaded = PropertyUtils.load(ctx);
73 properties.putAll(loaded);
74 }
75 }
76 String elapsed = FormatUtils.getTime(System.currentTimeMillis() - start);
77 logger.info("Loaded {} properties. Total time: {}", properties.size(), elapsed);
78 return properties;
79 }
80
81 @Override
82 public Class<Properties> getObjectType() {
83 return Properties.class;
84 }
85
86 @Override
87 public boolean isSingleton() {
88 return singleton;
89 }
90
91 public void setSingleton(boolean singleton) {
92 this.singleton = singleton;
93 }
94
95 public List<String> getLocations() {
96 return locations;
97 }
98
99 public void setLocations(List<String> locations) {
100 this.locations = locations;
101 }
102
103 public ProjectPropertiesComparator getComparator() {
104 return comparator;
105 }
106
107 public void setComparator(ProjectPropertiesComparator comparator) {
108 this.comparator = comparator;
109 }
110
111 }