View Javadoc

1   /**
2    * Copyright 2010-2012 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.processor;
17  
18  import java.util.List;
19  import java.util.Properties;
20  
21  import org.kuali.common.util.LocationUtils;
22  import org.kuali.common.util.Mode;
23  import org.kuali.common.util.ModeUtils;
24  import org.kuali.common.util.PropertyUtils;
25  import org.kuali.common.util.property.Constants;
26  import org.kuali.common.util.property.GlobalPropertiesMode;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  import org.springframework.util.PropertyPlaceholderHelper;
30  
31  public class LoadProcessor implements PropertyProcessor {
32  	private static final Logger logger = LoggerFactory.getLogger(LoadProcessor.class);
33  
34  	List<String> locations;
35  	GlobalPropertiesMode globalPropertiesOverrideMode = GlobalPropertiesMode.BOTH;
36  	String encoding;
37  	Mode missingLocationsMode = Mode.INFORM;
38  
39  	public LoadProcessor() {
40  		this(null, GlobalPropertiesMode.BOTH, null, Mode.INFORM);
41  	}
42  
43  	public LoadProcessor(List<String> locations, GlobalPropertiesMode globalPropertiesOverrideMode, String encoding, Mode missingLocationsMode) {
44  		super();
45  		this.locations = locations;
46  		this.globalPropertiesOverrideMode = globalPropertiesOverrideMode;
47  		this.encoding = encoding;
48  		this.missingLocationsMode = missingLocationsMode;
49  	}
50  
51  	@Override
52  	public void process(Properties properties) {
53  		PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper(Constants.DEFAULT_PLACEHOLDER_PREFIX, Constants.DEFAULT_PLACEHOLDER_SUFFIX);
54  		for (String location : locations) {
55  			Properties duplicate = PropertyUtils.getProperties(properties, globalPropertiesOverrideMode);
56  			String resolvedLocation = helper.replacePlaceholders(location, duplicate);
57  			if (!location.equals(resolvedLocation)) {
58  				logger.debug("Resolved location [{}] -> [{}]", location, resolvedLocation);
59  			}
60  			if (LocationUtils.exists(resolvedLocation)) {
61  				Properties newProperties = PropertyUtils.load(resolvedLocation, encoding);
62  				properties.putAll(newProperties);
63  			} else {
64  				ModeUtils.validate(missingLocationsMode, "Skipping non-existent location - [{}]", resolvedLocation, "Could not locate [" + resolvedLocation + "]");
65  			}
66  		}
67  	}
68  
69  	public List<String> getLocations() {
70  		return locations;
71  	}
72  
73  	public void setLocations(List<String> locations) {
74  		this.locations = locations;
75  	}
76  
77  	public GlobalPropertiesMode getGlobalPropertiesOverrideMode() {
78  		return globalPropertiesOverrideMode;
79  	}
80  
81  	public void setGlobalPropertiesOverrideMode(GlobalPropertiesMode globalPropertiesOverrideMode) {
82  		this.globalPropertiesOverrideMode = globalPropertiesOverrideMode;
83  	}
84  
85  	public String getEncoding() {
86  		return encoding;
87  	}
88  
89  	public void setEncoding(String encoding) {
90  		this.encoding = encoding;
91  	}
92  
93  	public Mode getMissingLocationsMode() {
94  		return missingLocationsMode;
95  	}
96  
97  	public void setMissingLocationsMode(Mode missingLocationsMode) {
98  		this.missingLocationsMode = missingLocationsMode;
99  	}
100 
101 }