1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util.property;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Properties;
21
22 import org.kuali.common.util.CollectionUtils;
23 import org.kuali.common.util.LocationUtils;
24 import org.kuali.common.util.Mode;
25 import org.kuali.common.util.ModeUtils;
26 import org.kuali.common.util.PropertyUtils;
27 import org.kuali.common.util.property.processor.GlobalOverrideProcessor;
28 import org.kuali.common.util.property.processor.HomeProcessor;
29 import org.kuali.common.util.property.processor.OrgProcessor;
30 import org.kuali.common.util.property.processor.PathProcessor;
31 import org.kuali.common.util.property.processor.PropertyProcessor;
32 import org.kuali.common.util.property.processor.ResolvePlaceholdersProcessor;
33 import org.kuali.common.util.property.processor.TrimProcessor;
34 import org.kuali.common.util.property.processor.VersionProcessor;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.util.Assert;
38
39 public class DefaultPropertyLoadContext extends DefaultPropertyContext implements PropertyLoadContext {
40
41 private static final Logger logger = LoggerFactory.getLogger(DefaultPropertyLoadContext.class);
42
43 List<String> locations;
44 String encoding = "UTF-8";
45 String missingLocationsMode = Mode.INFORM.name();
46 Properties locationHelperProperties;
47 String locationHelperInclude;
48 String locationHelperExclude;
49 List<String> locationHelperIncludes;
50 List<String> locationHelperExcludes;
51 String organizationGroupId;
52 String groupIdProperty = Constants.DEFAULT_GROUP_ID_PROPERTY;
53 String versionProperty = Constants.DEFAULT_VERSION_PROPERTY;
54
55 @Override
56 public Properties init() {
57 Assert.notNull(helper, "helper is null");
58 Properties global = getGlobalProperties(locationHelperProperties);
59 this.globalPropertiesMode = resolve(globalPropertiesMode, global);
60 this.missingLocationsMode = resolve(missingLocationsMode, global);
61 logger.info("Global properties mode - " + globalPropertiesMode);
62 logger.info("Missing locations mode - " + missingLocationsMode);
63 this.encoding = resolve(encoding, global);
64 this.organizationGroupId = resolve(organizationGroupId, global);
65 validateGlobalPropertiesMode(globalPropertiesMode);
66 GlobalPropertiesMode gpm = GlobalPropertiesMode.valueOf(globalPropertiesMode);
67 this.locationHelperProperties = getLocationHelperProperties(locationHelperProperties, gpm);
68 logger.info("Property file encoding - " + encoding);
69 logger.info("Using " + locationHelperProperties.size() + " properties to assist with property loading.");
70 validate();
71 if (logger.isDebugEnabled()) {
72 PropertyUtils.debug(locationHelperProperties);
73 }
74 Properties p = new Properties();
75 p.putAll(PropertyUtils.toEmpty(properties));
76 p.putAll(PropertyUtils.toEmpty(locationHelperProperties));
77 return p;
78 }
79
80 protected void validateGlobalPropertiesMode(String globalPropertiesMode) {
81 validateResolved(globalPropertiesMode);
82 GlobalPropertiesMode.valueOf(globalPropertiesMode);
83 }
84
85 @Override
86 protected void validate() {
87 validateGlobalPropertiesMode(globalPropertiesMode);
88 validateResolved(encoding);
89 validateResolved(missingLocationsMode);
90 Mode.valueOf(missingLocationsMode);
91 }
92
93 @Override
94 public String getLocation(String location, Properties properties) {
95 String resolvedLocation = getResolvedLocation(location, properties);
96 return getValidatedLocation(resolvedLocation, Mode.valueOf(missingLocationsMode));
97 }
98
99 protected String getValidatedLocation(String location, Mode missingLocationsMode) {
100 validateResolved(location);
101 if (LocationUtils.exists(location)) {
102 return location;
103 } else {
104 ModeUtils.validate(missingLocationsMode, "Skipping non-existent location - [{}]", location, "Could not locate [" + location + "]");
105 return null;
106 }
107 }
108
109 protected void validateResolved(String string) {
110 if (PropertyUtils.containsUnresolvedPlaceholder(string)) {
111 throw new IllegalArgumentException("Unable to resolve [" + string + "]");
112 }
113 }
114
115 protected String getResolvedLocation(String location, Properties properties) {
116 boolean resolve = PropertyUtils.containsUnresolvedPlaceholder(location);
117 if (resolve) {
118 GlobalPropertiesMode gpm = GlobalPropertiesMode.valueOf(globalPropertiesMode);
119 Properties duplicate = PropertyUtils.getProperties(properties, gpm);
120 List<PropertyProcessor> processors = getLocationProcessors();
121 for (PropertyProcessor processor : processors) {
122 processor.process(duplicate);
123 }
124 return helper.replacePlaceholders(location, duplicate);
125 } else {
126 return location;
127 }
128 }
129
130 protected Properties getGlobalProperties(Properties properties) {
131 return PropertyUtils.getGlobalProperties(PropertyUtils.toEmpty(properties));
132 }
133
134 protected Properties getLocationHelperProperties(Properties properties, GlobalPropertiesMode mode) {
135 Properties locationHelperProperties = PropertyUtils.duplicate(PropertyUtils.toEmpty(properties));
136 List<PropertyProcessor> processors = getLocationHelperProcessors();
137 for (PropertyProcessor processor : processors) {
138 processor.process(locationHelperProperties);
139 }
140 return locationHelperProperties;
141 }
142
143 protected List<PropertyProcessor> getLocationProcessors() {
144 GlobalPropertiesMode gpm = GlobalPropertiesMode.valueOf(globalPropertiesMode);
145 List<PropertyProcessor> processors = new ArrayList<PropertyProcessor>();
146 processors.add(new GlobalOverrideProcessor(gpm));
147 processors.add(new ResolvePlaceholdersProcessor(helper, gpm));
148 return processors;
149 }
150
151 protected String getGroupId() {
152 if (groupIdProperty == null) {
153 return null;
154 } else if (locationHelperProperties == null) {
155 return null;
156 } else {
157 return locationHelperProperties.getProperty(groupIdProperty);
158 }
159 }
160
161 protected List<PropertyProcessor> getLocationHelperProcessors() {
162 List<PropertyProcessor> processors = new ArrayList<PropertyProcessor>();
163
164 this.locationHelperIncludes = CollectionUtils.sortedMerge(locationHelperIncludes, locationHelperInclude);
165 this.locationHelperExcludes = CollectionUtils.sortedMerge(locationHelperExcludes, locationHelperExclude);
166 processors.add(new TrimProcessor(locationHelperIncludes, locationHelperExcludes));
167
168 String groupId = getGroupId();
169 if (organizationGroupId != null && groupId != null) {
170 processors.add(new OrgProcessor(organizationGroupId, groupId));
171 processors.add(new HomeProcessor(organizationGroupId, groupId));
172 }
173
174 if (groupIdProperty != null) {
175 processors.add(new PathProcessor(groupIdProperty));
176 }
177
178 if (versionProperty != null) {
179 processors.add(new VersionProcessor(versionProperty));
180 }
181
182 GlobalPropertiesMode gpm = GlobalPropertiesMode.valueOf(globalPropertiesMode);
183 processors.add(new GlobalOverrideProcessor(gpm));
184 processors.add(new ResolvePlaceholdersProcessor(helper, gpm));
185
186 return processors;
187 }
188
189 public String getMissingLocationsMode() {
190 return missingLocationsMode;
191 }
192
193 public void setMissingLocationsMode(String missingLocationsMode) {
194 this.missingLocationsMode = missingLocationsMode;
195 }
196
197 @Override
198 public List<String> getLocations() {
199 return locations;
200 }
201
202 public void setLocations(List<String> locations) {
203 this.locations = locations;
204 }
205
206 public Properties getLocationHelperProperties() {
207 return locationHelperProperties;
208 }
209
210 public void setLocationHelperProperties(Properties locationHelperProperties) {
211 this.locationHelperProperties = locationHelperProperties;
212 }
213
214 @Override
215 public String getEncoding() {
216 return encoding;
217 }
218
219 public void setEncoding(String encoding) {
220 this.encoding = encoding;
221 }
222
223 public String getOrganizationGroupId() {
224 return organizationGroupId;
225 }
226
227 public void setOrganizationGroupId(String organizationGroupId) {
228 this.organizationGroupId = organizationGroupId;
229 }
230
231 public String getGroupIdProperty() {
232 return groupIdProperty;
233 }
234
235 public void setGroupIdProperty(String groupIdProperty) {
236 this.groupIdProperty = groupIdProperty;
237 }
238
239 public String getVersionProperty() {
240 return versionProperty;
241 }
242
243 public void setVersionProperty(String versionProperty) {
244 this.versionProperty = versionProperty;
245 }
246
247 public List<String> getLocationHelperIncludes() {
248 return locationHelperIncludes;
249 }
250
251 public void setLocationHelperIncludes(List<String> locationHelperIncludes) {
252 this.locationHelperIncludes = locationHelperIncludes;
253 }
254
255 public List<String> getLocationHelperExcludes() {
256 return locationHelperExcludes;
257 }
258
259 public void setLocationHelperExcludes(List<String> locationHelperExcludes) {
260 this.locationHelperExcludes = locationHelperExcludes;
261 }
262
263 public String getLocationHelperInclude() {
264 return locationHelperInclude;
265 }
266
267 public void setLocationHelperInclude(String locationHelperInclude) {
268 this.locationHelperInclude = locationHelperInclude;
269 }
270
271 public String getLocationHelperExclude() {
272 return locationHelperExclude;
273 }
274
275 public void setLocationHelperExclude(String locationHelperExclude) {
276 this.locationHelperExclude = locationHelperExclude;
277 }
278
279 }