Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kick off a simple mbean that wraps jolokia's jvmagent client
- Loading branch information
Showing
6 changed files
with
274 additions
and
0 deletions.
There are no files selected for viewing
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,32 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>project</artifactId> | ||
<groupId>io.hawt</groupId> | ||
<version>1.1-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>hawtio-local-jvm-mbean</artifactId> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>org.jolokia</groupId> | ||
<artifactId>jolokia-jvm</artifactId> | ||
<version>${jolokia-version}</version> | ||
<classifier>agent</classifier> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>${junit-version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
</project> |
127 changes: 127 additions & 0 deletions
127
hawtio-local-jvm-mbean/src/main/java/io/hawt/jvm/local/JVMList.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,127 @@ | ||
package io.hawt.jvm.local; | ||
|
||
import org.jolokia.jvmagent.client.command.CommandDispatcher; | ||
import org.jolokia.jvmagent.client.util.OptionsAndArgs; | ||
import org.jolokia.jvmagent.client.util.ProcessDescription; | ||
import org.jolokia.jvmagent.client.util.VirtualMachineHandler; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author Stan Lewis | ||
*/ | ||
public class JVMList implements JVMListMBean { | ||
|
||
protected static final Map<String,String> vmAliasMap = new HashMap<String, String>(); | ||
private VirtualMachineHandler vmHandler; | ||
|
||
static { | ||
vmAliasMap.put("com.intellij.idea.Main", "IDEA"); | ||
vmAliasMap.put("com.intellij.rt.execution.application.AppMain", "IDEA"); | ||
vmAliasMap.put("org.apache.karaf.main.Main", "Karaf"); | ||
vmAliasMap.put("org.eclipse.equinox.launcher.Main", "Equinox"); | ||
vmAliasMap.put("org.jetbrains.idea.maven.server.RemoteMavenServer", "IDEA Maven server"); | ||
vmAliasMap.put("idea maven server", ""); | ||
vmAliasMap.put("scala.tools.nsc.MainGenericRunner", "scala repl"); | ||
vmAliasMap.put("jboss-eap-6.1/jboss-modules.jar", "JBoss EAP 6"); | ||
vmAliasMap.put("target/surefire", "Maven Surefire Test"); | ||
vmAliasMap.put("org.apache.camel:camel-maven-plugin:run", "Local Camel Context"); | ||
|
||
/* | ||
vmAliasMap.put("default", "Apache Karaf"); | ||
vmAliasMap.put("esb-version.jar", "JBoss Fuse"); | ||
vmAliasMap.put("fabric-version.jar", "Fuse Fabric"); | ||
vmAliasMap.put("mq-version.jar", "JBoss A-MQ"); | ||
vmAliasMap.put("servicemix-version.jar", "Apache ServiceMix"); | ||
*/ | ||
} | ||
|
||
public JVMList() { | ||
|
||
} | ||
|
||
public void init() { | ||
|
||
} | ||
|
||
@Override | ||
public List<VMDescriptorDTO> listLocalJVMs() { | ||
|
||
List<VMDescriptorDTO> rc = new ArrayList<VMDescriptorDTO>(); | ||
|
||
try { | ||
List<ProcessDescription> processes = new VirtualMachineHandler(null).listProcesses(); | ||
for(ProcessDescription process : processes) { | ||
rc.add(new VMDescriptorDTO(process)); | ||
} | ||
} catch (Exception e) { | ||
throw new RuntimeException("Failed to get local JVM processes due to " + e); | ||
} | ||
|
||
return rc; | ||
} | ||
|
||
private void doAction(String PID, String action) { | ||
OptionsAndArgs options = new OptionsAndArgs(CommandDispatcher.getAvailableCommands(), "--quiet", "start", PID); | ||
|
||
System.out.println("Jar file at : " + options.getJarFilePath()); | ||
|
||
VirtualMachineHandler vmHandler = new VirtualMachineHandler(options); | ||
CommandDispatcher dispatcher = new CommandDispatcher(options); | ||
|
||
Object vm = null; | ||
|
||
try { | ||
vm = vmHandler.attachVirtualMachine(); | ||
dispatcher.dispatchCommand(vm, vmHandler); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Failed to " + action + " agent in process " + PID, e); | ||
} finally { | ||
if (vm != null) { | ||
try { | ||
vmHandler.detachAgent(vm); | ||
} catch (Exception e) { | ||
// swallow this? | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void startAgent(String PID) { | ||
doAction(PID, "start"); | ||
} | ||
|
||
|
||
@Override | ||
public String agentStatus(String PID) { | ||
return null; | ||
} | ||
|
||
|
||
|
||
@Override | ||
public String agentVersion(String PID) { | ||
return null; | ||
} | ||
|
||
|
||
@Override | ||
public void stopAgent(String PID) { | ||
doAction(PID, "stop"); | ||
} | ||
|
||
static String getVmAlias(String displayName) { | ||
|
||
for (String key : vmAliasMap.keySet()) { | ||
if (displayName.contains(key)) { | ||
return vmAliasMap.get(key); | ||
} | ||
} | ||
|
||
return displayName; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
hawtio-local-jvm-mbean/src/main/java/io/hawt/jvm/local/JVMListMBean.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,20 @@ | ||
package io.hawt.jvm.local; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* @author Stan Lewis | ||
*/ | ||
public interface JVMListMBean { | ||
|
||
List<VMDescriptorDTO> listLocalJVMs(); | ||
|
||
void startAgent(String PID); | ||
|
||
String agentStatus(String PID); | ||
|
||
String agentVersion(String PID); | ||
|
||
void stopAgent(String PID); | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
hawtio-local-jvm-mbean/src/main/java/io/hawt/jvm/local/VMDescriptorDTO.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,36 @@ | ||
package io.hawt.jvm.local; | ||
|
||
import org.jolokia.jvmagent.client.util.ProcessDescription; | ||
|
||
/** | ||
* @author Stan Lewis | ||
*/ | ||
public class VMDescriptorDTO { | ||
|
||
private ProcessDescription descriptor; | ||
private String alias; | ||
|
||
public VMDescriptorDTO(ProcessDescription descriptor) { | ||
this.descriptor = descriptor; | ||
this.alias = JVMList.getVmAlias(descriptor.getDisplay()); | ||
} | ||
|
||
public String getId() { | ||
return descriptor.getId(); | ||
} | ||
|
||
public String getAlias() { | ||
return alias; | ||
} | ||
|
||
public String getDisplayName() { | ||
return descriptor.getDisplay(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getId() + " : " + getAlias() + " (" + getDisplayName() + ")"; | ||
} | ||
|
||
|
||
} |
58 changes: 58 additions & 0 deletions
58
hawtio-local-jvm-mbean/src/test/java/io/hawt/jvm/local/JVMListTest.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,58 @@ | ||
package io.hawt.jvm.local; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* @author Stan Lewis | ||
*/ | ||
public class JVMListTest { | ||
|
||
public JVMList getJVMList() { | ||
JVMList rc = new JVMList(); | ||
rc.init(); | ||
return rc; | ||
} | ||
|
||
|
||
@Test | ||
public void testListJVMs() { | ||
List<VMDescriptorDTO> jvms = getJVMList().listLocalJVMs(); | ||
|
||
for (VMDescriptorDTO jvm : jvms) { | ||
System.out.println("JVM: " + jvm); | ||
} | ||
} | ||
|
||
@Test | ||
public void testStartAndStopAgent() { | ||
JVMList list = getJVMList(); | ||
List<VMDescriptorDTO> jvms = list.listLocalJVMs(); | ||
|
||
VMDescriptorDTO me = null; | ||
for (VMDescriptorDTO jvm : jvms) { | ||
System.out.println("JVM: " + jvm); | ||
if (jvm.getAlias().equals("Maven Surefire Test")) { | ||
me = jvm; | ||
} | ||
} | ||
|
||
Assert.assertNotNull(me); | ||
|
||
System.out.println("Starting agent in " + me.getId()); | ||
list.startAgent(me.getId()); | ||
|
||
try { | ||
Thread.sleep(1000); | ||
} catch (Exception e) { | ||
|
||
} | ||
|
||
System.out.println("Stopping agent in " + me.getId()); | ||
list.stopAgent(me.getId()); | ||
|
||
|
||
} | ||
} |
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