Search in sources :

Example 6 with CoreModule

use of com.shulie.instrument.simulator.core.CoreModule in project LinkAgent by shulieTech.

the class DefaultCoreModuleManager method forceFlush.

/**
 * 强制刷新
 * 对所有已经加载的用户模块进行强行卸载并重新加载
 *
 * @throws ModuleException 模块操作失败
 */
private void forceFlush() throws ModuleException {
    if (isInfoEnabled) {
        logger.info("SIMULATOR: force-flushing modules:{}", loadedModuleMap.keySet());
    }
    // 1. 卸载模块
    // 等待卸载的模块集合
    final Collection<CoreModule> waitingUnloadCoreModules = new ArrayList<CoreModule>();
    // 找出所有USER的模块,所以这些模块都卸载了
    for (final CoreModule coreModule : loadedModuleMap.values()) {
        // 如果判断是属于USER模块目录下的模块,则加入到待卸载模块集合,稍后统一进行卸载
        if (!isSystemModule(coreModule.getJarFile())) {
            waitingUnloadCoreModules.add(coreModule);
        }
    }
    // 记录下即将被卸载的模块ID集合
    if (isInfoEnabled) {
        final Set<String> moduleIds = new LinkedHashSet<String>();
        for (final CoreModule coreModule : waitingUnloadCoreModules) {
            moduleIds.add(coreModule.getModuleId());
        }
        if (isInfoEnabled) {
            logger.info("SIMULATOR: force-flush modules: will be unloading modules : {}", moduleIds);
        }
    }
    // 强制卸载掉所有等待卸载的模块集合中的模块
    for (final CoreModule coreModule : waitingUnloadCoreModules) {
        unload(coreModule, true);
    }
    // 2. 加载模块
    // 用户模块加载目录,加载用户模块目录下的所有模块
    // 对模块访问权限进行校验
    // 用户模块目录
    List<File> userModuleLibJars = getAllModuleLibJar(userModuleLibs);
    List<ModuleSpec> userModuleSpecs = ModuleSpecUtils.loadModuleSpecs(userModuleLibJars, false);
    loadModules(userModuleSpecs, "force-flush");
}
Also used : CoreModule(com.shulie.instrument.simulator.core.CoreModule) File(java.io.File)

Example 7 with CoreModule

use of com.shulie.instrument.simulator.core.CoreModule in project LinkAgent by shulieTech.

the class DefaultModuleManager method unload.

@Override
public void unload(String moduleId) throws ModuleException {
    final CoreModule coreModule = coreModuleManager.getThrowsExceptionIfNull(moduleId);
    coreModuleManager.unload(coreModule, false);
}
Also used : CoreModule(com.shulie.instrument.simulator.core.CoreModule)

Example 8 with CoreModule

use of com.shulie.instrument.simulator.core.CoreModule in project LinkAgent by shulieTech.

the class DefaultCoreModuleManager method unloadAll.

@Override
public void unloadAll() {
    if (isInfoEnabled) {
        logger.info("SIMULATOR: force unloading all loaded modules:{}", loadedModuleMap.keySet());
    }
    // 先卸载所有有开关依赖的模块
    List<CoreModule> modules = new ArrayList<CoreModule>(loadedModuleMap.values());
    List<CoreModule> userModules = new ArrayList<CoreModule>();
    List<CoreModule> systemModules = new ArrayList<CoreModule>();
    for (CoreModule coreModule : modules) {
        if (coreModule.getModuleSpec().isSystemModule()) {
            systemModules.add(coreModule);
        } else {
            userModules.add(coreModule);
        }
    }
    // uninstall user modules first
    uninstall(userModules);
    uninstall(systemModules);
}
Also used : CoreModule(com.shulie.instrument.simulator.core.CoreModule)

Example 9 with CoreModule

use of com.shulie.instrument.simulator.core.CoreModule in project LinkAgent by shulieTech.

the class DefaultModuleCommandInvoker method invokeCommand.

@Override
public <T> CommandResponse<T> invokeCommand(final String moduleId, String command, Map<String, String> args) {
    final CoreModule coreModule = coreModuleManager.get(moduleId);
    if (coreModule == null) {
        throw new ModuleRuntimeException(moduleId, ModuleRuntimeException.ErrorCode.MODULE_NOT_EXISTED);
    }
    // 匹配对应的方法
    final Method method = matchingModuleMethod(command, coreModule.getModule().getClass());
    if (method == null) {
        throw new ModuleRuntimeException(moduleId, ModuleRuntimeException.ErrorCode.MODULE_COMMAND_NOT_EXISTED);
    }
    // 自动释放I/O资源
    final List<Closeable> autoCloseResources = coreModule.append(new ReleaseResource<List<Closeable>>(new ArrayList<Closeable>()) {

        @Override
        public void release() {
            final List<Closeable> closeables = get();
            if (CollectionUtils.isEmpty(closeables)) {
                return;
            }
            for (final Closeable closeable : get()) {
                if (closeable instanceof Flushable) {
                    try {
                        ((Flushable) closeable).flush();
                    } catch (Exception cause) {
                        logger.warn("SIMULATOR: moduleId={} flush I/O occur error!", moduleId, cause);
                    }
                }
                try {
                    closeable.close();
                } catch (IOException e) {
                }
            }
        }
    });
    // 生成方法调用参数
    final Object[] parameterObjectArray = generateParameterObjectArray(method, args);
    final boolean isAccessible = method.isAccessible();
    final ClassLoader oriThreadContextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        method.setAccessible(true);
        Thread.currentThread().setContextClassLoader(coreModule.getClassLoaderFactory().getDefaultClassLoader());
        Object value = method.invoke(coreModule.getModule(), parameterObjectArray);
        if (logger.isDebugEnabled()) {
            logger.debug("SIMULATOR: invoke module {} method {} success.", moduleId, method.getName());
        }
        return (CommandResponse) value;
    } catch (IllegalAccessException iae) {
        logger.warn("SIMULATOR: invoke module {} method {} occur access denied.", moduleId, method.getName(), iae);
        throw new SimulatorException(iae);
    } catch (InvocationTargetException ite) {
        logger.warn("SIMULATOR: invoke module {} method {} occur error.", moduleId, method.getName(), ite.getTargetException());
        final Throwable targetCause = ite.getTargetException();
        throw new SimulatorException(targetCause);
    } finally {
        Thread.currentThread().setContextClassLoader(oriThreadContextClassLoader);
        method.setAccessible(isAccessible);
        coreModule.release(autoCloseResources);
    }
}
Also used : Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) IOException(java.io.IOException) CommandResponse(com.shulie.instrument.simulator.api.CommandResponse) Flushable(java.io.Flushable) SimulatorException(com.shulie.instrument.simulator.core.exception.SimulatorException) ModuleRuntimeException(com.shulie.instrument.simulator.api.ModuleRuntimeException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ModuleRuntimeException(com.shulie.instrument.simulator.api.ModuleRuntimeException) ArrayList(java.util.ArrayList) List(java.util.List) CoreModule(com.shulie.instrument.simulator.core.CoreModule) SimulatorException(com.shulie.instrument.simulator.core.exception.SimulatorException)

Example 10 with CoreModule

use of com.shulie.instrument.simulator.core.CoreModule in project LinkAgent by shulieTech.

the class DefaultModuleManager method frozen.

@Override
public void frozen(String moduleId) throws ModuleException {
    final CoreModule coreModule = coreModuleManager.getThrowsExceptionIfNull(moduleId);
    coreModuleManager.frozen(coreModule, false);
}
Also used : CoreModule(com.shulie.instrument.simulator.core.CoreModule)

Aggregations

CoreModule (com.shulie.instrument.simulator.core.CoreModule)10 IOException (java.io.IOException)3 Closeable (java.io.Closeable)2 File (java.io.File)2 Flushable (java.io.Flushable)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CommandResponse (com.shulie.instrument.simulator.api.CommandResponse)1 ModuleRuntimeException (com.shulie.instrument.simulator.api.ModuleRuntimeException)1 ClassLoaderFactory (com.shulie.instrument.simulator.core.classloader.ClassLoaderFactory)1 SimulatorException (com.shulie.instrument.simulator.core.exception.SimulatorException)1 ServletException (javax.servlet.ServletException)1