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.car;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.Properties;
21  
22  import org.kuali.common.util.LocationUtils;
23  import org.kuali.common.util.PropertyUtils;
24  import org.kuali.common.util.Str;
25  import org.kuali.common.util.property.Constants;
26  import org.springframework.beans.factory.annotation.Autowired;
27  import org.springframework.context.annotation.Bean;
28  import org.springframework.context.annotation.Configuration;
29  import org.springframework.core.env.Environment;
30  import org.springframework.core.env.PropertiesPropertySource;
31  import org.springframework.util.PropertyPlaceholderHelper;
32  
33  @Configuration
34  public class CarPropertySourcesConfig {
35  
36  	PropertyPlaceholderHelper pph = Constants.DEFAULT_PROPERTY_PLACEHOLDER_HELPER;
37  
38  	@Autowired
39  	Environment env;
40  
41  	@Bean
42  	public String projectPropertiesLocation() {
43  		String groupId = "org.kuali.common";
44  		String artifactId = "kuali-car";
45  
46  		Properties properties = new Properties();
47  		properties.setProperty("project.groupId.path", Str.getPath(groupId));
48  		properties.setProperty("project.artifactId", artifactId);
49  
50  		return pph.replacePlaceholders(Constants.PROJECT_PROPERTIES_LOCATION, properties);
51  	}
52  
53  	@Bean()
54  	public PropertiesPropertySource pps() {
55  		// Default to UTF-8 for project properties unless they've specified something else
56  		String ppenc = env.getProperty("project.properties.encoding", "UTF-8");
57  		String pploc = projectPropertiesLocation();
58  		Properties projectProperties = PropertyUtils.load(pploc, ppenc);
59  		String encoding = projectProperties.getProperty("project.encoding");
60  		Properties global = PropertyUtils.getGlobalProperties();
61  		Properties source = new Properties();
62  		source.putAll(global);
63  		List<String> locations = new ArrayList<String>();
64  		locations.add("classpath:car.properties");
65  		locations.add("classpath:${car.make}.properties");
66  
67  		for (String location : locations) {
68  			String resolvedLocation = pph.replacePlaceholders(location, source);
69  			boolean exists = LocationUtils.exists(resolvedLocation);
70  			if (exists) {
71  				source.putAll(PropertyUtils.load(resolvedLocation, encoding));
72  				source.putAll(global);
73  			}
74  		}
75  		String name = "springProperties";
76  		return new PropertiesPropertySource(name, source);
77  	}
78  
79  }