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.
| 324 | * respectively. |
| 325 | */ |
| 326 | abstract static class Scanner { |
| 327 | |
| 328 | // We only scan each file once independent of the classloader that resource might be associated |
| 329 | // with. |
| 330 | private final Set<File> scannedUris = Sets.newHashSet(); |
| 331 | |
| 332 | public final void scan(ClassLoader classloader) throws IOException { |
| 333 | for (Map.Entry<File, ClassLoader> entry : getClassPathEntries(classloader).entrySet()) { |
| 334 | scan(entry.getKey(), entry.getValue()); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | /** Called when a directory is scanned for resource files. */ |
| 339 | protected abstract void scanDirectory(ClassLoader loader, File directory) throws IOException; |
| 340 | |
| 341 | /** Called when a jar file is scanned for resource entries. */ |
| 342 | protected abstract void scanJarFile(ClassLoader loader, JarFile file) throws IOException; |
| 343 | |
| 344 | @VisibleForTesting |
| 345 | final void scan(File file, ClassLoader classloader) throws IOException { |
| 346 | if (scannedUris.add(file.getCanonicalFile())) { |
| 347 | scanFrom(file, classloader); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | private void scanFrom(File file, ClassLoader classloader) throws IOException { |
| 352 | if (!file.exists()) { |
| 353 | return; |
| 354 | } |
| 355 | if (file.isDirectory()) { |
| 356 | scanDirectory(classloader, file); |
| 357 | } else { |
| 358 | scanJar(file, classloader); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | private void scanJar(File file, ClassLoader classloader) throws IOException { |
| 363 | JarFile jarFile; |
| 364 | try { |
| 365 | jarFile = new JarFile(file); |
| 366 | } catch (IOException e) { |
| 367 | // Not a jar file |
| 368 | return; |
| 369 | } |
| 370 | try { |
| 371 | for (File path : getClassPathFromManifest(file, jarFile.getManifest())) { |
| 372 | scan(path, classloader); |
| 373 | } |
| 374 | scanJarFile(classloader, jarFile); |
| 375 | } finally { |
| 376 | try { |
| 377 | jarFile.close(); |
| 378 | } catch (IOException ignored) { |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | /** |
nothing calls this directly
no test coverage detected