From 65bae084dab04ebe94247d3ca0500ec3e262f99c Mon Sep 17 00:00:00 2001 From: cburlinchon Date: Mon, 14 May 2018 11:33:06 +0100 Subject: [PATCH] ENT-1673 - Add command line arg to exclude jars from capsule on startup (#775) --- node/src/main/java/CordaCaplet.java | 30 ++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/node/src/main/java/CordaCaplet.java b/node/src/main/java/CordaCaplet.java index 0b08285cf9..861708f395 100644 --- a/node/src/main/java/CordaCaplet.java +++ b/node/src/main/java/CordaCaplet.java @@ -18,12 +18,17 @@ import java.io.File; import java.nio.file.Path; import java.nio.file.Paths; import java.util.*; +import java.util.stream.Collectors; public class CordaCaplet extends Capsule { private Config nodeConfig = null; private String baseDir = null; + // Jars to exclude which are embedded in the capsule jar + private final List excludeJars = new ArrayList<>(); + private final static String EXCLUDE_JAR_CMD_LINE_ARG = "-DexcludeJar:"; + protected CordaCaplet(Capsule pred) { super(pred); } @@ -86,26 +91,35 @@ public class CordaCaplet extends Capsule { // Equality is used here because Capsule never instantiates these attributes but instead reuses the ones // defined as public static final fields on the Capsule class, therefore referential equality is safe. if (ATTR_APP_CLASS_PATH == attr) { - T cp = super.attribute(attr); + List cp = (List) super.attribute(attr); + + // 2nd item contains all the embedded jars in our capsule. + // Can exclude jar using jvmArg -DexcludeJar. + if(cp.size() > 1 && cp.get(1) instanceof List) { + final List capsuleEmbeddedJars = (List) cp.get(1); + capsuleEmbeddedJars.removeAll(capsuleEmbeddedJars.stream(). + filter(path -> excludeJars.contains(path.toString())). + collect(Collectors.toList())); + } File cordappsDir = new File(baseDir, "cordapps"); // Create cordapps directory if it doesn't exist. requireCordappsDirExists(cordappsDir); // Add additional directories of JARs to the classpath (at the end), e.g., for JDBC drivers. - augmentClasspath((List) cp, new File(baseDir, "drivers")); - augmentClasspath((List) cp, cordappsDir); + augmentClasspath(cp, new File(baseDir, "drivers")); + augmentClasspath(cp, cordappsDir); try { List jarDirs = nodeConfig.getStringList("jarDirs"); log(LOG_VERBOSE, "Configured JAR directories = " + jarDirs); for (String jarDir : jarDirs) { - augmentClasspath((List) cp, new File(jarDir)); + augmentClasspath(cp, new File(jarDir)); } } catch (ConfigException.Missing e) { // Ignore since it's ok to be Missing. Other errors would be unexpected. } catch (ConfigException e) { log(LOG_QUIET, e); } - return cp; + return (T) cp; } else if (ATTR_JVM_ARGS == attr) { // Read JVM args from the config if specified, else leave alone. List jvmArgs = new ArrayList<>((List) super.attribute(attr)); @@ -114,6 +128,12 @@ public class CordaCaplet extends Capsule { jvmArgs.clear(); jvmArgs.addAll(configJvmArgs); log(LOG_VERBOSE, "Configured JVM args = " + jvmArgs); + + jvmArgs.forEach(arg -> { + if (arg.startsWith(EXCLUDE_JAR_CMD_LINE_ARG)) { + excludeJars.add(arg.substring(EXCLUDE_JAR_CMD_LINE_ARG.length())); + } + }); } catch (ConfigException.Missing e) { // Ignore since it's ok to be Missing. Other errors would be unexpected. } catch (ConfigException e) {