-
-
Notifications
You must be signed in to change notification settings - Fork 435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Detect dependencies by listing MANIFEST.MF files at runtime #2538
Changes from 3 commits
417eff2
d0462fe
7f45841
dbedad9
60e3a37
2c40f75
2f4d9bc
17dee9e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package io.sentry.internal.modules; | ||
|
||
import static io.sentry.util.ClassLoaderUtils.classLoaderOrDefault; | ||
|
||
import io.sentry.ILogger; | ||
import io.sentry.SentryLevel; | ||
import java.io.IOException; | ||
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They're not sorted when sending the JSON but show up sorted in the UI. Guess that's fine then? |
||
|
||
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 (IOException e) { | ||
adinauer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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; | ||
} | ||
} | ||
} |
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; | ||
} | ||
} | ||
} |
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) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m
: I think we don't need this check, 'cause this was the codepath for JVM platforms anyway, so I think CompositeModulesLoader covers everything alreadyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK Great, I presume this also won't cause problems with other SDKs using the Java SDK, correct?