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