Skip to content

Instantly share code, notes, and snippets.

@flandr
Created July 30, 2015 18:03
Show Gist options
  • Save flandr/bb2a9ceed64b3dac324e to your computer and use it in GitHub Desktop.
Save flandr/bb2a9ceed64b3dac324e to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.nio.file.Paths;
/** Caplet to include runtime classpath. */
public class RuntimeClasspathCaplet extends Capsule {
private static final String PROP_RUNTIME_CP = "caplet.runtime.classpath";
/** Composition constructor. */
public RuntimeClasspathCaplet(final Capsule prev) {
super(prev);
}
// Regrettably we can't use any external libraries in this method (e.g.
// Guava) that are not used by the core Capsule jar because this method is
// invoked prior to the capsule being expanded.
@Override
@SuppressWarnings("unchecked")
protected <T> T attribute(Map.Entry<String, T> attr) {
if (attr != ATTR_APP_CLASS_PATH) {
return super.attribute(attr);
}
String classpath = System.getProperty(PROP_RUNTIME_CP);
if (classpath == null || classpath.isEmpty()) {
return super.attribute(attr);
}
List<Object> cplist = new ArrayList<>(
(List<Object>) super.attribute(attr));
for (String component : classpath.split(":")) {
cplist.add(Paths.get(component).toAbsolutePath().normalize()
.toString());
}
return (T) cplist;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment