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