Search in sources :

Example 1 with ModuleRuntimeException

use of com.shulie.instrument.simulator.api.ModuleRuntimeException 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 2 with ModuleRuntimeException

use of com.shulie.instrument.simulator.api.ModuleRuntimeException in project LinkAgent by shulieTech.

the class ClassLoaderFactoryImpl method getClassLoader.

@Override
public ClassLoader getClassLoader(ClassLoader businessClassLoader) {
    if (!isMiddlewareModule) {
        return defaultClassLoader;
    }
    try {
        int id = ObjectIdUtils.identity(businessClassLoader);
        /**
         * 如果还没有默认的模块业务类加载器,则将默认模块业务类加载器设置成当前请求的业务类加载器
         * 返回默认模块类加载器
         */
        if (defaultBizClassLoaderRef.compareAndSet(null, id)) {
            return defaultClassLoader;
        }
        /**
         * 如果默认的模块业务类加载器与当前请求的业务类加载器一致,则使用默认模块类加载器
         */
        if (defaultBizClassLoaderRef.get() == id) {
            return defaultClassLoader;
        }
        ModuleClassLoader moduleClassLoader = this.classLoaderCache.get(id);
        if (moduleClassLoader != null) {
            return moduleClassLoader;
        }
        moduleClassLoader = new ModuleClassLoader(classLoaderService, moduleJarFile, moduleId, businessClassLoader == null ? null : businessClassLoader.toString());
        ModuleClassLoader oldModuleClassLoader = classLoaderCache.putIfAbsent(id, moduleClassLoader);
        if (oldModuleClassLoader != null) {
            moduleClassLoader.closeIfPossible();
            moduleClassLoader = oldModuleClassLoader;
        }
        return moduleClassLoader;
    } catch (IOException e) {
        throw new ModuleRuntimeException("SIMULATOR: getModuleClassLoader err", ModuleRuntimeException.ErrorCode.MODULE_LOAD_ERROR);
    }
}
Also used : ModuleRuntimeException(com.shulie.instrument.simulator.api.ModuleRuntimeException) ModuleClassLoader(com.shulie.instrument.simulator.core.classloader.ModuleClassLoader) IOException(java.io.IOException)

Aggregations

ModuleRuntimeException (com.shulie.instrument.simulator.api.ModuleRuntimeException)2 IOException (java.io.IOException)2 CommandResponse (com.shulie.instrument.simulator.api.CommandResponse)1 CoreModule (com.shulie.instrument.simulator.core.CoreModule)1 ModuleClassLoader (com.shulie.instrument.simulator.core.classloader.ModuleClassLoader)1 SimulatorException (com.shulie.instrument.simulator.core.exception.SimulatorException)1 Closeable (java.io.Closeable)1 Flushable (java.io.Flushable)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1