View Javadoc
1   /**
2    * Copyright 2010-2014 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.project.spring;
17  
18  import java.util.Properties;
19  
20  import org.kuali.common.util.maven.MavenConstants;
21  import org.kuali.common.util.maven.MavenUtils;
22  import org.kuali.common.util.maven.spring.AutowiredMavenProperties;
23  import org.kuali.common.util.maven.spring.NoAutowiredMavenProperties;
24  import org.kuali.common.util.project.ProjectService;
25  import org.kuali.common.util.project.ProjectUtils;
26  import org.kuali.common.util.project.model.Build;
27  import org.kuali.common.util.project.model.Project;
28  import org.kuali.common.util.project.model.ProjectIdentifier;
29  import org.springframework.beans.factory.annotation.Autowired;
30  import org.springframework.beans.factory.annotation.Qualifier;
31  import org.springframework.context.annotation.Bean;
32  import org.springframework.context.annotation.Configuration;
33  import org.springframework.context.annotation.Import;
34  import org.springframework.util.Assert;
35  
36  /**
37   * <p>
38   * This class automatically wires a <code>Project</code> object into the Spring context:
39   * 
40   * <pre>
41   * &#064;Autowired
42   * Project project;
43   * </pre>
44   * 
45   * </p>
46   * 
47   * <p>
48   * For Spring process launched by Maven via the spring-maven-plugin the project wiring is completed entirely in memory using the <code>java.util.Properties</code> object from the
49   * Maven runtime.
50   * </p>
51   * 
52   * <p>
53   * For Spring process launched using any other method, the project wiring is completed by loading the <code>project.properties</code> file corresponding to the
54   * <code>[groupId:artifactId]</code> for the current project. The properties file for the <code>kuali-util</code> project (for example) is located at:
55   * 
56   * <pre>
57   * classpath:META-INF/org/kuali/common/kuali-util/project.properties
58   * </pre>
59   * 
60   * The <code>project.properties</code> file for every Kuali project is automatically created by Maven early in the default build lifecycle, (the <code>generate-resources</code>
61   * phase) and is thus available to any build process that comes after that. For example, the <code>test</code> phase.
62   * 
63   * </p>
64   */
65  @Configuration
66  public class AutowiredProjectConfig {
67  
68  	// There can be only two results here:
69  	// 1 - A project object is successfully constructed and wired into the Spring context
70  	// 2 - An exception is thrown
71  	// The pair of static classes below, are setup to activate via the Spring profiles "autowiredMavenProperties" and "!autowiredMavenProperties"
72  	// This makes it so that one (and only one) of them will always load no matter what
73  
74  	// This config class loads if the Spring profile "autowiredMavenProperties" is NOT active (which will usually be the case)
75  	@Configuration
76  	@NoAutowiredMavenProperties
77  	@Import({ ProjectServiceConfig.class })
78  	static class NoAutowiredMavenPropertiesProjectConfig implements ProjectConfig {
79  
80  		// Something else needs to have wired this in
81  		@Autowired
82  		ProjectIdentifierConfig projectIdentifierConfig;
83  
84  		@Autowired
85  		ProjectServiceConfig projectServiceConfig;
86  
87  		@Override
88  		@Bean
89  		public Project project() {
90  
91  			// Get a reference to the project service
92  			ProjectService service = projectServiceConfig.projectService();
93  
94  			// Get a reference to a project identifier (groupId + artifactId)
95  			ProjectIdentifier identifier = projectIdentifierConfig.projectIdentifier();
96  
97  			// Use the service to load the correct project.properties file and convert to a Project object
98  			return service.getProject(identifier.getGroupId(), identifier.getArtifactId());
99  		}
100 	}
101 
102 	// This config class only loads if the Spring profile "autowiredMavenProperties" is active
103 	// spring-maven-plugin activates this profile by default when Spring is launched by Maven during a build
104 	@Configuration
105 	@AutowiredMavenProperties
106 	static class AutowiredMavenPropertiesProjectConfig implements ProjectConfig {
107 
108 		// spring-maven-plugin wires this in for us
109 		@Autowired
110 		@Qualifier(MavenConstants.PROPERTIES_BEAN_NAME)
111 		Properties mavenProperties;
112 
113 		@Override
114 		@Bean
115 		public Project project() {
116 
117 			// Make sure the maven properties got wired in correctly
118 			Assert.notNull(mavenProperties, "mavenProperties are null");
119 
120 			// Enhance the default set of Maven properties
121 			MavenUtils.augmentProjectProperties(mavenProperties);
122 
123 			// Convert the augmented properties into a Project object
124 			return ProjectUtils.getProject(mavenProperties);
125 		}
126 
127 		@Bean
128 		public Build build() {
129 			return ProjectUtils.getBuild(project());
130 		}
131 
132 	}
133 }