001/**
002 * Copyright 2010-2014 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.project;
017
018import java.nio.charset.Charset;
019import java.util.Properties;
020
021import org.kuali.common.util.Assert;
022import org.kuali.common.util.PropertyUtils;
023import org.kuali.common.util.Str;
024import org.kuali.common.util.cache.Cache;
025import org.kuali.common.util.cache.SimpleCache;
026import org.kuali.common.util.maven.MavenConstants;
027import org.kuali.common.util.project.model.ImmutableProject;
028import org.kuali.common.util.project.model.Project;
029import org.kuali.common.util.project.model.ProjectIdentifier;
030import org.kuali.common.util.property.Constants;
031import org.kuali.common.util.spring.env.EnvironmentService;
032import org.springframework.util.PropertyPlaceholderHelper;
033
034public class DefaultProjectService implements ProjectService {
035
036        private static final Cache<String, Project> CACHE = new SimpleCache<String, Project>();
037        private static final PropertyPlaceholderHelper PPH = Constants.DEFAULT_PROPERTY_PLACEHOLDER_HELPER;
038        private static final String PROPERTIES_ENCODING_KEY = "project.properties.encoding";
039        private static final String PROPERTIES_ENCODING_DEFAULT = Charset.defaultCharset().toString();
040
041        private final EnvironmentService env;
042
043        public DefaultProjectService(EnvironmentService env) {
044                Assert.noNulls(env);
045                this.env = env;
046        }
047
048        /**
049         * @deprecated Use ProjectUtils.getProject(properties) instead
050         */
051        @Deprecated
052        @Override
053        public Project getProject(Properties properties) {
054                String groupId = properties.getProperty(MavenConstants.GROUP_ID_KEY);
055                String artifactId = properties.getProperty(MavenConstants.ARTIFACT_ID_KEY);
056                String version = properties.getProperty(MavenConstants.VERSION_KEY);
057                return new ImmutableProject(groupId, artifactId, version, properties);
058        }
059
060        @Override
061        public Project getProject(ProjectIdentifier identifier) {
062                return getProject(identifier.getGroupId(), identifier.getArtifactId());
063        }
064
065        @Override
066        public Project getProject(String groupId, String artifactId) {
067
068                // Both of these are required
069                Assert.noBlanks("groupId and artifactId are required", groupId, artifactId);
070
071                // Construct the cache key
072                String cacheKey = groupId + ":" + artifactId;
073
074                // Check the cache
075                Project project = CACHE.get(cacheKey);
076                if (project == null) {
077                        // Construct a project object from project.properties
078                        project = load(groupId, artifactId);
079                        // Cache it
080                        CACHE.put(cacheKey, project);
081                }
082                return project;
083        }
084
085        protected void clearCache() {
086                CACHE.clear();
087        }
088
089        protected Project load(String groupId, String artifactId) {
090
091                // Get the unique path to the project.properties file
092                String location = getPropertiesFileLocation(groupId, artifactId);
093
094                // If that location doesn't exist, we have issues
095                Assert.exists(location, "[" + location + "] does not exist");
096
097                // Use platform default encoding to load project.properties
098                // Set the system property "project.properties.encoding" or the environment variable "PROJECT_PROPERTIES_ENCODING" to override
099                String encoding = env.getString(PROPERTIES_ENCODING_KEY, PROPERTIES_ENCODING_DEFAULT);
100
101                // Load the properties from disk
102                Properties properties = PropertyUtils.load(location, encoding);
103
104                // Convert the properties into a project
105                return getProject(properties);
106        }
107
108        protected String getPropertiesFileLocation(String groupId, String artifactId) {
109                Properties properties = new Properties();
110                properties.setProperty(Constants.GROUP_ID_PATH_KEY, Str.getPath(groupId));
111                properties.setProperty(Constants.ARTIFACT_ID_KEY, artifactId);
112                return PPH.replacePlaceholders(Constants.PROJECT_PROPERTIES_LOCATION, properties);
113        }
114
115        public EnvironmentService getEnv() {
116                return env;
117        }
118
119}