-
-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect dependencies by listing MANIFEST.MF files at runtime (#2538)
Co-authored-by: Roman Zavarnitsyn <rom4ek93@gmail.com> Co-authored-by: Sentry Github Bot <bot+github-bot@sentry.io>
- Loading branch information
1 parent
319f191
commit d5473b8
Showing
11 changed files
with
315 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
sentry/src/main/java/io/sentry/internal/modules/CompositeModulesLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.sentry.internal.modules; | ||
|
||
import io.sentry.ILogger; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
@ApiStatus.Experimental | ||
@ApiStatus.Internal | ||
public final class CompositeModulesLoader extends ModulesLoader { | ||
|
||
private final List<IModulesLoader> loaders; | ||
|
||
public CompositeModulesLoader( | ||
final @NotNull List<IModulesLoader> loaders, final @NotNull ILogger logger) { | ||
super(logger); | ||
this.loaders = loaders; | ||
} | ||
|
||
@Override | ||
protected Map<String, String> loadModules() { | ||
final @NotNull TreeMap<String, String> allModules = new TreeMap<>(); | ||
|
||
for (IModulesLoader loader : this.loaders) { | ||
final @Nullable Map<String, String> modules = loader.getOrLoadModules(); | ||
if (modules != null) { | ||
allModules.putAll(modules); | ||
} | ||
} | ||
|
||
return allModules; | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
sentry/src/main/java/io/sentry/internal/modules/ManifestModulesLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package io.sentry.internal.modules; | ||
|
||
import static io.sentry.util.ClassLoaderUtils.classLoaderOrDefault; | ||
|
||
import io.sentry.ILogger; | ||
import io.sentry.SentryLevel; | ||
import java.net.URL; | ||
import java.util.ArrayList; | ||
import java.util.Enumeration; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
@ApiStatus.Experimental | ||
@ApiStatus.Internal | ||
public final class ManifestModulesLoader extends ModulesLoader { | ||
private final Pattern URL_LIB_PATTERN = Pattern.compile(".*/(.+)!/META-INF/MANIFEST.MF"); | ||
private final Pattern NAME_AND_VERSION = Pattern.compile("(.*?)-(\\d+\\.\\d+.*).jar"); | ||
private final ClassLoader classLoader; | ||
|
||
public ManifestModulesLoader(final @NotNull ILogger logger) { | ||
this(ManifestModulesLoader.class.getClassLoader(), logger); | ||
} | ||
|
||
ManifestModulesLoader(final @Nullable ClassLoader classLoader, final @NotNull ILogger logger) { | ||
super(logger); | ||
this.classLoader = classLoaderOrDefault(classLoader); | ||
} | ||
|
||
@Override | ||
protected Map<String, String> loadModules() { | ||
final @NotNull Map<String, String> modules = new HashMap<>(); | ||
List<Module> detectedModules = detectModulesViaManifestFiles(); | ||
|
||
for (Module module : detectedModules) { | ||
modules.put(module.name, module.version); | ||
} | ||
|
||
return modules; | ||
} | ||
|
||
private @NotNull List<Module> detectModulesViaManifestFiles() { | ||
final @NotNull List<Module> modules = new ArrayList<>(); | ||
try { | ||
final @NotNull Enumeration<URL> manifestUrls = | ||
classLoader.getResources("META-INF/MANIFEST.MF"); | ||
while (manifestUrls.hasMoreElements()) { | ||
final @NotNull URL manifestUrl = manifestUrls.nextElement(); | ||
final @Nullable String originalName = extractDependencyNameFromUrl(manifestUrl); | ||
final @Nullable Module module = convertOriginalNameToModule(originalName); | ||
if (module != null) { | ||
modules.add(module); | ||
} | ||
} | ||
} catch (Throwable e) { | ||
logger.log(SentryLevel.ERROR, "Unable to detect modules via manifest files.", e); | ||
} | ||
|
||
return modules; | ||
} | ||
|
||
private @Nullable Module convertOriginalNameToModule(@Nullable String originalName) { | ||
if (originalName == null) { | ||
return null; | ||
} | ||
|
||
final @NotNull Matcher matcher = NAME_AND_VERSION.matcher(originalName); | ||
if (matcher.matches() && matcher.groupCount() == 2) { | ||
@NotNull String moduleName = matcher.group(1); | ||
@NotNull String moduleVersion = matcher.group(2); | ||
return new Module(moduleName, moduleVersion); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private @Nullable String extractDependencyNameFromUrl(final @NotNull URL url) { | ||
final @NotNull String urlString = url.toString(); | ||
final @NotNull Matcher matcher = URL_LIB_PATTERN.matcher(urlString); | ||
if (matcher.matches() && matcher.groupCount() == 1) { | ||
return matcher.group(1); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private static final class Module { | ||
private final @NotNull String name; | ||
private final @NotNull String version; | ||
|
||
public Module(final @NotNull String name, final @NotNull String version) { | ||
this.name = name; | ||
this.version = version; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.sentry.util; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public final class ClassLoaderUtils { | ||
|
||
public static @NotNull ClassLoader classLoaderOrDefault(final @Nullable ClassLoader classLoader) { | ||
// bootstrap classloader is represented as null, so using system classloader instead | ||
if (classLoader == null) { | ||
return ClassLoader.getSystemClassLoader(); | ||
} else { | ||
return classLoader; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
sentry/src/test/java/io/sentry/internal/modules/CompositeModulesLoaderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.sentry.internal.modules | ||
|
||
import io.sentry.ILogger | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.verify | ||
import org.mockito.kotlin.verifyNoMoreInteractions | ||
import org.mockito.kotlin.whenever | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class CompositeModulesLoaderTest { | ||
|
||
@Test | ||
fun `reads modules from multiple loaders and caches result`() { | ||
val logger = mock<ILogger>() | ||
val loader1 = mock<IModulesLoader>() | ||
val loader2 = mock<IModulesLoader>() | ||
|
||
whenever(loader1.orLoadModules).thenReturn(mapOf("spring-core" to "6.0.0")) | ||
whenever(loader2.orLoadModules).thenReturn(mapOf("spring-webmvc" to "6.0.2")) | ||
|
||
val sut = CompositeModulesLoader(listOf(loader1, loader2), logger) | ||
|
||
assertEquals( | ||
mapOf( | ||
"spring-core" to "6.0.0", | ||
"spring-webmvc" to "6.0.2" | ||
), | ||
sut.orLoadModules | ||
) | ||
|
||
verify(loader1).orLoadModules | ||
verify(loader2).orLoadModules | ||
|
||
assertEquals( | ||
mapOf( | ||
"spring-core" to "6.0.0", | ||
"spring-webmvc" to "6.0.2" | ||
), | ||
sut.orLoadModules | ||
) | ||
|
||
verifyNoMoreInteractions(loader1, loader2) | ||
} | ||
} |
Oops, something went wrong.