Skip to content

Commit

Permalink
Merge pull request alibaba#72 from feshfans/master
Browse files Browse the repository at this point in the history
CoreLauncher 代码优化
  • Loading branch information
oldmanpushcart authored Jun 17, 2018
2 parents 9cf89a3 + 619c728 commit 8cf106a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
7 changes: 7 additions & 0 deletions sandbox-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
<dependency>
<groupId>com.alibaba.jvm.sandbox</groupId>
<artifactId>sandbox-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.alibaba.jvm.sandbox.core;

import com.sun.tools.attach.VirtualMachine;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

import static com.alibaba.jvm.sandbox.core.util.SandboxStringUtils.getCauseMessage;

Expand All @@ -12,6 +12,7 @@
*/
public class CoreLauncher {


public CoreLauncher(final String targetJvmPid,
final String agentJarPath,
final String token) throws Exception {
Expand Down Expand Up @@ -40,8 +41,7 @@ public static void main(String[] args) {
throw new IllegalArgumentException("illegal args");
}

// call the core launcher
new CoreLauncher(args[0], args[1], args[2]);
new CoreLauncher(args[0],args[1],args[2]);
} catch (Throwable t) {
t.printStackTrace(System.err);
System.err.println("sandbox load jvm failed : " + getCauseMessage(t));
Expand All @@ -54,32 +54,17 @@ private void attachAgent(final String targetJvmPid,
final String agentJarPath,
final String cfg) throws Exception {

final ClassLoader loader = Thread.currentThread().getContextClassLoader();
final Class<?> vmdClass = loader.loadClass("com.sun.tools.attach.VirtualMachineDescriptor");
final Class<?> vmClass = loader.loadClass("com.sun.tools.attach.VirtualMachine");
VirtualMachine vmObj = null;
try {

Object attachVmdObj = null;
for (Object obj : (List<?>) vmClass.getMethod("list", (Class<?>[]) null).invoke(null, (Object[]) null)) {
if ((vmdClass.getMethod("id", (Class<?>[]) null).invoke(obj, (Object[]) null))
.equals(targetJvmPid)) {
attachVmdObj = obj;
vmObj = VirtualMachine.attach(targetJvmPid);
if(vmObj!=null){
vmObj.loadAgent(agentJarPath,cfg);
}
}

Object vmObj = null;
try {
// 使用 attach(String pid) 这种方式
if (null == attachVmdObj) {
vmObj = vmClass.getMethod("attach", String.class).invoke(null, targetJvmPid);
} else {
vmObj = vmClass.getMethod("attach", vmdClass).invoke(null, attachVmdObj);
}
vmClass
.getMethod("loadAgent", String.class, String.class)
.invoke(vmObj, agentJarPath, cfg);
} finally {
if (null != vmObj) {
vmClass.getMethod("detach", (Class<?>[]) null).invoke(vmObj, (Object[]) null);
vmObj.detach();
}
}

Expand Down

0 comments on commit 8cf106a

Please sign in to comment.