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