[CALCITE-1700] De-couple the HsqldbServer into a generic JDBC server We can provide a lot more value by just providing a standalone process which can use *any* JDBC driver instead of hard-coding it to be HSQLDB only.
Closes apache/calcite#404 Project: http://git-wip-us.apache.org/repos/asf/calcite/repo Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/8bac70e5 Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/8bac70e5 Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/8bac70e5 Branch: refs/heads/branch-1.12 Commit: 8bac70e5020c50505b34df6eaae18c0ca414f2c2 Parents: 554d963 Author: Josh Elser <[email protected]> Authored: Wed Mar 15 01:16:57 2017 -0400 Committer: Josh Elser <[email protected]> Committed: Sun Mar 19 16:23:08 2017 -0400 ---------------------------------------------------------------------- .../calcite/avatica/hsqldb/HsqldbServer.java | 137 ------------------- .../calcite/avatica/hsqldb/package-info.java | 26 ---- .../avatica/server/StandaloneServer.java | 137 +++++++++++++++++++ .../calcite/avatica/server/package-info.java | 26 ++++ 4 files changed, 163 insertions(+), 163 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/calcite/blob/8bac70e5/avatica/standalone-server/src/main/java/org/apache/calcite/avatica/hsqldb/HsqldbServer.java ---------------------------------------------------------------------- diff --git a/avatica/standalone-server/src/main/java/org/apache/calcite/avatica/hsqldb/HsqldbServer.java b/avatica/standalone-server/src/main/java/org/apache/calcite/avatica/hsqldb/HsqldbServer.java deleted file mode 100644 index 9e30dad..0000000 --- a/avatica/standalone-server/src/main/java/org/apache/calcite/avatica/hsqldb/HsqldbServer.java +++ /dev/null @@ -1,137 +0,0 @@ -/* - * 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 License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.calcite.avatica.hsqldb; - -import org.apache.calcite.avatica.jdbc.JdbcMeta; -import org.apache.calcite.avatica.remote.Driver.Serialization; -import org.apache.calcite.avatica.remote.LocalService; -import org.apache.calcite.avatica.server.HttpServer; -import org.apache.calcite.avatica.util.Unsafe; - -import com.beust.jcommander.IStringConverter; -import com.beust.jcommander.JCommander; -import com.beust.jcommander.Parameter; - -import net.hydromatic.scott.data.hsqldb.ScottHsqldb; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Locale; - -/** - * An Avatica server for HSQLDB. - */ -public class HsqldbServer { - private static final Logger LOG = LoggerFactory.getLogger(HsqldbServer.class); - - @Parameter(names = { "-p", "--port" }, required = false, - description = "Port the server should bind") - private int port = 0; - - @Parameter(names = { "-s", "--serialization" }, required = false, - description = "Serialization method to use", converter = SerializationConverter.class) - private Serialization serialization = Serialization.PROTOBUF; - - private HttpServer server; - - public void start() { - if (null != server) { - LOG.error("The server was already started"); - Unsafe.systemExit(ExitCodes.ALREADY_STARTED.ordinal()); - return; - } - - try { - // Set up Julian's ScottDB for HSQLDB - JdbcMeta meta = new JdbcMeta(ScottHsqldb.URI, ScottHsqldb.USER, ScottHsqldb.PASSWORD); - LocalService service = new LocalService(meta); - - // Construct the server - this.server = new HttpServer.Builder() - .withHandler(service, serialization) - .withPort(port) - .build(); - - // Then start it - server.start(); - - LOG.info("Started Avatica server on port {} with serialization {}", server.getPort(), - serialization); - } catch (Exception e) { - LOG.error("Failed to start Avatica server", e); - Unsafe.systemExit(ExitCodes.START_FAILED.ordinal()); - } - } - - public void stop() { - if (null != server) { - server.stop(); - server = null; - } - } - - public void join() throws InterruptedException { - server.join(); - } - - public static void main(String[] args) { - final HsqldbServer server = new HsqldbServer(); - new JCommander(server, args); - - server.start(); - - // Try to clean up when the server is stopped. - Runtime.getRuntime().addShutdownHook( - new Thread(new Runnable() { - @Override public void run() { - LOG.info("Stopping server"); - server.stop(); - LOG.info("Server stopped"); - } - })); - - try { - server.join(); - } catch (InterruptedException e) { - // Reset interruption - Thread.currentThread().interrupt(); - // And exit now. - return; - } - } - - /** - * Converter from String to Serialization. Must be public for JCommander. - */ - public static class SerializationConverter implements IStringConverter<Serialization> { - @Override public Serialization convert(String value) { - return Serialization.valueOf(value.toUpperCase(Locale.ROOT)); - } - } - - /** - * Codes for exit conditions - */ - private enum ExitCodes { - NORMAL, - ALREADY_STARTED, // 1 - START_FAILED; // 2 - } -} - -// End HsqldbServer.java http://git-wip-us.apache.org/repos/asf/calcite/blob/8bac70e5/avatica/standalone-server/src/main/java/org/apache/calcite/avatica/hsqldb/package-info.java ---------------------------------------------------------------------- diff --git a/avatica/standalone-server/src/main/java/org/apache/calcite/avatica/hsqldb/package-info.java b/avatica/standalone-server/src/main/java/org/apache/calcite/avatica/hsqldb/package-info.java deleted file mode 100644 index da97815..0000000 --- a/avatica/standalone-server/src/main/java/org/apache/calcite/avatica/hsqldb/package-info.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * 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 License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * Avatica Server for HSQLDB. - */ -@PackageMarker -package org.apache.calcite.avatica.hsqldb; - -import org.apache.calcite.avatica.util.PackageMarker; - -// End package-info.java http://git-wip-us.apache.org/repos/asf/calcite/blob/8bac70e5/avatica/standalone-server/src/main/java/org/apache/calcite/avatica/server/StandaloneServer.java ---------------------------------------------------------------------- diff --git a/avatica/standalone-server/src/main/java/org/apache/calcite/avatica/server/StandaloneServer.java b/avatica/standalone-server/src/main/java/org/apache/calcite/avatica/server/StandaloneServer.java new file mode 100644 index 0000000..ee80285 --- /dev/null +++ b/avatica/standalone-server/src/main/java/org/apache/calcite/avatica/server/StandaloneServer.java @@ -0,0 +1,137 @@ +/* + * 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 License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.calcite.avatica.server; + +import org.apache.calcite.avatica.jdbc.JdbcMeta; +import org.apache.calcite.avatica.remote.Driver.Serialization; +import org.apache.calcite.avatica.remote.LocalService; +import org.apache.calcite.avatica.util.Unsafe; + +import com.beust.jcommander.IStringConverter; +import com.beust.jcommander.JCommander; +import com.beust.jcommander.Parameter; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Locale; + +/** + * An Avatica server for arbitrary JDBC drivers. + */ +public class StandaloneServer { + private static final Logger LOG = LoggerFactory.getLogger(StandaloneServer.class); + + @Parameter(names = { "-u", "--url" }, required = true, + description = "JDBC driver url for the server") + private String url; + + @Parameter(names = { "-p", "--port" }, required = false, + description = "Port the server should bind") + private int port = 0; + + @Parameter(names = { "-s", "--serialization" }, required = false, + description = "Serialization method to use", converter = SerializationConverter.class) + private Serialization serialization = Serialization.PROTOBUF; + + private HttpServer server; + + public void start() { + if (null != server) { + LOG.error("The server was already started"); + Unsafe.systemExit(ExitCodes.ALREADY_STARTED.ordinal()); + return; + } + + try { + JdbcMeta meta = new JdbcMeta(url); + LocalService service = new LocalService(meta); + + // Construct the server + this.server = new HttpServer.Builder() + .withHandler(service, serialization) + .withPort(port) + .build(); + + // Then start it + server.start(); + + LOG.info("Started Avatica server on port {} with serialization {}", server.getPort(), + serialization); + } catch (Exception e) { + LOG.error("Failed to start Avatica server", e); + Unsafe.systemExit(ExitCodes.START_FAILED.ordinal()); + } + } + + public void stop() { + if (null != server) { + server.stop(); + server = null; + } + } + + public void join() throws InterruptedException { + server.join(); + } + + public static void main(String[] args) { + final StandaloneServer server = new StandaloneServer(); + new JCommander(server, args); + + server.start(); + + // Try to clean up when the server is stopped. + Runtime.getRuntime().addShutdownHook( + new Thread(new Runnable() { + @Override public void run() { + LOG.info("Stopping server"); + server.stop(); + LOG.info("Server stopped"); + } + })); + + try { + server.join(); + } catch (InterruptedException e) { + // Reset interruption + Thread.currentThread().interrupt(); + // And exit now. + return; + } + } + + /** + * Converter from String to Serialization. Must be public for JCommander. + */ + public static class SerializationConverter implements IStringConverter<Serialization> { + @Override public Serialization convert(String value) { + return Serialization.valueOf(value.toUpperCase(Locale.ROOT)); + } + } + + /** + * Codes for exit conditions + */ + private enum ExitCodes { + NORMAL, + ALREADY_STARTED, // 1 + START_FAILED; // 2 + } +} + +// End StandaloneServer.java http://git-wip-us.apache.org/repos/asf/calcite/blob/8bac70e5/avatica/standalone-server/src/main/java/org/apache/calcite/avatica/server/package-info.java ---------------------------------------------------------------------- diff --git a/avatica/standalone-server/src/main/java/org/apache/calcite/avatica/server/package-info.java b/avatica/standalone-server/src/main/java/org/apache/calcite/avatica/server/package-info.java new file mode 100644 index 0000000..69a4c52 --- /dev/null +++ b/avatica/standalone-server/src/main/java/org/apache/calcite/avatica/server/package-info.java @@ -0,0 +1,26 @@ +/* + * 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 License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Avatica Server without any authentication for any JDBC driver. + */ +@PackageMarker +package org.apache.calcite.avatica.server; + +import org.apache.calcite.avatica.util.PackageMarker; + +// End package-info.java
