LUCENE-6795: Improve SystemInfoHandler to get the bean directly instead of downcasting (thanks Peter Levart from jigsaw-dev), also more fixes with UOE thrown by RuntimeMXBean#getBootClassPath()

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1702628 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2015-09-12 12:52:23 +00:00
parent bca7bcc9d7
commit 84d98091b5
2 changed files with 40 additions and 28 deletions

View File

@ -23,6 +23,7 @@ import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean; import java.lang.management.OperatingSystemMXBean;
import java.lang.management.PlatformManagedObject;
import java.lang.management.RuntimeMXBean; import java.lang.management.RuntimeMXBean;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
@ -167,13 +168,17 @@ public class SystemInfoHandler extends RequestHandlerBase
// This is a public Oracle/OpenJDK extension, but may not be in other JDKs: // This is a public Oracle/OpenJDK extension, but may not be in other JDKs:
// com.sun.management.OperatingSystemMXBean // com.sun.management.OperatingSystemMXBean
try { try {
Class<?> intf = Class.forName("com.sun.management.OperatingSystemMXBean"); final Class<? extends PlatformManagedObject> intf = Class.forName("com.sun.management.OperatingSystemMXBean")
addGetterIfAvaliable( os, intf, "committedVirtualMemorySize", info); .asSubclass(PlatformManagedObject.class);
addGetterIfAvaliable( os, intf, "freePhysicalMemorySize", info); final PlatformManagedObject bean = ManagementFactory.getPlatformMXBean(intf);
addGetterIfAvaliable( os, intf, "freeSwapSpaceSize", info); if (bean != null) {
addGetterIfAvaliable( os, intf, "processCpuTime", info); addMXBeanProperty( bean, intf, "committedVirtualMemorySize", info);
addGetterIfAvaliable( os, intf, "totalPhysicalMemorySize", info); addMXBeanProperty( bean, intf, "freePhysicalMemorySize", info);
addGetterIfAvaliable( os, intf, "totalSwapSpaceSize", info); addMXBeanProperty( bean, intf, "freeSwapSpaceSize", info);
addMXBeanProperty( bean, intf, "processCpuTime", info);
addMXBeanProperty( bean, intf, "totalPhysicalMemorySize", info);
addMXBeanProperty( bean, intf, "totalSwapSpaceSize", info);
}
} catch (Exception e) { } catch (Exception e) {
// ignore // ignore
} }
@ -181,9 +186,13 @@ public class SystemInfoHandler extends RequestHandlerBase
// This is a public Oracle/OpenJDK extension, but may not be in other JDKs: // This is a public Oracle/OpenJDK extension, but may not be in other JDKs:
// com.sun.management.UnixOperatingSystemMXBean // com.sun.management.UnixOperatingSystemMXBean
try { try {
Class<?> intf = Class.forName("com.sun.management.UnixOperatingSystemMXBean"); final Class<? extends PlatformManagedObject> intf = Class.forName("com.sun.management.UnixOperatingSystemMXBean")
addGetterIfAvaliable( os, intf, "openFileDescriptorCount", info ); .asSubclass(PlatformManagedObject.class);
addGetterIfAvaliable( os, intf, "maxFileDescriptorCount", info ); final PlatformManagedObject bean = ManagementFactory.getPlatformMXBean(intf);
if (bean != null) {
addMXBeanProperty( bean, intf, "openFileDescriptorCount", info );
addMXBeanProperty( bean, intf, "maxFileDescriptorCount", info );
}
} catch (Exception e) { } catch (Exception e) {
// ignore // ignore
} }
@ -201,24 +210,25 @@ public class SystemInfoHandler extends RequestHandlerBase
} }
/** /**
* Try to run a getter function. This is useful because java 1.6 has a few extra * Try to run a getter function on a {@link PlatformManagedObject}.
* useful functions on the <code>OperatingSystemMXBean</code> * <p>
* * If you are running a OpenJDK/Oracle JVM, there are nice functions in:
* If you are running a sun jvm, there are nice functions in: * {@code com.sun.management.UnixOperatingSystemMXBean} and
* UnixOperatingSystemMXBean and com.sun.management.OperatingSystemMXBean * {@code com.sun.management.OperatingSystemMXBean}
* * <p>
* it is package protected so it can be tested... * If the given bean does not have the property, it is handled like {@code null}
* and not added to the given named list.
*/ */
static void addGetterIfAvaliable( Object obj, Class<?> intf, String property, NamedList<Object> info ) static void addMXBeanProperty(PlatformManagedObject obj, Class<? extends PlatformManagedObject> intf,
{ String property, NamedList<Object> info) {
try { try {
String method = "get" + Character.toUpperCase( property.charAt(0) ) + property.substring( 1 ); final String method = "get" + Character.toUpperCase( property.charAt(0) ) + property.substring( 1 );
Object v = intf.getMethod( method ).invoke( intf.cast(obj) ); final Object v = intf.getMethod( method ).invoke( intf.cast(obj) );
if( v != null ) { if( v != null ) {
info.add( property, v ); info.add( property, v );
} }
} catch( Exception ex ) { } catch (Exception e) {
// ignore log.warn("Cannot get property '{}' of MXBean interface: {}", property, intf.getName());
} }
} }
@ -322,7 +332,9 @@ public class SystemInfoHandler extends RequestHandlerBase
SimpleOrderedMap<Object> jmx = new SimpleOrderedMap<>(); SimpleOrderedMap<Object> jmx = new SimpleOrderedMap<>();
try{ try{
RuntimeMXBean mx = ManagementFactory.getRuntimeMXBean(); RuntimeMXBean mx = ManagementFactory.getRuntimeMXBean();
jmx.add( "bootclasspath", mx.getBootClassPath()); if (mx.isBootClassPathSupported()) {
jmx.add( "bootclasspath", mx.getBootClassPath());
}
jmx.add( "classpath", mx.getClassPath() ); jmx.add( "classpath", mx.getClassPath() );
// the input arguments passed to the Java virtual machine // the input arguments passed to the Java virtual machine

View File

@ -36,11 +36,11 @@ public class SystemInfoHandlerTest extends LuceneTestCase {
info.add( "version", os.getVersion() ); info.add( "version", os.getVersion() );
info.add( "arch", os.getArch() ); info.add( "arch", os.getArch() );
// make another using addGetterIfAvaliable // make another using addMXBeanProperty()
SimpleOrderedMap<Object> info2 = new SimpleOrderedMap<>(); SimpleOrderedMap<Object> info2 = new SimpleOrderedMap<>();
SystemInfoHandler.addGetterIfAvaliable( os, OperatingSystemMXBean.class, "name", info2 ); SystemInfoHandler.addMXBeanProperty( os, OperatingSystemMXBean.class, "name", info2 );
SystemInfoHandler.addGetterIfAvaliable( os, OperatingSystemMXBean.class, "version", info2 ); SystemInfoHandler.addMXBeanProperty( os, OperatingSystemMXBean.class, "version", info2 );
SystemInfoHandler.addGetterIfAvaliable( os, OperatingSystemMXBean.class, "arch", info2 ); SystemInfoHandler.addMXBeanProperty( os, OperatingSystemMXBean.class, "arch", info2 );
// make sure they got the same thing // make sure they got the same thing
assertEquals( info.toString(), info2.toString() ); assertEquals( info.toString(), info2.toString() );