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