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