1 14 15 package com.sun.facelets.util; 16 17 import java.io.File ; 18 import java.io.IOException ; 19 import java.net.JarURLConnection ; 20 import java.net.URL ; 21 import java.net.URLConnection ; 22 import java.util.Enumeration ; 23 import java.util.HashSet ; 24 import java.util.Set ; 25 import java.util.jar.JarEntry ; 26 import java.util.jar.JarFile ; 27 28 33 public final class Classpath { 34 35 38 public Classpath() { 39 super(); 40 } 41 42 public static URL [] search(String prefix, String suffix) throws IOException { 43 return search(Thread.currentThread().getContextClassLoader(), prefix, 44 suffix); 45 } 46 47 public static URL [] search(ClassLoader cl, String prefix, String suffix) 48 throws IOException { 49 Enumeration e = cl.getResources(prefix); 50 Set all = new HashSet (); 51 URL url; 52 URLConnection conn; 53 JarFile jarFile; 54 while (e.hasMoreElements()) { 55 url = (URL ) e.nextElement(); 56 conn = url.openConnection(); 57 conn.setUseCaches(false); 58 conn.setDefaultUseCaches(false); 59 if (conn instanceof JarURLConnection ) { 60 jarFile = ((JarURLConnection ) conn).getJarFile(); 61 } else { 62 jarFile = getAlternativeJarFile(url); 63 } 64 if (jarFile != null) { 65 searchJar(cl, all, jarFile, prefix, suffix); 66 } else { 67 searchDir(all, new File (url.getFile()), suffix); 68 } 69 } 70 URL [] urlArray = (URL []) all.toArray(new URL [all.size()]); 71 return urlArray; 72 } 73 74 private static void searchDir(Set result, File file, String suffix) 75 throws IOException { 76 if (file.exists() && file.isDirectory()) { 77 File [] fc = file.listFiles(); 78 String path; 79 URL src; 80 for (int i = 0; i < fc.length; i++) { 81 path = fc[i].getAbsolutePath(); 82 if (fc[i].isDirectory()) { 83 searchDir(result, fc[i], suffix); 84 } else if (path.endsWith(suffix)) { 85 result.add(fc[i].toURL()); 87 } 88 } 89 } 90 } 91 92 99 private static JarFile getAlternativeJarFile(URL url) throws IOException { 100 String urlFile = url.getFile(); 101 int separatorIndex = urlFile.indexOf("!/"); 103 104 if (separatorIndex == -1) { 106 separatorIndex = urlFile.indexOf('!'); 107 } 108 109 if (separatorIndex != -1) { 110 String jarFileUrl = urlFile.substring(0, separatorIndex); 111 if (jarFileUrl.startsWith("file:")) { 113 jarFileUrl = jarFileUrl.substring("file:".length()); 114 } 115 return new JarFile (jarFileUrl); 116 } 117 return null; 118 } 119 120 private static void searchJar(ClassLoader cl, Set result, JarFile file, 121 String prefix, String suffix) throws IOException { 122 Enumeration e = file.entries(); 123 JarEntry entry; 124 String name; 125 while (e.hasMoreElements()) { 126 try { 127 entry = (JarEntry ) e.nextElement(); 128 } catch (Throwable t) { 129 continue; 130 } 131 name = entry.getName(); 132 if (name.startsWith(prefix) && name.endsWith(suffix)) { 133 Enumeration e2 = cl.getResources(name); 134 while (e2.hasMoreElements()) { 135 result.add(e2.nextElement()); 136 } 137 } 138 } 139 } 140 141 } 142 | Popular Tags |