1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.classloader; |
21 |
|
|
22 |
|
import java.io.File; |
23 |
|
import java.io.IOException; |
24 |
|
import java.net.MalformedURLException; |
25 |
|
import java.net.URI; |
26 |
|
import java.net.URL; |
27 |
|
import java.net.URLStreamHandlerFactory; |
28 |
|
import java.security.AccessControlContext; |
29 |
|
import java.security.AccessController; |
30 |
|
import java.security.CodeSource; |
31 |
|
import java.security.PrivilegedAction; |
32 |
|
import java.security.PrivilegedExceptionAction; |
33 |
|
import java.util.Enumeration; |
34 |
|
import java.util.List; |
35 |
|
import java.util.jar.Attributes; |
36 |
|
import java.util.jar.Attributes.Name; |
37 |
|
import java.util.jar.Manifest; |
38 |
|
|
39 |
|
import org.xwiki.classloader.internal.ResourceLoader; |
40 |
|
|
41 |
|
import edu.emory.mathcs.util.classloader.ResourceFinder; |
42 |
|
import edu.emory.mathcs.util.classloader.ResourceHandle; |
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
@see |
70 |
|
@see |
71 |
|
@version |
72 |
|
@since |
73 |
|
|
|
|
| 55% |
Uncovered Elements: 58 (129) |
Complexity: 38 |
Complexity Density: 0.47 |
|
74 |
|
public class URIClassLoader extends ExtendedURLClassLoader |
75 |
|
{ |
76 |
|
final URIResourceFinder finder; |
77 |
|
|
78 |
|
final AccessControlContext acc; |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
@param |
84 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
85 |
0 |
public URIClassLoader(URI[] uris)... |
86 |
|
{ |
87 |
0 |
this(uris, (URLStreamHandlerFactory) null); |
88 |
|
} |
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
@param |
94 |
|
@param |
95 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
96 |
1 |
public URIClassLoader(URI[] uris, URLStreamHandlerFactory handlerFactory)... |
97 |
|
{ |
98 |
1 |
super(new URL[0]); |
99 |
1 |
this.finder = new URIResourceFinder(uris, handlerFactory); |
100 |
1 |
this.acc = AccessController.getContext(); |
101 |
|
} |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
@param |
107 |
|
@param |
108 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
109 |
13867 |
public URIClassLoader(URI[] uris, ClassLoader parent)... |
110 |
|
{ |
111 |
13864 |
this(uris, parent, null); |
112 |
|
} |
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
@param |
118 |
|
@param |
119 |
|
@param |
120 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
121 |
20152 |
public URIClassLoader(URI[] uris, ClassLoader parent, URLStreamHandlerFactory handlerFactory)... |
122 |
|
{ |
123 |
20151 |
super(new URL[0], parent); |
124 |
20147 |
this.finder = new URIResourceFinder(uris, handlerFactory); |
125 |
20107 |
this.acc = AccessController.getContext(); |
126 |
|
} |
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
@param |
132 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
133 |
0 |
protected void addURI(URI uri)... |
134 |
|
{ |
135 |
0 |
this.finder.addURI(uri); |
136 |
|
} |
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
|
141 |
|
@param |
142 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
143 |
137 |
@Override... |
144 |
|
public void addURL(URL url) |
145 |
|
{ |
146 |
137 |
this.finder.addURI(URI.create(url.toExternalForm())); |
147 |
|
} |
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
@param |
153 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
154 |
0 |
@Override... |
155 |
|
public void addURLs(List<URL> urls) |
156 |
|
{ |
157 |
0 |
for (URL url : urls) { |
158 |
0 |
addURL(url); |
159 |
|
} |
160 |
|
} |
161 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
162 |
8 |
@Override... |
163 |
|
public URL[] getURLs() |
164 |
|
{ |
165 |
8 |
return this.finder.getUrls().clone(); |
166 |
|
} |
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
@param |
172 |
|
@return |
173 |
|
@exception |
174 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
175 |
45770 |
@Override... |
176 |
|
protected Class<?> findClass(final String name) throws ClassNotFoundException |
177 |
|
{ |
178 |
45770 |
try { |
179 |
45770 |
return AccessController.doPrivileged(new PrivilegedExceptionAction<Class<?>>() |
180 |
|
{ |
|
|
| 88.9% |
Uncovered Elements: 1 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
181 |
45770 |
@Override... |
182 |
|
public Class<?> run() throws ClassNotFoundException |
183 |
|
{ |
184 |
45770 |
String path = name.replace('.', '/').concat(".class"); |
185 |
45770 |
ResourceHandle h = URIClassLoader.this.finder.getResource(path); |
186 |
45770 |
if (h != null) { |
187 |
338 |
try { |
188 |
338 |
return defineClass(name, h); |
189 |
|
} catch (IOException e) { |
190 |
0 |
throw new ClassNotFoundException(name, e); |
191 |
|
} |
192 |
|
} else { |
193 |
45432 |
throw new ClassNotFoundException(name); |
194 |
|
} |
195 |
|
} |
196 |
|
}, this.acc); |
197 |
|
} catch (java.security.PrivilegedActionException pae) { |
198 |
45432 |
throw (ClassNotFoundException) pae.getException(); |
199 |
|
} |
200 |
|
} |
201 |
|
|
|
|
| 80% |
Uncovered Elements: 6 (30) |
Complexity: 6 |
Complexity Density: 0.3 |
|
202 |
338 |
protected Class<?> defineClass(String name, ResourceHandle h) throws IOException... |
203 |
|
{ |
204 |
338 |
int i = name.lastIndexOf('.'); |
205 |
338 |
URL url = h.getCodeSourceURL(); |
206 |
338 |
if (i != -1) { |
207 |
336 |
String pkgname = name.substring(0, i); |
208 |
|
|
209 |
336 |
Package pkg = getPackage(pkgname); |
210 |
336 |
Manifest man = h.getManifest(); |
211 |
336 |
if (pkg != null) { |
212 |
|
|
213 |
295 |
boolean ok; |
214 |
295 |
if (pkg.isSealed()) { |
215 |
|
|
216 |
0 |
ok = pkg.isSealed(url); |
217 |
|
} else { |
218 |
|
|
219 |
|
|
220 |
295 |
ok = (man == null) || !isSealed(pkgname, man); |
221 |
|
} |
222 |
295 |
if (!ok) { |
223 |
0 |
throw new SecurityException("sealing violation: " + name); |
224 |
|
} |
225 |
|
} else { |
226 |
41 |
if (man != null) { |
227 |
41 |
definePackage(pkgname, man, url); |
228 |
|
} else { |
229 |
0 |
definePackage(pkgname, null, null, null, null, null, null, null); |
230 |
|
} |
231 |
|
} |
232 |
|
} |
233 |
|
|
234 |
|
|
235 |
338 |
byte[] b = h.getBytes(); |
236 |
338 |
java.security.cert.Certificate[] certs = h.getCertificates(); |
237 |
338 |
CodeSource cs = new CodeSource(url, certs); |
238 |
338 |
return defineClass(name, b, 0, b.length, cs); |
239 |
|
} |
240 |
|
|
241 |
|
|
242 |
|
|
243 |
|
|
|
|
| 66.7% |
Uncovered Elements: 5 (15) |
Complexity: 4 |
Complexity Density: 0.44 |
|
244 |
295 |
private boolean isSealed(String name, Manifest man)... |
245 |
|
{ |
246 |
295 |
String path = name.replace('.', '/').concat("/"); |
247 |
295 |
Attributes attr = man.getAttributes(path); |
248 |
295 |
String sealed = null; |
249 |
295 |
if (attr != null) { |
250 |
0 |
sealed = attr.getValue(Name.SEALED); |
251 |
|
} |
252 |
295 |
if (sealed == null) { |
253 |
? |
if ((attr = man.getMainAttributes()) != null) { |
254 |
295 |
sealed = attr.getValue(Name.SEALED); |
255 |
|
} |
256 |
|
} |
257 |
295 |
return "true".equalsIgnoreCase(sealed); |
258 |
|
} |
259 |
|
|
260 |
|
|
261 |
|
|
262 |
|
|
263 |
|
@param |
264 |
|
@return |
265 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
266 |
9006 |
@Override... |
267 |
|
public URL findResource(final String name) |
268 |
|
{ |
269 |
9006 |
return AccessController.doPrivileged(new PrivilegedAction<URL>() |
270 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
271 |
9006 |
@Override... |
272 |
|
public URL run() |
273 |
|
{ |
274 |
9006 |
return URIClassLoader.this.finder.findResource(name); |
275 |
|
} |
276 |
|
}, this.acc); |
277 |
|
} |
278 |
|
|
279 |
|
|
280 |
|
|
281 |
|
|
282 |
|
@param |
283 |
|
@exception |
284 |
|
@return |
285 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
286 |
21884 |
@Override... |
287 |
|
public Enumeration<URL> findResources(final String name) throws IOException |
288 |
|
{ |
289 |
21884 |
return AccessController.doPrivileged(new PrivilegedAction<Enumeration<URL>>() |
290 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
291 |
21884 |
@Override... |
292 |
|
public Enumeration<URL> run() |
293 |
|
{ |
294 |
21884 |
return URIClassLoader.this.finder.findResources(name); |
295 |
|
} |
296 |
|
}, this.acc); |
297 |
|
} |
298 |
|
|
299 |
|
|
300 |
|
|
301 |
|
|
302 |
|
|
303 |
|
@link |
304 |
|
|
305 |
|
|
306 |
|
|
307 |
|
|
308 |
|
@param |
309 |
|
@return |
310 |
|
@see |
311 |
|
@see |
312 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
313 |
0 |
@Override... |
314 |
|
protected String findLibrary(String libname) |
315 |
|
{ |
316 |
0 |
ResourceHandle md = getLibraryHandle(libname); |
317 |
0 |
if (md == null) { |
318 |
0 |
return null; |
319 |
|
} |
320 |
0 |
URL url = md.getURL(); |
321 |
0 |
if (!"file".equals(url.getProtocol())) { |
322 |
0 |
return null; |
323 |
|
} |
324 |
0 |
return new File(URI.create(url.toString())).getPath(); |
325 |
|
} |
326 |
|
|
327 |
|
|
328 |
|
|
329 |
|
|
330 |
|
|
331 |
|
@param |
332 |
|
@return |
333 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
334 |
0 |
protected ResourceHandle getClassHandle(final String name)... |
335 |
|
{ |
336 |
0 |
String path = name.replace('.', '/').concat(".class"); |
337 |
0 |
return getResourceHandle(path); |
338 |
|
} |
339 |
|
|
340 |
|
|
341 |
|
|
342 |
|
|
343 |
|
@param |
344 |
|
@return |
345 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
346 |
0 |
protected ResourceHandle getResourceHandle(final String name)... |
347 |
|
{ |
348 |
0 |
return AccessController.doPrivileged(new PrivilegedAction<ResourceHandle>() |
349 |
|
{ |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
350 |
0 |
@Override... |
351 |
|
public ResourceHandle run() |
352 |
|
{ |
353 |
0 |
return URIClassLoader.this.finder.getResource(name); |
354 |
|
} |
355 |
|
}, this.acc); |
356 |
|
} |
357 |
|
|
358 |
|
|
359 |
|
|
360 |
|
|
361 |
|
@link |
362 |
|
|
363 |
|
|
364 |
|
|
365 |
|
|
366 |
|
@param |
367 |
|
@return |
368 |
|
|
|
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 3 |
Complexity Density: 0.27 |
|
369 |
0 |
protected ResourceHandle getLibraryHandle(final String name)... |
370 |
|
{ |
371 |
0 |
int idx = name.lastIndexOf('/'); |
372 |
0 |
String path; |
373 |
0 |
String simplename; |
374 |
0 |
if (idx == -1) { |
375 |
0 |
path = ""; |
376 |
0 |
simplename = name; |
377 |
0 |
} else if (idx == name.length() - 1) { |
378 |
0 |
throw new IllegalArgumentException(name); |
379 |
|
} else { |
380 |
0 |
path = name.substring(0, idx + 1); |
381 |
0 |
simplename = name.substring(idx + 1); |
382 |
|
} |
383 |
0 |
return getResourceHandle(path + System.mapLibraryName(simplename)); |
384 |
|
} |
385 |
|
|
386 |
|
|
387 |
|
|
388 |
|
|
389 |
|
@param |
390 |
|
@return |
391 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
392 |
0 |
protected Enumeration<ResourceHandle> getResourceHandles(final String name)... |
393 |
|
{ |
394 |
0 |
return AccessController.doPrivileged(new PrivilegedAction<Enumeration<ResourceHandle>>() |
395 |
|
{ |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
396 |
0 |
@Override... |
397 |
|
public Enumeration<ResourceHandle> run() |
398 |
|
{ |
399 |
0 |
return URIClassLoader.this.finder.getResources(name); |
400 |
|
} |
401 |
|
}, this.acc); |
402 |
|
} |
403 |
|
|
|
|
| 86.1% |
Uncovered Elements: 5 (36) |
Complexity: 13 |
Complexity Density: 0.62 |
|
404 |
|
private static class URIResourceFinder implements ResourceFinder |
405 |
|
{ |
406 |
|
URL[] urls; |
407 |
|
|
408 |
|
final ResourceLoader loader; |
409 |
|
|
410 |
|
final URLStreamHandlerFactory handlerFactory; |
411 |
|
|
|
|
| 85.7% |
Uncovered Elements: 2 (14) |
Complexity: 5 |
Complexity Density: 0.62 |
|
412 |
20151 |
public URIResourceFinder(URI[] uris, URLStreamHandlerFactory handlerFactory)... |
413 |
|
{ |
414 |
20151 |
this.handlerFactory = handlerFactory; |
415 |
20087 |
try { |
416 |
20113 |
this.loader = |
417 |
20122 |
new ResourceLoader(handlerFactory != null ? handlerFactory.createURLStreamHandler("jar") : null); |
418 |
20107 |
URL[] urls = new URL[uris.length]; |
419 |
20132 |
for (int i = 0; i < uris.length; i++) { |
420 |
7 |
urls[i] = new URL(null, uris[i].toString(), |
421 |
7 |
handlerFactory != null ? handlerFactory.createURLStreamHandler(uris[i].getScheme()) : null); |
422 |
|
} |
423 |
20105 |
this.urls = urls; |
424 |
|
} catch (MalformedURLException e) { |
425 |
0 |
throw new IllegalArgumentException(e.getMessage()); |
426 |
|
} |
427 |
|
} |
428 |
|
|
|
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
|
429 |
137 |
public synchronized void addURI(URI uri)... |
430 |
|
{ |
431 |
137 |
try { |
432 |
137 |
URL url = new URL(null, uri.toString(), this.handlerFactory != null ? this.handlerFactory |
433 |
|
.createURLStreamHandler(uri.getScheme()) : null); |
434 |
137 |
int len = this.urls.length; |
435 |
137 |
URL[] urls = new URL[len + 1]; |
436 |
137 |
System.arraycopy(this.urls, 0, urls, 0, len); |
437 |
137 |
urls[len] = url; |
438 |
137 |
this.urls = urls; |
439 |
|
} catch (MalformedURLException e) { |
440 |
0 |
throw new IllegalArgumentException(e.getMessage()); |
441 |
|
} |
442 |
|
} |
443 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
444 |
76668 |
private synchronized URL[] getUrls()... |
445 |
|
{ |
446 |
76668 |
return this.urls; |
447 |
|
} |
448 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
449 |
45770 |
@Override... |
450 |
|
public ResourceHandle getResource(String name) |
451 |
|
{ |
452 |
45770 |
return this.loader.getResource(getUrls(), name); |
453 |
|
} |
454 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
455 |
0 |
@Override... |
456 |
|
public Enumeration<ResourceHandle> getResources(String name) |
457 |
|
{ |
458 |
0 |
return this.loader.getResources(getUrls(), name); |
459 |
|
} |
460 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
461 |
9006 |
@Override... |
462 |
|
public URL findResource(String name) |
463 |
|
{ |
464 |
9006 |
return this.loader.findResource(getUrls(), name); |
465 |
|
} |
466 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
467 |
21884 |
@Override... |
468 |
|
public Enumeration<URL> findResources(String name) |
469 |
|
{ |
470 |
21884 |
return this.loader.findResources(getUrls(), name); |
471 |
|
} |
472 |
|
} |
473 |
|
} |