Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
杜琨 committed Mar 12, 2018
1 parent 07f2992 commit 895d12a
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@

/**
* SandboxAgent启动器
* Created by luanjia@taobao.com on 16/7/30.
* <ul>
* <li>这个类的所有静态属性都必须和版本、环境无关</li>
* <li>这个类删除、修改方法时必须考虑多版本情况下,兼容性问题!</li>
* </ul>
*
* @author luanjia@taobao.com
*/
public class AgentLauncher {

Expand Down Expand Up @@ -56,11 +61,11 @@ private static String getSandboxCoreJarPath(String sandboxHome) {
}

private static String getSandboxSpyJarPath(String sandboxHome) {
return getSandboxCfgPath(sandboxHome) + File.separatorChar + "lib" + File.separator + "sandbox-spy.jar";
return sandboxHome + File.separatorChar + "lib" + File.separator + "sandbox-spy.jar";
}

private static String getSandboxPropertiesPath(String sandboxHome) {
return sandboxHome + File.separator + "sandbox.properties";
return getSandboxCfgPath(sandboxHome) + File.separator + "sandbox.properties";
}

private static String getSandboxProviderPath(String sandboxHome) {
Expand All @@ -69,11 +74,10 @@ private static String getSandboxProviderPath(String sandboxHome) {


// sandbox默认主目录
private static final String SANDBOX_HOME
private static final String DEFAULT_SANDBOX_HOME
= new File(AgentLauncher.class.getProtectionDomain().getCodeSource().getLocation().getFile())
.getParentFile()
.getParent();
private static final String DEFAULT_SANDBOX_HOME = SANDBOX_HOME;

private static final String SANDBOX_USER_MODULE_PATH
= System.getProperties().getProperty("user.home")
Expand All @@ -97,7 +101,8 @@ private static String getSandboxProviderPath(String sandboxHome) {
= new ConcurrentHashMap<String, SandboxClassLoader>();

private static final String CLASS_OF_CORE_CONFIGURE = "com.alibaba.jvm.sandbox.core.CoreConfigure";
private static final String CLASS_OF_JETTY_CORE_SERVER = "com.alibaba.jvm.sandbox.core.server.jetty.JettyCoreServer";
// private static final String CLASS_OF_JETTY_CORE_SERVER = "com.alibaba.jvm.sandbox.core.server.jetty.JettyCoreServer";
private static final String CLASS_OF_PROXY_CORE_SERVER = "com.alibaba.jvm.sandbox.core.server.ProxyCoreServer";


/**
Expand Down Expand Up @@ -258,35 +263,35 @@ private static synchronized InetSocketAddress main(final Map<String, String> fea
final Object objectOfCoreConfigure = classOfConfigure.getMethod("toConfigure", String.class, String.class)
.invoke(null, coreFeatureString, propertiesFilePath);

// JtServer类定义
final Class<?> classOfJtServer = agentLoader.loadClass(CLASS_OF_JETTY_CORE_SERVER);
// CoreServer类定义
final Class<?> classOfProxyServer = agentLoader.loadClass(CLASS_OF_PROXY_CORE_SERVER);

// 获取JtServer单例
final Object objectOfJtServer = classOfJtServer
// 获取CoreServer单例
final Object objectOfCoreServer = classOfProxyServer
.getMethod("getInstance")
.invoke(null);

// gaServer.isBind()
final boolean isBind = (Boolean) classOfJtServer.getMethod("isBind").invoke(objectOfJtServer);
// CoreServer.isBind()
final boolean isBind = (Boolean) classOfProxyServer.getMethod("isBind").invoke(objectOfCoreServer);


// 如果未绑定,则需要绑定一个地址
if (!isBind) {
try {
classOfJtServer
classOfProxyServer
.getMethod("bind", classOfConfigure, Instrumentation.class)
.invoke(objectOfJtServer, objectOfCoreConfigure, inst);
.invoke(objectOfCoreServer, objectOfCoreConfigure, inst);
} catch (Throwable t) {
classOfJtServer.getMethod("destroy").invoke(objectOfJtServer);
classOfProxyServer.getMethod("destroy").invoke(objectOfCoreServer);
throw t;
}

}

// 返回服务器绑定的地址
return (InetSocketAddress) classOfJtServer
return (InetSocketAddress) classOfProxyServer
.getMethod("getLocal")
.invoke(objectOfJtServer);
.invoke(objectOfCoreServer);


} catch (Throwable cause) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.alibaba.jvm.sandbox.api.Information;
import com.alibaba.jvm.sandbox.api.resource.ConfigInfo;
import com.alibaba.jvm.sandbox.core.CoreConfigure;
import com.alibaba.jvm.sandbox.core.server.ProxyCoreServer;
import com.alibaba.jvm.sandbox.core.server.jetty.JettyCoreServer;
import org.apache.commons.io.IOUtils;

Expand Down Expand Up @@ -113,7 +114,7 @@ public int getEventPoolMaxTotalPerEvent() {
@Override
public InetSocketAddress getServerAddress() {
try {
return JettyCoreServer.getInstance().getLocal();
return ProxyCoreServer.getInstance().getLocal();
} catch (Throwable cause) {
return new InetSocketAddress("0.0.0.0", 0);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.alibaba.jvm.sandbox.core.server;

import com.alibaba.jvm.sandbox.core.CoreConfigure;
import com.alibaba.jvm.sandbox.core.server.jetty.JettyCoreServer;

import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.net.InetSocketAddress;

public class ProxyCoreServer implements CoreServer {

private final static Class<? extends CoreServer> classOfCoreServerImpl
= JettyCoreServer.class;

private final CoreServer proxy;

private ProxyCoreServer(CoreServer proxy) {
this.proxy = proxy;
}


@Override
public boolean isBind() {
return proxy.isBind();
}

@Override
public void unbind() throws IOException {
proxy.unbind();
}

@Override
public InetSocketAddress getLocal() throws IOException {
return proxy.getLocal();
}

@Override
public void bind(CoreConfigure cfg, Instrumentation inst) throws IOException {
proxy.bind(cfg, inst);
}

@Override
public void destroy() {
proxy.destroy();
}

@Override
public String toString() {
return "proxy:" + proxy.toString();
}

public static CoreServer getInstance() {
try {
return new ProxyCoreServer(
(CoreServer) classOfCoreServerImpl
.getMethod("getInstance")
.invoke(null)
);
} catch (Throwable cause) {
throw new RuntimeException(cause);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,6 @@ private void initHttpServer(final CoreConfigure cfg) {
final QueuedThreadPool qtp = (QueuedThreadPool) httpServer.getThreadPool();
qtp.setName("sandbox-jetty-qtp" + qtp.hashCode());
}

logger.info("init httpSrv success. bind to {}:{}", cfg.getServerIp(), cfg.getServerPort());
}

// 初始化各种manager
Expand Down Expand Up @@ -227,6 +225,8 @@ public void process() throws Throwable {
httpServer.start();
}
});
final InetSocketAddress local = getLocal();
logger.info("init httpSrv success. bind to {}:{}", local.getHostName(), local.getPort());
} catch (Throwable cause) {
logger.warn("{} bind failed.", this);
throw new IOException(
Expand Down

0 comments on commit 895d12a

Please sign in to comment.