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