Skip to content

Commit

Permalink
set cache dir in CapsuleLauncher
Browse files Browse the repository at this point in the history
  • Loading branch information
pron committed Jan 23, 2015
1 parent 6efbf73 commit a2f294b
Showing 1 changed file with 26 additions and 21 deletions.
Expand Up @@ -44,7 +44,6 @@ public final class CapsuleLauncher {
private final Path jarFile;
private final Class capsuleClass;
private final Properties properties;
private Map<String, Path> javaHomes;

public CapsuleLauncher(Path jarFile) throws IOException {
this(jarFile, null);
Expand All @@ -54,7 +53,7 @@ public CapsuleLauncher(Path jarFile, Properties properties) throws IOException {
this.jarFile = jarFile;
this.properties = properties != null ? properties : new Properties(System.getProperties());
this.capsuleClass = loadCapsuleClass(jarFile);
setProperties(capsuleClass, this.properties);
set(null, getCapsuleField("PROPERTIES"), properties);
}

/**
Expand All @@ -64,7 +63,9 @@ public CapsuleLauncher(Path jarFile, Properties properties) throws IOException {
* @return {@code this}
*/
public CapsuleLauncher setJavaHomes(Map<String, Path> javaHomes) {
this.javaHomes = javaHomes;
final Field homes = getCapsuleField("JAVA_HOMES");
if (homes != null)
set(null, homes, javaHomes);
return this;
}

Expand All @@ -80,6 +81,17 @@ public CapsuleLauncher setProperty(String property, String value) {
return this;
}

/**
* Sets the location of the cache directory for the capsules created by {@code newCapsule}
*
* @param dir the cache directory
* @return {@code this}
*/
public CapsuleLauncher setCacheDir(Path dir) {
set(null, getCapsuleField("CACHE_DIR"), dir);
return this;
}

/**
* Creates a new capsule.
*/
Expand Down Expand Up @@ -117,9 +129,6 @@ public Capsule newCapsule(Path wrappedJar) {
* @return the capsule.
*/
public Capsule newCapsule(String mode, Path wrappedJar) {
if (javaHomes != null)
setJavaHomes(capsuleClass, javaHomes);

final String oldMode = properties.getProperty(PROP_MODE);
try {
properties.setProperty(PROP_MODE, mode);
Expand Down Expand Up @@ -171,9 +180,7 @@ private static Class<?> loadCapsuleClass(Manifest mf, ClassLoader cl) {
private static boolean isCapsuleClass(Class<?> clazz) {
if (clazz == null)
return false;
if (CAPSULE_CLASS_NAME.equals(clazz.getName()))
return true;
return isCapsuleClass(clazz.getSuperclass());
return getActualCapsuleClass(clazz) != null;
}

private static Capsule wrap(Object capsule) {
Expand Down Expand Up @@ -242,18 +249,6 @@ public static Map<String, Path> findJavaHomes() {
}
}

private static void setJavaHomes(Class<?> capsuleClass, Map<String, Path> javaHomes) {
final Field homes = getField(capsuleClass, "JAVA_HOMES");
if (homes == null)
return;
set(null, homes, javaHomes);
}

private static void setProperties(Class<?> capsuleClass, Properties properties) {
final Field props = getField(capsuleClass, "PROPERTIES");
set(null, props, properties);
}

/**
* Adds an option to the JVM arguments to enable JMX connection
*
Expand Down Expand Up @@ -305,6 +300,10 @@ private static boolean isWindows() {
return System.getProperty(PROP_OS_NAME).toLowerCase().startsWith("windows");
}

private Field getCapsuleField(String name) {
return getField(getActualCapsuleClass(capsuleClass), name);
}

//<editor-fold defaultstate="collapsed" desc="Reflection">
/////////// Reflection ///////////////////////////////////
private static Method getMethod(Class<?> clazz, String name, Class<?>... paramTypes) {
Expand All @@ -331,6 +330,12 @@ private static Field getField(Class<?> clazz, String name) {
}
}

private static Class<?> getActualCapsuleClass(Class<?> clazz) {
while (clazz != null && !clazz.getName().equals(CAPSULE_CLASS_NAME))
clazz = clazz.getSuperclass();
return clazz;
}

private static Object invoke(Object obj, Method method, Object... params) {
try {
return method.invoke(obj, params);
Expand Down

0 comments on commit a2f294b

Please sign in to comment.