1 33 34 package edu.rice.cs.util.classloader; 35 36 import java.net.*; 37 import java.io.File ; 38 import java.io.FileFilter ; 39 import java.util.ArrayList ; 40 import java.util.Arrays ; 41 import java.util.LinkedHashSet ; 42 43 import edu.rice.cs.plt.io.IOUtil; 44 import edu.rice.cs.util.FileOps; 45 import edu.rice.cs.util.swing.Utilities; 46 47 50 public class ToolsJarClassLoader extends URLClassLoader { 51 52 53 54 55 public ToolsJarClassLoader(File toolsJar) { super(getToolsJarURLs(toolsJar)); } 56 public ToolsJarClassLoader() { this(FileOps.NONEXISTENT_FILE); } 57 58 59 public static File [] getToolsJarFiles(File toolsJar) { 60 File javaHome = IOUtil.attemptCanonicalFile(new File (System.getProperty("java.home"))); 61 62 68 LinkedHashSet <File > javaHomeParents = new LinkedHashSet <File >(); 69 javaHomeParents.add(IOUtil.attemptCanonicalFile(new File (javaHome, ".."))); 70 javaHomeParents.add(IOUtil.attemptCanonicalFile(new File (javaHome, "../.."))); 71 72 final String jv = System.getProperty("java.version"); 73 if (!jv.startsWith("1.3") && !jv.startsWith("1.4")) { 74 String winPrograms = System.getenv("ProgramFiles"); 77 if (winPrograms != null) { 78 javaHomeParents.add(IOUtil.attemptCanonicalFile(new File (winPrograms, "Java"))); 79 javaHomeParents.add(IOUtil.attemptCanonicalFile(new File (winPrograms))); 80 } 81 else { javaHomeParents.add(IOUtil.attemptCanonicalFile(new File ("/C:/Program Files/Java/"))); 83 javaHomeParents.add(IOUtil.attemptCanonicalFile(new File ("/C:/Program Files/"))); 84 } 85 86 String winSystem = System.getenv("SystemDrive"); 87 if (winSystem != null) { 88 javaHomeParents.add(IOUtil.attemptCanonicalFile(new File (winSystem, "Java"))); 89 javaHomeParents.add(IOUtil.attemptCanonicalFile(new File (winSystem))); 90 } 91 else { javaHomeParents.add(IOUtil.attemptCanonicalFile(new File ("/C:/Java/"))); 93 javaHomeParents.add(IOUtil.attemptCanonicalFile(new File ("/C:/"))); 94 } 95 } 96 97 javaHomeParents.add(IOUtil.attemptCanonicalFile(new File ("/usr/"))); 98 javaHomeParents.add(IOUtil.attemptCanonicalFile(new File ("/usr/java/"))); 99 javaHomeParents.add(IOUtil.attemptCanonicalFile(new File ("/usr/j2se/"))); 100 javaHomeParents.add(IOUtil.attemptCanonicalFile(new File ("/usr/local/"))); 101 javaHomeParents.add(IOUtil.attemptCanonicalFile(new File ("/usr/local/java/"))); 102 javaHomeParents.add(IOUtil.attemptCanonicalFile(new File ("/usr/local/j2se/"))); 103 104 105 106 LinkedHashSet <File > javaHomes = new LinkedHashSet <File >(); 107 108 try { if (javaHome.isDirectory()) { javaHomes.add(javaHome); } } 109 catch (SecurityException e) { } 110 111 String version = System.getProperty("java.specification.version"); 112 final String prefix1 = "j2sdk" + version; 113 final String prefix2 = "jdk" + version; 114 FileFilter matchHomes = new FileFilter () { 115 public boolean accept(File f) { 116 return f.isDirectory() && (f.getName().startsWith(prefix1) || f.getName().startsWith(prefix2)); 117 } 118 }; 119 for (File parent : javaHomeParents) { 120 try { 121 File [] files = parent.listFiles(matchHomes); 122 if (files != null) { for (File f : files) javaHomes.add(f); } 123 } 124 catch (SecurityException e) { } 125 } 126 127 128 LinkedHashSet <File > result = new LinkedHashSet <File >(); 129 130 try { if (toolsJar.isFile()) result.add(IOUtil.attemptCanonicalFile(toolsJar)); } 131 catch (SecurityException e) { } 132 133 for (File home : javaHomes) { 134 try { 135 File tools = new File (home, "lib/tools.jar"); 136 if (tools.isFile()) { result.add(IOUtil.attemptCanonicalFile(tools)); } 137 } 138 catch (SecurityException e) { } 139 } 140 141 return result.toArray(new File [0]); 142 } 143 144 145 public static URL[] getToolsJarURLs() { return getToolsJarURLs(FileOps.NONEXISTENT_FILE); } 146 147 148 public static URL[] getToolsJarURLs(File toolsJar) { 149 File [] files = getToolsJarFiles(toolsJar); 150 try { 151 URL[] urls = new URL[files.length]; 152 for (int i=0; i < files.length; i++) { 153 urls[i] = FileOps.toURL(files[i]); 154 } 155 return urls; 156 } 157 catch (MalformedURLException e) { 158 return new URL[0]; 159 } 160 } 161 162 163 public static String getToolsJarClassPath() { return getToolsJarClassPath(FileOps.NONEXISTENT_FILE); } 164 165 166 public static String getToolsJarClassPath(File toolsJar) { 167 File [] files = getToolsJarFiles(toolsJar); 168 final StringBuilder classPath = new StringBuilder (); 169 String pathSep = System.getProperty("path.separator"); 170 171 for (int i=0; i < files.length; i++) { 172 if (i > 0) classPath.append(pathSep); 173 classPath.append(files[i].getAbsolutePath()); 174 } 175 return classPath.toString(); 176 } 177 178 179 public URL getResource(String name) { return findResource(name); } 180 } 181 | Popular Tags |