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.service;
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.ModeUtils;
23  import org.kuali.common.util.PropertyUtils;
24  import org.kuali.common.util.property.Constants;
25  import org.kuali.common.util.property.PropertyLoadContext;
26  import org.kuali.common.util.property.PropertyStoreContext;
27  import org.kuali.common.util.property.processor.PropertyProcessor;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  import org.springframework.util.PropertyPlaceholderHelper;
31  
32  public class DefaultPropertyService implements PropertyService {
33  
34  	private static final Logger logger = LoggerFactory.getLogger(DefaultPropertyService.class);
35  
36  	@Override
37  	public Properties load(PropertyLoadContext context) {
38  		Properties properties = loadProperties(context);
39  		context.initialize(properties);
40  		process(properties, context.getProcessors());
41  		return properties;
42  	}
43  
44  	@Override
45  	public void store(PropertyStoreContext context, Properties properties) {
46  		Properties duplicate = PropertyUtils.duplicate(properties);
47  		context.initialize(duplicate);
48  		process(duplicate, context.getProcessors());
49  		PropertyUtils.store(duplicate, context.getFile(), context.getEncoding(), context.getComment());
50  	}
51  
52  	protected void process(Properties properties, List<PropertyProcessor> processors) {
53  		if (processors == null) {
54  			return;
55  		}
56  		for (PropertyProcessor processor : processors) {
57  			processor.process(properties);
58  		}
59  	}
60  
61  	protected Properties loadProperties(PropertyLoadContext context) {
62  		PropertyPlaceholderHelper helper = Constants.DEFAULT_PROPERTY_PLACEHOLDER_HELPER;
63  		context.initializeLoadProcessors();
64  		Properties properties = new Properties();
65  		for (String location : context.getLocations()) {
66  			Properties duplicate = PropertyUtils.duplicate(properties);
67  			process(duplicate, context.getLoadProcessors());
68  			String resolvedLocation = helper.replacePlaceholders(location, duplicate);
69  			if (!location.equals(resolvedLocation)) {
70  				logger.debug("Resolved location [{}] -> [{}]", location, resolvedLocation);
71  			}
72  			if (LocationUtils.exists(resolvedLocation)) {
73  				Properties newProperties = PropertyUtils.load(resolvedLocation, context.getEncoding());
74  				properties.putAll(newProperties);
75  			} else {
76  				ModeUtils.validate(context.getMissingLocationsMode(), "Skipping non-existent location - [{}]", resolvedLocation, "Could not locate [" + resolvedLocation + "]");
77  			}
78  		}
79  		return properties;
80  	}
81  }