static ResourceInfo of(String resourceName, ClassLoader loader) { if (resourceName.endsWith(CLASS_FILE_NAME_EXTENSION)) { return new ClassInfo(resourceName, loader); } else { return new ResourceInfo(resourceName, loader); } }
ImmutableSet<ResourceInfo> getResources() { ImmutableSet.Builder<ResourceInfo> builder = ImmutableSet.builder(); for (Entry<ClassLoader, String> entry : resources.entries()) { builder.add(ResourceInfo.of(entry.getValue(), entry.getKey())); } return builder.build(); }
public static void unpackDummyResources(String prefix, Path output) throws IOException { ClassPath classPath = ClassPath.from(TestData.class.getClassLoader()); Map<String, URL> files = classPath.getResources().stream() .filter(info -> info.getResourceName().startsWith(prefix)) .collect(Collectors.toMap( info -> info.getResourceName().substring(prefix.length()), ClassPath.ResourceInfo::url) ); files.forEach((name, url) -> { Path file = output.resolve(name); try (InputStream is = url.openStream()) { Files.copy(is, file); } catch (IOException e) { throw new RuntimeException(String.format("name: %s, url: %s", name, url), e); } }); }
assertThat(resource.getResourceName()).doesNotContain("com/google/common/reflect/");
@AndroidIncompatible // Path (for symlink creation) public void testScanDirectory_symlinkCycle() throws IOException { ClassLoader loader = ClassPathTest.class.getClassLoader(); // directory with a cycle, // /root // /left // /[sibling -> right] // /right // /[sibling -> left] java.nio.file.Path root = createTempDirectory("ClassPathTest"); try { java.nio.file.Path left = createDirectory(root.resolve("left")); createFile(left.resolve("some.txt")); java.nio.file.Path right = createDirectory(root.resolve("right")); createFile(right.resolve("another.txt")); createSymbolicLink(left.resolve("sibling"), right); createSymbolicLink(right.resolve("sibling"), left); ClassPath.DefaultScanner scanner = new ClassPath.DefaultScanner(); scanner.scan(root.toFile(), loader); assertEquals( ImmutableSet.of( new ResourceInfo("left/some.txt", loader), new ResourceInfo("left/sibling/another.txt", loader), new ResourceInfo("right/another.txt", loader), new ResourceInfo("right/sibling/some.txt", loader)), scanner.getResources()); } finally { deleteRecursivelyOrLog(root); } }
private void scanDirectory( File directory, ClassLoader classloader, String packagePrefix, ImmutableSet<File> ancestors) throws IOException { File canonical = directory.getCanonicalFile(); if (ancestors.contains(canonical)) { // A cycle in the filesystem, for example due to a symbolic link. return; } File[] files = directory.listFiles(); if (files == null) { logger.warning("Cannot read directory " + directory); // IO error, just skip the directory return; } ImmutableSet<File> newAncestors = ImmutableSet.<File>builder() .addAll(ancestors) .add(canonical) .build(); for (File f : files) { String name = f.getName(); if (f.isDirectory()) { scanDirectory(f, classloader, packagePrefix + name + "/", newAncestors); } else { String resourceName = packagePrefix + name; if (!resourceName.equals(JarFile.MANIFEST_NAME)) { resources.add(ResourceInfo.of(resourceName, classloader)); } } } }
private void scanDirectory( File directory, ClassLoader classloader, String packagePrefix, ImmutableSet<File> ancestors) throws IOException { File canonical = directory.getCanonicalFile(); if (ancestors.contains(canonical)) { // A cycle in the filesystem, for example due to a symbolic link. return; } File[] files = directory.listFiles(); if (files == null) { logger.warning("Cannot read directory " + directory); // IO error, just skip the directory return; } ImmutableSet<File> newAncestors = ImmutableSet.<File>builder() .addAll(ancestors) .add(canonical) .build(); for (File f : files) { String name = f.getName(); if (f.isDirectory()) { scanDirectory(f, classloader, packagePrefix + name + "/", newAncestors); } else { String resourceName = packagePrefix + name; if (!resourceName.equals(JarFile.MANIFEST_NAME)) { resources.add(ResourceInfo.of(resourceName, classloader)); } } } }
private void scanDirectory( File directory, ClassLoader classloader, String packagePrefix, ImmutableSet<File> ancestors) throws IOException { File canonical = directory.getCanonicalFile(); if (ancestors.contains(canonical)) { // A cycle in the filesystem, for example due to a symbolic link. return; } File[] files = directory.listFiles(); if (files == null) { logger.warning("Cannot read directory " + directory); // IO error, just skip the directory return; } ImmutableSet<File> newAncestors = ImmutableSet.<File>builder() .addAll(ancestors) .add(canonical) .build(); for (File f : files) { String name = f.getName(); if (f.isDirectory()) { scanDirectory(f, classloader, packagePrefix + name + "/", newAncestors); } else { String resourceName = packagePrefix + name; if (!resourceName.equals(JarFile.MANIFEST_NAME)) { resources.add(ResourceInfo.of(resourceName, classloader)); } } } }
private void scanDirectory( File directory, ClassLoader classloader, String packagePrefix, ImmutableSet<File> ancestors) throws IOException { File canonical = directory.getCanonicalFile(); if (ancestors.contains(canonical)) { // A cycle in the filesystem, for example due to a symbolic link. return; } File[] files = directory.listFiles(); if (files == null) { logger.warning("Cannot read directory " + directory); // IO error, just skip the directory return; } ImmutableSet<File> newAncestors = ImmutableSet.<File>builder() .addAll(ancestors) .add(canonical) .build(); for (File f : files) { String name = f.getName(); if (f.isDirectory()) { scanDirectory(f, classloader, packagePrefix + name + "/", newAncestors); } else { String resourceName = packagePrefix + name; if (!resourceName.equals(JarFile.MANIFEST_NAME)) { resources.add(ResourceInfo.of(resourceName, classloader)); } } } }
private void scanJar(File file, ClassLoader classloader) throws IOException { JarFile jarFile; try { jarFile = new JarFile(file); } catch (IOException e) { // Not a jar file return; } try { for (URI uri : getClassPathFromManifest(file, jarFile.getManifest())) { scan(uri, classloader); } Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); if (entry.isDirectory() || entry.getName().equals(JarFile.MANIFEST_NAME)) { continue; } resources.add(ResourceInfo.of(entry.getName(), classloader)); } } finally { try { jarFile.close(); } catch (IOException ignored) {} } }
private void scanJar(File file, ClassLoader classloader) throws IOException { JarFile jarFile; try { jarFile = new JarFile(file); } catch (IOException e) { // Not a jar file return; } try { for (URI uri : getClassPathFromManifest(file, jarFile.getManifest())) { scan(uri, classloader); } Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); if (entry.isDirectory() || entry.getName().equals(JarFile.MANIFEST_NAME)) { continue; } resources.add(ResourceInfo.of(entry.getName(), classloader)); } } finally { try { jarFile.close(); } catch (IOException ignored) {} } }
private void scanDirectory( File directory, ClassLoader classloader, String packagePrefix, ImmutableSet<File> ancestors) throws IOException { File canonical = directory.getCanonicalFile(); if (ancestors.contains(canonical)) { // A cycle in the filesystem, for example due to a symbolic link. return; } File[] files = directory.listFiles(); if (files == null) { logger.warning("Cannot read directory " + directory); // IO error, just skip the directory return; } ImmutableSet<File> newAncestors = ImmutableSet.<File>builder() .addAll(ancestors) .add(canonical) .build(); for (File f : files) { String name = f.getName(); if (f.isDirectory()) { scanDirectory(f, classloader, packagePrefix + name + "/", newAncestors); } else { String resourceName = packagePrefix + name; if (!resourceName.equals(JarFile.MANIFEST_NAME)) { resources.add(ResourceInfo.of(resourceName, classloader)); } } } }
private void scanDirectory( File directory, ClassLoader classloader, String packagePrefix, ImmutableSet<File> ancestors) throws IOException { File canonical = directory.getCanonicalFile(); if (ancestors.contains(canonical)) { // A cycle in the filesystem, for example due to a symbolic link. return; } File[] files = directory.listFiles(); if (files == null) { logger.warning("Cannot read directory " + directory); // IO error, just skip the directory return; } ImmutableSet<File> newAncestors = ImmutableSet.<File>builder() .addAll(ancestors) .add(canonical) .build(); for (File f : files) { String name = f.getName(); if (f.isDirectory()) { scanDirectory(f, classloader, packagePrefix + name + "/", newAncestors); } else { String resourceName = packagePrefix + name; if (!resourceName.equals(JarFile.MANIFEST_NAME)) { resources.add(ResourceInfo.of(resourceName, classloader)); } } } }
private static void browseJar( File file, ClassLoader classloader, ImmutableSet.Builder<ResourceInfo> resources) throws IOException { JarFile jarFile; try { jarFile = new JarFile(file); } catch (IOException e) { // Not a jar file return; } try { for (URI uri : getClassPathFromManifest(file, jarFile.getManifest())) { browse(uri, classloader, resources); } Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); if (entry.isDirectory() || entry.getName().startsWith("META-INF/")) { continue; } resources.add(ResourceInfo.of(entry.getName(), classloader)); } } finally { try { jarFile.close(); } catch (IOException ignored) {} } }
private void scanJar(File file, ClassLoader classloader) throws IOException { JarFile jarFile; try { jarFile = new JarFile(file); } catch (IOException e) { // Not a jar file return; } try { for (URI uri : getClassPathFromManifest(file, jarFile.getManifest())) { scan(uri, classloader); } Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); if (entry.isDirectory() || entry.getName().equals(JarFile.MANIFEST_NAME)) { continue; } resources.add(ResourceInfo.of(entry.getName(), classloader)); } } finally { try { jarFile.close(); } catch (IOException ignored) {} } }
private void scanJar(File file, ClassLoader classloader) throws IOException { JarFile jarFile; try { jarFile = new JarFile(file); } catch (IOException e) { // Not a jar file return; } try { for (URI uri : getClassPathFromManifest(file, jarFile.getManifest())) { scan(uri, classloader); } Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); if (entry.isDirectory() || entry.getName().equals(JarFile.MANIFEST_NAME)) { continue; } resources.add(ResourceInfo.of(entry.getName(), classloader)); } } finally { try { jarFile.close(); } catch (IOException ignored) {} } }
private void scanJar(File file, ClassLoader classloader) throws IOException { JarFile jarFile; try { jarFile = new JarFile(file); } catch (IOException e) { // Not a jar file return; } try { for (URI uri : getClassPathFromManifest(file, jarFile.getManifest())) { scan(uri, classloader); } Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); if (entry.isDirectory() || entry.getName().equals(JarFile.MANIFEST_NAME)) { continue; } resources.add(ResourceInfo.of(entry.getName(), classloader)); } } finally { try { jarFile.close(); } catch (IOException ignored) {} } }
private void scanJar(File file, ClassLoader classloader) throws IOException { JarFile jarFile; try { jarFile = new JarFile(file); } catch (IOException e) { // Not a jar file return; } try { for (URI uri : getClassPathFromManifest(file, jarFile.getManifest())) { scan(uri, classloader); } Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); if (entry.isDirectory() || entry.getName().equals(JarFile.MANIFEST_NAME)) { continue; } resources.add(ResourceInfo.of(entry.getName(), classloader)); } } finally { try { jarFile.close(); } catch (IOException ignored) {} } }
/** * Unpack report face to given directory. * * @param outputDirectory the output directory to unpack face. * @throws IOException if any occurs. */ private static void unpackFace(File outputDirectory) throws IOException { ClassLoader loader = AllureMain.class.getClassLoader(); for (ClassPath.ResourceInfo info : ClassPath.from(loader).getResources()) { Matcher matcher = REPORT_RESOURCE_PATTERN.matcher(info.getResourceName()); if (matcher.find()) { String resourcePath = matcher.group(1); File dest = new File(outputDirectory, resourcePath); Files.createParentDirs(dest); try (FileOutputStream output = new FileOutputStream(dest); InputStream input = info.url().openStream()) { IOUtils.copy(input, output); LOGGER.debug("{} successfully copied.", resourcePath); } } } } }
@AndroidIncompatible // Path (for symlink creation) public void testScanDirectory_symlinkToRootCycle() throws IOException { ClassLoader loader = ClassPathTest.class.getClassLoader(); // directory with a cycle, // /root // /child // /[grandchild -> root] java.nio.file.Path root = createTempDirectory("ClassPathTest"); try { createFile(root.resolve("some.txt")); java.nio.file.Path child = createDirectory(root.resolve("child")); createSymbolicLink(child.resolve("grandchild"), root); ClassPath.DefaultScanner scanner = new ClassPath.DefaultScanner(); scanner.scan(root.toFile(), loader); assertEquals(ImmutableSet.of(new ResourceInfo("some.txt", loader)), scanner.getResources()); } finally { deleteRecursivelyOrLog(root); } }