001/**
002 * Copyright 2010-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.common.util.property.processor;
017
018import java.io.File;
019import java.util.Properties;
020
021import org.apache.commons.lang3.StringUtils;
022import org.kuali.common.util.OrgUtils;
023import org.kuali.common.util.Str;
024import org.kuali.common.util.maven.MavenConstants;
025import org.kuali.common.util.project.KualiProjectConstants;
026import org.kuali.common.util.project.ProjectUtils;
027import org.kuali.common.util.project.model.Project;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030import org.springframework.util.Assert;
031
032/**
033 * This processor is called *very* early in the Maven build lifecyle in order to augment the default set of Maven properties.
034 */
035public class ProjectProcessor implements PropertyProcessor {
036
037        private static final Logger logger = LoggerFactory.getLogger(ProjectProcessor.class);
038
039        private static final String KS_GROUP_ID = KualiProjectConstants.STUDENT_GROUP_ID;
040        private static final String FS = File.separator;
041        private static final String DOT = ".";
042        private static final String PROJECT_GROUP_ID_PATH = "project.groupId.path";
043
044        @Override
045        public void process(Properties properties) {
046
047                // Make sure we are configured correctly
048                Assert.notNull(properties, "properties are null");
049
050                // Make sure groupId, artifactId, orgId, and orgCode are present
051                validate(properties);
052
053                // Fix the funk in KS groupId's (if its a KS project)
054                fixKSGroupIds(properties);
055
056                // Now that the groupId is fixed, it is safe to use the properties to get a project object
057                Project p = ProjectUtils.getProject(properties);
058
059                // Extract org info
060                String orgId = properties.getProperty(MavenConstants.ORG_ID_KEY);
061                String orgCode = properties.getProperty(MavenConstants.ORG_ID_CODE_KEY);
062
063                // Figure out the group code (eg "rice", "student", "ole", etc)
064                String groupCode = OrgUtils.getGroupCode(orgId, p.getGroupId());
065
066                // Setup some org and group paths based on user.home
067                String userHome = System.getProperty("user.home");
068                String orgHome = userHome + FS + DOT + orgCode;
069                String groupHome = orgHome + FS + groupCode;
070
071                // Store the org and group paths
072                properties.setProperty(PROJECT_GROUP_ID_PATH, Str.getPath(p.getGroupId()));
073                properties.setProperty("project.orgId.home", orgHome);
074                properties.setProperty("project.groupId.home", groupHome);
075                properties.setProperty("project.home", groupHome);
076
077                // Store the groupCode
078                properties.setProperty("project.groupId.code", groupCode);
079
080                // Add the current milliseconds value as a project property
081                properties.setProperty("project.build.timestamp.millis", Long.toString(System.currentTimeMillis()));
082
083        }
084
085        // Make sure the properties hold basic project identifier info
086        protected void validate(Properties properties) {
087                Assert.notNull(properties.getProperty(MavenConstants.GROUP_ID_KEY), MavenConstants.GROUP_ID_KEY + " is null");
088                Assert.notNull(properties.getProperty(MavenConstants.ARTIFACT_ID_KEY), MavenConstants.ARTIFACT_ID_KEY + " is null");
089                Assert.notNull(properties.getProperty(MavenConstants.ORG_ID_KEY), MavenConstants.ORG_ID_KEY + " is null");
090                Assert.notNull(properties.getProperty(MavenConstants.ORG_ID_CODE_KEY), MavenConstants.ORG_ID_CODE_KEY + " is null");
091        }
092
093        protected void fixKSGroupIds(Properties properties) {
094                // Extract the groupId
095                String groupId = properties.getProperty(MavenConstants.GROUP_ID_KEY);
096
097                // Only muck with KS projects
098                if (!StringUtils.startsWith(groupId, KS_GROUP_ID)) {
099                        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}