Skip to content

Commit

Permalink
重构模块管理内核
Browse files Browse the repository at this point in the history
  • Loading branch information
dongchenxu committed Jan 6, 2019
1 parent bb04366 commit 1e73ca9
Show file tree
Hide file tree
Showing 45 changed files with 1,291 additions and 1,540 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,14 @@
/**
* SandboxAgent启动器
* <ul>
* <li>这个类的所有静态属性都必须和版本、环境无关</li>
* <li>这个类删除、修改方法时必须考虑多版本情况下,兼容性问题!</li>
* <li>这个类的所有静态属性都必须和版本、环境无关</li>
* <li>这个类删除、修改方法时必须考虑多版本情况下,兼容性问题!</li>
* </ul>
*
* @author luanjia@taobao.com
*/
public class AgentLauncher {

// // sandbox配置文件目录
// private static final String SANDBOX_CFG_PATH
// = SANDBOX_HOME + File.separatorChar + "cfg";
//
// // 模块目录
// private static final String SANDBOX_MODULE_PATH
// = SANDBOX_HOME + File.separatorChar + "module";
//
//
// // sandbox核心工程文件
// private static final String SANDBOX_CORE_JAR_PATH
// = SANDBOX_HOME + File.separatorChar + "lib" + File.separator + "sandbox-core.jar";
//
// // sandbox-spy工程文件
// private static final String SANDBOX_SPY_JAR_PATH
// = SANDBOX_HOME + File.separatorChar + "lib" + File.separator + "sandbox-spy.jar";
//
// private static final String SANDBOX_PROPERTIES_PATH
// = SANDBOX_CFG_PATH + File.separator + "sandbox.properties";
//
// // sandbox-provider库目录
// private static final String SANDBOX_PROVIDER_LIB_PATH
// = SANDBOX_HOME + File.separatorChar + "provider";


private static String getSandboxCfgPath(String sandboxHome) {
return sandboxHome + File.separatorChar + "cfg";
}
Expand Down Expand Up @@ -114,7 +89,7 @@ private static String getSandboxProviderPath(String sandboxHome) {
*/
public static void premain(String featureString, Instrumentation inst) {
LAUNCH_MODE = LAUNCH_MODE_AGENT;
main(toFeatureMap(featureString), inst);
install(toFeatureMap(featureString), inst);
}

/**
Expand All @@ -130,7 +105,7 @@ public static void agentmain(String featureString, Instrumentation inst) {
writeAttachResult(
getNamespace(featureMap),
getToken(featureMap),
main(featureMap, inst)
install(featureMap, inst)
);
}

Expand Down Expand Up @@ -202,40 +177,37 @@ private static synchronized ClassLoader loadOrDefineClassLoader(final String nam
}

/**
* 获取当前命名空间下的ClassLoader
* <p>
* 该方法将会被{@code ControlModule#shutdown}通过反射调用,
* 请保持方法声明一致
* 删除指定命名空间下的jvm-sandbox
*
* @param namespace 命名空间
* @return 当前的ClassLoader
* @since {@code sandbox-api:1.0.15}
* @param namespace 指定命名空间
* @throws Throwable 删除失败
*/
@SuppressWarnings("unused")
public static ClassLoader getClassLoader(final String namespace) {
return sandboxClassLoaderMap.get(namespace);
public static synchronized void uninstall(final String namespace) throws Throwable {
final SandboxClassLoader sandboxClassLoader = sandboxClassLoaderMap.get(namespace);
if (null == sandboxClassLoader) {
return;
}

// 关闭服务器
final Class<?> classOfProxyServer = sandboxClassLoader.loadClass(CLASS_OF_PROXY_CORE_SERVER);
classOfProxyServer.getMethod("destroy")
.invoke(classOfProxyServer.getMethod("getInstance").invoke(null));

// 关闭SandboxClassLoader
sandboxClassLoader.closeIfPossible();
sandboxClassLoaderMap.remove(namespace);
}

/**
* 清理namespace所指定的ClassLoader
* <p>
* 该方法将会被{@code ControlModule#shutdown}通过反射调用,
* 请保持方法声明一致
* 在当前JVM安装jvm-sandbox
*
* @param namespace 命名空间
* @return 被清理的ClassLoader
* @param featureMap 启动参数配置
* @param inst inst
* @return 服务器IP:PORT
*/
@SuppressWarnings("unused")
public static synchronized ClassLoader cleanClassLoader(final String namespace) {
final SandboxClassLoader sandboxClassLoader = sandboxClassLoaderMap.remove(namespace);
if (null != sandboxClassLoader) {
sandboxClassLoader.closeIfPossible();
}
return sandboxClassLoader;
}

private static synchronized InetSocketAddress main(final Map<String, String> featureMap,
final Instrumentation inst) {
private static synchronized InetSocketAddress install(final Map<String, String> featureMap,
final Instrumentation inst) {

final String namespace = getNamespace(featureMap);
final String propertiesFilePath = getPropertiesFilePath(featureMap);
Expand All @@ -250,39 +222,39 @@ private static synchronized InetSocketAddress main(final Map<String, String> fea
)));

// 构造自定义的类加载器,尽量减少Sandbox对现有工程的侵蚀
final ClassLoader agentLoader = loadOrDefineClassLoader(
final ClassLoader sandboxClassLoader = loadOrDefineClassLoader(
namespace,
getSandboxCoreJarPath(getSandboxHome(featureMap))
// SANDBOX_CORE_JAR_PATH
);

// CoreConfigure类定义
final Class<?> classOfConfigure = agentLoader.loadClass(CLASS_OF_CORE_CONFIGURE);
final Class<?> classOfConfigure = sandboxClassLoader.loadClass(CLASS_OF_CORE_CONFIGURE);

// 反序列化成CoreConfigure类实例
final Object objectOfCoreConfigure = classOfConfigure.getMethod("toConfigure", String.class, String.class)
.invoke(null, coreFeatureString, propertiesFilePath);

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

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

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


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

Expand All @@ -291,7 +263,7 @@ private static synchronized InetSocketAddress main(final Map<String, String> fea
// 返回服务器绑定的地址
return (InetSocketAddress) classOfProxyServer
.getMethod("getLocal")
.invoke(objectOfCoreServer);
.invoke(objectOfProxyServer);


} catch (Throwable cause) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
*/
public interface ModuleJarLifeCycleProvider {

/**
* 等待
* @param loaded
* @param inComing
* @return
*/
ClassLoader waitingFor(ClassLoader[] loaded, ClassLoader inComing);

/**
* 模块Jar文件卸载完所有模块后,正式卸载Jar文件之前之后调用!
*/
Expand Down
Loading

0 comments on commit 1e73ca9

Please sign in to comment.