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.CollectionUtils;
22  import org.kuali.common.util.PropertyUtils;
23  import org.kuali.common.util.property.PropertyLoadContext;
24  import org.kuali.common.util.property.PropertyStoreContext;
25  import org.kuali.common.util.property.processor.PropertyProcessor;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  public class DefaultPropertyService implements PropertyService {
30  
31  	private static final Logger logger = LoggerFactory.getLogger(DefaultPropertyService.class);
32  
33  	@Override
34  	public Properties load(PropertyLoadContext context) {
35  		Properties properties = loadProperties(context);
36  		logger.info("Working with " + properties.size() + " properties total.");
37  		context.initialize(properties);
38  		List<PropertyProcessor> processors = CollectionUtils.toEmpty(context.getProcessors());
39  		logger.info("Processing " + properties.size() + " properties using " + processors.size() + " processors.");
40  		process(properties, context.getProcessors());
41  		logger.info("Returning " + properties.size() + " properties.");
42  		return properties;
43  	}
44  
45  	@Override
46  	public void store(PropertyStoreContext context, Properties properties) {
47  		Properties duplicate = PropertyUtils.duplicate(properties);
48  		context.initialize(duplicate);
49  		process(duplicate, context.getProcessors());
50  		PropertyUtils.store(duplicate, context.getFile(), context.getEncoding(), context.getComment());
51  	}
52  
53  	protected void process(Properties properties, List<PropertyProcessor> processors) {
54  		for (PropertyProcessor processor : CollectionUtils.toEmpty(processors)) {
55  			processor.process(properties);
56  		}
57  	}
58  
59  	protected Properties loadProperties(PropertyLoadContext context) {
60  		Properties properties = context.init();
61  		int initialSize = properties.size();
62  		List<String> locations = CollectionUtils.toEmpty(context.getLocations());
63  		logger.info("Examining " + locations.size() + " property locations.");
64  		int count = 0;
65  		for (String location : locations) {
66  			String resolvedAndValidatedLocation = context.getLocation(location, properties);
67  			if (resolvedAndValidatedLocation != null) {
68  				Properties newProperties = PropertyUtils.load(resolvedAndValidatedLocation, context.getEncoding());
69  				properties.putAll(newProperties);
70  				count++;
71  			}
72  		}
73  		int newSize = properties.size();
74  		int skipped = locations.size() - count;
75  		logger.info("Loaded " + (newSize - initialSize) + " properties from " + count + " locations.  Skipped " + skipped + " locations.");
76  		return properties;
77  	}
78  }