1 54 package org.oddjob.launch; 55 56 import java.net.MalformedURLException ; 57 58 import java.net.URL ; 59 import java.io.File ; 60 import java.io.FilenameFilter ; 61 import java.text.CharacterIterator ; 62 import java.text.StringCharacterIterator ; 63 64 71 public final class Locator { 72 75 private Locator() { 76 } 77 78 87 public static File getClassSource(Class c) { 88 String classResource = c.getName().replace('.', '/') + ".class"; 89 return getResourceSource(c.getClassLoader(), classResource); 90 } 91 92 103 public static File getResourceSource(ClassLoader c, String resource) { 104 if (c == null) { 105 c = Locator.class.getClassLoader(); 106 } 107 108 URL url = c.getResource(resource); 109 if (url != null) { 110 String u = url.toString(); 111 if (u.startsWith("jar:file:")) { 112 int pling = u.indexOf("!"); 113 String jarName = u.substring(4, pling); 114 return new File (fromURI(jarName)); 115 } else if (u.startsWith("file:")) { 116 int tail = u.indexOf(resource); 117 String dirName = u.substring(0, tail); 118 return new File (fromURI(dirName)); 119 } 120 } 121 return null; 122 } 123 124 136 public static String fromURI(String uri) { 137 if (!uri.startsWith("file:")) { 138 throw new IllegalArgumentException ("Can only handle file: URIs"); 139 } 140 if (uri.startsWith("file://")) { 141 uri = uri.substring(7); 142 } else { 143 uri = uri.substring(5); 144 } 145 146 uri = uri.replace('/', File.separatorChar); 147 if (File.pathSeparatorChar == ';' && uri.startsWith("\\") && uri.length() > 2 148 && Character.isLetter(uri.charAt(1)) && uri.lastIndexOf(':') > -1) { 149 uri = uri.substring(1); 150 } 151 152 StringBuffer sb = new StringBuffer (); 153 CharacterIterator iter = new StringCharacterIterator (uri); 154 for (char c = iter.first(); c != CharacterIterator.DONE; 155 c = iter.next()) { 156 if (c == '%') { 157 char c1 = iter.next(); 158 if (c1 != CharacterIterator.DONE) { 159 int i1 = Character.digit(c1, 16); 160 char c2 = iter.next(); 161 if (c2 != CharacterIterator.DONE) { 162 int i2 = Character.digit(c2, 16); 163 sb.append((char) ((i1 << 4) + i2)); 164 } 165 } 166 } else { 167 sb.append(c); 168 } 169 } 170 171 String path = sb.toString(); 172 return path; 173 } 174 175 176 184 public static File getToolsJar() { 185 boolean toolsJarAvailable = false; 187 188 try { 189 Class.forName("com.sun.tools.javac.Main"); 191 toolsJarAvailable = true; 192 } catch (Exception e) { 193 try { 194 Class.forName("sun.tools.javac.Main"); 195 toolsJarAvailable = true; 196 } catch (Exception e2) { 197 } 199 } 200 201 if (toolsJarAvailable) { 202 return null; 203 } 204 205 String javaHome = System.getProperty("java.home"); 208 if (javaHome.endsWith("jre")) { 209 javaHome = javaHome.substring(0, javaHome.length() - 4); 210 } 211 File toolsJar = new File (javaHome + "/lib/tools.jar"); 212 if (!toolsJar.exists()) { 213 System.out.println("Unable to locate tools.jar. " 214 + "Expected to find it in " + toolsJar.getPath()); 215 return null; 216 } 217 return toolsJar; 218 } 219 220 233 public static URL [] getLocationURLs(File location) 234 throws MalformedURLException { 235 return getLocationURLs(location, new String []{".jar"}); 236 } 237 238 252 public static URL [] getLocationURLs(File location, 253 final String [] extensions) 254 throws MalformedURLException { 255 URL [] urls = new URL [0]; 256 257 if (!location.exists()) { 258 return urls; 259 } 260 261 if (!location.isDirectory()) { 262 urls = new URL [1]; 263 String path = location.getPath(); 264 for (int i = 0; i < extensions.length; ++i) { 265 if (path.toLowerCase().endsWith(extensions[i])) { 266 urls[0] = location.toURL(); 267 break; 268 } 269 } 270 return urls; 271 } 272 273 File [] matches = location.listFiles( 274 new FilenameFilter () { 275 public boolean accept(File dir, String name) { 276 for (int i = 0; i < extensions.length; ++i) { 277 if (name.toLowerCase().endsWith(extensions[i])) { 278 return true; 279 } 280 } 281 return false; 282 } 283 }); 284 285 urls = new URL [matches.length]; 286 for (int i = 0; i < matches.length; ++i) { 287 urls[i] = matches[i].toURL(); 288 } 289 return urls; 290 } 291 } 292 293 | Popular Tags |