1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.shale.tiger.view.faces; |
18 |
|
|
19 |
|
import org.apache.commons.logging.Log; |
20 |
|
import org.apache.commons.logging.LogFactory; |
21 |
|
|
22 |
|
import java.io.File; |
23 |
|
import java.io.FileFilter; |
24 |
|
import java.io.IOException; |
25 |
|
import java.net.JarURLConnection; |
26 |
|
import java.net.URISyntaxException; |
27 |
|
import java.net.URL; |
28 |
|
import java.net.URLConnection; |
29 |
|
import java.util.Enumeration; |
30 |
|
import java.util.List; |
31 |
|
import java.util.jar.JarEntry; |
32 |
|
import java.util.jar.JarFile; |
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
0 |
public final class PackageInfo { |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
0 |
private transient Log log = null; |
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
0 |
private final static PackageInfo INSTANCE = new PackageInfo(); |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
public final static PackageInfo getInstance() { |
56 |
|
|
57 |
0 |
return INSTANCE; |
58 |
|
|
59 |
|
} |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
public Class[] getClasses(final List<Class> classes, final String pckgname) |
72 |
|
throws ClassNotFoundException { |
73 |
|
|
74 |
|
Enumeration resources; |
75 |
|
ClassLoader cld; |
76 |
|
String path; |
77 |
|
try { |
78 |
|
|
79 |
|
|
80 |
0 |
path = pckgname.replace('.', '/'); |
81 |
|
|
82 |
0 |
cld = Thread.currentThread().getContextClassLoader(); |
83 |
0 |
if (cld == null) { |
84 |
0 |
throw new ClassNotFoundException("Can't get class loader."); |
85 |
|
} |
86 |
|
|
87 |
|
|
88 |
0 |
resources = cld.getResources(path); |
89 |
0 |
if (resources == null || !resources.hasMoreElements()) { |
90 |
0 |
throw new ClassNotFoundException("No resource for " + path); |
91 |
|
} |
92 |
|
|
93 |
0 |
} catch (NullPointerException e) { |
94 |
0 |
throw(ClassNotFoundException) new ClassNotFoundException(pckgname + " (" + pckgname |
95 |
|
+ ") does not appear to be a valid package", e); |
96 |
0 |
} catch (IOException e) { |
97 |
0 |
throw(ClassNotFoundException) new ClassNotFoundException(pckgname + " (" + pckgname |
98 |
|
+ ") does not appear to be a valid package", e); |
99 |
0 |
} |
100 |
|
|
101 |
|
|
102 |
0 |
while (resources.hasMoreElements()) { |
103 |
0 |
URL resource = (URL) resources.nextElement(); |
104 |
0 |
URLConnection connection = null; |
105 |
|
try { |
106 |
0 |
connection = resource.openConnection(); |
107 |
0 |
} catch (IOException e) { |
108 |
0 |
throw(ClassNotFoundException) new ClassNotFoundException(pckgname + " (" + pckgname |
109 |
|
+ ") does not appear to be a valid package", e); |
110 |
0 |
} |
111 |
|
|
112 |
0 |
if (connection instanceof JarURLConnection) { |
113 |
|
|
114 |
0 |
JarURLConnection juc = (JarURLConnection) connection; |
115 |
0 |
JarFile jarFile = null; |
116 |
|
try { |
117 |
0 |
jarFile = juc.getJarFile(); |
118 |
0 |
} catch (IOException e) { |
119 |
0 |
throw(ClassNotFoundException) new ClassNotFoundException(pckgname + " (" + pckgname |
120 |
|
+ ") does not appear to be a valid package", e); |
121 |
0 |
} |
122 |
0 |
Enumeration<JarEntry> entries = jarFile.entries(); |
123 |
0 |
while (entries.hasMoreElements()) { |
124 |
0 |
JarEntry jarEntry = entries.nextElement(); |
125 |
0 |
String entryName = jarEntry.getName(); |
126 |
0 |
if (!entryName.startsWith(path)) { |
127 |
0 |
continue; |
128 |
|
} |
129 |
0 |
if (!entryName.toLowerCase().endsWith(".class")) { |
130 |
0 |
continue; |
131 |
|
} |
132 |
0 |
String className = filenameToClassname(entryName); |
133 |
0 |
loadClass(classes, cld, className); |
134 |
0 |
} |
135 |
0 |
} else { |
136 |
|
|
137 |
|
File file; |
138 |
|
try { |
139 |
0 |
file = new File(connection.getURL().toURI()); |
140 |
0 |
} catch (URISyntaxException e) { |
141 |
0 |
log().warn("error loading directory " + connection, e); |
142 |
0 |
continue; |
143 |
0 |
} |
144 |
|
|
145 |
0 |
listFilesRecursive(classes, file, cld, pckgname); |
146 |
|
} |
147 |
0 |
} |
148 |
|
|
149 |
0 |
if (classes.size() < 1) { |
150 |
0 |
throw new ClassNotFoundException(pckgname |
151 |
|
+ " does not appear to be a valid package"); |
152 |
|
} |
153 |
|
|
154 |
0 |
Class[] resolvedClasses = new Class[classes.size()]; |
155 |
0 |
classes.toArray(resolvedClasses); |
156 |
0 |
return resolvedClasses; |
157 |
|
|
158 |
|
} |
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
protected String filenameToClassname(String entryName) { |
167 |
|
|
168 |
0 |
return entryName.substring(0, entryName.length() - 6).replace('/', '.'); |
169 |
|
|
170 |
|
} |
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
protected void loadClass(List<Class> classes, ClassLoader cld, String className) { |
182 |
|
|
183 |
|
try { |
184 |
0 |
classes.add(cld.loadClass(className)); |
185 |
|
} |
186 |
0 |
catch (NoClassDefFoundError e) { |
187 |
0 |
log().warn("error loading class " + className, e); |
188 |
0 |
} catch (ClassNotFoundException e) { |
189 |
0 |
log().warn("error loading class " + className, e); |
190 |
0 |
} |
191 |
|
|
192 |
0 |
} |
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
|
204 |
|
protected void listFilesRecursive(final List<Class> classes, final File base, |
205 |
|
final ClassLoader cld, final String pckgname) { |
206 |
|
|
207 |
0 |
base.listFiles(new FileFilter() { |
208 |
|
|
209 |
0 |
public boolean accept(File file) { |
210 |
0 |
if (file.isDirectory()) { |
211 |
0 |
listFilesRecursive(classes, file, cld, pckgname + "." + file.getName()); |
212 |
0 |
return false; |
213 |
|
} |
214 |
0 |
if (!file.getName().toLowerCase().endsWith(".class")) { |
215 |
0 |
return false; |
216 |
|
} |
217 |
|
|
218 |
0 |
String className = filenameToClassname(pckgname + "." + file.getName()); |
219 |
0 |
loadClass(classes, cld, className); |
220 |
|
|
221 |
0 |
return false; |
222 |
|
} |
223 |
|
|
224 |
|
}); |
225 |
|
|
226 |
0 |
} |
227 |
|
|
228 |
|
|
229 |
|
|
230 |
|
|
231 |
|
|
232 |
|
|
233 |
|
private Log log() { |
234 |
|
|
235 |
0 |
if (log == null) { |
236 |
0 |
log = LogFactory.getLog(PackageInfo.class); |
237 |
|
} |
238 |
0 |
return log; |
239 |
|
|
240 |
|
} |
241 |
|
|
242 |
|
|
243 |
|
} |