From a2f294b6f6e974580091fe55b750842e0d5cc3e2 Mon Sep 17 00:00:00 2001 From: pron Date: Sat, 24 Jan 2015 00:54:30 +0200 Subject: [PATCH] set cache dir in CapsuleLauncher --- .../capsule/CapsuleLauncher.java | 47 ++++++++++--------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/capsule-util/src/main/java/co/paralleluniverse/capsule/CapsuleLauncher.java b/capsule-util/src/main/java/co/paralleluniverse/capsule/CapsuleLauncher.java index 8a9981a..2ffa6d0 100644 --- a/capsule-util/src/main/java/co/paralleluniverse/capsule/CapsuleLauncher.java +++ b/capsule-util/src/main/java/co/paralleluniverse/capsule/CapsuleLauncher.java @@ -44,7 +44,6 @@ public final class CapsuleLauncher { private final Path jarFile; private final Class capsuleClass; private final Properties properties; - private Map javaHomes; public CapsuleLauncher(Path jarFile) throws IOException { this(jarFile, null); @@ -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); } /** @@ -64,7 +63,9 @@ public CapsuleLauncher(Path jarFile, Properties properties) throws IOException { * @return {@code this} */ public CapsuleLauncher setJavaHomes(Map javaHomes) { - this.javaHomes = javaHomes; + final Field homes = getCapsuleField("JAVA_HOMES"); + if (homes != null) + set(null, homes, javaHomes); return this; } @@ -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. */ @@ -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); @@ -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) { @@ -242,18 +249,6 @@ public static Map findJavaHomes() { } } - private static void setJavaHomes(Class capsuleClass, Map 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 * @@ -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); + } + // /////////// Reflection /////////////////////////////////// private static Method getMethod(Class clazz, String name, Class... paramTypes) { @@ -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);