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.Properties;
21  
22  import org.kuali.common.util.MavenUtils;
23  import org.kuali.common.util.property.Constants;
24  import org.kuali.common.util.property.ProjectProperties;
25  import org.springframework.beans.factory.annotation.Autowired;
26  import org.springframework.beans.factory.annotation.Qualifier;
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.PropertySource;
31  import org.springframework.util.Assert;
32  
33  /**
34   * Add properties from the currently executing Maven project to the list of explicitly configured <code>ProjectProperties</code> and then combine all of them into a property source
35   * object.
36   */
37  @Configuration
38  public class MavenPropertySourceConfig {
39  
40  	public static final String SPRING_PROPERTY_SOURCE = "springPropertySource";
41  
42  	@Autowired
43  	Environment env;
44  
45  	@Autowired
46  	@Qualifier(Constants.DEFAULT_MAVEN_PROPERTIES_BEAN_NAME)
47  	Properties mavenProperties;
48  
49  	@Bean
50  	public ProjectProperties mavenProjectProperties() {
51  		// Make sure the maven properties got wired in correctly
52  		Assert.notNull(mavenProperties, "mavenProperties are null");
53  
54  		// Add in org, group, home, and enhanced version properties
55  		MavenUtils.augmentProjectProperties(mavenProperties);
56  
57  		// Create a ProjectProperties pojo from the properties
58  		return MavenUtils.getMavenProjectProperties(env, mavenProperties);
59  	}
60  
61  	/**
62  	 * This returns an empty list by default. Add in <code>ProjectProperties</code> as appropriate. Properties from this list use a "last one in wins" strategy.
63  	 */
64  	protected List<ProjectProperties> getProjectPropertiesList() {
65  		return new ArrayList<ProjectProperties>();
66  	}
67  
68  	@Bean
69  	public PropertySource<?> springPropertySource() {
70  		ProjectProperties mpp = mavenProjectProperties();
71  
72  		// Property loading uses a "last one in wins" strategy
73  		List<ProjectProperties> pps = new ArrayList<ProjectProperties>();
74  		// Add maven properties first so they can be used to resolve locations
75  		pps.add(mpp);
76  		// Load in project properties
77  		pps.addAll(getProjectPropertiesList());
78  		// Add maven properties last so they override loaded properties
79  		pps.add(mpp);
80  
81  		// Get a PropertySource object backed by the properties loaded from the list as well as system/environment properties
82  		return SpringUtils.getGlobalPropertySource(SPRING_PROPERTY_SOURCE, pps);
83  	}
84  
85  }