1 25 package org.snipsnap.server; 26 27 import java.io.BufferedInputStream ; 28 import java.io.File ; 29 import java.io.FileOutputStream ; 30 import java.io.IOException ; 31 import java.io.InputStream ; 32 import java.lang.reflect.InvocationTargetException ; 33 import java.lang.reflect.Method ; 34 import java.net.MalformedURLException ; 35 import java.net.URL ; 36 import java.net.URLClassLoader ; 37 import java.security.Policy ; 38 import java.util.ArrayList ; 39 import java.util.List ; 40 import java.util.StringTokenizer ; 41 import java.util.jar.Attributes ; 42 import java.util.jar.JarInputStream ; 43 import java.util.jar.Manifest ; 44 45 51 public class Launcher { 52 public final static String CLASSPATH = "launcher.classpath"; 53 54 private final static URL location = Launcher.class.getProtectionDomain().getCodeSource().getLocation(); 55 56 public static void invokeMain(String mainClassName, final String args[]) 57 throws ClassNotFoundException , NoSuchMethodException , IllegalAccessException , InvocationTargetException { 58 ClassLoader parentClassLoader = Thread.currentThread().getContextClassLoader(); 59 if (null == parentClassLoader) { 60 parentClassLoader = Launcher.class.getClassLoader(); 61 } 62 if (null == parentClassLoader) { 63 parentClassLoader = ClassLoader.getSystemClassLoader(); 64 } 65 URLClassLoader classLoader = new URLClassLoader (initClassPath(System.getProperty(CLASSPATH)), 66 parentClassLoader); 67 Thread.currentThread().setContextClassLoader(classLoader); 68 if (System.getSecurityManager() != null) { 69 System.err.println("Launcher: uninstalling security manager ..."); 70 System.setSecurityManager(null); 71 } 72 73 try { 74 Policy.getPolicy().refresh(); 75 } catch (Exception e) { 76 e.printStackTrace(); 77 } 78 79 Class mainClass = classLoader.loadClass(mainClassName); 81 final Method main = mainClass.getDeclaredMethod("main", new Class []{String [].class}); 82 main.invoke(null, new Object []{args}); 83 } 84 85 protected static URL [] initClassPath(String extraClassPath) { 86 List urlArray = new ArrayList (); 87 try { 88 Manifest launcherManifest = new JarInputStream (location.openStream()).getManifest(); 89 Attributes launcherAttribs = launcherManifest.getMainAttributes(); 90 String mainJarAttr = launcherAttribs.getValue("Launcher-Main-Jar"); 91 if (System.getProperty("launcher.main.jar") != null) { 92 mainJarAttr = System.getProperty("launcher.main.jar"); 93 } 94 URL mainJarUrl = getResourceUrl(mainJarAttr); 95 Manifest mainManifest = new JarInputStream (mainJarUrl.openStream()).getManifest(); 96 Attributes mainAttributes = mainManifest.getMainAttributes(); 97 String manifestClassPath = mainAttributes.getValue("Class-Path"); 98 99 urlArray.add(mainJarUrl); 100 if (extraClassPath != null && extraClassPath.length() > 0) { 102 manifestClassPath += " " + extraClassPath.replace(File.pathSeparatorChar, ' '); 103 } 104 105 StringBuffer classPath = new StringBuffer (location.getFile()); 106 StringTokenizer tokenizer = new StringTokenizer (manifestClassPath, " \t" + File.pathSeparatorChar, false); 107 while (tokenizer.hasMoreTokens()) { 108 classPath.append(File.pathSeparatorChar); 109 URL classPathEntry = getResourceUrl(tokenizer.nextToken()); 110 urlArray.add(classPathEntry); 111 classPath.append(classPathEntry.getFile()); 112 } 113 System.setProperty("java.class.path", classPath.toString()); 114 } catch (IOException e) { 115 System.err.println("Error: Set the system property launcher.main.jar to specify the jar file to start."); 116 } 117 return (URL []) urlArray.toArray(new URL [0]); 118 } 119 120 private static URL getResourceUrl(String resource) throws IOException { 121 File directoryBase = new File (location.getFile()).getParentFile(); 122 File file = new File (resource); 123 if (file.isAbsolute() && file.exists()) { 125 return file.toURL(); 126 } 127 file = new File (directoryBase, resource); 129 if (file.exists()) { 130 return file.toURL(); 131 } 132 133 URL resourceURL = Launcher.class.getResource("/" + resource); 134 if (null != resourceURL) { 135 return extract(resourceURL); 136 } 137 138 throw new MalformedURLException ("missing resource: " + resource); 139 } 140 141 148 private static URL extract(URL resource) throws IOException { 149 System.err.println("Launcher: extracting '" + resource.getFile() + "' ..."); 150 File f = File.createTempFile("launcher_", ".jar"); 151 f.deleteOnExit(); 152 if (f.getParentFile() != null) { 153 f.getParentFile().mkdirs(); 154 } 155 InputStream is = new BufferedInputStream (resource.openStream()); 156 FileOutputStream os = new FileOutputStream (f); 157 byte[] arr = new byte[8192]; 158 for (int i = 0; i >= 0; i = is.read(arr)) { 159 os.write(arr, 0, i); 160 } 161 is.close(); 162 os.close(); 163 return f.toURL(); 164 } 165 } | Popular Tags |