Skip to content

Commit

Permalink
Merge pull request #1549 from newrelic/jboss-fix-jsr77
Browse files Browse the repository at this point in the history
Jboss fix jsr77
  • Loading branch information
obenkenobi committed Oct 17, 2023
2 parents c157fca + eb27b15 commit cad7865
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@

public class JbossUtils {

// Used to check if JBoss modules is used
public static final String JBOSS_MODULES_MAIN_CLASS = "org/jboss/modules/Main.class";
public static final String JBOSS_MODULES_MAIN = "org.jboss.modules.Main";

// System properties
public static final String NR_JBOSS_JSR_77_FIX = "com.newrelic.jboss.jsr77.fix";
public static final String JBOSS_MODULES_SYSTEM_PKGS = "jboss.modules.system.pkgs";
public static final String COM_NR_INSTRUMENTATION_SECURITY = "com.nr.instrumentation.security";

// Java packages
public static final String COM_NR_INSTRUMENTATION_SECURITY = "com.nr.instrumentation.security";
public static final String JAVA_UTIL_LOGGING = "java.util.logging";
public static final String JAVA_LANG_MANAGEMENT = "java.lang.management";
public static final String JAVAX_MANAGEMENT = "javax.management";

public static final String JOIN_STR_JBOSS_SYSTEM_PKGS_VALUE = String.join(",",
JAVA_UTIL_LOGGING, JAVAX_MANAGEMENT, COM_NR_INSTRUMENTATION_SECURITY, JAVA_LANG_MANAGEMENT);

/**
* DO NOT call this outside of premain. This will iterate thru all loaded classes in the Instrumentation,
* possibly causing a LOT of overhead.
Expand All @@ -27,13 +29,22 @@ public void checkAndApplyJbossAdjustments(Instrumentation inst) {
if (isJbossServer(inst)) {
String cur = System.getProperty(JBOSS_MODULES_SYSTEM_PKGS);
if (StringUtils.isBlank(cur)) {
System.setProperty(JBOSS_MODULES_SYSTEM_PKGS, JOIN_STR_JBOSS_SYSTEM_PKGS_VALUE);
System.setProperty(JBOSS_MODULES_SYSTEM_PKGS, getJbossSystemPackages());
} else if (!StringUtils.containsIgnoreCase(cur, COM_NR_INSTRUMENTATION_SECURITY)) {
System.setProperty(JBOSS_MODULES_SYSTEM_PKGS, cur + "," + JOIN_STR_JBOSS_SYSTEM_PKGS_VALUE);
System.setProperty(JBOSS_MODULES_SYSTEM_PKGS, cur + "," + getJbossSystemPackages());
}
}
}

public String getJbossSystemPackages() {
String defaultJbossSysPackages = String.join(",", JAVA_UTIL_LOGGING, COM_NR_INSTRUMENTATION_SECURITY, JAVA_LANG_MANAGEMENT);
String jsr77FixProp = System.getProperty(NR_JBOSS_JSR_77_FIX);
if (Boolean.TRUE.toString().equalsIgnoreCase(jsr77FixProp)) {
return defaultJbossSysPackages;
}
return JAVAX_MANAGEMENT + "," + defaultJbossSysPackages;
}

public boolean isJbossServer(Instrumentation inst) {
if (ClassLoader.getSystemClassLoader().getResource(JBOSS_MODULES_MAIN_CLASS) != null) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

public class JbossUtilsTest {
@Test
public void checkAndApplyJbossAdjustments_when_sysPropNotSet() {
public void applyJbossAdjustments() {
String startVal = System.getProperty(JbossUtils.JBOSS_MODULES_SYSTEM_PKGS);
try {
Instrumentation inst = Mockito.mock(Instrumentation.class);
Expand All @@ -25,7 +25,7 @@ public void checkAndApplyJbossAdjustments_when_sysPropNotSet() {

utils.checkAndApplyJbossAdjustments(inst);

String expectedSysProp = "java.util.logging,javax.management,com.nr.instrumentation.security,java.lang.management";
String expectedSysProp = "javax.management,java.util.logging,com.nr.instrumentation.security,java.lang.management";
String actual = System.getProperty(JbossUtils.JBOSS_MODULES_SYSTEM_PKGS);
Assert.assertEquals(expectedSysProp, actual);
} finally {
Expand All @@ -34,7 +34,30 @@ public void checkAndApplyJbossAdjustments_when_sysPropNotSet() {
}

@Test
public void checkAndApplyJbossAdjustments_when_sysPropSet() {
public void applyJbossAdjustments_when_jsr77Fix_is_true() {
String startVal = System.getProperty(JbossUtils.JBOSS_MODULES_SYSTEM_PKGS);
String startJsr77FixVal = System.getProperty(JbossUtils.NR_JBOSS_JSR_77_FIX);
try {
System.setProperty(JbossUtils.NR_JBOSS_JSR_77_FIX, "true");

Instrumentation inst = Mockito.mock(Instrumentation.class);

JbossUtils utils = Mockito.spy(new JbossUtils());
Mockito.when(utils.isJbossServer(inst)).thenReturn(true);

utils.checkAndApplyJbossAdjustments(inst);

String expectedSysProp = "java.util.logging,com.nr.instrumentation.security,java.lang.management";
String actual = System.getProperty(JbossUtils.JBOSS_MODULES_SYSTEM_PKGS);
Assert.assertEquals(expectedSysProp, actual);
} finally {
System.setProperty(JbossUtils.JBOSS_MODULES_SYSTEM_PKGS, startVal != null ? startVal: "");
System.setProperty(JbossUtils.NR_JBOSS_JSR_77_FIX, startJsr77FixVal != null ? startJsr77FixVal: "");
}
}

@Test
public void applyJbossAdjustments_when_jbossSysPkgsSet() {
String startVal = System.getProperty(JbossUtils.JBOSS_MODULES_SYSTEM_PKGS);
try {
System.setProperty(JbossUtils.JBOSS_MODULES_SYSTEM_PKGS, "org.junit");
Expand All @@ -45,11 +68,34 @@ public void checkAndApplyJbossAdjustments_when_sysPropSet() {

utils.checkAndApplyJbossAdjustments(inst);

String expectedSysProp = "org.junit,java.util.logging,javax.management,com.nr.instrumentation.security,java.lang.management";
String expectedSysProp = "org.junit,javax.management,java.util.logging,com.nr.instrumentation.security,java.lang.management";
String actual = System.getProperty(JbossUtils.JBOSS_MODULES_SYSTEM_PKGS);
Assert.assertEquals(expectedSysProp, actual);
} finally {
System.setProperty(JbossUtils.JBOSS_MODULES_SYSTEM_PKGS, startVal != null ? startVal: "");
}
}

@Test
public void applyJbossAdjustments_when_jbossSysPkgsSet_and_jsr77Fix_is_true() {
String startSysPkgVal = System.getProperty(JbossUtils.JBOSS_MODULES_SYSTEM_PKGS);
String startJsr77FixVal = System.getProperty(JbossUtils.NR_JBOSS_JSR_77_FIX);
try {
System.setProperty(JbossUtils.JBOSS_MODULES_SYSTEM_PKGS, "org.junit");
System.setProperty(JbossUtils.NR_JBOSS_JSR_77_FIX, "true");
Instrumentation inst = Mockito.mock(Instrumentation.class);

JbossUtils utils = Mockito.spy(new JbossUtils());
Mockito.when(utils.isJbossServer(inst)).thenReturn(true);

utils.checkAndApplyJbossAdjustments(inst);

String expectedSysProp = "org.junit,java.util.logging,com.nr.instrumentation.security,java.lang.management";
String actual = System.getProperty(JbossUtils.JBOSS_MODULES_SYSTEM_PKGS);
Assert.assertEquals(expectedSysProp, actual);
} finally {
System.setProperty(JbossUtils.JBOSS_MODULES_SYSTEM_PKGS, startSysPkgVal != null ? startSysPkgVal: "");
System.setProperty(JbossUtils.NR_JBOSS_JSR_77_FIX, startJsr77FixVal != null ? startJsr77FixVal: "");
}
}
}

0 comments on commit cad7865

Please sign in to comment.