Skip to content

Commit

Permalink
[WFCORE-4748] Allow the MODULE_OPTS to be set and if an -javaagent: i…
Browse files Browse the repository at this point in the history
…s found in the options automatically add jboss-modules as an agent to the VM.

https://issues.redhat.com/browse/WFCORE-4748
  • Loading branch information
jamezp committed Apr 3, 2020
1 parent 2d1449e commit 34598d5
Show file tree
Hide file tree
Showing 16 changed files with 565 additions and 63 deletions.
12 changes: 10 additions & 2 deletions core-feature-pack/src/main/resources/content/bin/standalone.bat
Expand Up @@ -270,10 +270,18 @@ if not "%PRESERVE_JAVA_OPT%" == "true" (


rem Set the module options
set "MODULE_OPTS="
set "MODULE_OPTS=%MODULE_OPTS%"
if "%SECMGR%" == "true" (
set "MODULE_OPTS=-secmgr"
set "MODULE_OPTS=%MODULE_OPTS% -secmgr"
)
setlocal EnableDelayedExpansion
rem Add -client to the JVM options, if supported (32 bit VM), and not overridden
echo "!MODULE_OPTS!" | findstr /I \-javaagent: > nul
if not errorlevel == 1 (
set AGENT_PARAM=-javaagent:"!JBOSS_HOME!\jboss-modules.jar"
set "JAVA_OPTS=!AGENT_PARAM! !JAVA_OPTS!"
)
setlocal DisableDelayedExpansion

echo ===============================================================================
echo.
Expand Down
Expand Up @@ -84,3 +84,6 @@ fi

# Uncomment and edit to use a custom java.security file to override all the Java security properties
#JAVA_OPTS="$JAVA_OPTS -Djava.security.properties==/path/to/custom/java.security"

# Uncomment to add a Java agent
# MODULE_OPTS="-javaagent:agent.jar"
Expand Up @@ -82,3 +82,6 @@ rem # Uncomment and edit to use a custom java.security file to override all the
rem set "JAVA_OPTS=%JAVA_OPTS% -Djava.security.properties==C:\path\to\custom\java.security"

:JAVA_OPTS_SET

rem # Uncomment to add a Java agent
rem set "MODULE_OPTS=-javaagent:agent.jar"
Expand Up @@ -82,3 +82,6 @@ if (-Not $JAVA_OPTS) {

# Uncomment this out to control garbage collection logging
# $GC_LOG=$true

# Uncomment to add a Java agent
# $MODULE_OPTS="-javaagent:agent.jar"
Expand Up @@ -18,6 +18,11 @@ Write-Debug "debug is: $global:DEBUG_MODE"
Write-Debug "debug port: $global:DEBUG_PORT"
Write-Debug "sec mgr: $SECMGR"

$MODULE_OPTS = Get-Env MODULE_OPTS $null
if ($MODULE_OPTS -like "*-javaagent:*") {
$JAVA_OPTS += "-javaagent:$JBOSS_HOME\jboss-modules.jar"
}
Write-Debug "MODULE_OPTS: $MODULE_OPTS"
if ($SECMGR) {
$MODULE_OPTS +="-secmgr";
}
Expand Down
Expand Up @@ -309,10 +309,14 @@ if [ "x$SECURITY_MANAGER_SET" != "x" ]; then
fi

# Set up the module arguments
MODULE_OPTS=""
MODULE_OPTS="$MODULE_OPTS"
if [ "$SECMGR" = "true" ]; then
MODULE_OPTS="$MODULE_OPTS -secmgr";
fi
AGENT_SET=$(echo "$MODULE_OPTS" | $GREP "\-javaagent:")
if [ "x$AGENT_SET" != "x" ]; then
JAVA_OPTS="-javaagent:\"${JBOSS_HOME}/jboss-modules.jar\" ${JAVA_OPTS}"
fi

# Display our environment
echo "========================================================================="
Expand Down
Expand Up @@ -26,6 +26,7 @@

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -39,7 +40,7 @@
*
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
*/
@SuppressWarnings("unused")
@SuppressWarnings({"unused", "MagicNumber", "UnusedReturnValue"})
public class StandaloneCommandBuilder extends AbstractCommandBuilder<StandaloneCommandBuilder> implements CommandBuilder {

// JPDA remote socket debugging
Expand All @@ -55,6 +56,8 @@ public class StandaloneCommandBuilder extends AbstractCommandBuilder<StandaloneC
private String modulesLocklessArg;
private String modulesMetricsArg;
private final Map<String, String> securityProperties;
private boolean addModuleAgent;
private final Collection<String> moduleOpts;

/**
* Creates a new command builder for a standalone instance.
Expand All @@ -69,6 +72,8 @@ private StandaloneCommandBuilder(final Path wildflyHome) {
javaOpts = new Arguments();
javaOpts.addAll(DEFAULT_VM_ARGUMENTS);
securityProperties = new LinkedHashMap<>();
moduleOpts = new ArrayList<>();
addModuleAgent = false;
}

/**
Expand Down Expand Up @@ -202,6 +207,103 @@ public List<String> getJavaOptions() {
return javaOpts.asList();
}

/**
* Adds an option which will be passed to JBoss Modules.
* <p>
* Note that {@code -mp} or {@code --modulepath} is not supported. Use {@link #addModuleDir(String)} to add a module
* directory.
* </p>
*
* @param arg the argument to add
*
* @return the builder
*/
public StandaloneCommandBuilder addModuleOption(final String arg) {
if (arg != null) {
if (arg.startsWith("-javaagent:")) {
addModuleAgent = true;
} else if ("-mp".equals(arg) || "--modulepath".equals(arg)) {
throw MESSAGES.invalidArgument(arg, "addModuleOption");
}
moduleOpts.add(arg);
}
return this;
}

/**
* Adds the options which will be passed to JBoss Modules.
* <p>
* Note that {@code -mp} or {@code --modulepath} is not supported. Use {@link #addModuleDirs(String...)} to add a
* module directory.
* </p>
*
* @param args the argument to add
*
* @return the builder
*/
public StandaloneCommandBuilder addModuleOptions(final String... args) {
if (args != null) {
for (String arg : args) {
addModuleOption(arg);
}
}
return this;
}

/**
* Adds the options which will be passed to JBoss Modules.
* <p>
* Note that {@code -mp} or {@code --modulepath} is not supported. Use {@link #addModuleDirs(Iterable)} to add a
* module directory.
* </p>
*
* @param args the argument to add
*
* @return the builder
*/
public StandaloneCommandBuilder addModuleOptions(final Iterable<String> args) {
if (args != null) {
for (String arg : args) {
addModuleOption(arg);
}
}
return this;
}

/**
* Clears the current module options and adds the options which will be passed to JBoss Modules.
* <p>
* Note that {@code -mp} or {@code --modulepath} is not supported. Use {@link #addModuleDirs(String...)} to add a
* module directory.
* </p>
*
* @param args the argument to use
*
* @return the builder
*/
public StandaloneCommandBuilder setModuleOptions(final String... args) {
moduleOpts.clear();
addModuleOptions(args);
return this;
}

/**
* Clears the current module options and adds the options which will be passed to JBoss Modules.
* <p>
* Note that {@code -mp} or {@code --modulepath} is not supported. Use {@link #addModuleDirs(Iterable)} to add a
* module directory.
* </p>
*
* @param args the argument to use
*
* @return the builder
*/
public StandaloneCommandBuilder setModuleOptions(final Iterable<String> args) {
moduleOpts.clear();
addModuleOptions(args);
return this;
}

/**
* Sets the debug argument for the JVM with a default port of {@code 8787}.
*
Expand Down Expand Up @@ -405,7 +507,7 @@ public StandaloneCommandBuilder addSecurityProperties(final Map<String, String>
}

public StandaloneCommandBuilder setGitRepository(final String gitRepository, final String gitBranch, final String gitAuthentication) {
if(gitRepository == null) {
if (gitRepository == null) {
throw MESSAGES.nullParam("git-repo");
}
addServerArg("--git-repo", gitRepository);
Expand All @@ -423,10 +525,19 @@ public StandaloneCommandBuilder setGitRepository(final String gitRepository, fin
public List<String> buildArguments() {
final List<String> cmd = new ArrayList<>();
cmd.add("-D[Standalone]");
// Check to see if an agent was added as a module option, if so we want to add JBoss Modules as an agent.
if (addModuleAgent) {
cmd.add("-javaagent:" + getModulesJarName());
}
cmd.addAll(getJavaOptions());
if (environment.getJvm().isModular()) {
cmd.addAll(DEFAULT_MODULAR_VM_ARGUMENTS);
}
// Add these to JVM level system properties
addSystemPropertyArg(cmd, HOME_DIR, getWildFlyHome());
addSystemPropertyArg(cmd, SERVER_BASE_DIR, getBaseDirectory());
addSystemPropertyArg(cmd, SERVER_LOG_DIR, getLogDirectory());
addSystemPropertyArg(cmd, SERVER_CONFIG_DIR, getConfigurationDirectory());
if (modulesLocklessArg != null) {
cmd.add(modulesLocklessArg);
}
Expand All @@ -443,13 +554,11 @@ public List<String> buildArguments() {
if (useSecurityManager()) {
cmd.add(SECURITY_MANAGER_ARG);
}
// Add the agent argument for jboss-modules
cmd.addAll(moduleOpts);
cmd.add("-mp");
cmd.add(getModulePaths());
cmd.add("org.jboss.as.standalone");
addSystemPropertyArg(cmd, HOME_DIR, getWildFlyHome());
addSystemPropertyArg(cmd, SERVER_BASE_DIR, getBaseDirectory());
addSystemPropertyArg(cmd, SERVER_LOG_DIR, getLogDirectory());
addSystemPropertyArg(cmd, SERVER_CONFIG_DIR, getConfigurationDirectory());

// Add the security properties
StringBuilder sb = new StringBuilder(64);
Expand Down
Expand Up @@ -59,4 +59,7 @@ public interface LauncherMessages {

@Message(id = 6, value = "Invalid hostname: %s")
IllegalArgumentException invalidHostname(CharSequence hostname);

@Message(id = 7, value = "The argument %s is not allowed for %s.")
IllegalArgumentException invalidArgument(String argument, String methodName);
}
Expand Up @@ -64,6 +64,7 @@ public void testStandaloneBuilder() {
.addJavaOption("-Djava.security.manager")
.addJavaOption("-Djava.net.preferIPv4Stack=true")
.addJavaOption("-Djava.net.preferIPv4Stack=false")
.addModuleOption("-javaagent:test-agent1.jar")
.setBindAddressHint("management", "0.0.0.0");

// Get all the commands
Expand All @@ -81,6 +82,9 @@ public void testStandaloneBuilder() {

Assert.assertTrue("Missing -secmgr option", commands.contains("-secmgr"));

Assert.assertTrue("Missing jboss-modules.jar", commands.stream().anyMatch(entry -> entry.matches("-javaagent:.*jboss-modules.jar$")));
Assert.assertTrue("Missing test-agent1.jar", commands.contains("-javaagent:test-agent1.jar"));

// If we're using Java 9+ ensure the modular JDK options were added
testModularJvmArguments(commands, 1);

Expand Down
63 changes: 63 additions & 0 deletions testsuite/scripts/pom.xml
Expand Up @@ -40,14 +40,43 @@
<wildfly.home>${project.basedir}${file.separator}target${file.separator}${server.name}</wildfly.home>
<test.log.level>INFO</test.log.level>
<test.log.file>${project.build.directory}${file.separator}test.log</test.log.file>
<maven.test.skip>false</maven.test.skip>
<skipTests>${maven.test.skip}</skipTests>
</properties>

<build>
<plugins>
<!-- Create a test agent for the BootWithAgentTestCase -->
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>package-agent</id>
<goals>
<goal>test-jar</goal>
</goals>
<phase>process-test-classes</phase>
<configuration>
<skip>${skipTests}</skip>
<finalName>logging-agent</finalName>
<includes>
<include>**/LoggingAgent.class</include>
</includes>
<archive>
<manifestEntries>
<Premain-Class>org.wildfly.common.test.LoggingAgent</Premain-Class>
</manifestEntries>
</archive>
<outputDirectory>${wildfly.home}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>${skipTests}</skip>
<failIfNoTests>false</failIfNoTests>
<redirectTestOutputToFile>${testLogToFile}</redirectTestOutputToFile>

Expand Down Expand Up @@ -80,6 +109,40 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>${version.wildfly.plugin}</version>
<configuration>
<skip>${skipTests}</skip>
<jboss-home>${wildfly.home}</jboss-home>
<java-opts>
<java-opt>-Dmaven.repo.local=${settings.localRepository}</java-opt>
</java-opts>
<system-properties>
<maven.repo.local>${settings.localRepository}</maven.repo.local>
</system-properties>
</configuration>
<executions>
<execution>
<id>configure-standalone</id>
<phase>process-test-classes</phase>
<goals>
<goal>start</goal>
<goal>execute-commands</goal>
<goal>shutdown</goal>
</goals>
<configuration>
<stdout>${project.build.directory}${file.separator}standalone-config.log</stdout>
<commands>
<command>/subsystem=logging/custom-formatter=json-formatter:add(module=org.jboss.logmanager, class=org.jboss.logmanager.formatters.JsonFormatter, properties={exceptionOutputType=FORMATTED, prettyPrint=false})</command>
<command>/subsystem=logging/file-handler=json:add(file={relative-to=jboss.server.log.dir, path=json.log}, append=false, level=DEBUG, autoflush=true, named-formatter=json-formatter)</command>
<command>/subsystem=logging/logger=org.wildfly.common.test.LoggingAgent:add(handlers=[json], level=DEBUG)</command>
</commands>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down

0 comments on commit 34598d5

Please sign in to comment.