mirror of
https://github.com/corda/corda.git
synced 2025-06-17 06:38:21 +00:00
CORDA-736 Add some new features to corda.jar via node.conf for testing (#1926)
This commit is contained in:
@ -2,18 +2,73 @@
|
||||
// must also be in the default package. When using Kotlin there are a whole host of exceptions
|
||||
// trying to construct this from Capsule, so it is written in Java.
|
||||
|
||||
import sun.misc.*;
|
||||
import com.typesafe.config.*;
|
||||
import sun.misc.Signal;
|
||||
import sun.misc.SignalHandler;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.*;
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.*;
|
||||
|
||||
public class CordaCaplet extends Capsule {
|
||||
|
||||
private Config nodeConfig = null;
|
||||
private String baseDir = null;
|
||||
|
||||
protected CordaCaplet(Capsule pred) {
|
||||
super(pred);
|
||||
}
|
||||
|
||||
private Config parseConfigFile(List<String> args) {
|
||||
String baseDirOption = getOption(args, "--base-directory");
|
||||
this.baseDir = Paths.get((baseDirOption == null) ? "." : baseDirOption).toAbsolutePath().normalize().toString();
|
||||
String config = getOption(args, "--config-file");
|
||||
File configFile = (config == null) ? new File(baseDir, "node.conf") : new File(config);
|
||||
try {
|
||||
ConfigParseOptions parseOptions = ConfigParseOptions.defaults().setAllowMissing(false);
|
||||
Config defaultConfig = ConfigFactory.parseResources("reference.conf", parseOptions);
|
||||
Config baseDirectoryConfig = ConfigFactory.parseMap(Collections.singletonMap("baseDirectory", baseDir));
|
||||
Config nodeConfig = ConfigFactory.parseFile(configFile, parseOptions);
|
||||
return baseDirectoryConfig.withFallback(nodeConfig).withFallback(defaultConfig).resolve();
|
||||
} catch (ConfigException e) {
|
||||
log(LOG_QUIET, e);
|
||||
return ConfigFactory.empty();
|
||||
}
|
||||
}
|
||||
|
||||
private String getOption(List<String> args, String option) {
|
||||
final String lowerCaseOption = option.toLowerCase();
|
||||
int index = 0;
|
||||
for (String arg : args) {
|
||||
if (arg.toLowerCase().equals(lowerCaseOption)) {
|
||||
if (index < args.size() - 1) {
|
||||
return args.get(index + 1);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
index++;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ProcessBuilder prelaunch(List<String> jvmArgs, List<String> args) {
|
||||
nodeConfig = parseConfigFile(args);
|
||||
return super.prelaunch(jvmArgs, args);
|
||||
}
|
||||
|
||||
// Add working directory variable to capsules string replacement variables.
|
||||
@Override
|
||||
protected String getVarValue(String var) {
|
||||
if (var.equals("baseDirectory")) {
|
||||
return baseDir;
|
||||
} else {
|
||||
return super.getVarValue(var);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Overriding the Caplet classpath generation via the intended interface in Capsule.
|
||||
*/
|
||||
@ -25,18 +80,55 @@ public class CordaCaplet extends Capsule {
|
||||
if (ATTR_APP_CLASS_PATH == attr) {
|
||||
T cp = super.attribute(attr);
|
||||
|
||||
(new File("cordapps")).mkdir();
|
||||
augmentClasspath((List<Path>) cp, "cordapps");
|
||||
augmentClasspath((List<Path>) cp, "plugins");
|
||||
(new File(baseDir, "cordapps")).mkdir();
|
||||
augmentClasspath((List<Path>) cp, new File(baseDir, "cordapps"));
|
||||
augmentClasspath((List<Path>) cp, new File(baseDir, "plugins"));
|
||||
// Add additional directories of JARs to the classpath (at the end). e.g. for JDBC drivers
|
||||
try {
|
||||
List<String> jarDirs = nodeConfig.getStringList("jarDirs");
|
||||
log(LOG_VERBOSE, "Configured JAR directories = " + jarDirs);
|
||||
for (String jarDir : jarDirs) {
|
||||
augmentClasspath((List<Path>) 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 super.attribute(attr);
|
||||
} else if (ATTR_JVM_ARGS == attr) {
|
||||
// Read JVM args from the config if specified, else leave alone.
|
||||
List<String> jvmArgs = new ArrayList<>((List<String>) super.attribute(attr));
|
||||
try {
|
||||
List<String> configJvmArgs = nodeConfig.getStringList("jvmArgs");
|
||||
jvmArgs.clear();
|
||||
jvmArgs.addAll(configJvmArgs);
|
||||
log(LOG_VERBOSE, "Configured JVM args = " + jvmArgs);
|
||||
} 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 (T) jvmArgs;
|
||||
} else if (ATTR_SYSTEM_PROPERTIES == attr) {
|
||||
// Add system properties, if specified, from the config.
|
||||
Map<String, String> systemProps = new LinkedHashMap<>((Map<String, String>) super.attribute(attr));
|
||||
try {
|
||||
Config overrideSystemProps = nodeConfig.getConfig("systemProperties");
|
||||
log(LOG_VERBOSE, "Configured system properties = " + overrideSystemProps);
|
||||
for (Map.Entry<String, ConfigValue> entry : overrideSystemProps.entrySet()) {
|
||||
systemProps.put(entry.getKey(), entry.getValue().unwrapped().toString());
|
||||
}
|
||||
} 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 (T) systemProps;
|
||||
} else return super.attribute(attr);
|
||||
}
|
||||
|
||||
// TODO: Make directory configurable via the capsule manifest.
|
||||
// TODO: Add working directory variable to capsules string replacement variables.
|
||||
private void augmentClasspath(List<Path> classpath, String dirName) {
|
||||
File dir = new File(dirName);
|
||||
private void augmentClasspath(List<Path> classpath, File dir) {
|
||||
if (dir.exists()) {
|
||||
File[] files = dir.listFiles();
|
||||
for (File file : files) {
|
||||
|
Reference in New Issue
Block a user