Skip to content
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

Issue #117: Develop ClassLoader Agent #136

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions metricshub-classloader-agent/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.sentrysoftware</groupId>
<artifactId>metricshub-parent</artifactId>
<version>0.9.02-SNAPSHOT</version>
</parent>

<artifactId>metricshub-classloader-agent</artifactId>
<name>MetricsHub ClassLoader Agent</name>
<description>MetricsHub ClassLoader Agent for dynamic class loading</description>

<properties>
<agentClass>org.sentrysoftware.metricshub.classloader.agent.ClassLoaderAgent</agentClass>
</properties>

<dependencies>

</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Premain-Class>${agentClass}</Premain-Class>
<Agent-Class>${agentClass}</Agent-Class>
</manifestEntries>
</transformer>
</transformers>
<minimizeJar>false</minimizeJar>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.sentrysoftware.metricshub.classloader.agent;

import java.lang.instrument.Instrumentation;
import java.util.Optional;
import java.util.jar.JarFile;

/**
* Class Loader Agent for dynamic class loading. (Needed for module-winrm and
* java 11)
*
* @link https://cgjennings.ca/articles/java-9-dynamic-jar-loading/
*/
public class ClassLoaderAgent {

private ClassLoaderAgent() {}

private static Instrumentation inst;

/**
* Called by the JRE. <em>Do not call this method from user code.</em>
* <p>
*
* @param unusedArgs currently ignored
* @param instrumentation provided by the JRE
*/
public static void premain(final String unusedArgs, final Instrumentation instrumentation) {
agentmain(unusedArgs, instrumentation);
}

/**
* Called by the JRE. <em>Do not call this method from user code.</em>
* <p>
*
* @param unusedArgs currently ignored
* @param instrumentation provided by the JRE
*/
public static void agentmain(final String unusedArgs, final Instrumentation instrumentation) {
inst = instrumentation;
}

/**
* Adds a JAR file to the system class loader's classpath.
*
* @param jarFile the JAR file to add to the classpath
*/
public static synchronized void addToClassPath(final JarFile jarFile) {
getInstrumentation().ifPresent(instrumentation -> instrumentation.appendToSystemClassLoaderSearch(jarFile));
}

/**
* Retrieves the current instrumentation instance, if available.
*
* @return an {@code Optional} containing the current instrumentation instance, or an empty {@code Optional} if not available
*/
static Optional<Instrumentation> getInstrumentation() {
return Optional.ofNullable(inst);
}
}
4 changes: 4 additions & 0 deletions metricshub-engine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
<description>MetricsHub Monitoring Engine</description>

<dependencies>
<dependency>
<groupId>org.sentrysoftware</groupId>
<artifactId>metricshub-classloader-agent</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ public class HostConfiguration {
/**
* Determine the accepted sources that can be executed using the current engine configuration
*
* @param isLocalhost Whether the host should be localhost or not
* @param isLocalhost Whether the host should be localhost or not.
* @param extensionManager Where all the extensions are managed.
* @return {@link Set} of accepted source types
*/
public Set<Class<? extends Source>> determineAcceptedSources(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@
import java.util.List;
import java.util.ServiceLoader;
import java.util.ServiceLoader.Provider;
import java.util.jar.JarFile;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.sentrysoftware.metricshub.classloader.agent.ClassLoaderAgent;

/**
* Manages the loading of extensions from a specified directory and produces an {@link ExtensionManager}.
Expand Down Expand Up @@ -73,7 +75,7 @@ public ExtensionManager load() throws IOException {
final File jarFile = extensionJars[i];

// Make sure to add the jar to the system class loader search

ClassLoaderAgent.addToClassPath(new JarFile(jarFile));
urls[i] = jarFile.toURI().toURL();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,6 @@ void testCheckHealth() throws Exception {
clientsExecutorMock,
extensionManager
);

assertDoesNotThrow(() -> healthCheckStrategy.run());
}

Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
</properties>

<modules>
<module>metricshub-classloader-agent</module>
<module>metricshub-engine</module>
<module>metricshub-it-common</module>
<module>metricshub-snmp-extension</module>
Expand Down Expand Up @@ -221,6 +222,11 @@
<artifactId>metricshub-engine</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.sentrysoftware</groupId>
<artifactId>metricshub-classloader-agent</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.sentrysoftware</groupId>
<artifactId>metricshub-it-common</artifactId>
Expand Down