--- old/application/org.openjdk.jmc.rjmx/src/main/java/org/openjdk/jmc/rjmx/services/internal/HotSpot23CommercialFeaturesService.java 2018-05-25 13:10:58.608324300 +0530 +++ new/application/org.openjdk.jmc.rjmx/src/main/java/org/openjdk/jmc/rjmx/services/internal/HotSpot23CommercialFeaturesService.java 2018-05-25 13:10:57.351573000 +0530 @@ -35,16 +35,20 @@ import javax.management.MBeanServerConnection; import org.openjdk.jmc.rjmx.ConnectionException; +import org.openjdk.jmc.rjmx.ConnectionToolkit; import org.openjdk.jmc.rjmx.IConnectionHandle; import org.openjdk.jmc.rjmx.ServiceNotAvailableException; import org.openjdk.jmc.rjmx.services.ICommercialFeaturesService; import org.openjdk.jmc.rjmx.services.IDiagnosticCommandService; +import javax.management.ObjectName; public class HotSpot23CommercialFeaturesService implements ICommercialFeaturesService { private final static String VM_FLAG = "UnlockCommercialFeatures"; //$NON-NLS-1$ private final static String UNLOCK_COMMAND = "VM.unlock_commercial_features"; //$NON-NLS-1$ private final MBeanServerConnection server; private final IDiagnosticCommandService dcs; + private final static String JFR2_MBEAN_OBJECT_NAME_OLD = "jdk.jfr.management:type=FlightRecorder"; //$NON-NLS-1$ + private final static String JFR2_MBEAN_OBJECT_NAME = "jdk.management.jfr:type=FlightRecorder"; //$NON-NLS-1$ public HotSpot23CommercialFeaturesService(IConnectionHandle handle) throws ConnectionException, ServiceNotAvailableException { @@ -53,7 +57,10 @@ try { HotspotManagementToolkit.getVMOption(server, VM_FLAG); // Will fail if option is not available } catch (Exception e) { - throw new ServiceNotAvailableException(""); //$NON-NLS-1$ + // JDK11 and above Commercial Feature option is not available but Flighter Recorder is. + if (!isJfrMBeanAvailable()) { + throw new ServiceNotAvailableException(""); //$NON-NLS-1$ + } } } @@ -62,6 +69,10 @@ try { return ((String) HotspotManagementToolkit.getVMOption(server, VM_FLAG)).contains("true"); //$NON-NLS-1$ } catch (Exception e) { + // JDK11 and above Commercial Feature option is not available but Flighter Recorder is. + if (isJfrMBeanAvailable()) { + return true; + } return false; } } @@ -75,4 +86,27 @@ HotspotManagementToolkit.setVMOption(server, VM_FLAG, "true"); //$NON-NLS-1$ } } + + private boolean isJfrMBeanAvailable() { + try { + getJfrMBeanObjectName(); + return true; + } catch (Exception e) { + return false; + } + } + + private ObjectName getJfrMBeanObjectName() throws Exception { + //FlightRecorder MXBean name JDK10 and later. + try { + ObjectName candidate2ObjectName = ConnectionToolkit.createObjectName(JFR2_MBEAN_OBJECT_NAME); + server.getMBeanInfo(candidate2ObjectName); + return candidate2ObjectName; + } catch (Exception e) { + } + //FlightRecorder MXBean name JDK9. + ObjectName candidate1ObjectName = ConnectionToolkit.createObjectName(JFR2_MBEAN_OBJECT_NAME_OLD); + server.getMBeanInfo(candidate1ObjectName); + return candidate1ObjectName; + } }