From 895d12a1e162ffe49d2fa8a6f203f631718aa740 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=9C=E7=90=A8?= Date: Mon, 12 Mar 2018 17:10:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20#67=20#69?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jvm/sandbox/agent/AgentLauncher.java | 39 ++++++----- .../core/manager/impl/DefaultConfigInfo.java | 3 +- .../sandbox/core/server/ProxyCoreServer.java | 64 +++++++++++++++++++ .../core/server/jetty/JettyCoreServer.java | 4 +- 4 files changed, 90 insertions(+), 20 deletions(-) create mode 100644 sandbox-core/src/main/java/com/alibaba/jvm/sandbox/core/server/ProxyCoreServer.java diff --git a/sandbox-agent/src/main/java/com/alibaba/jvm/sandbox/agent/AgentLauncher.java b/sandbox-agent/src/main/java/com/alibaba/jvm/sandbox/agent/AgentLauncher.java index e62f816b..c9e0e4c7 100755 --- a/sandbox-agent/src/main/java/com/alibaba/jvm/sandbox/agent/AgentLauncher.java +++ b/sandbox-agent/src/main/java/com/alibaba/jvm/sandbox/agent/AgentLauncher.java @@ -14,7 +14,12 @@ /** * SandboxAgent启动器 - * Created by luanjia@taobao.com on 16/7/30. + * + * + * @author luanjia@taobao.com */ public class AgentLauncher { @@ -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) { @@ -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") @@ -97,7 +101,8 @@ private static String getSandboxProviderPath(String sandboxHome) { = new ConcurrentHashMap(); 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"; /** @@ -258,35 +263,35 @@ private static synchronized InetSocketAddress main(final Map 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) { diff --git a/sandbox-core/src/main/java/com/alibaba/jvm/sandbox/core/manager/impl/DefaultConfigInfo.java b/sandbox-core/src/main/java/com/alibaba/jvm/sandbox/core/manager/impl/DefaultConfigInfo.java index d5c45aa1..8dfaff0f 100755 --- a/sandbox-core/src/main/java/com/alibaba/jvm/sandbox/core/manager/impl/DefaultConfigInfo.java +++ b/sandbox-core/src/main/java/com/alibaba/jvm/sandbox/core/manager/impl/DefaultConfigInfo.java @@ -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; @@ -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); } diff --git a/sandbox-core/src/main/java/com/alibaba/jvm/sandbox/core/server/ProxyCoreServer.java b/sandbox-core/src/main/java/com/alibaba/jvm/sandbox/core/server/ProxyCoreServer.java new file mode 100644 index 00000000..18b5b3fe --- /dev/null +++ b/sandbox-core/src/main/java/com/alibaba/jvm/sandbox/core/server/ProxyCoreServer.java @@ -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 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); + } + } + +} diff --git a/sandbox-core/src/main/java/com/alibaba/jvm/sandbox/core/server/jetty/JettyCoreServer.java b/sandbox-core/src/main/java/com/alibaba/jvm/sandbox/core/server/jetty/JettyCoreServer.java index 72e02bf5..d6c317f5 100755 --- a/sandbox-core/src/main/java/com/alibaba/jvm/sandbox/core/server/jetty/JettyCoreServer.java +++ b/sandbox-core/src/main/java/com/alibaba/jvm/sandbox/core/server/jetty/JettyCoreServer.java @@ -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 @@ -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(