001package net.gdface.cli;
002
003import java.util.Map;
004import java.util.logging.Logger;
005
006import org.apache.commons.cli.CommandLine;
007import org.apache.commons.cli.CommandLineParser;
008import org.apache.commons.cli.DefaultParser;
009import org.apache.commons.cli.HelpFormatter;
010import org.apache.commons.cli.Options;
011
012/**
013 * 应用程序配置参数抽象类
014 * @author guyadong
015 *
016 */
017public abstract class BaseAppConfig extends AbstractConfiguration 
018        implements CommonCliConstants {
019        protected static final Logger logger = Logger.getLogger(BaseAppConfig.class.getSimpleName());
020
021        /**
022         * 定义命令行参数
023         */
024        protected final Options options = new Options();
025        /**
026         * 定义命令行参数的默认值
027         */
028        protected final Context defaultValue = Context.builder().build();
029        protected BaseAppConfig() {
030        }
031
032        @Override
033        protected Map<String, Object> getDefaultValueMap() {
034                return defaultValue.getContext();
035        }
036
037        /**
038         * 解析命令行参数
039         * @param args
040         * @return
041         */
042        public BaseAppConfig parseCommandLine(String[] args) {
043                HelpFormatter formatter = new HelpFormatter();
044                CommandLineParser parser = new DefaultParser();
045                CommandLine cl = null;
046                Options opts = getOptions();
047                opts.addOption(HELP_OPTION, HELP_OPTION_LONG, false, HELP_OPTION_DESC);
048                boolean exit = false;
049                try {
050                        // 处理Options和参数
051                        cl = parser.parse(opts, args);
052                        if (!cl.hasOption(HELP_OPTION)) {
053                                if (cl.hasOption(DEFINE_OPTION)) {
054                                        setSystemProperty(cl.getOptionValues(DEFINE_OPTION));
055                                }
056                                loadConfig(opts, cl);
057                        } else{
058                                exit = true;
059                        }
060                }catch (Exception e) {
061                        logger.warning(e.getMessage());
062                        exit = true;
063                }
064                if (exit) {
065                         // 如果发生异常,则打印出帮助信息
066                        formatter.printHelp(getCmdLineSyntax(), getHeader(),getOptions(),getFooter());
067                        System.exit(1);
068                }
069                return this;
070        }
071        private void setSystemProperty(String[] properties) {
072                if(properties.length %2 != 0){
073                        throw new IllegalArgumentException("INVALID properties length");
074                }
075                for (int i = 0; i < properties.length; i += 2) {
076                        System.setProperty(properties[i], properties[i + 1]);
077                        logger.info(String.format("set property [%s]=[%s]", properties[i], properties[i + 1]));
078                }
079        }
080        protected String getCmdLineSyntax() {
081                return String.format("%s [options]", getAppName());
082        }
083
084        public Options getOptions() {
085                return options;
086        }
087        protected String getAppName(){
088                return "Appname";
089        }
090        /**
091         * @return
092         * @see HelpFormatter#printHelp(String, String, Options, String)
093         */
094        protected String getHeader() {
095                return null;
096        }
097        /**
098         * @return
099         * @see HelpFormatter#printHelp(String, String, Options, String)
100         */
101        protected String getFooter() {
102                return null;
103                
104        }
105}