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.util.HashMap;
19  import java.util.Map;
20  import java.util.Properties;
21  
22  import org.apache.commons.io.Charsets;
23  import org.apache.commons.lang3.StringUtils;
24  import org.kuali.common.util.Assert;
25  import org.kuali.common.util.PropertyUtils;
26  import org.kuali.common.util.Str;
27  import org.kuali.common.util.maven.MavenConstants;
28  import org.kuali.common.util.property.Constants;
29  import org.springframework.util.PropertyPlaceholderHelper;
30  
31  public class DefaultProjectService implements ProjectService {
32  
33  	private static final Map<String, Project> CACHE = new HashMap<String, Project>();
34  	private static final PropertyPlaceholderHelper PPH = Constants.DEFAULT_PROPERTY_PLACEHOLDER_HELPER;
35  	private static final String PROPERTIES_ENCODING_KEY = "project.properties.encoding";
36  	private static final String PROPERTIES_ENCODING_DEFAULT = Charsets.UTF_8.toString();
37  
38  	@Override
39  	public Project getProject(Properties properties) {
40  		String groupId = properties.getProperty(MavenConstants.GROUP_ID_KEY);
41  		String artifactId = properties.getProperty(MavenConstants.ARTIFACT_ID_KEY);
42  		String version = properties.getProperty(MavenConstants.VERSION_KEY);
43  		return new ImmutableProject(groupId, artifactId, version, properties);
44  
45  	}
46  
47  	@Override
48  	public Project getProject(String projectId) {
49  
50  		Assert.hasText(projectId, "projectId is blank");
51  
52  		Project project = CACHE.get(projectId);
53  		if (project == null) {
54  			project = loadAndCache(projectId);
55  		}
56  		return project;
57  	}
58  
59  	protected Project loadAndCache(String projectId) {
60  
61  		// Split the id into tokens
62  		String[] tokens = StringUtils.split(projectId, ":");
63  
64  		// Project id's should always have exactly 2 tokens
65  		Assert.isTrue(tokens.length == 2, "tokens.length != 2");
66  
67  		// 1st token is groupId, 2nd token is artifactId
68  		String groupId = tokens[0];
69  		String artifactId = tokens[1];
70  
71  		// Get the unique path to the project.properties file
72  		String location = getPropertiesFileLocation(groupId, artifactId);
73  
74  		// If that location doesn't exist, we have issues
75  		Assert.exists(location, "[" + location + "] does not exist");
76  
77  		// Load system/environment properties
78  		Properties global = PropertyUtils.getGlobalProperties();
79  
80  		// Use UTF-8 to load project.properties, unless they've set the system property "project.properties.encoding"
81  		String encoding = global.getProperty(PROPERTIES_ENCODING_KEY, PROPERTIES_ENCODING_DEFAULT);
82  
83  		// Load the properties from disk
84  		Properties properties = PropertyUtils.load(location, encoding);
85  
86  		// Convert the properties into a project
87  		Project project = getProject(properties);
88  
89  		// Store the project in our cache
90  		CACHE.put(projectId, project);
91  
92  		// Return the project
93  		return project;
94  	}
95  
96  	protected String getPropertiesFileLocation(String groupId, String artifactId) {
97  		Properties properties = new Properties();
98  		properties.setProperty(Constants.GROUP_ID_BASE_PATH_KEY, Str.getPath(groupId));
99  		properties.setProperty(Constants.ARTIFACT_ID_KEY, artifactId);
100 		return PPH.replacePlaceholders(Constants.PROJECT_PROPERTIES_LOCATION, properties);
101 	}
102 
103 }