aboutsummaryrefslogblamecommitdiff
path: root/nailgun_launcher/NailgunLauncher.java
blob: 8b3b746c1e206c1e7d913dd6cafb4084646db07e (plain) (tree)
1
2
3
4
5
6
7
8
9








                                              

                                                                                          



                                                                                     

                                               
                             










                                                                                                   

                                                                          
                          
                                                                        
            
                                                                
                                                      
 

                                                                             
 

                                                                 
 



                                                                 

     
 
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 its
 * classpath except for the launcher itself. That's why it is written in Java without
 * dependencies outside the JDK.
 *
 * The main method loads the given class from the given class path, 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) throws ClassNotFoundException, 
                                                NoSuchMethodException, 
                                                IllegalAccessException, 
                                                InvocationTargetException,
                                                MalformedURLException {
    if (args.length < 3) {
      System.out.println("usage: <main class> <class path> <... args>");
    } else {
      // TODO: cache this classloader, but invalidate on changes
      String[] cp = args[1].split(File.pathSeparator);

      URL[] urls = new URL[cp.length];
      for(int i = 0; i < cp.length; i++){ urls[i] = new URL("file:"+cp[i]); }

      String[] newArgs = new String[cp.length - 2];
      for(int i = 2; i < cp.length; i++){ newArgs[i] = args[i]; }

      new URLClassLoader( urls )
        .loadClass(args[0])
        .getMethod("main", String[].class)
        .invoke( null/* _cls.newInstance()*/, (Object) newArgs );
    }
  }
}