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;
17  
18  import java.lang.reflect.InvocationTargetException;
19  import java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.Properties;
24  import java.util.Set;
25  
26  import org.apache.commons.beanutils.BeanUtils;
27  import org.apache.commons.lang3.StringUtils;
28  import org.kuali.common.util.property.Constants;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  import org.springframework.util.PropertyPlaceholderHelper;
32  
33  /**
34   * 
35   */
36  public class ProjectUtils {
37  
38  	private static final Logger logger = LoggerFactory.getLogger(ProjectUtils.class);
39  	private static final PropertyPlaceholderHelper PPH = Constants.DEFAULT_PROPERTY_PLACEHOLDER_HELPER;
40  
41  	public static Project loadProject(String gav) {
42  		Project project = getProject(gav);
43  		Properties properties = getProperties(project);
44  		return getProject(properties);
45  	}
46  
47  	public static Project getProject(String gav) {
48  		logger.debug("Processing [{}]", gav);
49  		String[] tokens = StringUtils.split(gav, ":");
50  
51  		Project project = new Project();
52  		if (tokens.length > 0) {
53  			project.setGroupId(RepositoryUtils.toNull(tokens[0]));
54  		}
55  		if (tokens.length > 1) {
56  			project.setArtifactId(RepositoryUtils.toNull(tokens[1]));
57  		}
58  		if (tokens.length > 2) {
59  			project.setPackaging(RepositoryUtils.toNull(tokens[2]));
60  		}
61  		if (tokens.length > 3) {
62  			project.setVersion(RepositoryUtils.toNull(tokens[3]));
63  		}
64  		if (tokens.length > 4) {
65  			project.setClassifier(RepositoryUtils.toNull(tokens[4]));
66  		}
67  		return project;
68  	}
69  
70  	public static List<Dependency> getDependencies(String csv) {
71  		List<String> tokens = CollectionUtils.getTrimmedListFromCSV(csv);
72  		List<Dependency> dependencies = new ArrayList<Dependency>();
73  		for (String token : tokens) {
74  			Dependency dependency = RepositoryUtils.parseDependency(token);
75  			dependencies.add(dependency);
76  		}
77  		return dependencies;
78  	}
79  
80  	public static Project getProject(Properties properties) {
81  		List<String> skipKeys = Arrays.asList("project.dependencies");
82  		String startsWith = "project.";
83  		List<String> keys = PropertyUtils.getStartsWithKeys(properties, startsWith);
84  		Project project = new Project();
85  		project.setProperties(properties);
86  		Map<String, Object> description = describe(project);
87  		Set<String> beanProperties = description.keySet();
88  		for (String key : keys) {
89  			if (skipKeys.contains(key)) {
90  				continue;
91  			}
92  			String value = properties.getProperty(key);
93  			String beanProperty = getBeanProperty(key, startsWith);
94  			if (beanProperties.contains(beanProperty)) {
95  				copyProperty(project, beanProperty, value);
96  			}
97  		}
98  		String csv = RepositoryUtils.toNull(properties.getProperty("project.dependencies"));
99  		List<Dependency> dependencies = getDependencies(csv);
100 		project.setDependencies(dependencies);
101 		return project;
102 	}
103 
104 	protected static String getBeanProperty(String key, String startsWith) {
105 		String s = StringUtils.substring(key, startsWith.length());
106 		String[] tokens = StringUtils.split(s, ".");
107 		StringBuilder sb = new StringBuilder();
108 		for (int i = 0; i < tokens.length; i++) {
109 			String token = tokens[i];
110 			if (i == 0) {
111 				sb.append(token);
112 			} else {
113 				sb.append(StringUtils.capitalize(token));
114 			}
115 		}
116 		return sb.toString();
117 	}
118 
119 	public static Properties getProperties(String gav) {
120 		return getProperties(getProject(gav));
121 	}
122 
123 	public static Properties getProperties(Project project) {
124 		String location = getPropertiesLocation(project);
125 		if (!LocationUtils.exists(location)) {
126 			throw new IllegalArgumentException("[" + location + "] does not exist");
127 		}
128 		return PropertyUtils.load(location);
129 	}
130 
131 	public static String getPropertiesLocation(Project project) {
132 		Assert.hasText(project.getGroupId(), "groupId has no text");
133 		Assert.hasText(project.getArtifactId(), "artifactId has no text");
134 
135 		Properties properties = new Properties();
136 		properties.setProperty("project.groupId.path", Str.getPath(project.getGroupId()));
137 		properties.setProperty("project.artifactId", project.getArtifactId());
138 
139 		return PPH.replacePlaceholders(Constants.PROJECT_PROPERTIES_LOCATION, properties);
140 	}
141 
142 	public static String getJavaSourceFileTemplate() {
143 		StringBuilder sb = new StringBuilder();
144 		sb.append("package ${project.groupId};\n");
145 		sb.append("\n");
146 		sb.append("/**\n");
147 		sb.append(" * ############################# WARNING ##############################\n");
148 		sb.append(" * \n");
149 		sb.append(" * This is a generated file. Do NOT edit.\n");
150 		sb.append(" * \n");
151 		sb.append(" * Automated process keeps this source file in sync with the Maven POM.\n");
152 		sb.append(" */\n");
153 		sb.append("public abstract class ${project.artifactId.classname} {\n");
154 		sb.append("\n");
155 		sb.append("	public static final String GROUP_ID = \"${project.groupId}\";\n");
156 		sb.append("	public static final String ARTIFACT_ID = \"${project.artifactId}\";\n");
157 		sb.append("	public static final String VERSION = \"${project.version}\";\n");
158 		sb.append("\n");
159 		sb.append("}\n");
160 		return sb.toString();
161 	}
162 
163 	@SuppressWarnings("unchecked")
164 	protected static Map<String, Object> describe(Object bean) {
165 		try {
166 			return BeanUtils.describe(bean);
167 		} catch (IllegalAccessException e) {
168 			throw new IllegalStateException(e);
169 		} catch (InvocationTargetException e) {
170 			throw new IllegalStateException(e);
171 		} catch (NoSuchMethodException e) {
172 			throw new IllegalStateException(e);
173 		}
174 	}
175 
176 	protected static void copyProperty(Object bean, String name, Object value) {
177 		try {
178 			BeanUtils.copyProperty(bean, name, value);
179 		} catch (IllegalAccessException e) {
180 			throw new IllegalStateException(e);
181 		} catch (InvocationTargetException e) {
182 			throw new IllegalStateException(e);
183 		}
184 	}
185 
186 }