2017-06-07 14:11:29 +08:00
|
|
|
/*
|
|
|
|
|
* Copyright 2017 HugeGraph Authors
|
|
|
|
|
*
|
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with this
|
|
|
|
|
* work for additional information regarding copyright ownership. The ASF
|
|
|
|
|
* licenses this file to You under the Apache License, Version 2.0 (the
|
|
|
|
|
* "License"); you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
2017-08-16 16:13:20 +08:00
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
|
* under the License.
|
2017-06-07 14:11:29 +08:00
|
|
|
*/
|
2017-08-17 16:26:01 +08:00
|
|
|
|
2017-05-17 21:16:40 +08:00
|
|
|
package com.baidu.hugegraph.server;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.net.URI;
|
2017-06-07 20:28:23 +08:00
|
|
|
import java.util.Arrays;
|
2017-05-17 21:16:40 +08:00
|
|
|
|
|
|
|
|
import javax.ws.rs.core.UriBuilder;
|
|
|
|
|
|
2017-05-27 10:29:30 +08:00
|
|
|
import org.apache.commons.configuration.ConfigurationException;
|
2017-05-17 21:16:40 +08:00
|
|
|
import org.glassfish.grizzly.http.server.HttpServer;
|
|
|
|
|
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
|
|
|
|
|
import org.glassfish.jersey.server.ResourceConfig;
|
|
|
|
|
import org.slf4j.Logger;
|
2017-08-17 13:12:04 +08:00
|
|
|
import com.baidu.hugegraph.util.Log;
|
2017-05-17 21:16:40 +08:00
|
|
|
|
2017-06-07 20:28:23 +08:00
|
|
|
import com.baidu.hugegraph.HugeException;
|
2017-05-27 10:29:30 +08:00
|
|
|
import com.baidu.hugegraph.config.HugeConfig;
|
|
|
|
|
import com.baidu.hugegraph.config.ServerOptions;
|
|
|
|
|
import com.baidu.hugegraph.util.E;
|
2017-07-31 11:36:24 +08:00
|
|
|
import com.baidu.hugegraph.version.ApiVersion;
|
2017-05-17 21:16:40 +08:00
|
|
|
|
2017-11-02 10:33:26 +08:00
|
|
|
public class RestServer {
|
2017-05-17 21:16:40 +08:00
|
|
|
|
2017-11-02 10:33:26 +08:00
|
|
|
private static final Logger LOG = Log.logger(RestServer.class);
|
2017-05-17 21:16:40 +08:00
|
|
|
|
2017-08-18 20:47:19 +08:00
|
|
|
private final HugeConfig conf;
|
2017-05-17 21:16:40 +08:00
|
|
|
private HttpServer httpServer = null;
|
|
|
|
|
|
2017-11-02 10:33:26 +08:00
|
|
|
public RestServer(HugeConfig conf) {
|
2017-05-22 22:14:53 +08:00
|
|
|
this.conf = conf;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-07 20:28:23 +08:00
|
|
|
public void start() throws IOException {
|
2017-11-07 19:22:35 +08:00
|
|
|
String url = this.conf.get(ServerOptions.REST_SERVER_URL);
|
2017-05-27 10:29:30 +08:00
|
|
|
URI uri = UriBuilder.fromUri(url).build();
|
2017-05-17 21:16:40 +08:00
|
|
|
|
2017-05-22 22:14:53 +08:00
|
|
|
ResourceConfig rc = new ApplicationConfig(this.conf);
|
2017-05-17 21:16:40 +08:00
|
|
|
|
|
|
|
|
this.httpServer = GrizzlyHttpServerFactory.createHttpServer(uri, rc);
|
|
|
|
|
this.httpServer.start();
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-23 15:17:46 +08:00
|
|
|
@SuppressWarnings("deprecation") // TODO: use shutdown instead
|
2017-05-17 21:16:40 +08:00
|
|
|
public void stop() {
|
2017-07-18 11:34:30 +08:00
|
|
|
E.checkNotNull(this.httpServer, "http server");
|
2017-05-17 21:16:40 +08:00
|
|
|
this.httpServer.stop();
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-02 10:33:26 +08:00
|
|
|
public static RestServer start(String conf) throws Exception {
|
|
|
|
|
LOG.info("RestServer starting...");
|
2017-07-31 11:36:24 +08:00
|
|
|
ApiVersion.check();
|
2017-05-17 21:16:40 +08:00
|
|
|
|
2017-11-02 10:33:26 +08:00
|
|
|
RestServer server = new RestServer(RestServer.loadConf(conf));
|
|
|
|
|
server.start();
|
|
|
|
|
LOG.info("RestServer started");
|
2017-05-17 21:16:40 +08:00
|
|
|
|
|
|
|
|
return server;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-02 10:33:26 +08:00
|
|
|
protected static HugeConfig loadConf(String conf) throws HugeException {
|
2017-05-27 10:29:30 +08:00
|
|
|
try {
|
2017-11-02 10:33:26 +08:00
|
|
|
return new HugeConfig(conf);
|
2017-05-27 10:29:30 +08:00
|
|
|
} catch (ConfigurationException e) {
|
2017-06-07 20:28:23 +08:00
|
|
|
throw new HugeException("Failed to load config file", e);
|
2017-05-27 10:29:30 +08:00
|
|
|
}
|
2017-05-22 22:14:53 +08:00
|
|
|
}
|
|
|
|
|
|
2017-05-17 21:16:40 +08:00
|
|
|
public static void main(String[] args) throws Exception {
|
2017-11-02 10:33:26 +08:00
|
|
|
if (args.length != 1) {
|
|
|
|
|
LOG.error("RestServer need one config file, but given {}",
|
|
|
|
|
Arrays.asList(args));
|
|
|
|
|
throw new HugeException("RestServer need one config file");
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-07 20:28:23 +08:00
|
|
|
try {
|
2017-11-02 10:33:26 +08:00
|
|
|
RestServer.start(args[0]);
|
2017-06-07 20:28:23 +08:00
|
|
|
Thread.currentThread().join();
|
|
|
|
|
} catch (Exception e) {
|
2017-11-02 10:33:26 +08:00
|
|
|
LOG.error("RestServer error:", e);
|
2017-06-07 20:28:23 +08:00
|
|
|
throw e;
|
|
|
|
|
}
|
2017-11-02 10:33:26 +08:00
|
|
|
LOG.info("RestServer stopped");
|
2017-05-17 21:16:40 +08:00
|
|
|
}
|
|
|
|
|
}
|