Skip to content

Commit

Permalink
CORDA-1354 Check for jars in nested folders (to add in classpath) (co…
Browse files Browse the repository at this point in the history
…rda#2981)

+ edge cases (logging if we don't have permission to jar folders)
  • Loading branch information
Konstantinos Chalkias authored Apr 24, 2018
1 parent 5f70b6e commit 03fb9d4
Showing 1 changed file with 47 additions and 13 deletions.
60 changes: 47 additions & 13 deletions node/src/main/java/CordaCaplet.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

import com.typesafe.config.*;
import sun.misc.Signal;
import sun.misc.SignalHandler;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -80,9 +78,11 @@ protected <T> T attribute(Map.Entry<String, T> attr) {
if (ATTR_APP_CLASS_PATH == attr) {
T cp = super.attribute(attr);

(new File(baseDir, "cordapps")).mkdir();
// Add additional directories of JARs to the classpath (at the end). e.g. for JDBC drivers
augmentClasspath((List<Path>) cp, new File(baseDir, "cordapps"));
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<Path>) cp, cordappsDir);
try {
List<String> jarDirs = nodeConfig.getStringList("jarDirs");
log(LOG_VERBOSE, "Configured JAR directories = " + jarDirs);
Expand Down Expand Up @@ -128,24 +128,58 @@ protected <T> T attribute(Map.Entry<String, T> attr) {
}

private void augmentClasspath(List<Path> classpath, File dir) {
if (dir.exists()) {
File[] files = dir.listFiles();
for (File file : files) {
try {
if (dir.exists()) {
// The following might return null if the directory is not there (we check this already) or if an I/O error occurs.
for (File file : dir.listFiles()) {
addToClasspath(classpath, file);
}
} else {
log(LOG_VERBOSE, "Directory to add in Classpath was not found " + dir.getAbsolutePath());
}
} catch (SecurityException | NullPointerException e) {
log(LOG_QUIET, e);
}
}

private void requireCordappsDirExists(File dir) {
try {
if (!dir.mkdir() && !dir.exists()) { // It is unlikely to enter this if-branch, but just in case.
logOnFailedCordappDir();
throw new RuntimeException("Cordapps dir could not be created"); // Let Capsule handle the error (log error, clean up, die).
}
}
catch (SecurityException | NullPointerException e) {
logOnFailedCordappDir();
throw e; // Let Capsule handle the error (log error, clean up, die).
}
}

private void logOnFailedCordappDir() {
log(LOG_VERBOSE, "Cordapps dir could not be created");
}

private void addToClasspath(List<Path> classpath, File file) {
try {
if (file.canRead()) {
if (file.isFile() && isJAR(file)) {
classpath.add(file.toPath().toAbsolutePath());
} else if (file.isDirectory()) { // Search in nested folders as well. TODO: check for circular symlinks.
augmentClasspath(classpath, file);
}
} else {
log(LOG_VERBOSE, "File or directory to add in Classpath could not be read " + file.getAbsolutePath());
}
} catch (SecurityException | NullPointerException e) {
log(LOG_QUIET, e);
}
}

@Override
protected void liftoff() {
super.liftoff();
Signal.handle(new Signal("INT"), new SignalHandler() {
@Override
public void handle(Signal signal) {
// Disable Ctrl-C for this process, so the child process can handle it in the shell instead.
}
Signal.handle(new Signal("INT"), signal -> {
// Disable Ctrl-C for this process, so the child process can handle it in the shell instead.
});
}

Expand Down

0 comments on commit 03fb9d4

Please sign in to comment.