-
Notifications
You must be signed in to change notification settings - Fork 155
Avoid usage of jdk.jconsole module in Java 9+ #113
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| /** Classes to implement {@link org.cyclopsgroup.jmxterm.JavaProcessManager} in JDK5 specifi way */ | ||
| /** Classes to implement {@link org.cyclopsgroup.jmxterm.JavaProcessManager} in JDK5 specific way */ | ||
| package org.cyclopsgroup.jmxterm.jdk5; |
63 changes: 63 additions & 0 deletions
63
src/main/java/org/cyclopsgroup/jmxterm/jdk9/Jdk9JavaProcess.java
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you forget to pass
vmtocast?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed I did! I created a new PR to fix that: #129.