001package net.gdface.service.sdk;
002
003import org.apache.commons.cli.CommandLine;
004import org.apache.commons.cli.Option;
005import org.apache.commons.cli.Options;
006import org.apache.commons.cli.ParseException;
007import com.google.common.base.Strings;
008import com.google.common.base.Throwables;
009import com.google.common.collect.ImmutableMap;
010import net.gdface.cli.ThriftServiceConfig;
011import net.gdface.sdk.ContextLoader;
012import net.gdface.sdk.FaceApi;
013import net.gdface.sdk.FaceApiContext;
014import net.gdface.sdk.ContextLoader.GlobalContextField;
015import net.gdface.utils.ReflectionUtils;
016
017import static com.google.common.base.Preconditions.checkArgument;
018import static com.google.common.base.Preconditions.checkNotNull;;
019
020/**
021 * 服务配置参数
022 * @author guyadong
023 *
024 */
025public class FaceApiServiceConfig extends ThriftServiceConfig implements FaceApiServiceConstants {
026        private FaceApi faceapi;
027        private int xhrPort;
028        private int restfulPort;
029        private boolean xhrStart;
030        private boolean restfulStart;
031        private boolean swaggerEnable = true;
032        private boolean corsEnable = true;
033        private int faceapiConcurrency;
034        private static final  FaceApiServiceConfig INSTANCE = new FaceApiServiceConfig();
035        public FaceApiServiceConfig() {
036                super(DEFAULT_PORT);
037                options.addOption(Option.builder().longOpt(FACEAPI_CLASS_OPTION_LONG)
038                                .desc(FACEAPI_CLASS_OPTION_DESC).numberOfArgs(1).build());
039                options.addOption(Option.builder().longOpt(FACEAPI_STATIC_OPTION_LONG)
040                                .desc(FACEAPI_STATIC_OPTION_DESC).numberOfArgs(1).build());
041                options.addOption(Option.builder(FACEAPI_CONCURRENCY_OPTION).longOpt(FACEAPI_CONCURRENCY_OPTION_LONG)
042                                .desc(FACEAPI_CONCURRENCY_OPTION_DESC).numberOfArgs(1).type(Number.class).build());
043
044                options.addOption(Option.builder(FACEAPI_XHR_OPTION).longOpt(FACEAPI_XHR_OPTION_LONG)
045                                .desc(FACEAPI_XHR_OPTION_DESC + DEFAULT_PORT_XHR).numberOfArgs(1).type(Number.class).build());
046                options.addOption(Option.builder(FACEAPI_XHR_START_OPTION).longOpt(FACEAPI_XHR_START_OPTION_LONG)
047                                .desc(FACEAPI_XHR_START_OPTION_DESC).numberOfArgs(0).build());
048                options.addOption(Option.builder(FACEAPI_RESTFUL_OPTION).longOpt(FACEAPI_RESTFUL_OPTION_LONG)
049                                .desc(FACEAPI_RESTFUL_OPTION_DESC + DEFAULT_PORT_RESTFUL).numberOfArgs(1).type(Number.class).build());
050                options.addOption(Option.builder(FACEAPI_RESTFUL_START_OPTION).longOpt(FACEAPI_RESTFUL_START_OPTION_LONG)
051                                .desc(FACEAPI_RESTFUL_START_OPTION_DESC).numberOfArgs(0).build());
052                options.addOption(Option.builder(FACEAPI_SWAGGER_ENABLE_OPTION).longOpt(FACEAPI_SWAGGER_ENABLE_OPTION_LONG)
053                                .desc(FACEAPI_SWAGGER_ENABLE_OPTION_DESC).numberOfArgs(0).build());
054                options.addOption(Option.builder(FACEAPI_CORS_ENABLE_OPTION).longOpt(FACEAPI_CORS_ENABLE_OPTION_LONG)
055                                .desc(FACEAPI_CORS_ENABLE_OPTION_DESC).numberOfArgs(0).build());
056                
057                defaultValue.setProperty(FACEAPI_CLASS_OPTION_LONG,"");
058                defaultValue.setProperty(FACEAPI_STATIC_OPTION_LONG,DEFAULT_STATIC_METHOD);
059                defaultValue.setProperty(FACEAPI_CONCURRENCY_OPTION_LONG,Runtime.getRuntime().availableProcessors());
060                defaultValue.setProperty(FACEAPI_XHR_OPTION_LONG,DEFAULT_PORT_XHR);
061                defaultValue.setProperty(FACEAPI_XHR_START_OPTION_LONG,false);
062                defaultValue.setProperty(FACEAPI_RESTFUL_OPTION_LONG,DEFAULT_PORT_RESTFUL);
063                defaultValue.setProperty(FACEAPI_RESTFUL_START_OPTION_LONG,false);
064                defaultValue.setProperty(FACEAPI_SWAGGER_ENABLE_OPTION_LONG,true);      
065                defaultValue.setProperty(FACEAPI_CORS_ENABLE_OPTION_LONG,true);
066        }
067
068        @Override
069        public void loadConfig(Options options, CommandLine cmd) throws ParseException {
070                super.loadConfig(options, cmd);
071                this.faceapiConcurrency = ((Number)getProperty(FACEAPI_CONCURRENCY_OPTION_LONG)).intValue();
072                this.faceapi = getFaceApiInstance();
073                this.xhrStart = (Boolean)getProperty(FACEAPI_XHR_START_OPTION_LONG);
074                this.xhrPort = ((Number)getProperty(FACEAPI_XHR_OPTION_LONG)).intValue();
075                this.restfulStart = (Boolean)getProperty(FACEAPI_RESTFUL_START_OPTION_LONG);
076                this.restfulPort = ((Number)getProperty(FACEAPI_RESTFUL_OPTION_LONG)).intValue();
077                this.swaggerEnable = (Boolean)getProperty(FACEAPI_SWAGGER_ENABLE_OPTION_LONG);
078                this.corsEnable = (Boolean)getProperty(FACEAPI_CORS_ENABLE_OPTION_LONG);
079        }
080        /**
081         * 返回{@link FaceApi}实例<br>
082         * 如果定义了命令行参数 {@link FaceApiServiceConstants#FACEAPI_CLASS_OPTION_LONG}则从该参数获取{@link FaceApi}实例<br>
083         * 否则尝试从从SPI加载的上下文实例({@link FaceApiContext})中获取获取{@link FaceApi}实例,获取不到则抛出异常
084         * @return {@link FaceApi}实例
085         */
086        private FaceApi getFaceApiInstance(){
087                // 设置并发线程数
088                ContextLoader.setGlobalContext(GlobalContextField.CONCURRENCY, faceapiConcurrency);
089                logger.info(String.format("FaceApi concurrency capcaity(SDK并发):%s",faceapiConcurrency));
090                String clazzName = (String)getProperty(FACEAPI_CLASS_OPTION_LONG);
091                if(Strings.isNullOrEmpty(clazzName)){
092                        FaceApiContext context = ContextLoader.getInstance().getContext();
093                        checkArgument(context != null,
094                                        "NOT FOUND FaceApiContext instance,NOT define option %s",
095                                        FACEAPI_CLASS_OPTION_LONG);
096                        return (FaceApi) checkNotNull(context.getContext().get(ContextLoader.ContextField.INSTANCE),"NOT DEFINE FaceApi Instance in context");
097                }
098                ImmutableMap<String, Object> params = ImmutableMap.<String, Object>of(ReflectionUtils.PROP_CLASSNAME, clazzName, 
099                                ReflectionUtils.PROP_STATICMETHODNAME, (String)getProperty(FACEAPI_STATIC_OPTION_LONG));
100                try {
101                        return ReflectionUtils.getInstance(FaceApi.class, params);
102                } catch (Exception e) {
103                        Throwables.throwIfUnchecked(e);
104                        throw new RuntimeException(e);
105                }
106        }
107        @Override
108        protected String getAppName() {
109                return FaceApiServiceMain.class.getName();
110        }
111
112        public static FaceApiServiceConfig getInstance() {
113                return INSTANCE;
114        }
115
116        public FaceApi getFaceapi() {
117                return faceapi;
118        }
119
120        /**
121         * @return xhrPort
122         */
123        public int getXhrPort() {
124                return xhrPort;
125        }
126
127        /**
128         * @return restfulPort
129         */
130        public int getRestfulPort() {
131                return restfulPort;
132        }
133
134        /**
135         * @return xhrStart
136         */
137        public boolean isXhrStart() {
138                return xhrStart;
139        }
140
141        /**
142         * @return restfulStart
143         */
144        public boolean isRestfulStart() {
145                return restfulStart;
146        }
147
148        public boolean isSwaggerEnable() {
149                return swaggerEnable;
150        }
151
152        public boolean isCorsEnable() {
153                return corsEnable;
154        }
155
156        public void setCorsEnable(boolean corsEnable) {
157                this.corsEnable = corsEnable;
158        }
159        
160}