001package org.kuali.common.devops.metadata.function; 002 003import static org.kuali.common.util.base.Precondition.checkNotNull; 004 005import java.util.List; 006import java.util.Properties; 007 008import org.kuali.common.util.Str; 009 010import com.google.common.base.Function; 011import com.google.common.base.Optional; 012import com.google.common.base.Splitter; 013 014public final class ProjectPropertiesUrlFragmentFunction implements Function<Properties, Optional<String>> { 015 016 private static final String FILENAME = "project.properties"; 017 private static final String BUNDLE_SYMBOLIC_NAME_KEY = "Bundle-SymbolicName"; 018 private static final String METAINF_FRAGMENT = "/tomcat/webapps/ROOT/WEB-INF/classes/META-INF"; 019 020 @Override 021 public Optional<String> apply(Properties manifest) { 022 checkNotNull(manifest, "manifest"); 023 return getProjectPropertiesUrlFragment(manifest); 024 } 025 026 protected Optional<String> getProjectPropertiesUrlFragment(Properties manifest) { 027 Optional<String> name = Optional.fromNullable(manifest.getProperty(BUNDLE_SYMBOLIC_NAME_KEY)); 028 if (name.isPresent()) { 029 return Optional.of(getProjectPropertiesUrlFragment(name.get())); 030 } else { 031 return Optional.absent(); 032 } 033 } 034 035 protected String getProjectPropertiesUrlFragment(String bundleSymbolicName) { 036 String groupId = getGroupId(bundleSymbolicName); 037 String artifactId = getArtifactId(bundleSymbolicName); 038 StringBuilder sb = new StringBuilder(); 039 sb.append(METAINF_FRAGMENT); 040 sb.append("/"); 041 sb.append(Str.getPath(groupId)); 042 sb.append("/"); 043 sb.append(artifactId); 044 sb.append("/"); 045 sb.append(FILENAME); 046 return sb.toString(); 047 } 048 049 protected String getGroupId(String bundleSymbolicName) { 050 char separator = '.'; 051 List<String> tokens = Splitter.on(separator).splitToList(bundleSymbolicName); 052 StringBuilder sb = new StringBuilder(); 053 for (int i = 0; i < tokens.size() - 1; i++) { 054 if (i != 0) { 055 sb.append(separator); 056 } 057 String token = tokens.get(i); 058 sb.append(token); 059 // Shorten "org.kuali.student.*" -> "org.kuali.student" 060 if (token.equals("student")) { 061 break; 062 } 063 } 064 return sb.toString(); 065 } 066 067 protected String getArtifactId(String bundleSymbolicName) { 068 List<String> tokens = Splitter.on('.').splitToList(bundleSymbolicName); 069 return tokens.get(tokens.size() - 1); 070 } 071 072}