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