Search in sources :

Example 1 with ExtensionModule

use of com.shulie.instrument.simulator.api.ExtensionModule 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;
}
Also used : ModuleInfo(com.shulie.instrument.simulator.api.ModuleInfo) ExtensionModule(com.shulie.instrument.simulator.api.ExtensionModule) ArrayList(java.util.ArrayList)

Example 2 with ExtensionModule

use of com.shulie.instrument.simulator.api.ExtensionModule 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);
    }
}
Also used : CommandInfo(com.shulie.instrument.simulator.module.model.info.CommandInfo) ModuleInfo(com.shulie.instrument.simulator.api.ModuleInfo) Command(com.shulie.instrument.simulator.api.annotation.Command) ArrayList(java.util.ArrayList) ExtensionModule(com.shulie.instrument.simulator.api.ExtensionModule) Method(java.lang.reflect.Method) Command(com.shulie.instrument.simulator.api.annotation.Command)

Example 3 with ExtensionModule

use of com.shulie.instrument.simulator.api.ExtensionModule 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;
}
Also used : ModuleSpec(com.shulie.instrument.simulator.api.ModuleSpec) ModuleInfo(com.shulie.instrument.simulator.api.ModuleInfo) ExtensionModule(com.shulie.instrument.simulator.api.ExtensionModule) HashSet(java.util.HashSet)

Aggregations

ExtensionModule (com.shulie.instrument.simulator.api.ExtensionModule)3 ModuleInfo (com.shulie.instrument.simulator.api.ModuleInfo)3 ArrayList (java.util.ArrayList)2 ModuleSpec (com.shulie.instrument.simulator.api.ModuleSpec)1 Command (com.shulie.instrument.simulator.api.annotation.Command)1 CommandInfo (com.shulie.instrument.simulator.module.model.info.CommandInfo)1 Method (java.lang.reflect.Method)1 HashSet (java.util.HashSet)1