Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.lang.management.ManagementFactory;
import java.util.Properties;

import javax.management.JMException;
Expand All @@ -50,6 +49,20 @@ public class AppInfoParser {
COMMIT_ID = props.getProperty("commitId", DEFAULT_VALUE).trim();
}

static MBeanServer getPlatformMBeanServer(ClassLoader loader) {
try {
// Not all platforms (*cough*Android*cough*) have MBeans, query using reflection
Class<?> managementFactory = Class.forName("java.lang.management.ManagementFactory", true, loader);
return (MBeanServer) managementFactory.getMethod("getPlatformMBeanServer").invoke(null);
} catch (ReflectiveOperationException e) {
return null;
}
}

static MBeanServer getPlatformMBeanServer() {
return getPlatformMBeanServer(AppInfoParser.class.getClassLoader());
}

public static String getVersion() {
return VERSION;
}
Expand All @@ -59,9 +72,12 @@ public static String getCommitId() {
}

public static synchronized void registerAppInfo(String prefix, String id, Metrics metrics, long nowMs) {
MBeanServer server = getPlatformMBeanServer();
if (server == null) {
return;
}
try {
ObjectName name = new ObjectName(prefix + ":type=app-info,id=" + Sanitizer.jmxSanitize(id));
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
if (server.isRegistered(name)) {
log.info("The mbean of App info: [{}], id: [{}] already exists, so skipping a new mbean creation.", prefix, id);
return;
Expand All @@ -76,7 +92,10 @@ public static synchronized void registerAppInfo(String prefix, String id, Metric
}

public static synchronized void unregisterAppInfo(String prefix, String id, Metrics metrics) {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
MBeanServer server = getPlatformMBeanServer();
if (server == null) {
return;
}
try {
ObjectName name = new ObjectName(prefix + ":type=app-info,id=" + Sanitizer.jmxSanitize(id));
if (server.isRegistered(name))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand All @@ -55,6 +56,23 @@ public void tearDown() {
metrics.close();
}

@Test
public void testGetPlatformMBeanServer() {
assertNotNull(AppInfoParser.getPlatformMBeanServer(AppInfoParser.class.getClassLoader()));
}

@Test
public void testFailGetPlatformMBeanServer() {
ClassLoader nullLoader = new ClassLoader(null) {
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
throw new ClassNotFoundException(name);
}
};

assertNull(AppInfoParser.getPlatformMBeanServer(nullLoader));
}

@Test
public void testRegisterAppInfoRegistersMetrics() throws JMException {
registerAppInfo();
Expand Down