| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.classloader.internal; |
| 21 |
|
|
| 22 |
|
import java.io.BufferedReader; |
| 23 |
|
import java.io.IOException; |
| 24 |
|
import java.io.InputStream; |
| 25 |
|
import java.io.InputStreamReader; |
| 26 |
|
import java.net.HttpURLConnection; |
| 27 |
|
import java.net.MalformedURLException; |
| 28 |
|
import java.net.URI; |
| 29 |
|
import java.net.URISyntaxException; |
| 30 |
|
import java.net.URL; |
| 31 |
|
import java.net.URLConnection; |
| 32 |
|
import java.net.URLStreamHandler; |
| 33 |
|
import java.security.Permission; |
| 34 |
|
import java.security.cert.Certificate; |
| 35 |
|
import java.util.ArrayList; |
| 36 |
|
import java.util.Arrays; |
| 37 |
|
import java.util.Collections; |
| 38 |
|
import java.util.Enumeration; |
| 39 |
|
import java.util.HashMap; |
| 40 |
|
import java.util.HashSet; |
| 41 |
|
import java.util.LinkedHashMap; |
| 42 |
|
import java.util.List; |
| 43 |
|
import java.util.Map; |
| 44 |
|
import java.util.NoSuchElementException; |
| 45 |
|
import java.util.Set; |
| 46 |
|
import java.util.StringTokenizer; |
| 47 |
|
import java.util.jar.Attributes; |
| 48 |
|
import java.util.jar.JarEntry; |
| 49 |
|
import java.util.jar.JarFile; |
| 50 |
|
import java.util.jar.Manifest; |
| 51 |
|
|
| 52 |
|
import edu.emory.mathcs.util.classloader.ResourceHandle; |
| 53 |
|
import edu.emory.mathcs.util.classloader.ResourceUtils; |
| 54 |
|
|
| 55 |
|
|
| 56 |
|
@link |
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
@link |
| 68 |
|
|
| 69 |
|
|
| 70 |
|
@link |
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
@version |
| 85 |
|
@since |
| 86 |
|
|
| |
|
| 34.3% |
Uncovered Elements: 132 (201) |
Complexity: 56 |
Complexity Density: 0.4 |
|
| 87 |
|
public class ResourceLoader |
| 88 |
|
{ |
| 89 |
|
private static final String JAR_INDEX_ENTRY_NAME = "META-INF/INDEX.LIST"; |
| 90 |
|
|
| 91 |
|
private URLStreamHandler jarHandler; |
| 92 |
|
|
| 93 |
|
private Map<String, JarInfo> url2jarInfo = new HashMap<String, JarInfo>(); |
| 94 |
|
|
| 95 |
|
|
| 96 |
|
|
| 97 |
|
|
| 98 |
|
@param |
| 99 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 100 |
20136 |
public ResourceLoader(URLStreamHandler jarHandler)... |
| 101 |
|
{ |
| 102 |
20140 |
this.jarHandler = jarHandler; |
| 103 |
|
} |
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
|
|
| 108 |
|
|
| 109 |
|
|
| 110 |
|
|
| 111 |
|
@param |
| 112 |
|
@param |
| 113 |
|
@return |
| 114 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 115 |
0 |
public ResourceHandle getResource(URL source, String name)... |
| 116 |
|
{ |
| 117 |
0 |
return getResource(source, name, new HashSet<URL>(), null); |
| 118 |
|
} |
| 119 |
|
|
| 120 |
|
|
| 121 |
|
|
| 122 |
|
|
| 123 |
|
|
| 124 |
|
|
| 125 |
|
|
| 126 |
|
|
| 127 |
|
@param |
| 128 |
|
@param |
| 129 |
|
@return |
| 130 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
| 131 |
45770 |
public ResourceHandle getResource(URL[] sources, String name)... |
| 132 |
|
{ |
| 133 |
45770 |
Set<URL> visited = new HashSet<URL>(); |
| 134 |
45770 |
for (URL source : sources) { |
| 135 |
1320 |
ResourceHandle h = getResource(source, name, visited, null); |
| 136 |
1320 |
if (h != null) { |
| 137 |
338 |
return h; |
| 138 |
|
} |
| 139 |
|
} |
| 140 |
45432 |
return null; |
| 141 |
|
} |
| 142 |
|
|
| 143 |
|
|
| 144 |
|
|
| 145 |
|
|
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
| 149 |
|
|
| 150 |
|
@link |
| 151 |
|
|
| 152 |
|
@param |
| 153 |
|
@param |
| 154 |
|
@return |
| 155 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 156 |
0 |
public Enumeration<ResourceHandle> getResources(URL source, String name)... |
| 157 |
|
{ |
| 158 |
0 |
return new ResourceEnumeration<ResourceHandle>(new URL[] { source }, name, false); |
| 159 |
|
} |
| 160 |
|
|
| 161 |
|
|
| 162 |
|
|
| 163 |
|
|
| 164 |
|
|
| 165 |
|
|
| 166 |
|
|
| 167 |
|
|
| 168 |
|
@link |
| 169 |
|
|
| 170 |
|
@param |
| 171 |
|
@param |
| 172 |
|
@return |
| 173 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 174 |
0 |
public Enumeration<ResourceHandle> getResources(URL[] sources, String name)... |
| 175 |
|
{ |
| 176 |
0 |
return new ResourceEnumeration<ResourceHandle>(sources.clone(), name, false); |
| 177 |
|
} |
| 178 |
|
|
| |
|
| 20.8% |
Uncovered Elements: 19 (24) |
Complexity: 8 |
Complexity Density: 0.4 |
|
| 179 |
1320 |
private ResourceHandle getResource(final URL source, String name, Set<URL> visitedJars, Set<URL> skip)... |
| 180 |
|
{ |
| 181 |
1320 |
name = ResourceUtils.canonizePath(name); |
| 182 |
1320 |
if (isDir(source)) { |
| 183 |
|
|
| 184 |
0 |
final URL url; |
| 185 |
0 |
try { |
| 186 |
|
|
| 187 |
0 |
URI relUri = new URI(null, null, null, -1, name, null, null); |
| 188 |
0 |
url = new URL(source, relUri.getRawPath()); |
| 189 |
|
} catch (URISyntaxException e) { |
| 190 |
0 |
throw new IllegalArgumentException("Illegal resource name: " + name); |
| 191 |
|
} catch (MalformedURLException e) { |
| 192 |
0 |
return null; |
| 193 |
|
} |
| 194 |
|
|
| 195 |
0 |
if (skip != null && skip.contains(url)) { |
| 196 |
0 |
return null; |
| 197 |
|
} |
| 198 |
0 |
final URLConnection conn; |
| 199 |
0 |
try { |
| 200 |
0 |
conn = url.openConnection(); |
| 201 |
0 |
conn.getInputStream(); |
| 202 |
|
} catch (IOException e) { |
| 203 |
0 |
return null; |
| 204 |
|
} |
| 205 |
0 |
final String finalName = name; |
| 206 |
0 |
return new ResourceHandle() |
| 207 |
|
{ |
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 208 |
0 |
@Override... |
| 209 |
|
public String getName() |
| 210 |
|
{ |
| 211 |
0 |
return finalName; |
| 212 |
|
} |
| 213 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 214 |
0 |
@Override... |
| 215 |
|
public URL getURL() |
| 216 |
|
{ |
| 217 |
0 |
return url; |
| 218 |
|
} |
| 219 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 220 |
0 |
@Override... |
| 221 |
|
public URL getCodeSourceURL() |
| 222 |
|
{ |
| 223 |
0 |
return source; |
| 224 |
|
} |
| 225 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 226 |
0 |
@Override... |
| 227 |
|
public InputStream getInputStream() throws IOException |
| 228 |
|
{ |
| 229 |
0 |
return conn.getInputStream(); |
| 230 |
|
} |
| 231 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 232 |
0 |
@Override... |
| 233 |
|
public int getContentLength() |
| 234 |
|
{ |
| 235 |
0 |
return conn.getContentLength(); |
| 236 |
|
} |
| 237 |
|
|
| |
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 2 |
Complexity Density: 1 |
|
| 238 |
0 |
@Override... |
| 239 |
|
public void close() |
| 240 |
|
{ |
| 241 |
0 |
try { |
| 242 |
0 |
getInputStream().close(); |
| 243 |
|
} catch (IOException e) { |
| 244 |
|
} |
| 245 |
|
} |
| 246 |
|
}; |
| 247 |
|
} else { |
| 248 |
|
|
| 249 |
1320 |
try { |
| 250 |
1320 |
return getJarInfo(source).getResource(name, visitedJars, skip); |
| 251 |
|
} catch (MalformedURLException e) { |
| 252 |
0 |
return null; |
| 253 |
|
} |
| 254 |
|
} |
| 255 |
|
} |
| 256 |
|
|
| 257 |
|
|
| 258 |
|
|
| 259 |
|
|
| 260 |
|
|
| 261 |
|
|
| 262 |
|
|
| 263 |
|
@param |
| 264 |
|
@param |
| 265 |
|
@return |
| 266 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 267 |
0 |
public URL findResource(URL source, String name)... |
| 268 |
|
{ |
| 269 |
0 |
return findResource(source, name, new HashSet<URL>(), null); |
| 270 |
|
} |
| 271 |
|
|
| 272 |
|
|
| 273 |
|
|
| 274 |
|
|
| 275 |
|
|
| 276 |
|
|
| 277 |
|
|
| 278 |
|
|
| 279 |
|
@param |
| 280 |
|
@param |
| 281 |
|
@return |
| 282 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
| 283 |
9006 |
public URL findResource(URL[] sources, String name)... |
| 284 |
|
{ |
| 285 |
9006 |
Set<URL> visited = new HashSet<URL>(); |
| 286 |
9006 |
for (URL source : sources) { |
| 287 |
98 |
URL url = findResource(source, name, visited, null); |
| 288 |
98 |
if (url != null) { |
| 289 |
2 |
return url; |
| 290 |
|
} |
| 291 |
|
} |
| 292 |
9004 |
return null; |
| 293 |
|
} |
| 294 |
|
|
| 295 |
|
|
| 296 |
|
|
| 297 |
|
|
| 298 |
|
|
| 299 |
|
|
| 300 |
|
|
| 301 |
|
|
| 302 |
|
@link |
| 303 |
|
|
| 304 |
|
@param |
| 305 |
|
@param |
| 306 |
|
@return |
| 307 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 308 |
0 |
public Enumeration<URL> findResources(URL source, String name)... |
| 309 |
|
{ |
| 310 |
0 |
return new ResourceEnumeration<URL>(new URL[] { source }, name, true); |
| 311 |
|
} |
| 312 |
|
|
| 313 |
|
|
| 314 |
|
|
| 315 |
|
|
| 316 |
|
|
| 317 |
|
|
| 318 |
|
|
| 319 |
|
|
| 320 |
|
@link |
| 321 |
|
|
| 322 |
|
@param |
| 323 |
|
@param |
| 324 |
|
@return |
| 325 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 326 |
21884 |
public Enumeration<URL> findResources(URL[] sources, String name)... |
| 327 |
|
{ |
| 328 |
21884 |
return new ResourceEnumeration<URL>(sources.clone(), name, true); |
| 329 |
|
} |
| 330 |
|
|
| |
|
| 27.3% |
Uncovered Elements: 24 (33) |
Complexity: 10 |
Complexity Density: 0.43 |
|
| 331 |
105 |
private URL findResource(final URL source, String name, Set<URL> visitedJars, Set<URL> skip)... |
| 332 |
|
{ |
| 333 |
105 |
URL url; |
| 334 |
105 |
name = ResourceUtils.canonizePath(name); |
| 335 |
105 |
if (isDir(source)) { |
| 336 |
|
|
| 337 |
0 |
try { |
| 338 |
0 |
url = new URL(source, name); |
| 339 |
|
} catch (MalformedURLException e) { |
| 340 |
0 |
return null; |
| 341 |
|
} |
| 342 |
0 |
if (skip != null && skip.contains(url)) { |
| 343 |
0 |
return null; |
| 344 |
|
} |
| 345 |
0 |
final URLConnection conn; |
| 346 |
0 |
try { |
| 347 |
0 |
conn = url.openConnection(); |
| 348 |
0 |
if (conn instanceof HttpURLConnection) { |
| 349 |
0 |
HttpURLConnection httpConn = (HttpURLConnection) conn; |
| 350 |
0 |
httpConn.setRequestMethod("HEAD"); |
| 351 |
0 |
if (httpConn.getResponseCode() >= 400) { |
| 352 |
0 |
return null; |
| 353 |
|
} |
| 354 |
|
} else { |
| 355 |
0 |
conn.getInputStream().close(); |
| 356 |
|
} |
| 357 |
|
} catch (IOException e) { |
| 358 |
0 |
return null; |
| 359 |
|
} |
| 360 |
0 |
return url; |
| 361 |
|
} else { |
| 362 |
|
|
| 363 |
105 |
try { |
| 364 |
105 |
ResourceHandle rh = getJarInfo(source).getResource(name, visitedJars, skip); |
| 365 |
105 |
return (rh != null) ? rh.getURL() : null; |
| 366 |
|
} catch (MalformedURLException e) { |
| 367 |
0 |
return null; |
| 368 |
|
} |
| 369 |
|
} |
| 370 |
|
} |
| 371 |
|
|
| 372 |
|
|
| 373 |
|
|
| 374 |
|
|
| 375 |
|
|
| 376 |
|
@param |
| 377 |
|
@return |
| 378 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 379 |
1425 |
protected static boolean isDir(URL url)... |
| 380 |
|
{ |
| 381 |
1425 |
String file = url.getFile(); |
| 382 |
1425 |
return file != null && file.endsWith("/"); |
| 383 |
|
} |
| 384 |
|
|
| |
|
| 45% |
Uncovered Elements: 82 (149) |
Complexity: 35 |
Complexity Density: 0.36 |
|
| 385 |
|
private static class JarInfo |
| 386 |
|
{ |
| 387 |
|
private ResourceLoader loader; |
| 388 |
|
|
| 389 |
|
private URL source; |
| 390 |
|
|
| 391 |
|
private URL base; |
| 392 |
|
|
| 393 |
|
private JarFile jar; |
| 394 |
|
|
| 395 |
|
private boolean resolved; |
| 396 |
|
|
| 397 |
|
private Permission perm; |
| 398 |
|
|
| 399 |
|
private URL[] classPath; |
| 400 |
|
|
| 401 |
|
private String[] index; |
| 402 |
|
|
| 403 |
|
private Map<String, URL[]> package2url; |
| 404 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 405 |
135 |
JarInfo(ResourceLoader loader, URL source) throws MalformedURLException... |
| 406 |
|
{ |
| 407 |
135 |
this.loader = loader; |
| 408 |
135 |
this.source = source; |
| 409 |
135 |
this.base = new URL("jar", "", -1, source + "!/", loader.jarHandler); |
| 410 |
|
} |
| 411 |
|
|
| |
|
| 51% |
Uncovered Elements: 24 (49) |
Complexity: 15 |
Complexity Density: 0.45 |
|
| 412 |
1425 |
ResourceHandle getResource(String name, Set<URL> visited, Set<URL> skip)... |
| 413 |
|
{ |
| 414 |
1425 |
visited.add(this.source); |
| 415 |
1425 |
URL url; |
| 416 |
1425 |
try { |
| 417 |
|
|
| 418 |
1425 |
URI relUri = new URI(null, null, null, -1, name, null, null); |
| 419 |
1425 |
url = new URL(this.base, relUri.getRawPath()); |
| 420 |
|
} catch (URISyntaxException e) { |
| 421 |
0 |
throw new IllegalArgumentException("Illegal resource name: " + name); |
| 422 |
|
} catch (MalformedURLException e) { |
| 423 |
0 |
return null; |
| 424 |
|
} |
| 425 |
1425 |
try { |
| 426 |
1425 |
JarFile jfile = getJarFileIfPossiblyContains(name); |
| 427 |
1424 |
if (jfile != null) { |
| 428 |
1424 |
JarEntry jentry = this.jar.getJarEntry(name); |
| 429 |
1424 |
if (jentry != null && (skip == null || !skip.contains(url))) { |
| 430 |
340 |
return new JarResourceHandle(jfile, jentry, url, this.source); |
| 431 |
|
} |
| 432 |
|
} |
| 433 |
|
} catch (IOException e) { |
| 434 |
1 |
return null; |
| 435 |
|
} |
| 436 |
|
|
| 437 |
|
|
| 438 |
1084 |
URL[] dependencies; |
| 439 |
1084 |
synchronized (this) { |
| 440 |
1084 |
if (this.package2url != null) { |
| 441 |
0 |
int idx = name.lastIndexOf("/"); |
| 442 |
0 |
String prefix = (idx > 0) ? name.substring(0, idx) : name; |
| 443 |
0 |
dependencies = this.package2url.get(prefix); |
| 444 |
|
} else { |
| 445 |
|
|
| 446 |
|
|
| 447 |
|
|
| 448 |
|
|
| 449 |
|
|
| 450 |
503 |
assert this.classPath != null; |
| 451 |
1084 |
dependencies = this.classPath; |
| 452 |
|
} |
| 453 |
|
} |
| 454 |
|
|
| 455 |
1084 |
if (dependencies == null) { |
| 456 |
0 |
return null; |
| 457 |
|
} |
| 458 |
|
|
| 459 |
1084 |
for (URL cpUrl : dependencies) { |
| 460 |
0 |
if (visited.contains(cpUrl)) { |
| 461 |
0 |
continue; |
| 462 |
|
} |
| 463 |
0 |
JarInfo depJInfo; |
| 464 |
0 |
try { |
| 465 |
0 |
depJInfo = this.loader.getJarInfo(cpUrl); |
| 466 |
0 |
ResourceHandle rh = depJInfo.getResource(name, visited, skip); |
| 467 |
0 |
if (rh != null) { |
| 468 |
0 |
return rh; |
| 469 |
|
} |
| 470 |
|
} catch (MalformedURLException e) { |
| 471 |
|
|
| 472 |
|
} |
| 473 |
|
} |
| 474 |
|
|
| 475 |
|
|
| 476 |
1084 |
return null; |
| 477 |
|
} |
| 478 |
|
|
| |
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 4 |
Complexity Density: 0.44 |
|
| 479 |
0 |
synchronized void setIndex(List<String> newIndex)... |
| 480 |
|
{ |
| 481 |
0 |
if (this.jar != null) { |
| 482 |
|
|
| 483 |
0 |
return; |
| 484 |
|
} |
| 485 |
0 |
if (this.index != null) { |
| 486 |
|
|
| 487 |
0 |
Set<String> violating = new HashSet<String>(Arrays.asList(this.index)); |
| 488 |
0 |
violating.removeAll(newIndex); |
| 489 |
0 |
if (!violating.isEmpty()) { |
| 490 |
0 |
throw new RuntimeException("Invalid JAR index: " |
| 491 |
|
+ "the following entries were previously declared, but " |
| 492 |
|
+ "they are not present in the new index: " + violating.toString()); |
| 493 |
|
} |
| 494 |
|
} |
| 495 |
0 |
this.index = newIndex.toArray(new String[newIndex.size()]); |
| 496 |
0 |
Arrays.sort(this.index); |
| 497 |
|
} |
| 498 |
|
|
| |
|
| 46.2% |
Uncovered Elements: 42 (78) |
Complexity: 15 |
Complexity Density: 0.29 |
|
| 499 |
1425 |
public JarFile getJarFileIfPossiblyContains(String name) throws IOException... |
| 500 |
|
{ |
| 501 |
1425 |
Map<URL, List<String>> indexes; |
| 502 |
1425 |
synchronized (this) { |
| 503 |
1425 |
if (this.jar != null) { |
| 504 |
|
|
| 505 |
1290 |
SecurityManager security = System.getSecurityManager(); |
| 506 |
1290 |
if (security != null) { |
| 507 |
0 |
security.checkPermission(this.perm); |
| 508 |
|
} |
| 509 |
|
|
| 510 |
|
|
| 511 |
|
|
| 512 |
1290 |
try { |
| 513 |
1290 |
while (!this.resolved) { |
| 514 |
0 |
wait(); |
| 515 |
|
} |
| 516 |
|
} catch (InterruptedException e) { |
| 517 |
0 |
throw new IOException("Interrupted"); |
| 518 |
|
} |
| 519 |
1290 |
return this.jar; |
| 520 |
|
} |
| 521 |
|
|
| 522 |
135 |
if (this.index != null) { |
| 523 |
|
|
| 524 |
0 |
int pos = name.lastIndexOf('/'); |
| 525 |
0 |
if (pos > 0) { |
| 526 |
0 |
name = name.substring(0, pos); |
| 527 |
|
} |
| 528 |
0 |
if (Arrays.binarySearch(this.index, name) < 0) { |
| 529 |
0 |
return null; |
| 530 |
|
} |
| 531 |
|
} |
| 532 |
|
|
| 533 |
|
|
| 534 |
135 |
URLConnection connection = this.base.openConnection(); |
| 535 |
135 |
this.perm = connection.getPermission(); |
| 536 |
|
|
| 537 |
135 |
JarFile jar; |
| 538 |
|
|
| 539 |
135 |
if (connection instanceof org.xwiki.classloader.internal.JarURLConnection) { |
| 540 |
6 |
jar = ((org.xwiki.classloader.internal.JarURLConnection) connection).getJarFile(); |
| 541 |
|
} else { |
| 542 |
129 |
jar = ((java.net.JarURLConnection) connection).getJarFile(); |
| 543 |
|
} |
| 544 |
|
|
| 545 |
|
|
| 546 |
|
|
| 547 |
134 |
if (this.index != null) { |
| 548 |
0 |
Set<String> indices = new HashSet<String>(Arrays.asList(this.index)); |
| 549 |
0 |
Enumeration<JarEntry> entries = jar.entries(); |
| 550 |
0 |
while (entries.hasMoreElements()) { |
| 551 |
0 |
JarEntry entry = entries.nextElement(); |
| 552 |
0 |
String indexEntry = entry.getName(); |
| 553 |
|
|
| 554 |
0 |
int pos = indexEntry.lastIndexOf('/'); |
| 555 |
0 |
if (pos > 0) { |
| 556 |
0 |
indexEntry = indexEntry.substring(0, pos); |
| 557 |
|
} |
| 558 |
0 |
indices.remove(indexEntry); |
| 559 |
|
} |
| 560 |
0 |
if (!indices.isEmpty()) { |
| 561 |
0 |
throw new RuntimeException("Invalid JAR index: the following entries not found in JAR: " |
| 562 |
|
+ indices); |
| 563 |
|
} |
| 564 |
|
} |
| 565 |
134 |
this.jar = jar; |
| 566 |
|
|
| 567 |
134 |
this.classPath = parseClassPath(jar, this.source); |
| 568 |
|
|
| 569 |
134 |
indexes = parseJarIndex(this.source, jar); |
| 570 |
134 |
indexes.remove(this.source.toExternalForm()); |
| 571 |
|
|
| 572 |
134 |
if (!indexes.isEmpty()) { |
| 573 |
0 |
this.package2url = package2url(indexes); |
| 574 |
|
} |
| 575 |
|
} |
| 576 |
|
|
| 577 |
134 |
try { |
| 578 |
134 |
for (Map.Entry<URL, List<String>> entry : indexes.entrySet()) { |
| 579 |
0 |
URL url = entry.getKey(); |
| 580 |
0 |
if (url.toExternalForm().equals(this.source.toExternalForm())) { |
| 581 |
0 |
continue; |
| 582 |
|
} |
| 583 |
0 |
List<String> index = entry.getValue(); |
| 584 |
0 |
this.loader.getJarInfo(url).setIndex(index); |
| 585 |
|
} |
| 586 |
|
} finally { |
| 587 |
134 |
synchronized (this) { |
| 588 |
134 |
this.resolved = true; |
| 589 |
134 |
notifyAll(); |
| 590 |
|
} |
| 591 |
|
} |
| 592 |
134 |
return this.jar; |
| 593 |
|
} |
| 594 |
|
} |
| 595 |
|
|
| |
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 2 |
Complexity Density: 0.14 |
|
| 596 |
0 |
private static Map<String, URL[]> package2url(Map<URL, List<String>> indexes)... |
| 597 |
|
{ |
| 598 |
0 |
Map<String, List<URL>> prefix2url = new HashMap<String, List<URL>>(); |
| 599 |
0 |
for (Map.Entry<URL, List<String>> entry : indexes.entrySet()) { |
| 600 |
0 |
URL url = entry.getKey(); |
| 601 |
0 |
for (String prefix : entry.getValue()) { |
| 602 |
0 |
List<URL> prefixList = prefix2url.get(prefix); |
| 603 |
0 |
if (prefixList == null) { |
| 604 |
0 |
prefixList = new ArrayList<URL>(); |
| 605 |
0 |
prefix2url.put(prefix, prefixList); |
| 606 |
|
} |
| 607 |
0 |
prefixList.add(url); |
| 608 |
|
} |
| 609 |
|
} |
| 610 |
|
|
| 611 |
0 |
Map<String, URL[]> result = new HashMap<String, URL[]>(prefix2url.size()); |
| 612 |
|
|
| 613 |
|
|
| 614 |
0 |
for (Map.Entry<String, List<URL>> entry : prefix2url.entrySet()) { |
| 615 |
0 |
List<URL> list = entry.getValue(); |
| 616 |
0 |
result.put(entry.getKey(), list.toArray(new URL[list.size()])); |
| 617 |
|
} |
| 618 |
0 |
return result; |
| 619 |
|
} |
| 620 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
| 621 |
1425 |
private JarInfo getJarInfo(URL url) throws MalformedURLException... |
| 622 |
|
{ |
| 623 |
1425 |
JarInfo jinfo; |
| 624 |
1425 |
synchronized (this.url2jarInfo) { |
| 625 |
|
|
| 626 |
|
|
| 627 |
|
|
| 628 |
|
|
| 629 |
1425 |
jinfo = this.url2jarInfo.get(url.toExternalForm()); |
| 630 |
1425 |
if (jinfo == null) { |
| 631 |
135 |
jinfo = new JarInfo(this, url); |
| 632 |
135 |
this.url2jarInfo.put(url.toExternalForm(), jinfo); |
| 633 |
|
} |
| 634 |
|
} |
| 635 |
1425 |
return jinfo; |
| 636 |
|
} |
| 637 |
|
|
| |
|
| 77.3% |
Uncovered Elements: 5 (22) |
Complexity: 10 |
Complexity Density: 0.83 |
|
| 638 |
|
private static class JarResourceHandle extends ResourceHandle |
| 639 |
|
{ |
| 640 |
|
final JarFile jar; |
| 641 |
|
|
| 642 |
|
final JarEntry jentry; |
| 643 |
|
|
| 644 |
|
final URL url; |
| 645 |
|
|
| 646 |
|
final URL codeSource; |
| 647 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 648 |
340 |
JarResourceHandle(JarFile jar, JarEntry jentry, URL url, URL codeSource)... |
| 649 |
|
{ |
| 650 |
340 |
this.jar = jar; |
| 651 |
340 |
this.jentry = jentry; |
| 652 |
340 |
this.url = url; |
| 653 |
340 |
this.codeSource = codeSource; |
| 654 |
|
} |
| 655 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 656 |
0 |
@Override... |
| 657 |
|
public String getName() |
| 658 |
|
{ |
| 659 |
0 |
return this.jentry.getName(); |
| 660 |
|
} |
| 661 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 662 |
2 |
@Override... |
| 663 |
|
public URL getURL() |
| 664 |
|
{ |
| 665 |
2 |
return this.url; |
| 666 |
|
} |
| 667 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 668 |
338 |
@Override... |
| 669 |
|
public URL getCodeSourceURL() |
| 670 |
|
{ |
| 671 |
338 |
return this.codeSource; |
| 672 |
|
} |
| 673 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 674 |
338 |
@Override... |
| 675 |
|
public InputStream getInputStream() throws IOException |
| 676 |
|
{ |
| 677 |
338 |
return this.jar.getInputStream(this.jentry); |
| 678 |
|
} |
| 679 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 680 |
338 |
@Override... |
| 681 |
|
public int getContentLength() |
| 682 |
|
{ |
| 683 |
338 |
return (int) this.jentry.getSize(); |
| 684 |
|
} |
| 685 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 686 |
336 |
@Override... |
| 687 |
|
public Manifest getManifest() throws IOException |
| 688 |
|
{ |
| 689 |
336 |
return this.jar.getManifest(); |
| 690 |
|
} |
| 691 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 692 |
0 |
@Override... |
| 693 |
|
public Attributes getAttributes() throws IOException |
| 694 |
|
{ |
| 695 |
0 |
return this.jentry.getAttributes(); |
| 696 |
|
} |
| 697 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 698 |
338 |
@Override... |
| 699 |
|
public Certificate[] getCertificates() |
| 700 |
|
{ |
| 701 |
338 |
return this.jentry.getCertificates(); |
| 702 |
|
} |
| 703 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 704 |
0 |
@Override... |
| 705 |
|
public void close() |
| 706 |
|
{ |
| 707 |
|
} |
| 708 |
|
} |
| 709 |
|
|
| |
|
| 12.9% |
Uncovered Elements: 27 (31) |
Complexity: 7 |
Complexity Density: 0.3 |
|
| 710 |
134 |
private static Map<URL, List<String>> parseJarIndex(URL cxt, JarFile jar) throws IOException... |
| 711 |
|
{ |
| 712 |
134 |
JarEntry entry = jar.getJarEntry(JAR_INDEX_ENTRY_NAME); |
| 713 |
134 |
if (entry == null) { |
| 714 |
134 |
return Collections.emptyMap(); |
| 715 |
|
} |
| 716 |
0 |
InputStream is = jar.getInputStream(entry); |
| 717 |
0 |
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); |
| 718 |
|
|
| 719 |
0 |
Map<URL, List<String>> result = new LinkedHashMap<URL, List<String>>(); |
| 720 |
|
|
| 721 |
0 |
String line; |
| 722 |
|
|
| 723 |
|
|
| 724 |
0 |
do { |
| 725 |
0 |
line = reader.readLine(); |
| 726 |
0 |
} while (line != null && line.trim().length() > 0); |
| 727 |
|
|
| 728 |
0 |
URL currentURL; |
| 729 |
0 |
List<String> currentList = null; |
| 730 |
0 |
while (true) { |
| 731 |
|
|
| 732 |
0 |
line = reader.readLine(); |
| 733 |
0 |
if (line == null) { |
| 734 |
0 |
return result; |
| 735 |
|
} |
| 736 |
|
|
| 737 |
0 |
currentURL = new URL(cxt, line); |
| 738 |
0 |
currentList = new ArrayList<String>(); |
| 739 |
0 |
result.put(currentURL, currentList); |
| 740 |
|
|
| 741 |
0 |
while (true) { |
| 742 |
0 |
line = reader.readLine(); |
| 743 |
0 |
if (line == null || line.trim().length() == 0) { |
| 744 |
0 |
break; |
| 745 |
|
} |
| 746 |
0 |
currentList.add(line); |
| 747 |
|
} |
| 748 |
|
} |
| 749 |
|
} |
| 750 |
|
|
| |
|
| 35.3% |
Uncovered Elements: 22 (34) |
Complexity: 8 |
Complexity Density: 0.33 |
|
| 751 |
134 |
private static URL[] parseClassPath(JarFile jar, URL source) throws IOException... |
| 752 |
|
{ |
| 753 |
134 |
Manifest man = jar.getManifest(); |
| 754 |
134 |
if (man == null) { |
| 755 |
2 |
return new URL[0]; |
| 756 |
|
} |
| 757 |
132 |
Attributes attr = man.getMainAttributes(); |
| 758 |
132 |
if (attr == null) { |
| 759 |
0 |
return new URL[0]; |
| 760 |
|
} |
| 761 |
132 |
String cp = attr.getValue(Attributes.Name.CLASS_PATH); |
| 762 |
132 |
if (cp == null) { |
| 763 |
132 |
return new URL[0]; |
| 764 |
|
} |
| 765 |
0 |
StringTokenizer tokenizer = new StringTokenizer(cp); |
| 766 |
0 |
List<URL> cpList = new ArrayList<URL>(); |
| 767 |
0 |
URI sourceURI = URI.create(source.toString()); |
| 768 |
0 |
while (tokenizer.hasMoreTokens()) { |
| 769 |
0 |
String token = tokenizer.nextToken(); |
| 770 |
0 |
try { |
| 771 |
0 |
try { |
| 772 |
0 |
URI uri = new URI(token); |
| 773 |
0 |
if (!uri.isAbsolute()) { |
| 774 |
0 |
uri = sourceURI.resolve(uri); |
| 775 |
|
} |
| 776 |
0 |
cpList.add(uri.toURL()); |
| 777 |
|
} catch (URISyntaxException e) { |
| 778 |
|
|
| 779 |
0 |
URL url = new URL(source, token); |
| 780 |
0 |
cpList.add(url); |
| 781 |
|
} |
| 782 |
|
} catch (MalformedURLException e) { |
| 783 |
0 |
throw new IOException(e.getMessage()); |
| 784 |
|
} |
| 785 |
|
} |
| 786 |
0 |
return cpList.toArray(new URL[cpList.size()]); |
| 787 |
|
} |
| 788 |
|
|
| |
|
| 45.5% |
Uncovered Elements: 24 (44) |
Complexity: 10 |
Complexity Density: 0.36 |
|
| 789 |
|
private class ResourceEnumeration<T> implements Enumeration<T> |
| 790 |
|
{ |
| 791 |
|
final URL[] urls; |
| 792 |
|
|
| 793 |
|
final String name; |
| 794 |
|
|
| 795 |
|
final boolean findOnly; |
| 796 |
|
|
| 797 |
|
int idx; |
| 798 |
|
|
| 799 |
|
T next; |
| 800 |
|
|
| 801 |
|
Set<URL> previousURLs = new HashSet<URL>(); |
| 802 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 803 |
21884 |
ResourceEnumeration(URL[] urls, String name, boolean findOnly)... |
| 804 |
|
{ |
| 805 |
21884 |
this.urls = urls; |
| 806 |
21884 |
this.name = name; |
| 807 |
21884 |
this.findOnly = findOnly; |
| 808 |
21884 |
this.idx = 0; |
| 809 |
|
} |
| 810 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 811 |
9060 |
@Override... |
| 812 |
|
public boolean hasMoreElements() |
| 813 |
|
{ |
| 814 |
9060 |
fetchNext(); |
| 815 |
9060 |
return this.next != null; |
| 816 |
|
} |
| 817 |
|
|
| |
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
| 818 |
0 |
@Override... |
| 819 |
|
public T nextElement() |
| 820 |
|
{ |
| 821 |
0 |
fetchNext(); |
| 822 |
0 |
if (this.next == null) { |
| 823 |
0 |
throw new NoSuchElementException(); |
| 824 |
|
} |
| 825 |
0 |
; |
| 826 |
|
|
| 827 |
0 |
T nextElement = this.next; |
| 828 |
0 |
this.next = null; |
| 829 |
|
|
| 830 |
0 |
return nextElement; |
| 831 |
|
} |
| 832 |
|
|
| |
|
| 44% |
Uncovered Elements: 14 (25) |
Complexity: 6 |
Complexity Density: 0.4 |
|
| 833 |
9060 |
@SuppressWarnings("unchecked")... |
| 834 |
|
private void fetchNext() |
| 835 |
|
{ |
| 836 |
9060 |
if (this.next != null) { |
| 837 |
0 |
return; |
| 838 |
|
} |
| 839 |
9067 |
while (this.idx < this.urls.length) { |
| 840 |
7 |
if (this.findOnly) { |
| 841 |
7 |
URL url = findResource(this.urls[this.idx], this.name, new HashSet<URL>(), this.previousURLs); |
| 842 |
7 |
if (url != null) { |
| 843 |
0 |
this.previousURLs.add(url); |
| 844 |
0 |
this.next = (T) url; |
| 845 |
0 |
return; |
| 846 |
|
} |
| 847 |
|
} else { |
| 848 |
0 |
ResourceHandle h = |
| 849 |
|
getResource(this.urls[this.idx], this.name, new HashSet<URL>(), this.previousURLs); |
| 850 |
0 |
if (h != null) { |
| 851 |
0 |
this.previousURLs.add(h.getURL()); |
| 852 |
0 |
this.next = (T) h; |
| 853 |
0 |
return; |
| 854 |
|
} |
| 855 |
|
} |
| 856 |
7 |
this.idx++; |
| 857 |
|
} |
| 858 |
|
} |
| 859 |
|
} |
| 860 |
|
} |