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();
}
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;
}
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();
}
}
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());
}
}
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();
}
}
Aggregations