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.Comparator;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Properties;
25
26 import org.kuali.common.util.Assert;
27 import org.kuali.common.util.PropertyUtils;
28 import org.kuali.common.util.log.LoggerUtils;
29 import org.kuali.common.util.properties.Location;
30 import org.kuali.common.util.properties.PropertiesService;
31 import org.kuali.common.util.property.ImmutableProperties;
32 import org.kuali.common.util.spring.service.PropertySourceContext;
33 import org.kuali.common.util.spring.service.SpringContext;
34 import org.slf4j.Logger;
35 import org.springframework.beans.factory.BeanFactoryUtils;
36 import org.springframework.context.ConfigurableApplicationContext;
37 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
38 import org.springframework.core.env.ConfigurableEnvironment;
39 import org.springframework.core.env.EnumerablePropertySource;
40 import org.springframework.core.env.MutablePropertySources;
41 import org.springframework.core.env.PropertiesPropertySource;
42 import org.springframework.core.env.PropertySource;
43
44 import com.google.common.base.Preconditions;
45
46 public class PropertySourceUtils {
47
48 private static final String PROPERTIES_PROPERTY_SOURCE = "propertiesPropertySource";
49
50 private static final Logger logger = LoggerUtils.make();
51
52
53
54
55 public static PropertySource<?> getDefaultPropertySource() {
56 return new PropertiesPropertySource(PROPERTIES_PROPERTY_SOURCE, PropertyUtils.getGlobalProperties());
57 }
58
59
60
61
62 public static PropertySource<?> getPropertySource(Properties properties) {
63 return new PropertiesPropertySource(PROPERTIES_PROPERTY_SOURCE, PropertyUtils.getGlobalProperties(properties));
64 }
65
66
67
68
69 public static PropertySource<?> getPropertySource(PropertiesService service, List<Location> locations) {
70 return getPropertySource(service, locations, false);
71 }
72
73
74
75
76
77 public static PropertySource<?> getPropertySource(PropertiesService service, List<Location> locations, boolean includeGlobal) {
78 Properties properties = service.getProperties(locations);
79 if (includeGlobal) {
80 properties = PropertyUtils.getGlobalProperties(properties);
81 }
82 return new PropertiesPropertySource(PROPERTIES_PROPERTY_SOURCE, properties);
83 }
84
85
86
87
88
89
90
91 public static Properties getAllEnumerableProperties(ConfigurableEnvironment env) {
92
93
94 List<PropertySource<?>> sources = getPropertySources(env);
95
96
97
98 Collections.reverse(sources);
99
100
101 List<EnumerablePropertySource<?>> enumerables = asEnumerableList(sources);
102
103
104 return convert(enumerables);
105 }
106
107
108
109
110
111
112 public static Properties getAllEnumerablePropertiesQuietly(ConfigurableEnvironment env) {
113
114
115 List<PropertySource<?>> sources = getPropertySources(env);
116
117
118
119 Collections.reverse(sources);
120
121
122 List<EnumerablePropertySource<?>> enumerables = asEnumerableListQuietly(sources);
123
124
125 return convert(enumerables);
126 }
127
128
129
130
131
132
133
134 public static ImmutableProperties getEnvAsImmutableProperties(ConfigurableEnvironment env) {
135 return new ImmutableProperties(getAllEnumerableProperties(env));
136 }
137
138
139
140
141
142
143 public static List<EnumerablePropertySource<?>> asEnumerableList(List<PropertySource<?>> sources) {
144 List<EnumerablePropertySource<?>> list = new ArrayList<EnumerablePropertySource<?>>();
145 for (PropertySource<?> source : sources) {
146 boolean expression = source instanceof EnumerablePropertySource<?>;
147 String errorMessage = "'%s' is not enumerable [%s]";
148 Object[] args = { source.getName(), source.getClass().getCanonicalName() };
149 Preconditions.checkState(expression, errorMessage, args);
150 EnumerablePropertySource<?> element = (EnumerablePropertySource<?>) source;
151 list.add(element);
152 }
153 return list;
154 }
155
156
157
158
159
160
161 public static List<EnumerablePropertySource<?>> asEnumerableListQuietly(List<PropertySource<?>> sources) {
162 List<EnumerablePropertySource<?>> list = new ArrayList<EnumerablePropertySource<?>>();
163 for (PropertySource<?> source : sources) {
164 if (source instanceof EnumerablePropertySource<?>) {
165 EnumerablePropertySource<?> element = (EnumerablePropertySource<?>) source;
166 list.add(element);
167 } else {
168 logger.warn("'{}' is not enumerable [{}]", source.getName(), source.getClass().getCanonicalName());
169 }
170 }
171 return list;
172 }
173
174 public static List<PropertySource<?>> getPropertySources(Class<?> annotatedClass) {
175 ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(annotatedClass);
176 return extractPropertySourcesAndClose(context);
177 }
178
179 public static List<PropertySource<?>> extractPropertySourcesAndClose(ConfigurableApplicationContext context) {
180
181 List<PropertySource<?>> sources = getPropertySources(context);
182
183
184 SpringUtils.closeQuietly(context);
185
186
187 return sources;
188 }
189
190
191
192
193
194
195 public static Properties convert(EnumerablePropertySource<?> source) {
196 Assert.notNull(source, "source is null");
197 Properties properties = new Properties();
198 String[] names = source.getPropertyNames();
199 for (String name : names) {
200 Object object = source.getProperty(name);
201 Assert.notNull(object, "[" + name + "] is null");
202 Assert.isTrue(object instanceof String, "[" + name + "] is not a string");
203 properties.setProperty(name, (String) object);
204 }
205 return properties;
206 }
207
208
209
210
211
212
213 public static Properties convert(List<EnumerablePropertySource<?>> sources) {
214 Properties converted = new Properties();
215 for (EnumerablePropertySource<?> source : sources) {
216 Properties properties = convert(source);
217 converted.putAll(properties);
218 }
219 return converted;
220 }
221
222
223
224
225 public static List<PropertySource<?>> getPropertySources(ConfigurableEnvironment env) {
226 Preconditions.checkNotNull(env, "'env' cannot be null");
227 MutablePropertySources mps = env.getPropertySources();
228 List<PropertySource<?>> sources = new ArrayList<PropertySource<?>>();
229 Iterator<PropertySource<?>> itr = mps.iterator();
230 while (itr.hasNext()) {
231 PropertySource<?> source = itr.next();
232 sources.add(source);
233 }
234 return sources;
235 }
236
237
238
239
240 public static void reconfigurePropertySources(ConfigurableEnvironment env, String name, Properties properties) {
241
242 removeAllPropertySources(env);
243
244
245 MutablePropertySources mps = env.getPropertySources();
246
247
248 Assert.isTrue(mps.size() == 0);
249
250
251 PropertiesPropertySource pps = new PropertiesPropertySource(name, properties);
252
253
254 mps.addFirst(pps);
255 }
256
257
258
259
260 public static void removeAllPropertySources(ConfigurableEnvironment env) {
261 MutablePropertySources mps = env.getPropertySources();
262 List<PropertySource<?>> sources = getPropertySources(env);
263 for (PropertySource<?> source : sources) {
264 String name = source.getName();
265 mps.remove(name);
266 }
267 }
268
269
270
271
272 public static List<PropertySource<?>> asList(PropertySource<?>... sources) {
273 List<PropertySource<?>> list = new ArrayList<PropertySource<?>>();
274 if (sources == null) {
275 return list;
276 }
277 for (PropertySource<?> element : sources) {
278 if (element != null) {
279 list.add(element);
280 }
281 }
282 return list;
283 }
284
285
286
287
288 public static List<PropertySource<?>> getPropertySources(ConfigurableApplicationContext context, Comparator<PropertySource<?>> comparator) {
289
290 @SuppressWarnings("rawtypes")
291 Map<String, PropertySource> map = BeanFactoryUtils.beansOfTypeIncludingAncestors(context, PropertySource.class);
292
293
294 List<PropertySource<?>> list = new ArrayList<PropertySource<?>>();
295 for (PropertySource<?> source : map.values()) {
296 list.add(source);
297 }
298
299
300 Collections.sort(list, comparator);
301
302
303 return list;
304 }
305
306
307
308
309 public static List<PropertySource<?>> getPropertySources(ConfigurableApplicationContext context) {
310
311 return getPropertySources(context, new PropertySourceNameComparator());
312 }
313
314
315
316
317 public static SpringContext getSinglePropertySourceContext(PropertySource<?> source) {
318
319
320 PropertySourceContext psc = new PropertySourceContext(source, true);
321
322
323 SpringContext context = new SpringContext();
324
325
326 context.setPropertySourceContext(psc);
327
328
329 return context;
330 }
331
332 }