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

#1519 fix NoClassDefFoundError on loading com/sun/management/UnixOper… #1562

Merged
merged 4 commits into from
Apr 9, 2020
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.codahale.metrics.jvm;

import com.codahale.metrics.RatioGauge;
import com.sun.management.UnixOperatingSystemMXBean;

import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
Expand All @@ -10,8 +9,19 @@
* A gauge for the ratio of used to total file descriptors.
*/
public class FileDescriptorRatioGauge extends RatioGauge {
private static boolean unixOperatingSystemMXBeanExists = false;

private final OperatingSystemMXBean os;

static {
try {
Class.forName("com.sun.management.UnixOperatingSystemMXBean");
unixOperatingSystemMXBeanExists = true;
} catch (ClassNotFoundException e) {
// do nothing
}
}

/**
* Creates a new gauge using the platform OS bean.
*/
Expand All @@ -30,8 +40,8 @@ public FileDescriptorRatioGauge(OperatingSystemMXBean os) {

@Override
protected Ratio getRatio() {
if (os instanceof UnixOperatingSystemMXBean) {
final UnixOperatingSystemMXBean unixOs = (UnixOperatingSystemMXBean) os;
if (unixOperatingSystemMXBeanExists && os instanceof com.sun.management.UnixOperatingSystemMXBean) {
final com.sun.management.UnixOperatingSystemMXBean unixOs = (com.sun.management.UnixOperatingSystemMXBean) os;
return Ratio.of(unixOs.getOpenFileDescriptorCount(), unixOs.getMaxFileDescriptorCount());
} else {
return Ratio.of(Double.NaN, Double.NaN);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.codahale.metrics.jvm;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.AccessController;
import java.security.CodeSource;
import java.security.PermissionCollection;
import java.security.Permissions;
import java.security.PrivilegedAction;
import java.util.LinkedHashSet;
import java.util.Set;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;

@RunWith(FileDescriptorRatioGaugeSunManagementNotExistsTest.SunManagementNotExistsTestRunner.class)
public class FileDescriptorRatioGaugeSunManagementNotExistsTest {

@Test
public void validateFileDescriptorRatioWhenSunManagementNotExists() {
assertThat(new FileDescriptorRatioGauge().getValue()).isNaN();
}

public static class SunManagementNotExistsTestRunner extends BlockJUnit4ClassRunner {

public SunManagementNotExistsTestRunner(Class<?> clazz) throws InitializationError {
super(getFromSunManagementNotExistsClassLoader(clazz));
}

private static Class<?> getFromSunManagementNotExistsClassLoader(Class<?> clazz) throws InitializationError {
try {
return Class.forName(clazz.getName(), true,
new SunManagementNotExistsClassLoader(SunManagementNotExistsTestRunner.class.getClassLoader()));
} catch (ClassNotFoundException e) {
throw new InitializationError(e);
}
}
}

public static class SunManagementNotExistsClassLoader extends URLClassLoader {
private static final URL[] CLASSPATH_ENTRY_URLS;
private static final PermissionCollection NO_PERMS = new Permissions();

static {
String[] classpathEntries = AccessController.doPrivileged(new PrivilegedAction<String>() {
@Override
public String run() {
return System.getProperty("java.class.path");
}
}).split(File.pathSeparator);
CLASSPATH_ENTRY_URLS = getClasspathEntryUrls(classpathEntries);
}

private static URL[] getClasspathEntryUrls(String... classpathEntries) {
Set<URL> classpathEntryUrls = new LinkedHashSet<>(classpathEntries.length, 1);
for (String classpathEntry : classpathEntries) {
URL classpathEntryUrl = getClasspathEntryUrl(classpathEntry);
if (classpathEntryUrl != null) {
classpathEntryUrls.add(classpathEntryUrl);
}
}
return classpathEntryUrls.toArray(new URL[classpathEntryUrls.size()]);
}

private static URL getClasspathEntryUrl(String classpathEntry) {
try {
if (classpathEntry.endsWith(".jar")) {
return new URL("file:jar:" + classpathEntry);
}
if (!classpathEntry.endsWith("/")) {
classpathEntry = classpathEntry + "/";
}
return new URL("file:" + classpathEntry);
} catch (MalformedURLException mue) {
// do nothing
}
return null;
}

public SunManagementNotExistsClassLoader(ClassLoader parent) {
super(CLASSPATH_ENTRY_URLS, parent);
}

@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
if (getClass().getName().equals(name)) {
return getClass();
}
if (name.startsWith("com.sun.management.")) {
throw new ClassNotFoundException(name);
}
if (name.startsWith("com.codahale.metrics.jvm.")) {
return loadMetricsClasses(name);
}
return super.loadClass(name, resolve);
}

private Class<?> loadMetricsClasses(String name) throws ClassNotFoundException {
Class<?> ret = findLoadedClass(name);
if (ret != null) {
return ret;
}
return findClass(name);
}

@Override
protected PermissionCollection getPermissions(CodeSource codesource) {
return NO_PERMS;
}
}
}