001    /**
002     * Copyright 2010-2013 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.spring;
017    
018    import java.util.Properties;
019    
020    import org.kuali.common.util.Assert;
021    import org.kuali.common.util.LocationUtils;
022    import org.kuali.common.util.ModeUtils;
023    import org.kuali.common.util.PropertyUtils;
024    import org.kuali.common.util.property.PropertiesContext;
025    import org.springframework.beans.factory.FactoryBean;
026    
027    /**
028     * The improvement over Springs <code>PropertiesFactoryBean</code> is the ability to dynamically resolve placeholders in the property locations themselves.
029     */
030    public class PropertiesLoaderFactoryBean extends PropertiesContext implements FactoryBean<Properties> {
031    
032            protected Properties global = PropertyUtils.getGlobalProperties();
033            boolean singleton = true;
034    
035            @Override
036            public Properties getObject() throws Exception {
037                    Assert.notNull(helper, "helper is null");
038                    Assert.notNull(locations, "locations are null");
039                    Assert.notNull(encoding, "encoding is null");
040                    Assert.notNull(missingLocationsMode, "missingLocationsMode is null");
041                    Properties global = PropertyUtils.getGlobalProperties();
042                    this.properties = PropertyUtils.toEmpty(properties);
043                    Properties result = new Properties();
044                    for (String location : locations) {
045                            Properties resolverProperties = PropertyUtils.combine(properties, result, global);
046                            String resolvedLocation = helper.replacePlaceholders(location, resolverProperties);
047                            if (LocationUtils.exists(resolvedLocation)) {
048                                    Properties properties = PropertyUtils.load(location, encoding);
049                                    result.putAll(properties);
050                            } else {
051                                    ModeUtils.validate(missingLocationsMode, "Skipping non-existent location [" + resolvedLocation + "]");
052                            }
053                    }
054                    return result;
055            }
056    
057            @Override
058            public Class<Properties> getObjectType() {
059                    return Properties.class;
060            }
061    
062            @Override
063            public boolean isSingleton() {
064                    return singleton;
065            }
066    
067            public void setSingleton(boolean singleton) {
068                    this.singleton = singleton;
069            }
070    }