Search in sources :

Example 1 with CommandOptionDefine

use of com.terran4j.commons.jfinger.CommandOptionDefine 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)

Example 2 with CommandOptionDefine

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

the class CommandLineService method execute.

private void execute(String groupName, String commandName, CommandDefine command, String[] args, PrintStream out) throws BusinessException {
    Options options = new Options();
    List<CommandOptionDefine> optionConfigs = command.getOptions();
    if (optionConfigs != null && optionConfigs.size() > 0) {
        for (CommandOptionDefine optionConfig : optionConfigs) {
            String key = optionConfig.getKey();
            String name = optionConfig.getName();
            String desc = optionConfig.getDesc();
            OptionType type = optionConfig.getType();
            if (type == OptionType.BOOLEAN) {
                options.addOption(key, name, false, desc);
            } else if (type == OptionType.PROPERTIES) {
                Option option = // 
                Option.builder(key).argName("property=value").numberOfArgs(5).valueSeparator('=').desc(// 
                desc).build();
                options.addOption(option);
            } else {
                Builder builder = Option.builder(key).hasArg();
                if (!StringUtils.isEmpty(name)) {
                    builder = builder.longOpt(name);
                }
                Option option = builder.desc(desc).build();
                options.addOption(option);
            }
        }
    }
    CommandLine commandLine = null;
    try {
        commandLine = parser.parse(options, args);
    } catch (UnrecognizedOptionException e) {
        String optionKey = e.getOption();
        out.println("无效的命令行选项: " + optionKey);
        out.println(getHelp(groupName, commandName));
        return;
    } catch (ParseException e) {
        out.println("解析命令出错:" + e.getMessage());
        out.println(getHelp(groupName, commandName));
        return;
    }
    CommandExecutor executor = command.getExecutor();
    CommandInterpreter ci = new CommandInterpreterImpl(out, commandLine, command);
    if (optionConfigs != null && optionConfigs.size() > 0) {
        for (CommandOptionDefine optionConfig : optionConfigs) {
            if (optionConfig.isRequired() && optionConfig.getType() != OptionType.BOOLEAN) {
                String key = optionConfig.getKey();
                String name = optionConfig.getName();
                String value = ci.getOption(key);
                if (value == null) {
                    throw // 
                    new CommandException(CommandErrorCode.OPTION_KEY_EMPTY.getName()).put("group", // 
                    groupName).put("commandName", // 
                    commandName).put("optionKey", // 
                    key).put("optionName", // 
                    name).setMessage("命令 [${group} ${commandName}] 需要 ${optionName} 选项," + "请在命令后面附上 -${optionKey} [${optionName}]");
                }
            }
        }
    }
    executor.execute(ci);
}
Also used : Options(org.apache.commons.cli.Options) Builder(org.apache.commons.cli.Option.Builder) CommandExecutor(com.terran4j.commons.jfinger.CommandExecutor) CommandException(com.terran4j.commons.jfinger.CommandException) CommandOptionDefine(com.terran4j.commons.jfinger.CommandOptionDefine) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) CommandLine(org.apache.commons.cli.CommandLine) Option(org.apache.commons.cli.Option) ParseException(org.apache.commons.cli.ParseException) OptionType(com.terran4j.commons.jfinger.OptionType) CommandInterpreter(com.terran4j.commons.jfinger.CommandInterpreter)

Aggregations

CommandOptionDefine (com.terran4j.commons.jfinger.CommandOptionDefine)2 CommandDefine (com.terran4j.commons.jfinger.CommandDefine)1 CommandException (com.terran4j.commons.jfinger.CommandException)1 CommandExecutor (com.terran4j.commons.jfinger.CommandExecutor)1 CommandGroup (com.terran4j.commons.jfinger.CommandGroup)1 CommandGroupDefine (com.terran4j.commons.jfinger.CommandGroupDefine)1 CommandGroups (com.terran4j.commons.jfinger.CommandGroups)1 CommandInterpreter (com.terran4j.commons.jfinger.CommandInterpreter)1 CommandOption (com.terran4j.commons.jfinger.CommandOption)1 OptionType (com.terran4j.commons.jfinger.OptionType)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 CommandLine (org.apache.commons.cli.CommandLine)1 Option (org.apache.commons.cli.Option)1 Builder (org.apache.commons.cli.Option.Builder)1 Options (org.apache.commons.cli.Options)1 ParseException (org.apache.commons.cli.ParseException)1 UnrecognizedOptionException (org.apache.commons.cli.UnrecognizedOptionException)1 ApplicationContext (org.springframework.context.ApplicationContext)1