use of com.shulie.instrument.simulator.api.ModuleInfo in project LinkAgent by shulieTech.
the class ModuleJarLoader method loadingModules.
private boolean loadingModules(final SimulatorConfig simulatorConfig, final ClassLoaderFactory moduleClassLoader, final ModuleLoadCallback moduleLoadCallback) {
final ServiceLoader<ExtensionModule> moduleServiceLoader = ServiceLoader.load(ExtensionModule.class, moduleClassLoader.getDefaultClassLoader());
final Iterator<ExtensionModule> it = moduleServiceLoader.iterator();
List<ExtensionModule> moduleList = new ArrayList<ExtensionModule>();
while (it.hasNext()) {
final ExtensionModule module;
try {
module = it.next();
} catch (Throwable cause) {
logger.warn("SIMULATOR: loading module instance failed: instance occur error, will be ignored. module-jar={}", moduleJarFile, cause);
continue;
}
final Class<?> classOfModule = module.getClass();
// 判断模块是否实现了@ModuleInfo标记
if (!classOfModule.isAnnotationPresent(ModuleInfo.class)) {
logger.warn("SIMULATOR: loading module instance failed: not implements @Information, will be ignored. class={};module-jar={};", classOfModule, moduleJarFile);
continue;
}
final ModuleInfo info = classOfModule.getAnnotation(ModuleInfo.class);
// 判断模块要求的启动模式和容器的启动模式是否匹配
if (!ArrayUtils.contains(info.supportedModes(), loadMode)) {
logger.warn("SIMULATOR: loading module instance failed: launch-mode is not match module required, will be ignored. module={};launch-mode={};required-mode={};class={};module-jar={};", moduleSpec.getModuleId(), loadMode, com.shulie.instrument.simulator.api.util.ArrayUtils.join(moduleSpec.getSupportedModes()), classOfModule, moduleJarFile);
continue;
}
if (module != null) {
moduleList.add(module);
}
}
if (moduleList.size() != 1) {
logger.warn("SIMULATOR: loading module has more than 1 module definition. {};", moduleJarFile);
return false;
}
boolean loadingSuccess = false;
for (ExtensionModule module : moduleList) {
final Class<?> classOfModule = module.getClass();
final ModuleInfo info = classOfModule.getAnnotation(ModuleInfo.class);
moduleSpec.setClassOfModule(classOfModule);
moduleSpec.setModule(module);
moduleSpec.loadModuleInfo(info);
try {
if (null != moduleLoadCallback) {
moduleLoadCallback.onLoad(simulatorConfig, moduleSpec, moduleSpec.getClassOfModule(), moduleSpec.getModule(), moduleJarFile, moduleClassLoader);
}
loadingSuccess = true;
} catch (Throwable cause) {
logger.warn("SIMULATOR: loading module instance failed: MODULE-LOADER-PROVIDER denied, will be ignored. module={};class={};module-jar={};", moduleSpec.getModuleId(), moduleSpec.getClassOfModule(), moduleJarFile, cause);
continue;
}
}
if (isInfoEnabled) {
logger.info("SIMULATOR: loaded module-jar completed, loaded module in module-jar={}, modules={}", moduleJarFile, moduleSpec.getModuleId());
}
return loadingSuccess;
}
use of com.shulie.instrument.simulator.api.ModuleInfo in project LinkAgent by shulieTech.
the class InfoModule method modules.
@Command(value = "commands", description = "查看所有的命令")
public CommandResponse modules() {
try {
List<CommandInfo> commands = new ArrayList<CommandInfo>();
for (ExtensionModule module : moduleManager.list()) {
Class<?> classOfModule = module.getClass();
// 判断模块是否实现了@Information标记
if (!classOfModule.isAnnotationPresent(ModuleInfo.class)) {
continue;
}
final ModuleInfo info = classOfModule.getAnnotation(ModuleInfo.class);
if (info == null) {
continue;
}
for (final Method method : getMethodsListWithAnnotation(module.getClass(), Command.class)) {
final Command commandAnnotation = method.getAnnotation(Command.class);
if (null == commandAnnotation) {
continue;
}
CommandInfo commandInfo = new CommandInfo();
commandInfo.setModuleId(info.id());
commandInfo.setCommand(commandAnnotation.value());
commandInfo.setCommandDescription(commandAnnotation.description());
commands.add(commandInfo);
}
}
return CommandResponse.success(commands);
} catch (Throwable e) {
LOGGER.error("SIMULATOR: execute command info/commands error.", e);
return CommandResponse.failure(e);
}
}
use of com.shulie.instrument.simulator.api.ModuleInfo in project LinkAgent by shulieTech.
the class DefaultModuleManager method listModuleSpecs.
@Override
public Collection<ModuleSpec> listModuleSpecs() {
Set<ModuleSpec> set = new HashSet<ModuleSpec>();
Collection<ExtensionModule> modules = list();
for (ExtensionModule module : modules) {
final ModuleInfo info = module.getClass().getAnnotation(ModuleInfo.class);
ModuleSpec moduleSpec = getModuleSpec(info.id());
if (moduleSpec != null) {
set.add(moduleSpec);
}
}
return set;
}
Aggregations