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);
}
}
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);
}
}
Aggregations