Abstract class that scans through the class path represented by a ClassLoader and calls #scanDirectory and #scanJarFile for directories and jar files on the class path respectively.
| 352 | */ |
| 353 | |
| 354 | abstract static class Scanner { |
| 355 | |
| 356 | // We only scan each file once independent of the classloader that resource might be associated |
| 357 | // with. |
| 358 | private final Set<File> scannedUris = Sets.newHashSet(); |
| 359 | |
| 360 | public final void scan(ClassLoader classloader) throws IOException { |
| 361 | for (Map.Entry<File, ClassLoader> entry : getClassPathEntries(classloader).entrySet()) { |
| 362 | scan(entry.getKey(), entry.getValue()); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | /** Called when a directory is scanned for resource files. */ |
| 367 | |
| 368 | |
| 369 | protected abstract void scanDirectory(ClassLoader loader, File directory) throws IOException; |
| 370 | |
| 371 | /** Called when a jar file is scanned for resource entries. */ |
| 372 | |
| 373 | |
| 374 | protected abstract void scanJarFile(ClassLoader loader, JarFile file) throws IOException; |
| 375 | |
| 376 | |
| 377 | @VisibleForTesting |
| 378 | final void scan(File file, ClassLoader classloader) throws IOException { |
| 379 | if (scannedUris.add(file.getCanonicalFile())) { |
| 380 | scanFrom(file, classloader); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | private void scanFrom(File file, ClassLoader classloader) throws IOException { |
| 385 | if (!file.exists()) { |
| 386 | return; |
| 387 | } |
| 388 | if (file.isDirectory()) { |
| 389 | scanDirectory(classloader, file); |
| 390 | } else { |
| 391 | scanJar(file, classloader); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | private void scanJar(File file, ClassLoader classloader) throws IOException { |
| 396 | JarFile jarFile; |
| 397 | try { |
| 398 | jarFile = new JarFile(file); |
| 399 | } catch (IOException e) { |
| 400 | // Not a jar file |
| 401 | return; |
| 402 | } |
| 403 | try { |
| 404 | for (File path : getClassPathFromManifest(file, jarFile.getManifest())) { |
| 405 | scan(path, classloader); |
| 406 | } |
| 407 | scanJarFile(classloader, jarFile); |
| 408 | } finally { |
| 409 | try { |
| 410 | jarFile.close(); |
| 411 | } catch (IOException ignored) {} |
nothing calls this directly
no test coverage detected