forked from jiaqi/jmxterm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid usage of jdk.jconsole module in Java 9+
Copied from PR jiaqi#113 Co-Authored-By: nyg <[email protected]>
- Loading branch information
Showing
8 changed files
with
246 additions
and
6 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
src/main/java/org/cyclopsgroup/jmxterm/jdk9/Jdk9JavaProcess.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.cyclopsgroup.jmxterm.jdk9; | ||
|
||
import java.io.IOException; | ||
import org.apache.commons.lang3.Validate; | ||
import org.cyclopsgroup.jmxterm.JavaProcess; | ||
import org.cyclopsgroup.jmxterm.utils.WeakCastUtils; | ||
|
||
/** | ||
* JDK9 specific implementation of {@link JavaProcess} | ||
* | ||
* @author <a href="https://github.com/nyg">nyg</a> | ||
*/ | ||
public class Jdk9JavaProcess implements JavaProcess { | ||
|
||
private final StaticVirtualMachine staticVirtualMachine; | ||
private final VirtualMachineDescriptor vmd; | ||
private final String address; | ||
|
||
/** | ||
* @param staticVm Static VirtualMachine proxy | ||
* @param vmd Local VM | ||
* @param address Connector address, if any | ||
*/ | ||
Jdk9JavaProcess(StaticVirtualMachine staticVm, VirtualMachineDescriptor vmd, String address) { | ||
Validate.notNull(vmd, "StaticVirtualMachine can't be NULL"); | ||
Validate.notNull(vmd, "VirtualMachineDescriptor can't be NULL"); | ||
this.staticVirtualMachine = staticVm; | ||
this.vmd = vmd; | ||
this.address = address; | ||
} | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return vmd.displayName(); | ||
} | ||
|
||
@Override | ||
public int getProcessId() { | ||
return Integer.parseInt(vmd.id()); | ||
} | ||
|
||
@Override | ||
public boolean isManageable() { | ||
return address != null; | ||
} | ||
|
||
@Override | ||
public void startManagementAgent() throws IOException { | ||
Object vm = staticVirtualMachine.attach(vmd.id()); | ||
try { | ||
Class<?> originalVirtualMachine = Class.forName(VirtualMachine.ORIGINAL_CLASS_NAME); | ||
VirtualMachine vmProxy = WeakCastUtils.cast(originalVirtualMachine, VirtualMachine.class); | ||
vmProxy.startLocalManagementAgent(); | ||
} catch (ClassNotFoundException | SecurityException | NoSuchMethodException e) { | ||
throw new RuntimeException("Can't cast " + vm + " to VirtualMachineDescriptor", e); | ||
} | ||
} | ||
|
||
@Override | ||
public String toUrl() { | ||
return address; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/org/cyclopsgroup/jmxterm/jdk9/Jdk9JavaProcessManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.cyclopsgroup.jmxterm.jdk9; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Properties; | ||
import org.apache.commons.lang3.Validate; | ||
import org.cyclopsgroup.jmxterm.JavaProcess; | ||
import org.cyclopsgroup.jmxterm.JavaProcessManager; | ||
import org.cyclopsgroup.jmxterm.utils.WeakCastUtils; | ||
|
||
/** | ||
* JDK9 specific java process manager | ||
* | ||
* @author <a href="https://github.com/nyg">nyg</a> | ||
*/ | ||
public class Jdk9JavaProcessManager extends JavaProcessManager { | ||
private final StaticVirtualMachine staticVirtualMachine; | ||
private final Class<?> originalVirtualMachine; | ||
|
||
public Jdk9JavaProcessManager(ClassLoader classLoader) | ||
throws SecurityException, NoSuchMethodException, ClassNotFoundException { | ||
Validate.notNull(classLoader, "ClassLoader can't be NULL"); | ||
originalVirtualMachine = classLoader.loadClass(VirtualMachine.ORIGINAL_CLASS_NAME); | ||
staticVirtualMachine = | ||
WeakCastUtils.staticCast(originalVirtualMachine, StaticVirtualMachine.class); | ||
} | ||
|
||
@Override | ||
public JavaProcess get(int pid) { | ||
return list().stream().filter(process -> process.getProcessId() == pid).findAny().orElse(null); | ||
} | ||
|
||
@Override | ||
public List<JavaProcess> list() { | ||
List<Object> vmDescriptors = staticVirtualMachine.list(); | ||
List<JavaProcess> result = new ArrayList<>(vmDescriptors.size()); | ||
|
||
for (Object vmd : vmDescriptors) { | ||
VirtualMachineDescriptor vmdProxy = null; | ||
VirtualMachine vmProxy = null; | ||
|
||
try { | ||
vmdProxy = WeakCastUtils.cast(vmd, VirtualMachineDescriptor.class); | ||
Object vm = staticVirtualMachine.attach(vmdProxy.id()); | ||
vmProxy = WeakCastUtils.cast(originalVirtualMachine, vm, VirtualMachine.class); | ||
|
||
Properties agentProps = vmProxy.getAgentProperties(); | ||
String address = (String) agentProps.get(VirtualMachine.LOCAL_CONNECTOR_ADDRESS_PROP); | ||
result.add(new Jdk9JavaProcess(staticVirtualMachine, vmdProxy, address)); | ||
} catch (SecurityException | NoSuchMethodException e) { | ||
throw new RuntimeException("Error casting object", e); | ||
} catch (Exception e) { | ||
// could not attach or some other exception | ||
result.add(new Jdk9JavaProcess(staticVirtualMachine, vmdProxy, null)); | ||
} finally { | ||
if (vmProxy != null) { | ||
vmProxy.detach(); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/org/cyclopsgroup/jmxterm/jdk9/StaticVirtualMachine.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.cyclopsgroup.jmxterm.jdk9; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Static interface of com.sun.tools.attach.VirtualMachine | ||
* | ||
* @author <a href="https://github.com/nyg">nyg</a> | ||
*/ | ||
public interface StaticVirtualMachine { | ||
/** @return List of all virtual machines running on local */ | ||
List<Object> list(); | ||
|
||
/** Attaches to a Java virtual machine. */ | ||
Object attach(String id); | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/org/cyclopsgroup/jmxterm/jdk9/VirtualMachine.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.cyclopsgroup.jmxterm.jdk9; | ||
|
||
import java.util.Properties; | ||
|
||
/** | ||
* Reflect class com.sun.tools.attach.VirtualMachine | ||
* | ||
* @author <a href="https://github.com/nyg">nyg</a> | ||
*/ | ||
public interface VirtualMachine { | ||
/** Name of original class this interface reflects */ | ||
String ORIGINAL_CLASS_NAME = "com.sun.tools.attach.VirtualMachine"; | ||
|
||
/** Property for the local connector address */ | ||
String LOCAL_CONNECTOR_ADDRESS_PROP = "com.sun.management.jmxremote.localConnectorAddress"; | ||
|
||
/** Detach from the virtual machine. */ | ||
void detach(); | ||
|
||
/** @return The current agent properties in the target virtual machine. */ | ||
Properties getAgentProperties(); | ||
|
||
/** Starts the local JMX management agent in the target virtual machine. */ | ||
void startLocalManagementAgent(); | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/org/cyclopsgroup/jmxterm/jdk9/VirtualMachineDescriptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.cyclopsgroup.jmxterm.jdk9; | ||
|
||
/** | ||
* Reflect class com.sun.tools.attach.VirtualMachineDescriptor | ||
* | ||
* @author <a href="https://github.com/nyg">nyg</a> | ||
*/ | ||
public interface VirtualMachineDescriptor { | ||
/** Name of original class this interface reflects */ | ||
String ORIGINAL_CLASS_NAME = "com.sun.tools.attach.VirtualMachineDescriptor"; | ||
|
||
/** @return The display name component of this descriptor */ | ||
String displayName(); | ||
|
||
/** @return The identifier component of this descriptor */ | ||
String id(); | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/org/cyclopsgroup/jmxterm/jdk9/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* Classes to implement {@link org.cyclopsgroup.jmxterm.JavaProcessManager} in JDK9 specific way | ||
* | ||
* @author <a href="https://github.com/nyg">nyg</a> | ||
*/ | ||
package org.cyclopsgroup.jmxterm.jdk9; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/test/java/org/cyclopsgroup/jmxterm/jdk9/Jdk9JavaProcessManagerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.cyclopsgroup.jmxterm.jdk9; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
|
||
import java.util.List; | ||
import org.apache.commons.lang3.JavaVersion; | ||
import org.apache.commons.lang3.SystemUtils; | ||
import org.cyclopsgroup.jmxterm.JavaProcess; | ||
import org.cyclopsgroup.jmxterm.pm.JConsoleClassLoaderFactory; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Test case of {@link Jdk9JavaProcessManager} | ||
* | ||
* @author <a href="https://github.com/nyg">nyg</a> | ||
*/ | ||
public class Jdk9JavaProcessManagerTest { | ||
|
||
@Test | ||
public void testConstruction() throws Exception { | ||
if (!SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_9)) { | ||
return; | ||
} | ||
Jdk9JavaProcessManager jpm = | ||
new Jdk9JavaProcessManager(JConsoleClassLoaderFactory.getClassLoader()); | ||
List<JavaProcess> ps = jpm.list(); | ||
assertFalse(ps.isEmpty()); | ||
} | ||
} |