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.property.processor;
17  
18  import java.io.File;
19  import java.util.Properties;
20  
21  import org.apache.commons.lang3.StringUtils;
22  import org.kuali.common.util.OrgUtils;
23  import org.kuali.common.util.Str;
24  import org.kuali.common.util.maven.MavenConstants;
25  import org.kuali.common.util.project.KualiProjectConstants;
26  import org.kuali.common.util.project.ProjectUtils;
27  import org.kuali.common.util.project.model.Project;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  import org.springframework.util.Assert;
31  
32  /**
33   * This processor is called *very* early in the Maven build lifecyle in order to augment the default set of Maven properties.
34   */
35  public class ProjectProcessor implements PropertyProcessor {
36  
37  	private static final Logger logger = LoggerFactory.getLogger(ProjectProcessor.class);
38  
39  	private static final String KS_GROUP_ID = KualiProjectConstants.STUDENT_GROUP_ID;
40  	private static final String FS = File.separator;
41  	private static final String DOT = ".";
42  	private static final String PROJECT_GROUP_ID_PATH = "project.groupId.path";
43  
44  	@Override
45  	public void process(Properties properties) {
46  
47  		// Make sure we are configured correctly
48  		Assert.notNull(properties, "properties are null");
49  
50  		// Make sure groupId, artifactId, orgId, and orgCode are present
51  		validate(properties);
52  
53  		// Fix the funk in KS groupId's (if its a KS project)
54  		fixKSGroupIds(properties);
55  
56  		// Now that the groupId is fixed, it is safe to use the properties to get a project object
57  		Project p = ProjectUtils.getProject(properties);
58  
59  		// Extract org info
60  		String orgId = properties.getProperty(MavenConstants.ORG_ID_KEY);
61  		String orgCode = properties.getProperty(MavenConstants.ORG_ID_CODE_KEY);
62  
63  		// Figure out the group code (eg "rice", "student", "ole", etc)
64  		String groupCode = OrgUtils.getGroupCode(orgId, p.getGroupId());
65  
66  		// Setup some org and group paths based on user.home
67  		String userHome = System.getProperty("user.home");
68  		String orgHome = userHome + FS + DOT + orgCode;
69  		String groupHome = orgHome + FS + groupCode;
70  
71  		// Store the org and group paths
72  		properties.setProperty(PROJECT_GROUP_ID_PATH, Str.getPath(p.getGroupId()));
73  		properties.setProperty("project.orgId.home", orgHome);
74  		properties.setProperty("project.groupId.home", groupHome);
75  		properties.setProperty("project.home", groupHome);
76  
77  		// Store the groupCode
78  		properties.setProperty("project.groupId.code", groupCode);
79  
80  		// Add the current milliseconds value as a project property
81  		properties.setProperty("project.build.timestamp.millis", Long.toString(System.currentTimeMillis()));
82  
83  	}
84  
85  	// Make sure the properties hold basic project identifier info
86  	protected void validate(Properties properties) {
87  		Assert.notNull(properties.getProperty(MavenConstants.GROUP_ID_KEY), MavenConstants.GROUP_ID_KEY + " is null");
88  		Assert.notNull(properties.getProperty(MavenConstants.ARTIFACT_ID_KEY), MavenConstants.ARTIFACT_ID_KEY + " is null");
89  		Assert.notNull(properties.getProperty(MavenConstants.ORG_ID_KEY), MavenConstants.ORG_ID_KEY + " is null");
90  		Assert.notNull(properties.getProperty(MavenConstants.ORG_ID_CODE_KEY), MavenConstants.ORG_ID_CODE_KEY + " is null");
91  	}
92  
93  	protected void fixKSGroupIds(Properties properties) {
94  		// Extract the groupId
95  		String groupId = properties.getProperty(MavenConstants.GROUP_ID_KEY);
96  
97  		// Only muck with KS projects
98  		if (!StringUtils.startsWith(groupId, KS_GROUP_ID)) {
99  			return;
100 		}
101 
102 		// All KS projects should have a groupId of "org.kuali.student" no matter what
103 		properties.setProperty(MavenConstants.GROUP_ID_KEY, KualiProjectConstants.STUDENT_GROUP_ID);
104 
105 		// If this KS project is using some other group id for some reason
106 		// Store it in project.properties just for posterity
107 		if (!StringUtils.equals(groupId, KualiProjectConstants.STUDENT_GROUP_ID)) {
108 			logger.debug("original={}", groupId);
109 			properties.setProperty(MavenConstants.GROUP_ID_ORIGINAL_KEY, groupId);
110 		}
111 	}
112 
113 }