View Javadoc

1   /**
2    * Copyright 2010-2013 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.spring;
17  
18  import java.util.Properties;
19  
20  import org.kuali.common.util.Assert;
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.PropertiesContext;
25  import org.springframework.beans.factory.FactoryBean;
26  
27  /**
28   * The improvement over Springs <code>PropertiesFactoryBean</code> is the ability to dynamically resolve placeholders in the property locations themselves.
29   */
30  public class PropertiesLoaderFactoryBean extends PropertiesContext implements FactoryBean<Properties> {
31  
32  	protected Properties global = PropertyUtils.getGlobalProperties();
33  	boolean singleton = true;
34  
35  	@Override
36  	public Properties getObject() throws Exception {
37  		Assert.notNull(helper, "helper is null");
38  		Assert.notNull(locations, "locations are null");
39  		Assert.notNull(encoding, "encoding is null");
40  		Assert.notNull(missingLocationsMode, "missingLocationsMode is null");
41  		Properties global = PropertyUtils.getGlobalProperties();
42  		this.properties = PropertyUtils.toEmpty(properties);
43  		Properties result = new Properties();
44  		for (String location : locations) {
45  			Properties resolverProperties = PropertyUtils.combine(properties, result, global);
46  			String resolvedLocation = helper.replacePlaceholders(location, resolverProperties);
47  			if (LocationUtils.exists(resolvedLocation)) {
48  				Properties properties = PropertyUtils.load(location, encoding);
49  				result.putAll(properties);
50  			} else {
51  				ModeUtils.validate(missingLocationsMode, "Skipping non-existent location [" + resolvedLocation + "]");
52  			}
53  		}
54  		return result;
55  	}
56  
57  	@Override
58  	public Class<Properties> getObjectType() {
59  		return Properties.class;
60  	}
61  
62  	@Override
63  	public boolean isSingleton() {
64  		return singleton;
65  	}
66  
67  	public void setSingleton(boolean singleton) {
68  		this.singleton = singleton;
69  	}
70  }