1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.configuration2.spring;
19
20 import java.util.Properties;
21 import java.util.stream.Stream;
22
23 import org.apache.commons.configuration2.CompositeConfiguration;
24 import org.apache.commons.configuration2.Configuration;
25 import org.apache.commons.configuration2.ConfigurationConverter;
26 import org.apache.commons.configuration2.builder.fluent.Configurations;
27 import org.apache.commons.lang3.ArrayUtils;
28 import org.springframework.beans.factory.FactoryBean;
29 import org.springframework.beans.factory.InitializingBean;
30 import org.springframework.core.io.Resource;
31 import org.springframework.util.Assert;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class ConfigurationPropertiesFactoryBean implements InitializingBean, FactoryBean<Properties> {
48
49
50
51
52
53
54
55
56 private static <T> T[] clone(final T[] src) {
57 return src != null ? src.clone() : null;
58 }
59
60
61 private CompositeConfiguration compositeConfiguration;
62
63
64 private Configuration[] configurations;
65
66
67 private Resource[] locations;
68
69
70 private boolean throwExceptionOnMissing = true;
71
72
73
74
75 public ConfigurationPropertiesFactoryBean() {
76 }
77
78
79
80
81
82
83 public ConfigurationPropertiesFactoryBean(final Configuration configuration) {
84 Assert.notNull(configuration, "configuration");
85 this.compositeConfiguration = new CompositeConfiguration(configuration);
86 }
87
88
89
90
91 @Override
92 public void afterPropertiesSet() throws Exception {
93 if (compositeConfiguration == null && ArrayUtils.isEmpty(configurations) && ArrayUtils.isEmpty(locations)) {
94 throw new IllegalArgumentException("no configuration object or location specified");
95 }
96
97 if (compositeConfiguration == null) {
98 compositeConfiguration = new CompositeConfiguration();
99 }
100
101 compositeConfiguration.setThrowExceptionOnMissing(throwExceptionOnMissing);
102
103 if (configurations != null) {
104 Stream.of(configurations).forEach(compositeConfiguration::addConfiguration);
105 }
106
107 if (locations != null) {
108 for (final Resource location : locations) {
109 compositeConfiguration.addConfiguration(new Configurations().properties(location.getURL()));
110 }
111 }
112 }
113
114
115
116
117
118
119 public CompositeConfiguration getConfiguration() {
120 return compositeConfiguration;
121 }
122
123
124
125
126
127
128 public Configuration[] getConfigurations() {
129 return clone(configurations);
130 }
131
132
133
134
135
136
137 public Resource[] getLocations() {
138 return clone(locations);
139 }
140
141
142
143
144 @Override
145 public Properties getObject() throws Exception {
146 return compositeConfiguration != null ? ConfigurationConverter.getProperties(compositeConfiguration) : null;
147 }
148
149
150
151
152 @Override
153 public Class<?> getObjectType() {
154 return Properties.class;
155 }
156
157
158
159
160 @Override
161 public boolean isSingleton() {
162 return true;
163 }
164
165
166
167
168
169
170 public boolean isThrowExceptionOnMissing() {
171 return throwExceptionOnMissing;
172 }
173
174
175
176
177
178
179 public void setConfigurations(final Configuration... configurations) {
180 this.configurations = clone(configurations);
181 }
182
183
184
185
186
187
188
189 public void setLocations(final Resource... locations) {
190 this.locations = clone(locations);
191 }
192
193
194
195
196
197
198
199 public void setThrowExceptionOnMissing(final boolean throwExceptionOnMissing) {
200 this.throwExceptionOnMissing = throwExceptionOnMissing;
201 }
202 }