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;
17  
18  import java.nio.charset.Charset;
19  import java.util.Properties;
20  
21  import org.kuali.common.util.Assert;
22  import org.kuali.common.util.PropertyUtils;
23  import org.kuali.common.util.Str;
24  import org.kuali.common.util.cache.Cache;
25  import org.kuali.common.util.cache.SimpleCache;
26  import org.kuali.common.util.maven.MavenConstants;
27  import org.kuali.common.util.project.model.ImmutableProject;
28  import org.kuali.common.util.project.model.Project;
29  import org.kuali.common.util.project.model.ProjectIdentifier;
30  import org.kuali.common.util.property.Constants;
31  import org.kuali.common.util.spring.env.EnvironmentService;
32  import org.springframework.util.PropertyPlaceholderHelper;
33  
34  public class DefaultProjectService implements ProjectService {
35  
36  	private static final Cache<String, Project> CACHE = new SimpleCache<String, Project>();
37  	private static final PropertyPlaceholderHelper PPH = Constants.DEFAULT_PROPERTY_PLACEHOLDER_HELPER;
38  	private static final String PROPERTIES_ENCODING_KEY = "project.properties.encoding";
39  	private static final String PROPERTIES_ENCODING_DEFAULT = Charset.defaultCharset().toString();
40  
41  	private final EnvironmentService env;
42  
43  	public DefaultProjectService(EnvironmentService env) {
44  		Assert.noNulls(env);
45  		this.env = env;
46  	}
47  
48  	/**
49  	 * @deprecated Use ProjectUtils.getProject(properties) instead
50  	 */
51  	@Deprecated
52  	@Override
53  	public Project getProject(Properties properties) {
54  		String groupId = properties.getProperty(MavenConstants.GROUP_ID_KEY);
55  		String artifactId = properties.getProperty(MavenConstants.ARTIFACT_ID_KEY);
56  		String version = properties.getProperty(MavenConstants.VERSION_KEY);
57  		return new ImmutableProject(groupId, artifactId, version, properties);
58  	}
59  
60  	@Override
61  	public Project getProject(ProjectIdentifier identifier) {
62  		return getProject(identifier.getGroupId(), identifier.getArtifactId());
63  	}
64  
65  	@Override
66  	public Project getProject(String groupId, String artifactId) {
67  
68  		// Both of these are required
69  		Assert.noBlanks("groupId and artifactId are required", groupId, artifactId);
70  
71  		// Construct the cache key
72  		String cacheKey = groupId + ":" + artifactId;
73  
74  		// Check the cache
75  		Project project = CACHE.get(cacheKey);
76  		if (project == null) {
77  			// Construct a project object from project.properties
78  			project = load(groupId, artifactId);
79  			// Cache it
80  			CACHE.put(cacheKey, project);
81  		}
82  		return project;
83  	}
84  
85  	protected void clearCache() {
86  		CACHE.clear();
87  	}
88  
89  	protected Project load(String groupId, String artifactId) {
90  
91  		// Get the unique path to the project.properties file
92  		String location = getPropertiesFileLocation(groupId, artifactId);
93  
94  		// If that location doesn't exist, we have issues
95  		Assert.exists(location, "[" + location + "] does not exist");
96  
97  		// Use platform default encoding to load project.properties
98  		// Set the system property "project.properties.encoding" or the environment variable "PROJECT_PROPERTIES_ENCODING" to override
99  		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 }