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();
}
}
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();
}
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;
}
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();
}
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();
}
}
Aggregations