Search in sources :

Example 1 with SimulatorException

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

the class ModuleJarLoader method load.

void load(final SimulatorConfig simulatorConfig, final ModuleLoadCallback moduleLoadCallback) throws IOException {
    boolean hasModuleLoadedSuccessFlag = false;
    ClassLoaderFactory classLoaderFactory = null;
    if (isInfoEnabled) {
        logger.info("SIMULATOR: prepare loading module-jar={};", moduleJarFile);
    }
    try {
        classLoaderFactory = classLoaderService.getModuleClassLoaderFactory(moduleSpec.getModuleId());
        if (classLoaderFactory == null) {
            throw new SimulatorException("can't found ModuleClassLoaderFactory. moduleId=" + moduleSpec.getModuleId());
        }
        final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
        ClassLoader moduleClassLoader = classLoaderFactory.getDefaultClassLoader();
        Thread.currentThread().setContextClassLoader(moduleClassLoader);
        try {
            hasModuleLoadedSuccessFlag = loadingModules(simulatorConfig, classLoaderFactory, moduleLoadCallback);
        } finally {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    } finally {
        if (!hasModuleLoadedSuccessFlag && null != classLoaderFactory) {
            logger.warn("SIMULATOR: loading module-jar completed, but NONE module loaded, will be close ModuleClassLoader. module-jar={};", moduleJarFile);
            classLoaderFactory.release();
        }
    }
}
Also used : ClassLoaderFactory(com.shulie.instrument.simulator.core.classloader.ClassLoaderFactory) SimulatorException(com.shulie.instrument.simulator.core.exception.SimulatorException)

Example 2 with SimulatorException

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

the class URLClassLoaderHandler method injectClass.

@Override
@SuppressWarnings("unchecked")
public void injectClass(ClassLoader classLoader, String className) {
    try {
        if (classLoader instanceof URLClassLoader) {
            final URLClassLoader urlClassLoader = (URLClassLoader) classLoader;
            injectClass0(urlClassLoader, className);
            return;
        }
    } catch (Exception e) {
        logger.warn("SIMULATOR: Failed to load plugin class {} with classLoader {}", className, classLoader, e);
        throw new SimulatorException("Failed to load plugin class " + className + " with classLoader " + classLoader, e);
    }
    throw new SimulatorException("invalid ClassLoader");
}
Also used : URLClassLoader(java.net.URLClassLoader) SimulatorException(com.shulie.instrument.simulator.core.exception.SimulatorException) SimulatorException(com.shulie.instrument.simulator.core.exception.SimulatorException) MalformedURLException(java.net.MalformedURLException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with SimulatorException

use of com.shulie.instrument.simulator.core.exception.SimulatorException 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)

Aggregations

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