KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > facelets > util > Classpath


1 /**
2  * Licensed under the Common Development and Distribution License,
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.sun.com/cddl/
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */

14
15 package com.sun.facelets.util;
16
17 import java.io.File JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.net.JarURLConnection JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.net.URLConnection JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.jar.JarEntry JavaDoc;
26 import java.util.jar.JarFile JavaDoc;
27
28 /**
29  * @author Jacob Hookom
30  * @author Roland Huss
31  * @version $Id: Classpath.java,v 1.5 2006/01/14 06:46:14 jhook Exp $
32  */

33 public final class Classpath {
34
35     /**
36      *
37      */

38     public Classpath() {
39         super();
40     }
41
42     public static URL JavaDoc[] search(String JavaDoc prefix, String JavaDoc suffix) throws IOException JavaDoc {
43         return search(Thread.currentThread().getContextClassLoader(), prefix,
44                 suffix);
45     }
46
47     public static URL JavaDoc[] search(ClassLoader JavaDoc cl, String JavaDoc prefix, String JavaDoc suffix)
48             throws IOException JavaDoc {
49         Enumeration JavaDoc e = cl.getResources(prefix);
50         Set JavaDoc all = new HashSet JavaDoc();
51         URL JavaDoc url;
52         URLConnection JavaDoc conn;
53         JarFile JavaDoc jarFile;
54         while (e.hasMoreElements()) {
55             url = (URL JavaDoc) e.nextElement();
56             conn = url.openConnection();
57             conn.setUseCaches(false);
58             conn.setDefaultUseCaches(false);
59             if (conn instanceof JarURLConnection JavaDoc) {
60                 jarFile = ((JarURLConnection JavaDoc) 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 JavaDoc(url.getFile()), suffix);
68             }
69         }
70         URL JavaDoc[] urlArray = (URL JavaDoc[]) all.toArray(new URL JavaDoc[all.size()]);
71         return urlArray;
72     }
73
74     private static void searchDir(Set JavaDoc result, File JavaDoc file, String JavaDoc suffix)
75             throws IOException JavaDoc {
76         if (file.exists() && file.isDirectory()) {
77             File JavaDoc[] fc = file.listFiles();
78             String JavaDoc path;
79             URL JavaDoc 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(new URL("file:/" + path));
86
result.add(fc[i].toURL());
87                 }
88             }
89         }
90     }
91
92     /** For URLs to JARs that do not use JarURLConnection - allowed by
93      * the servlet spec - attempt to produce a JarFile object all the same.
94      * Known servlet engines that function like this include Weblogic
95      * and OC4J.
96      * This is not a full solution, since an unpacked WAR or EAR will not
97      * have JAR "files" as such.
98      */

99     private static JarFile JavaDoc getAlternativeJarFile(URL JavaDoc url) throws IOException JavaDoc {
100         String JavaDoc urlFile = url.getFile();
101         // Trim off any suffix - which is prefixed by "!/" on Weblogic
102
int separatorIndex = urlFile.indexOf("!/");
103
104         // OK, didn't find that. Try the less safe "!", used on OC4J
105
if (separatorIndex == -1) {
106           separatorIndex = urlFile.indexOf('!');
107         }
108
109         if (separatorIndex != -1) {
110             String JavaDoc jarFileUrl = urlFile.substring(0, separatorIndex);
111             // And trim off any "file:" prefix.
112
if (jarFileUrl.startsWith("file:")) {
113                 jarFileUrl = jarFileUrl.substring("file:".length());
114             }
115             return new JarFile JavaDoc(jarFileUrl);
116         }
117         return null;
118     }
119
120     private static void searchJar(ClassLoader JavaDoc cl, Set JavaDoc result, JarFile JavaDoc file,
121             String JavaDoc prefix, String JavaDoc suffix) throws IOException JavaDoc {
122         Enumeration JavaDoc e = file.entries();
123         JarEntry JavaDoc entry;
124         String JavaDoc name;
125         while (e.hasMoreElements()) {
126             try {
127                 entry = (JarEntry JavaDoc) e.nextElement();
128             } catch (Throwable JavaDoc t) {
129                 continue;
130             }
131             name = entry.getName();
132             if (name.startsWith(prefix) && name.endsWith(suffix)) {
133                 Enumeration JavaDoc e2 = cl.getResources(name);
134                 while (e2.hasMoreElements()) {
135                     result.add(e2.nextElement());
136                 }
137             }
138         }
139     }
140
141 }
142
Popular Tags