001package net.gdface.service.sdk.mavenplugin;
002
003import java.util.ArrayList;
004
005import org.apache.maven.plugin.AbstractMojo;
006import org.apache.maven.plugin.MojoExecutionException;
007import org.apache.maven.plugin.MojoFailureException;
008import org.apache.maven.plugins.annotations.Mojo;
009import org.apache.maven.plugins.annotations.Parameter;
010
011import net.gdface.service.sdk.FaceApiServiceConstants;
012
013/**
014 * maven 插件基类<br>
015 * 执行FaceApi RPC服务启动
016 * 
017 * @author guyadong
018 *
019 */
020@Mojo(name = "run", requiresProject = false)
021public abstract class BaseWrapperRunMojo extends AbstractMojo implements FaceApiServiceConstants{
022        /**
023         * Set this to true to allow Maven to continue to execute after invoking the
024         * run goal.
025         *
026         */
027        @Parameter(property = "maven.service.fork", defaultValue = "false")
028        private boolean fork;
029        /**
030         * FaceApi并发能力<br>
031         * SDK concurrency capacity,dependent on the sdk implementation,default:count of available processors
032         */
033        @Parameter(property="maven.faceapi.concurrency")
034        private int concurrency = Runtime.getRuntime().availableProcessors();
035
036        /**
037         * FaceApi服务端口号<br>
038         * FaceApi service port number,default value: 26421
039         */
040        @Parameter(property="maven.thrift.servicePort")
041        private int servicePort = DEFAULT_PORT;
042        /**
043         * 工作线程数量<br>
044         * work thread number,default: count of available processors
045         */
046        @Parameter(property="maven.thrift.workThreads")
047        private int workThreads = Runtime.getRuntime().availableProcessors();
048        /**
049         * 连接上限<br>
050         * an upper bound on the number of concurrent connections the server will accept.default:32
051         */
052        @Parameter(property="maven.thrift.connectionLimit")
053        private int connectionLimit = DEFAULT_CONNECTION_LIMIT;
054        /**
055         * 空闲连接超时(秒)<br>
056         * Sets a timeout period between receiving requests from a client connection. 
057         * If the timeout is exceeded (no complete requests have arrived from the client within the timeout), 
058         * the server will disconnect the idle client.default: 60s
059         */
060        @Parameter(property="maven.thrift.idleConnectionTimeout")
061        private int idleConnectionTimeout = DEFAULT_IDLE_TIMEOUT;
062        /**
063         * 启动XHR服务<br>
064         * start XHR(XML Http Request) service
065         */
066        @Parameter(property="maven.thrift.xhrEnable")
067        private boolean xhrEnable;
068        /**
069         * XHR服务端口<br>
070         * XHR(XML Http Request) port,default:26422
071         */
072        @Parameter(property="maven.thrift.xhrPort")
073        private int xhrPort = DEFAULT_PORT_XHR;
074        /**
075         * 启动RESTful服务(start RESTful service)<br>
076         */
077        @Parameter(property="maven.thrift.restfulEnable")
078        private boolean restfulEnable;
079        /**
080         * RESTfu服务端口<br>
081         * RESTful service port,default: 26423
082         */
083        @Parameter(property="maven.thrift.restfulPort")
084        private int restfulPort = DEFAULT_PORT_RESTFUL;
085        /**
086         * 启用swagger文档<br>
087         * start swagger document
088         */
089        @Parameter(property="maven.thrift.swaggerEnable")
090        private boolean swaggerEnable;
091        /**
092         * 启用RESTful服务跨域(CORS)访问<br>
093         * enable CORS(Cross-origin resource sharing) for RESTful(spring) service
094         */
095        @Parameter(property="maven.thrift.corsEnable")
096        private boolean corsEnable;
097
098        public BaseWrapperRunMojo() {
099        }
100
101        abstract protected void doExecute();
102        @Override
103        public final void execute() throws MojoExecutionException, MojoFailureException {
104                try{
105                        doExecute();
106                }catch(Exception e){
107                        throw new MojoExecutionException(e.getMessage(),e);
108                }
109                if (!fork) {
110                        waitIndefinitely();
111                }
112        }
113        protected String[] makeArgs(){
114                ArrayList<String> list = new ArrayList<String>(16);
115                list.add("--" + FACEAPI_CONCURRENCY_OPTION_LONG);
116                list.add(Integer.toString(concurrency));
117                list.add("--" + SERVICE_PORT_OPTION_LONG);
118                list.add(Integer.toString(servicePort));
119                list.add("--" + WORK_THREADS_OPTION_LONG);
120                list.add(Integer.toString(workThreads));
121                list.add("--" + CONNECTION_LIMIT_OPTION_LONG);
122                list.add(Integer.toString(connectionLimit));
123                list.add("--" + IDLE_CONNECTION_TIMEOUT_OPTION_LONG);
124                list.add(Integer.toString(idleConnectionTimeout));
125                list.add("--" + FACEAPI_XHR_START_OPTION_LONG);
126                list.add("--" + FACEAPI_XHR_OPTION_LONG);
127                list.add(Integer.toString(xhrPort));
128                list.add("--" + FACEAPI_RESTFUL_START_OPTION_LONG);
129                list.add("--" + FACEAPI_RESTFUL_OPTION_LONG);
130                list.add(Integer.toString(restfulPort));
131                list.add("--" + FACEAPI_SWAGGER_ENABLE_OPTION_LONG);
132                list.add("--" + FACEAPI_CORS_ENABLE_OPTION_LONG);
133                return list.toArray(new String[list.size()]);
134        }
135        /**
136         * Causes the current thread to wait indefinitely. This method does not
137         * return.
138         */
139        private void waitIndefinitely() {
140                Object lock = new Object();
141                synchronized (lock) {
142                        try {
143                                lock.wait();
144                        } catch (InterruptedException e) {
145                                getLog().warn("RunMojo.interrupted", e);
146                        }
147                }
148        }
149}