Search in sources :

Example 1 with CommandGroup

use of com.terran4j.commons.jfinger.CommandGroup in project commons by terran4j.

the class CommandLineApplicationListener method onApplicationEvent.

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
    ApplicationContext app = event.getApplicationContext();
    // 从配置上判断用户是否禁用了命令行服务。
    String cliDisable = app.getEnvironment().getProperty(PROP_KEY_DISABLE);
    if (cliDisable != null && cliDisable.trim().equalsIgnoreCase("true")) {
        if (log.isWarnEnabled()) {
            log.warn("Command Line Service was disable, will not be started.");
        }
        return;
    }
    // 如果没有任何命令被注册,也不需要启用命令行服务。
    Map<String, Object> beans = app.getBeansWithAnnotation(CommandGroup.class);
    if (beans == null || beans.size() == 0) {
        if (log.isWarnEnabled()) {
            // 
            log.warn(// 
            "No Bean has Annotation: @{}. Command Line Service will not be started.", CommandGroup.class.getSimpleName());
        }
        return;
    }
    // 获取提示符。
    String cliPrompt = app.getEnvironment().getProperty(PROP_KEY_PROMPT);
    if (!StringUtils.isEmpty(cliPrompt)) {
        cliPrompt = cliPrompt.trim();
    } else {
        cliPrompt = PROMPT_DEFAULT;
    }
    PROMPT = cliPrompt;
    // 解析所有的命令行。
    CommandGroups groups = new CommandGroups();
    Iterator<String> it = beans.keySet().iterator();
    while (it.hasNext()) {
        String beanName = it.next();
        Object bean = app.getBean(beanName);
        Class<?> beanClass = bean.getClass();
        List<Method> commandMethods = getCommandMethod(beanClass);
        if (commandMethods.size() == 0) {
            continue;
        }
        // 解析命令组。
        CommandGroup commandGroup = beanClass.getAnnotation(CommandGroup.class);
        CommandGroupDefine commandGroupDefine = new CommandGroupDefine(commandGroup);
        if (StringUtils.isEmpty(commandGroup.name())) {
            String groupName = beanName;
            if (groupName.endsWith("Command") && groupName.length() > "Command".length()) {
                groupName = groupName.substring(0, groupName.length() - "Command".length());
            }
            commandGroupDefine.setName(groupName);
        }
        // 解析通用命令选项。
        List<CommandOptionDefine> commonOptions = new ArrayList<CommandOptionDefine>();
        CommandOption[] commandGroupOptions = commandGroup.options();
        if (commandGroupOptions != null && commandGroupOptions.length > 0) {
            for (CommandOption commandOption : commandGroupOptions) {
                CommandOptionDefine commonOption = new CommandOptionDefine(commandOption);
                commonOptions.add(commonOption);
            }
        }
        // 解析本组命令。
        for (Method commandMethod : commandMethods) {
            CommandDefine commandDefine = new CommandDefine(commandGroupDefine, bean, commandMethod, beanName);
            commandDefine.addOptions(commonOptions);
            commandGroupDefine.addCommand(commandDefine);
        }
        // 记录下本命令组。
        groups.addCommandGroup(commandGroupDefine);
    }
    service = new CommandLineService();
    service.setCommands(groups);
    service.setPrompt(cliPrompt);
    thread = new CommandLineTask(service);
    // 线程一直存在,不让主进程退出。
    thread.setDaemon(false);
    thread.setName("JFinger Demo Thread");
    thread.start();
    if (log.isInfoEnabled()) {
        log.info("Command Line Service is started.");
    }
}
Also used : CommandOption(com.terran4j.commons.jfinger.CommandOption) CommandGroup(com.terran4j.commons.jfinger.CommandGroup) CommandGroups(com.terran4j.commons.jfinger.CommandGroups) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) CommandOptionDefine(com.terran4j.commons.jfinger.CommandOptionDefine) ApplicationContext(org.springframework.context.ApplicationContext) CommandDefine(com.terran4j.commons.jfinger.CommandDefine) CommandGroupDefine(com.terran4j.commons.jfinger.CommandGroupDefine)

Aggregations

CommandDefine (com.terran4j.commons.jfinger.CommandDefine)1 CommandGroup (com.terran4j.commons.jfinger.CommandGroup)1 CommandGroupDefine (com.terran4j.commons.jfinger.CommandGroupDefine)1 CommandGroups (com.terran4j.commons.jfinger.CommandGroups)1 CommandOption (com.terran4j.commons.jfinger.CommandOption)1 CommandOptionDefine (com.terran4j.commons.jfinger.CommandOptionDefine)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 ApplicationContext (org.springframework.context.ApplicationContext)1