diff options
author | Christopher Vogt <oss.nsp@cvogt.org> | 2016-02-06 13:03:36 -0500 |
---|---|---|
committer | Christopher Vogt <oss.nsp@cvogt.org> | 2016-03-04 15:06:30 -0500 |
commit | 974942db43ff2d1fa7ba71ad60f9bb9eae2d8631 (patch) | |
tree | d7235df9d4d6a67753dc2a20ab6bfcb7a24dc74c /nailgun_launcher | |
download | cbt-974942db43ff2d1fa7ba71ad60f9bb9eae2d8631.tar.gz cbt-974942db43ff2d1fa7ba71ad60f9bb9eae2d8631.tar.bz2 cbt-974942db43ff2d1fa7ba71ad60f9bb9eae2d8631.zip |
CBT Version 1.0-BETA
Diffstat (limited to 'nailgun_launcher')
-rw-r--r-- | nailgun_launcher/NailgunLauncher.java | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/nailgun_launcher/NailgunLauncher.java b/nailgun_launcher/NailgunLauncher.java new file mode 100644 index 0000000..a6858d9 --- /dev/null +++ b/nailgun_launcher/NailgunLauncher.java @@ -0,0 +1,48 @@ +package cbt; +import java.io.*; +import java.lang.reflect.*; +import java.net.*; +import java.nio.*; +import java.nio.file.*; +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; + +/* +This launcher allows to use Nailgun without loading anything else permanenetly into it's classpath. +The main method loads the given class from the given class math, calls it's main methods passing +in the additional arguments. +*/ +public class NailgunLauncher{ + /* Persistent cache for caching classloaders for the JVM life time. + Can be used as needed by user code to improve startup time. */ + public static ConcurrentHashMap<String,ClassLoader> classLoaderCache = new ConcurrentHashMap<String,ClassLoader>(); + public static void main(String[] args){ + try{ + if(args.length < 3){ + System.out.println("usage: <main class> <class path> <... args>"); + } else { + URLClassLoader cl = new URLClassLoader( // TODO: cache this classloader, but invalidate on changes + Arrays.stream( + args[1].split(File.pathSeparator) + ).filter( cp -> !(cp == "") ).map( cp -> { + try { return new URL("file:" + cp); } + catch(MalformedURLException e) { throw new RuntimeException(e); } + }).toArray(URL[]::new) + ){ + public String toString(){ + String suffix = ""; + if(getParent() != ClassLoader.getSystemClassLoader()) + suffix = ", "+getParent(); + return "URLClassLoader(" + Arrays.toString(getURLs()) + suffix +")"; + } + }; + cl .loadClass( args[0] ) + .getMethod( "main", String[].class ) + .invoke( + null/* _cls.newInstance()*/, + (Object) Arrays.stream(args).skip(2).toArray( String[]::new ) + ); + } + } catch (Exception e) { throw new RuntimeException(e); } + } +} |