1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
69 Assert.noBlanks("groupId and artifactId are required", groupId, artifactId);
70
71
72 String cacheKey = groupId + ":" + artifactId;
73
74
75 Project project = CACHE.get(cacheKey);
76 if (project == null) {
77
78 project = load(groupId, artifactId);
79
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
92 String location = getPropertiesFileLocation(groupId, artifactId);
93
94
95 Assert.exists(location, "[" + location + "] does not exist");
96
97
98
99 String encoding = env.getString(PROPERTIES_ENCODING_KEY, PROPERTIES_ENCODING_DEFAULT);
100
101
102 Properties properties = PropertyUtils.load(location, encoding);
103
104
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 }