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.toEmptyList(context.getProcessors());
39  		logger.info("Processing " + properties.size() + " properties using " + processors.size() + " processors.");
40  		PropertyUtils.process(properties, context.getProcessors());
41  		logger.info("Returning " + properties.size() + " properties.");
42  		if (logger.isDebugEnabled()) {
43  			logger.debug(PropertyUtils.toString(properties));
44  		}
45  		return properties;
46  	}
47  
48  	@Override
49  	public void store(PropertyStoreContext context, Properties properties) {
50  		Properties duplicate = PropertyUtils.duplicate(properties);
51  		context.initialize(duplicate);
52  		PropertyUtils.process(duplicate, context.getProcessors());
53  		PropertyUtils.store(duplicate, context.getFile(), context.getEncoding(), context.getComment());
54  	}
55  
56  	protected Properties loadProperties(PropertyLoadContext context) {
57  		Properties properties = context.init();
58  		int initialSize = properties.size();
59  		List<String> locations = CollectionUtils.toEmptyList(context.getLocations());
60  		logger.info("Examining " + locations.size() + " property locations.");
61  		int count = 0;
62  		for (String location : locations) {
63  			String resolvedAndValidatedLocation = context.getLocation(location, properties);
64  			if (resolvedAndValidatedLocation != null) {
65  				Properties newProperties = PropertyUtils.load(resolvedAndValidatedLocation, context.getEncoding());
66  				properties.putAll(newProperties);
67  				count++;
68  			}
69  		}
70  		int newSize = properties.size();
71  		int skipped = locations.size() - count;
72  		logger.info("Loaded " + (newSize - initialSize) + " properties from " + count + " locations.  Skipped " + skipped + " locations.");
73  		return properties;
74  	}
75  }