Search in sources :

Example 1 with TcpServer

use of org.h2.server.TcpServer in project h2database by h2database.

the class TcpServerThread method run.

@Override
public void run() {
    try {
        transfer.init();
        trace("Connect");
        // and a list of allowed clients
        try {
            Socket socket = transfer.getSocket();
            if (socket == null) {
                // the transfer is already closed, prevent NPE in TcpServer#allow(Socket)
                return;
            }
            if (!server.allow(transfer.getSocket())) {
                throw DbException.get(ErrorCode.REMOTE_CONNECTION_NOT_ALLOWED);
            }
            int minClientVersion = transfer.readInt();
            int maxClientVersion = transfer.readInt();
            if (maxClientVersion < Constants.TCP_PROTOCOL_VERSION_MIN_SUPPORTED) {
                throw DbException.get(ErrorCode.DRIVER_VERSION_ERROR_2, "" + clientVersion, "" + Constants.TCP_PROTOCOL_VERSION_MIN_SUPPORTED);
            } else if (minClientVersion > Constants.TCP_PROTOCOL_VERSION_MAX_SUPPORTED) {
                throw DbException.get(ErrorCode.DRIVER_VERSION_ERROR_2, "" + clientVersion, "" + Constants.TCP_PROTOCOL_VERSION_MAX_SUPPORTED);
            }
            if (maxClientVersion >= Constants.TCP_PROTOCOL_VERSION_MAX_SUPPORTED) {
                clientVersion = Constants.TCP_PROTOCOL_VERSION_MAX_SUPPORTED;
            } else {
                clientVersion = maxClientVersion;
            }
            transfer.setVersion(clientVersion);
            String db = transfer.readString();
            String originalURL = transfer.readString();
            if (db == null && originalURL == null) {
                String targetSessionId = transfer.readString();
                int command = transfer.readInt();
                stop = true;
                if (command == SessionRemote.SESSION_CANCEL_STATEMENT) {
                    // cancel a running statement
                    int statementId = transfer.readInt();
                    server.cancelStatement(targetSessionId, statementId);
                } else if (command == SessionRemote.SESSION_CHECK_KEY) {
                    // check if this is the correct server
                    db = server.checkKeyAndGetDatabaseName(targetSessionId);
                    if (!targetSessionId.equals(db)) {
                        transfer.writeInt(SessionRemote.STATUS_OK);
                    } else {
                        transfer.writeInt(SessionRemote.STATUS_ERROR);
                    }
                }
            }
            String baseDir = server.getBaseDir();
            if (baseDir == null) {
                baseDir = SysProperties.getBaseDir();
            }
            db = server.checkKeyAndGetDatabaseName(db);
            ConnectionInfo ci = new ConnectionInfo(db);
            ci.setOriginalURL(originalURL);
            ci.setUserName(transfer.readString());
            ci.setUserPasswordHash(transfer.readBytes());
            ci.setFilePasswordHash(transfer.readBytes());
            int len = transfer.readInt();
            for (int i = 0; i < len; i++) {
                ci.setProperty(transfer.readString(), transfer.readString());
            }
            // override client's requested properties with server settings
            if (baseDir != null) {
                ci.setBaseDir(baseDir);
            }
            if (server.getIfExists()) {
                ci.setProperty("IFEXISTS", "TRUE");
            }
            transfer.writeInt(SessionRemote.STATUS_OK);
            transfer.writeInt(clientVersion);
            transfer.flush();
            if (clientVersion >= Constants.TCP_PROTOCOL_VERSION_13) {
                if (ci.getFilePasswordHash() != null) {
                    ci.setFileEncryptionKey(transfer.readBytes());
                }
            }
            session = Engine.getInstance().createSession(ci);
            transfer.setSession(session);
            server.addConnection(threadId, originalURL, ci.getUserName());
            trace("Connected");
        } catch (Throwable e) {
            sendError(e);
            stop = true;
        }
        while (!stop) {
            try {
                process();
            } catch (Throwable e) {
                sendError(e);
            }
        }
        trace("Disconnect");
    } catch (Throwable e) {
        server.traceError(e);
    } finally {
        close();
    }
}
Also used : ConnectionInfo(org.h2.engine.ConnectionInfo) Socket(java.net.Socket)

Example 2 with TcpServer

use of org.h2.server.TcpServer in project x-pipe by ctripcorp.

the class H2Test method startH2Server.

protected void startH2Server() throws SQLException {
    Server tcpServer = Server.createTcpServer("-tcpPort", String.valueOf(h2Port), "-tcpAllowOthers");
    tcpServer.start();
}
Also used : Server(org.h2.tools.Server)

Example 3 with TcpServer

use of org.h2.server.TcpServer in project h2database by h2database.

the class Server method createTcpServer.

/**
 * Create a new TCP server, but does not start it yet. Example:
 *
 * <pre>
 * Server server = Server.createTcpServer(
 *     "-tcpPort", "9123", "-tcpAllowOthers").start();
 * </pre>
 * Supported options are:
 * -tcpPort, -tcpSSL, -tcpPassword, -tcpAllowOthers, -tcpDaemon,
 * -trace, -ifExists, -baseDir, -key.
 * See the main method for details.
 * <p>
 * If no port is specified, the default port is used if possible,
 * and if this port is already used, a random port is used.
 * Use getPort() or getURL() after starting to retrieve the port.
 * </p>
 *
 * @param args the argument list
 * @return the server
 */
public static Server createTcpServer(String... args) throws SQLException {
    TcpServer service = new TcpServer();
    Server server = new Server(service, args);
    service.setShutdownHandler(server);
    return server;
}
Also used : TcpServer(org.h2.server.TcpServer) WebServer(org.h2.server.web.WebServer) PgServer(org.h2.server.pg.PgServer) TcpServer(org.h2.server.TcpServer)

Example 4 with TcpServer

use of org.h2.server.TcpServer in project h2database by h2database.

the class TcpServer method stopServer.

/**
 * Stop a running server. This method is called via reflection from the
 * STOP_SERVER function.
 *
 * @param port the port where the server runs, or 0 for all running servers
 * @param password the password (or null)
 * @param shutdownMode the shutdown mode, SHUTDOWN_NORMAL or SHUTDOWN_FORCE.
 */
public static void stopServer(int port, String password, int shutdownMode) {
    if (port == 0) {
        for (int p : SERVERS.keySet().toArray(new Integer[0])) {
            if (p != 0) {
                stopServer(p, password, shutdownMode);
            }
        }
        return;
    }
    TcpServer server = SERVERS.get(port);
    if (server == null) {
        return;
    }
    if (!server.managementPassword.equals(password)) {
        return;
    }
    if (shutdownMode == SHUTDOWN_NORMAL) {
        server.stopManagementDb();
        server.stop = true;
        try {
            Socket s = NetUtils.createLoopbackSocket(port, false);
            s.close();
        } catch (Exception e) {
        // try to connect - so that accept returns
        }
    } else if (shutdownMode == SHUTDOWN_FORCE) {
        server.stop();
    }
    server.shutdown();
}
Also used : Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) DbException(org.h2.message.DbException) UnknownHostException(java.net.UnknownHostException) SQLException(java.sql.SQLException)

Example 5 with TcpServer

use of org.h2.server.TcpServer in project h2database by h2database.

the class TestTools method testServer.

private void testServer() throws SQLException {
    Connection conn;
    try {
        deleteDb("test");
        Server tcpServer = Server.createTcpServer("-baseDir", getBaseDir(), "-tcpAllowOthers").start();
        remainingServers.add(tcpServer);
        final int port = tcpServer.getPort();
        conn = getConnection("jdbc:h2:tcp://localhost:" + port + "/test", "sa", "");
        conn.close();
        // must not be able to use a different base dir
        new AssertThrows(ErrorCode.IO_EXCEPTION_1) {

            @Override
            public void test() throws SQLException {
                getConnection("jdbc:h2:tcp://localhost:" + port + "/../test", "sa", "");
            }
        };
        new AssertThrows(ErrorCode.IO_EXCEPTION_1) {

            @Override
            public void test() throws SQLException {
                getConnection("jdbc:h2:tcp://localhost:" + port + "/../test2/test", "sa", "");
            }
        };
        tcpServer.stop();
        Server tcpServerWithPassword = Server.createTcpServer("-ifExists", "-tcpPassword", "abc", "-baseDir", getBaseDir()).start();
        final int prt = tcpServerWithPassword.getPort();
        remainingServers.add(tcpServerWithPassword);
        // must not be able to create new db
        new AssertThrows(ErrorCode.DATABASE_NOT_FOUND_1) {

            @Override
            public void test() throws SQLException {
                getConnection("jdbc:h2:tcp://localhost:" + prt + "/test2", "sa", "");
            }
        };
        new AssertThrows(ErrorCode.DATABASE_NOT_FOUND_1) {

            @Override
            public void test() throws SQLException {
                getConnection("jdbc:h2:tcp://localhost:" + prt + "/test2;ifexists=false", "sa", "");
            }
        };
        conn = getConnection("jdbc:h2:tcp://localhost:" + prt + "/test", "sa", "");
        conn.close();
        new AssertThrows(ErrorCode.WRONG_USER_OR_PASSWORD) {

            @Override
            public void test() throws SQLException {
                Server.shutdownTcpServer("tcp://localhost:" + prt, "", true, false);
            }
        };
        conn = getConnection("jdbc:h2:tcp://localhost:" + prt + "/test", "sa", "");
        // conn.close();
        Server.shutdownTcpServer("tcp://localhost:" + prt, "abc", true, false);
        // check that the database is closed
        deleteDb("test");
        // server must have been closed
        assertThrows(ErrorCode.CONNECTION_BROKEN_1, this).getConnection("jdbc:h2:tcp://localhost:" + prt + "/test", "sa", "");
        JdbcUtils.closeSilently(conn);
        // Test filesystem prefix and escape from baseDir
        deleteDb("testSplit");
        server = Server.createTcpServer("-baseDir", getBaseDir(), "-tcpAllowOthers").start();
        final int p = server.getPort();
        conn = getConnection("jdbc:h2:tcp://localhost:" + p + "/split:testSplit", "sa", "");
        conn.close();
        assertThrows(ErrorCode.IO_EXCEPTION_1, this).getConnection("jdbc:h2:tcp://localhost:" + p + "/../test", "sa", "");
        server.stop();
        deleteDb("testSplit");
    } finally {
        shutdownServers();
    }
}
Also used : AssertThrows(org.h2.test.utils.AssertThrows) Server(org.h2.tools.Server) Connection(java.sql.Connection)

Aggregations

Server (org.h2.tools.Server)3 Socket (java.net.Socket)2 IOException (java.io.IOException)1 ServerSocket (java.net.ServerSocket)1 UnknownHostException (java.net.UnknownHostException)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 ConnectionInfo (org.h2.engine.ConnectionInfo)1 DbException (org.h2.message.DbException)1 TcpServer (org.h2.server.TcpServer)1 PgServer (org.h2.server.pg.PgServer)1 WebServer (org.h2.server.web.WebServer)1 AssertThrows (org.h2.test.utils.AssertThrows)1