1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util.spring.service;
17
18 import java.util.Arrays;
19 import java.util.List;
20 import java.util.Map;
21
22 import org.apache.commons.lang3.StringUtils;
23 import org.kuali.common.util.Assert;
24 import org.kuali.common.util.CollectionUtils;
25 import org.kuali.common.util.spring.PropertySourceUtils;
26 import org.springframework.context.ConfigurableApplicationContext;
27 import org.springframework.core.env.PropertySource;
28
29 public final class DefaultPropertySourceService implements PropertySourceService {
30
31 public DefaultPropertySourceService(SpringService springService) {
32 Assert.noNulls(springService);
33 this.springService = springService;
34 }
35
36 private final SpringService springService;
37
38 @Override
39 public PropertySource<?> getPropertySource(Class<? extends PropertySourceConfig> config) {
40 return getPropertySource(null, null, null, config);
41 }
42
43 @Override
44 public PropertySource<?> getPropertySource(Map<String, Object> beans, List<String> defaultProfiles, List<String> activeProfiles, Class<? extends PropertySourceConfig> config) {
45 return getPropertySource(beans, defaultProfiles, activeProfiles, config, null);
46 }
47
48 @Override
49 public List<PropertySource<?>> getPropertySources(Map<String, Object> beans, List<String> defaultProfiles, List<String> activeProfiles, Class<?> config) {
50 return getPropertySources(beans, defaultProfiles, activeProfiles, config, null);
51 }
52
53 @Override
54 public List<PropertySource<?>> getPropertySources(Map<String, Object> beans, List<String> defaultProfiles, List<String> activeProfiles, String location) {
55 return getPropertySources(beans, defaultProfiles, activeProfiles, null, location);
56 }
57
58 protected PropertySource<?> getPropertySource(Map<String, Object> beans, List<String> defaultProfiles, List<String> activeProfiles, Class<?> config, String location) {
59 List<PropertySource<?>> sources = getPropertySources(beans, defaultProfiles, activeProfiles, config, location);
60 Assert.isTrue(sources.size() == 1, "sizes != 1");
61 return sources.get(0);
62 }
63
64 protected List<PropertySource<?>> getPropertySources(Map<String, Object> beans, List<String> defaultProfiles, List<String> activeProfiles, Class<?> config, String location) {
65
66 Assert.notNull(springService, "springService is null");
67
68 SpringContext context = new SpringContext();
69 context.setContextBeans(beans);
70 if (!StringUtils.isBlank(location)) {
71 context.setLocations(Arrays.asList(location));
72 } else if (config != null) {
73 context.setAnnotatedClasses(CollectionUtils.asList(config));
74 } else {
75 throw new IllegalArgumentException("Must supply either a location or annotated config");
76 }
77 context.setActiveProfiles(activeProfiles);
78 context.setDefaultProfiles(defaultProfiles);
79 ConfigurableApplicationContext ctx = springService.getApplicationContext(context);
80 ctx.refresh();
81 return PropertySourceUtils.getPropertySources(ctx);
82 }
83
84 public SpringService getSpringService() {
85 return springService;
86 }
87
88 }