Search in sources :

Example 1 with ResourceList

use of io.github.classgraph.ResourceList in project sda-dropwizard-commons by SDA-SE.

the class DuplicateClassesTest method checkForDuplicateClasses.

/**
 * This test finds and logs duplicate classes in the classpath. Such duplicates appear for example
 * when some libraries repackage standard functionality or APIs like javax.* or jakarta.* or when
 * providers change their Maven GAV without changing the internal package structure. In both cases
 * the dependency management can't identify the duplication.
 *
 * <p>When this test is not ignored any more by the assumption, the assumption can be turned into
 * an assertion.
 *
 * <p>This approach of finding duplicates is inspired by <a
 * href="https://stackoverflow.com/a/52639079">Stackoverflow</a>
 */
@Test
public void checkForDuplicateClasses() {
    int numberOfDuplicates = 0;
    ResourceList allResourcesInClasspath = new ClassGraph().scan().getAllResources();
    ResourceList classFilesInClasspath = allResourcesInClasspath.filter(resource -> !resource.getURL().toString().contains("/.gradle/wrapper/")).classFilesOnly();
    for (Map.Entry<String, ResourceList> duplicate : classFilesInClasspath.findDuplicatePaths()) {
        if ("module-info.class".equals(duplicate.getKey())) {
            continue;
        }
        // Classfile path
        LOG.warn("Class files path: {}", duplicate.getKey());
        numberOfDuplicates++;
        for (Resource res : duplicate.getValue()) {
            // Resource URL, showing classpath element
            LOG.warn(" -> {}", res.getURL());
        }
    }
    LOG.warn("Found {} duplicates.", numberOfDuplicates);
    assertThat(numberOfDuplicates).describedAs("already saw only %s duplicate classes but now there are %s", LAST_SEEN_NUMBER_OF_DUPLICATES, numberOfDuplicates).isLessThanOrEqualTo(LAST_SEEN_NUMBER_OF_DUPLICATES);
    assumeThat(numberOfDuplicates).describedAs("expecting no duplicate classes but found %s", numberOfDuplicates).isZero();
}
Also used : ResourceList(io.github.classgraph.ResourceList) ClassGraph(io.github.classgraph.ClassGraph) Resource(io.github.classgraph.Resource) Logger(org.slf4j.Logger) Assumptions.assumeThat(org.assertj.core.api.Assumptions.assumeThat) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Map(java.util.Map) LoggerFactory(org.slf4j.LoggerFactory) Test(org.junit.Test) ResourceList(io.github.classgraph.ResourceList) ClassGraph(io.github.classgraph.ClassGraph) Resource(io.github.classgraph.Resource) Map(java.util.Map) Test(org.junit.Test)

Example 2 with ResourceList

use of io.github.classgraph.ResourceList in project tsunami-security-scanner-plugins by google.

the class ResourceFingerprintLoader method loadFingerprints.

@Override
public ImmutableMap<SoftwareIdentity, FingerprintData> loadFingerprints() throws IOException {
    Stopwatch loadTimeStopwatch = Stopwatch.createStarted();
    ResourceList fingerprintsResources = scanResult.getResourcesMatchingPattern(FINGERPRINTS_RESOURCE_PATTERN);
    ImmutableMap.Builder<SoftwareIdentity, FingerprintData> fingerprintsBuilder = ImmutableMap.builder();
    for (Resource resource : fingerprintsResources) {
        logger.atInfo().log("Loading fingerprints from resource %s.", resource.getPath());
        Fingerprints fingerprints = Fingerprints.parseFrom(resource.load());
        fingerprintsBuilder.put(fingerprints.getSoftwareIdentity(), FingerprintData.fromProto(fingerprints));
    }
    ImmutableMap<SoftwareIdentity, FingerprintData> fingerprints = fingerprintsBuilder.build();
    logger.atInfo().log("Finished loading %s web fingerprints data in %s.", fingerprints.size(), loadTimeStopwatch.stop());
    return fingerprints;
}
Also used : ResourceList(io.github.classgraph.ResourceList) Fingerprints(com.google.tsunami.plugins.fingerprinters.web.proto.Fingerprints) Stopwatch(com.google.common.base.Stopwatch) SoftwareIdentity(com.google.tsunami.plugins.fingerprinters.web.proto.SoftwareIdentity) Resource(io.github.classgraph.Resource) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 3 with ResourceList

use of io.github.classgraph.ResourceList in project mockserver by mock-server.

the class FileReader method expandFilePathGlobs.

public static List<String> expandFilePathGlobs(String filePath) {
    if (isNotBlank(filePath) && filePath.contains(".")) {
        if (filePath.contains("*") || filePath.contains("?")) {
            List<String> expandedFilePaths = new ArrayList<>();
            PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + filePath);
            Finder finder = new Finder(filePath, pathMatcher);
            try {
                String startingDir = filePath.contains("/") ? StringUtils.substringBeforeLast(StringUtils.substringBefore(filePath, "*"), "/") : ".";
                if (new File(startingDir).exists()) {
                    Files.walkFileTree(new File(startingDir).toPath(), EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, finder);
                    expandedFilePaths.addAll(finder.getMatchingPaths());
                } else {
                    new MockServerLogger(FileReader.class).logEvent(new LogEntry().setLogLevel(ERROR).setMessageFormat("can't find directory:{}for file path:{}").setArguments(startingDir, filePath));
                }
            } catch (Throwable throwable) {
                new MockServerLogger(FileReader.class).logEvent(new LogEntry().setLogLevel(ERROR).setMessageFormat("exception finding files for file path:{}").setArguments(filePath).setThrowable(throwable));
                throw new RuntimeException(throwable);
            }
            try (ScanResult result = new ClassGraph().scan()) {
                ResourceList resources = result.getResourcesWithExtension(StringUtils.substringAfterLast(filePath, "."));
                expandedFilePaths.addAll(resources.stream().map(Resource::getPath).filter(path -> pathMatcher.matches(new File(path).toPath())).collect(Collectors.toList()));
            }
            return expandedFilePaths.stream().sorted().collect(Collectors.toList());
        } else {
            return Collections.singletonList(filePath);
        }
    } else {
        return Collections.emptyList();
    }
}
Also used : ScanResult(io.github.classgraph.ScanResult) MockServerLogger(org.mockserver.logging.MockServerLogger) ClassGraph(io.github.classgraph.ClassGraph) ArrayList(java.util.ArrayList) ResourceList(io.github.classgraph.ResourceList) LogEntry(org.mockserver.log.model.LogEntry)

Example 4 with ResourceList

use of io.github.classgraph.ResourceList in project karate by karatelabs.

the class ResourceUtils method getResource.

public static Resource getResource(File workingDir, String path) {
    if (path.startsWith(Resource.CLASSPATH_COLON)) {
        path = removePrefix(path);
        File file = classPathToFile(path);
        if (file != null) {
            return new FileResource(file, true, path);
        }
        List<Resource> resources = new ArrayList<>();
        synchronized (SCAN_RESULT) {
            ResourceList rl = SCAN_RESULT.getResourcesWithPath(path);
            if (rl == null) {
                rl = ResourceList.emptyList();
            }
            rl.forEachByteArrayIgnoringIOException((res, bytes) -> {
                URI uri = res.getURI();
                if ("file".equals(uri.getScheme())) {
                    File found = Paths.get(uri).toFile();
                    resources.add(new FileResource(found, true, res.getPath()));
                } else {
                    resources.add(new JarResource(bytes, res.getPath(), uri));
                }
            });
        }
        if (resources.isEmpty()) {
            throw new RuntimeException("not found: " + path);
        }
        return resources.get(0);
    } else {
        File file = new File(removePrefix(path));
        if (!file.exists()) {
            throw new RuntimeException("not found: " + path);
        }
        Path relativePath = workingDir.toPath().relativize(file.getAbsoluteFile().toPath());
        return new FileResource(file, false, relativePath.toString());
    }
}
Also used : Path(java.nio.file.Path) ResourceList(io.github.classgraph.ResourceList) ArrayList(java.util.ArrayList) File(java.io.File) URI(java.net.URI)

Example 5 with ResourceList

use of io.github.classgraph.ResourceList in project mockserver by mock-server.

the class FilePath method expandFilePathGlobs.

public static List<String> expandFilePathGlobs(String filePath) {
    if (isNotBlank(filePath) && filePath.contains(".")) {
        if (filePath.contains("*") || filePath.contains("?")) {
            List<String> expandedFilePaths = new ArrayList<>();
            PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + filePath);
            Finder finder = new Finder(filePath, pathMatcher);
            try {
                String startingDir = filePath.contains("/") ? StringUtils.substringBeforeLast(StringUtils.substringBefore(filePath, "*"), "/") : ".";
                if (new File(startingDir).exists()) {
                    Files.walkFileTree(new File(startingDir).toPath(), EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, finder);
                    expandedFilePaths.addAll(finder.getMatchingPaths());
                } else {
                    new MockServerLogger(FileReader.class).logEvent(new LogEntry().setLogLevel(ERROR).setMessageFormat("can't find directory:{}for file path:{}").setArguments(startingDir, filePath));
                }
            } catch (Throwable throwable) {
                new MockServerLogger(FileReader.class).logEvent(new LogEntry().setLogLevel(ERROR).setMessageFormat("exception finding files for file path:{}").setArguments(filePath).setThrowable(throwable));
                throw new RuntimeException(throwable);
            }
            try (ScanResult result = new ClassGraph().scan()) {
                ResourceList resources = result.getResourcesWithExtension(StringUtils.substringAfterLast(filePath, "."));
                expandedFilePaths.addAll(resources.stream().map(Resource::getPath).filter(path -> pathMatcher.matches(new File(path).toPath())).collect(Collectors.toList()));
            }
            return expandedFilePaths.stream().sorted().collect(Collectors.toList());
        } else {
            return Collections.singletonList(filePath);
        }
    } else {
        return Collections.emptyList();
    }
}
Also used : ScanResult(io.github.classgraph.ScanResult) MockServerLogger(org.mockserver.logging.MockServerLogger) ClassGraph(io.github.classgraph.ClassGraph) ArrayList(java.util.ArrayList) ResourceList(io.github.classgraph.ResourceList) File(java.io.File) LogEntry(org.mockserver.log.model.LogEntry)

Aggregations

ResourceList (io.github.classgraph.ResourceList)16 ScanResult (io.github.classgraph.ScanResult)12 ClassGraph (io.github.classgraph.ClassGraph)10 Test (org.junit.jupiter.api.Test)8 ArrayList (java.util.ArrayList)6 Resource (io.github.classgraph.Resource)4 File (java.io.File)3 URI (java.net.URI)2 URL (java.net.URL)2 URLClassLoader (java.net.URLClassLoader)2 Path (java.nio.file.Path)2 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 LogEntry (org.mockserver.log.model.LogEntry)2 MockServerLogger (org.mockserver.logging.MockServerLogger)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Stopwatch (com.google.common.base.Stopwatch)1 ImmutableMap (com.google.common.collect.ImmutableMap)1