001    /**
002     * Copyright 2010-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.common.util.service;
017    
018    import java.util.List;
019    import java.util.Properties;
020    
021    import org.kuali.common.util.CollectionUtils;
022    import org.kuali.common.util.PropertyUtils;
023    import org.kuali.common.util.property.PropertyLoadContext;
024    import org.kuali.common.util.property.PropertyStoreContext;
025    import org.kuali.common.util.property.processor.PropertyProcessor;
026    import org.slf4j.Logger;
027    import org.slf4j.LoggerFactory;
028    
029    public class DefaultPropertyService implements PropertyService {
030    
031            private static final Logger logger = LoggerFactory.getLogger(DefaultPropertyService.class);
032    
033            @Override
034            public Properties load(PropertyLoadContext context) {
035                    Properties properties = loadProperties(context);
036                    logger.info("Working with " + properties.size() + " properties total.");
037                    context.initialize(properties);
038                    List<PropertyProcessor> processors = CollectionUtils.toEmptyList(context.getProcessors());
039                    logger.info("Processing " + properties.size() + " properties using " + processors.size() + " processors.");
040                    PropertyUtils.process(properties, context.getProcessors());
041                    logger.info("Returning " + properties.size() + " properties.");
042                    if (logger.isDebugEnabled()) {
043                            logger.debug(PropertyUtils.toString(properties));
044                    }
045                    return properties;
046            }
047    
048            @Override
049            public void store(PropertyStoreContext context, Properties properties) {
050                    Properties duplicate = PropertyUtils.duplicate(properties);
051                    context.initialize(duplicate);
052                    PropertyUtils.process(duplicate, context.getProcessors());
053                    PropertyUtils.store(duplicate, context.getFile(), context.getEncoding(), context.getComment());
054            }
055    
056            protected Properties loadProperties(PropertyLoadContext context) {
057                    Properties properties = context.init();
058                    int initialSize = properties.size();
059                    List<String> locations = CollectionUtils.toEmptyList(context.getLocations());
060                    logger.info("Examining " + locations.size() + " property locations.");
061                    int count = 0;
062                    for (String location : locations) {
063                            String resolvedAndValidatedLocation = context.getLocation(location, properties);
064                            if (resolvedAndValidatedLocation != null) {
065                                    Properties newProperties = PropertyUtils.load(resolvedAndValidatedLocation, context.getEncoding());
066                                    properties.putAll(newProperties);
067                                    count++;
068                            }
069                    }
070                    int newSize = properties.size();
071                    int skipped = locations.size() - count;
072                    logger.info("Loaded " + (newSize - initialSize) + " properties from " + count + " locations.  Skipped " + skipped + " locations.");
073                    return properties;
074            }
075    }