1 17 package org.apache.geronimo.system.main; 18 19 import java.io.File ; 20 import java.lang.reflect.Field ; 21 import java.net.MalformedURLException ; 22 import java.net.URL ; 23 import java.net.URLClassLoader ; 24 import java.security.AccessController ; 25 import java.security.PrivilegedAction ; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import sun.misc.URLClassPath; 30 31 34 public class ToolsJarHack { 35 private static boolean installed = false; 36 private static Log log; 37 38 public static void install() { 39 if (installed) { 40 return; 41 } 42 43 if (log == null) { 44 log = LogFactory.getLog(ToolsJarHack.class); 45 } 46 47 ClassLoader myClassLoader = ToolsJarHack.class.getClassLoader(); 49 Class compilerClass = null; 50 try { 51 compilerClass = myClassLoader.loadClass("sun.tools.javac.Main"); 52 } catch (ClassNotFoundException ignored) { 53 } 54 if (compilerClass != null) { 55 installed = true; 56 return; 57 } 58 59 File toolsJarFile = findToolsJarFile(); 60 if (toolsJarFile == null) { 61 return; 63 } 64 65 URL toolsJarURL; 66 try { 67 toolsJarURL = toolsJarFile.toURL(); 68 } catch (MalformedURLException e) { 69 log.warn("Could not all find java compiler: tools.jar file not a regular file: " + toolsJarFile.getAbsolutePath(), e); 70 return; 71 } 72 addJarToPath(toolsJarURL); 73 installed = true; 74 } 75 76 private static File findToolsJarFile() { 77 String javaHome = System.getProperty("java.home"); 78 if (javaHome == null) { 79 return null; 80 } 81 82 File javaHomeDir = new File (javaHome); 83 if (!javaHomeDir.isDirectory()) { 84 return null; 85 } 86 87 File toolsJarFile = findToolsJarFile(javaHomeDir); 88 if (toolsJarFile != null) { 89 return toolsJarFile; 90 } 91 92 toolsJarFile = findToolsJarFile(javaHomeDir.getParentFile()); 93 if (toolsJarFile != null) { 94 return toolsJarFile; 95 } 96 97 log.warn("Could not all find java compiler: lib" + File.separator + "tools.jar file not found in " + 98 javaHomeDir.getAbsolutePath() + " or " + javaHomeDir.getParentFile().getAbsolutePath()); 99 return null; 100 } 101 102 private static File findToolsJarFile(File javaHomeDir) { 103 File toolsJarFile; 104 toolsJarFile = new File (javaHomeDir, "lib" + File.separator + "tools.jar"); 105 if (!toolsJarFile.exists()) { 106 return null; 107 } 108 if (!toolsJarFile.isFile()) { 109 return null; 110 } 111 return toolsJarFile; 112 } 113 114 115 private static void addJarToPath(URL jar) { 116 URLClassLoader urlClassLoader = null; 118 try { 119 urlClassLoader = (URLClassLoader ) ClassLoader.getSystemClassLoader(); 120 } catch (Throwable e) { 121 log.warn("Could not install compiler: Could not obtain access to system class loader", e); 122 return; 123 } 124 125 URLClassPath urlClassPath = getURLClassPath(urlClassLoader); 126 if (urlClassPath == null) { 127 return; 129 } 130 urlClassPath.addURL(jar); 131 132 rebuildJavaClassPathVariable(urlClassPath); 133 } 134 135 private static URLClassPath getURLClassPath(URLClassLoader loader) { 136 Field ucpField = (Field ) AccessController.doPrivileged(new PrivilegedAction () { 137 public Object run() { 138 Field ucp = null; 139 try { 140 ucp = URLClassLoader .class.getDeclaredField("ucp"); 141 ucp.setAccessible(true); 142 } catch (Exception e) { 143 log.warn("Could not install compiler: Could not obtain access to ucp field of the URLClassLoader", e); 144 } 145 return ucp; 146 } 147 }); 148 149 if (ucpField == null) { 150 return null; 151 } 152 try { 153 return (URLClassPath) ucpField.get(loader); 154 } catch (IllegalAccessException e) { 155 log.warn("Could not install compiler: Could not obtain access to ucp field of the URLClassLoader", e); 156 return null; 157 } 158 } 159 160 private static void rebuildJavaClassPathVariable(URLClassPath urlClassPath) { 161 URL [] urls = urlClassPath.getURLs(); 162 if (urls.length < 1) { 163 return; 164 } 165 166 StringBuffer path = new StringBuffer (urls.length * 32); 167 168 for (int i = 0; i < urls.length; i++) { 169 if (i != 0) { 170 path.append(File.pathSeparator); 171 } 172 173 path.append(new File (urls[i].getFile()).getPath()); 174 } 175 try { 176 System.setProperty("java.class.path", path.toString()); 177 } catch (Exception e) { 178 log.warn("Error installing compiler: Could not update java.class.path property which may cause compiler to not work correctly", e); 179 } 180 } 181 } 182 | Popular Tags |