View Javadoc
1   package org.kuali.common.devops.metadata.function;
2   
3   import static org.kuali.common.util.base.Precondition.checkNotNull;
4   
5   import java.util.List;
6   import java.util.Properties;
7   
8   import org.kuali.common.util.Str;
9   
10  import com.google.common.base.Function;
11  import com.google.common.base.Optional;
12  import com.google.common.base.Splitter;
13  
14  public final class ProjectPropertiesUrlFragmentFunction implements Function<Properties, Optional<String>> {
15  
16  	private static final String FILENAME = "project.properties";
17  	private static final String BUNDLE_SYMBOLIC_NAME_KEY = "Bundle-SymbolicName";
18  	private static final String METAINF_FRAGMENT = "/tomcat/webapps/ROOT/WEB-INF/classes/META-INF";
19  
20  	@Override
21  	public Optional<String> apply(Properties manifest) {
22  		checkNotNull(manifest, "manifest");
23  		return getProjectPropertiesUrlFragment(manifest);
24  	}
25  
26  	protected Optional<String> getProjectPropertiesUrlFragment(Properties manifest) {
27  		Optional<String> name = Optional.fromNullable(manifest.getProperty(BUNDLE_SYMBOLIC_NAME_KEY));
28  		if (name.isPresent()) {
29  			return Optional.of(getProjectPropertiesUrlFragment(name.get()));
30  		} else {
31  			return Optional.absent();
32  		}
33  	}
34  
35  	protected String getProjectPropertiesUrlFragment(String bundleSymbolicName) {
36  		String groupId = getGroupId(bundleSymbolicName);
37  		String artifactId = getArtifactId(bundleSymbolicName);
38  		StringBuilder sb = new StringBuilder();
39  		sb.append(METAINF_FRAGMENT);
40  		sb.append("/");
41  		sb.append(Str.getPath(groupId));
42  		sb.append("/");
43  		sb.append(artifactId);
44  		sb.append("/");
45  		sb.append(FILENAME);
46  		return sb.toString();
47  	}
48  
49  	protected String getGroupId(String bundleSymbolicName) {
50  		char separator = '.';
51  		List<String> tokens = Splitter.on(separator).splitToList(bundleSymbolicName);
52  		StringBuilder sb = new StringBuilder();
53  		for (int i = 0; i < tokens.size() - 1; i++) {
54  			if (i != 0) {
55  				sb.append(separator);
56  			}
57  			String token = tokens.get(i);
58  			sb.append(token);
59  			// Shorten "org.kuali.student.*" -> "org.kuali.student"
60  			if (token.equals("student")) {
61  				break;
62  			}
63  		}
64  		return sb.toString();
65  	}
66  
67  	protected String getArtifactId(String bundleSymbolicName) {
68  		List<String> tokens = Splitter.on('.').splitToList(bundleSymbolicName);
69  		return tokens.get(tokens.size() - 1);
70  	}
71  
72  }