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.ArrayList;
19  import java.util.List;
20  import java.util.Map;
21  import java.util.Properties;
22  
23  import org.kuali.common.util.Assert;
24  import org.kuali.common.util.FormatUtils;
25  import org.kuali.common.util.PropertyUtils;
26  import org.kuali.common.util.property.ProjectProperties;
27  import org.kuali.common.util.property.ProjectPropertiesComparator;
28  import org.kuali.common.util.property.PropertiesContext;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  import org.springframework.beans.factory.FactoryBean;
32  import org.springframework.util.CollectionUtils;
33  
34  /**
35   * 
36   */
37  public class ProjectPropertiesLoaderFactoryBean implements FactoryBean<Properties> {
38  
39  	private static final Logger logger = LoggerFactory.getLogger(ProjectPropertiesLoaderFactoryBean.class);
40  
41  	List<String> locations;
42  	boolean singleton = true;
43  	ProjectPropertiesComparator comparator;
44  
45  	@Override
46  	public Properties getObject() {
47  		Assert.isFalse(CollectionUtils.isEmpty(locations), "locations is empty");
48  		long start = System.currentTimeMillis();
49  		List<ProjectProperties> pps = new ArrayList<ProjectProperties>();
50  		// Extract any ProjectProperties beans anywhere in the context
51  		for (String location : locations) {
52  			Map<String, ProjectProperties> beans = SpringUtils.getAllBeans(location, ProjectProperties.class);
53  			logger.info("Located {} sets of project properties", beans.size());
54  
55  			List<ProjectProperties> list = new ArrayList<ProjectProperties>();
56  			// Add them to a list
57  			for (ProjectProperties bean : beans.values()) {
58  				list.add(bean);
59  			}
60  			// Sort them by sequence (only relevant if there is more than one which there typically is not)
61  			pps.addAll(list);
62  		}
63  
64  		// Cycle through the list we have adding in properties as we go
65  		Properties properties = new Properties();
66  		for (ProjectProperties pp : pps) {
67  			PropertiesContext ctx = pp.getPropertiesContext();
68  			Properties combined = PropertyUtils.combine(properties, ctx.getProperties());
69  			ctx.setProperties(combined);
70  			Properties loaded = PropertyUtils.load(ctx);
71  			properties.putAll(loaded);
72  		}
73  		String elapsed = FormatUtils.getTime(System.currentTimeMillis() - start);
74  		logger.info("Loaded {} properties.  Total time: {}", properties.size(), elapsed);
75  		return properties;
76  	}
77  
78  	@Override
79  	public Class<Properties> getObjectType() {
80  		return Properties.class;
81  	}
82  
83  	@Override
84  	public boolean isSingleton() {
85  		return singleton;
86  	}
87  
88  	public void setSingleton(boolean singleton) {
89  		this.singleton = singleton;
90  	}
91  
92  	public List<String> getLocations() {
93  		return locations;
94  	}
95  
96  	public void setLocations(List<String> locations) {
97  		this.locations = locations;
98  	}
99  
100 	public ProjectPropertiesComparator getComparator() {
101 		return comparator;
102 	}
103 
104 	public void setComparator(ProjectPropertiesComparator comparator) {
105 		this.comparator = comparator;
106 	}
107 
108 }