Created
July 26, 2010 19:13
-
-
Save gamlerhart/491066 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BootsTrapper { | |
private final static String APPLICATION_PATH = "./application"; | |
private final static String APPLICATION_MAIN = "info.gamlor.application.ApplicationMain"; | |
public static void main(String[] args) { | |
final File file = new File(APPLICATION_PATH); | |
try { | |
tryStartUp(file,args); | |
} catch (Exception e) { | |
System.err.println("Unexpected exception'" + file + "'."); | |
e.printStackTrace(System.err); | |
} | |
} | |
private static void tryStartUp(File applicationRoot,String[] arguments) throws Exception { | |
if (applicationRoot.exists()) { | |
final URL[] applicationJars = enumerateLibs(applicationRoot); | |
// Here we setup our native library class loader | |
final NativeLibPathClassLoader classLoader = new NativeLibPathClassLoader(applicationJars); | |
Thread.currentThread().setContextClassLoader(classLoader); | |
// and load the real-applicaiton with it | |
final Class<?> bootClass = classLoader.loadClass(APPLICATION_MAIN); | |
final Method mainMethod = bootClass.getMethod("main", arguments.getClass()); | |
mainMethod.invoke(null,new Object[]{arguments}); | |
} else { | |
System.err.println("Couldn't find application-jars in '" + applicationRoot + "'."); | |
} | |
} | |
private static URL[] enumerateLibs(File file) throws MalformedURLException { | |
final File[] files = file.listFiles(); | |
final List<URL> urls = new ArrayList<URL>(); | |
for (File jarFile : files) { | |
urls.add(jarFile.toURI().toURL()); | |
} | |
return urls.toArray(new URL[urls.size()]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NativeLibPathClassLoader extends URLClassLoader{ | |
private final static String LIB_PATH = "./native"; | |
public NativeLibPathClassLoader(URL[] urls) { | |
super(urls, Thread.currentThread().getContextClassLoader()); | |
} | |
@Override | |
protected String findLibrary(String libname) { | |
// Implement your fancy library name resolving mechanism: | |
String parentResolvedPath = super.findLibrary(libname); | |
if(null==parentResolvedPath){ | |
final File nativeLibDir = new File(LIB_PATH); | |
final File libraryPath = new File(nativeLibDir, getCPUArchPath()); | |
final File library = new File(libraryPath,System.mapLibraryName(libname)); | |
if(library.exists()){ | |
return library.getAbsolutePath(); | |
} | |
} | |
return null; | |
} | |
private String getCPUArchPath() { | |
String arch = System.getProperty("os.arch"); | |
if(arch.contains("64")){ | |
return "amd64"; | |
} else{ | |
return "x86"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment