📄 server.java
字号:
exitCode = EXIT_ERROR;
}
out.println(pg.getStatus());
}
if (webStart) {
web = createWebServer(args);
web.setShutdownHandler(this);
try {
web.start();
} catch (SQLException e) {
// ignore (status is displayed)
e.printStackTrace();
exitCode = EXIT_ERROR;
}
out.println(web.getStatus());
// start browser anyway (even if the server is already running)
// because some people don't look at the output,
// but are wondering why nothing happens
if (browserStart) {
StartBrowser.openURL(web.getURL());
}
}
if (ftpStart) {
ftp = createFtpServer(args);
try {
ftp.start();
} catch (SQLException e) {
// ignore (status is displayed)
e.printStackTrace();
exitCode = EXIT_ERROR;
}
out.println(ftp.getStatus());
}
return exitCode;
}
/**
* Shutdown a TCP server. If force is set to false, the server will not
* allow new connections, but not kill existing connections, instead it will
* stop if the last connection is closed. If force is set to true, existing
* connections are killed. After calling the method with force=false, it is
* not possible to call it again with force=true because new connections are
* not allowed. Example:
*
* <pre>
* Server.shutdownTcpServer("tcp://localhost:9094", password, true);
* </pre>
*
* @param url example: tcp://localhost:9094
* @param password the password to use ("" for no password)
* @param force the shutdown (don't wait)
* @throws ClassNotFoundException
* @throws SQLException
*/
public static void shutdownTcpServer(String url, String password, boolean force) throws SQLException {
TcpServer.shutdown(url, password, force);
}
String getStatus() {
StringBuffer buff = new StringBuffer();
if (isRunning()) {
buff.append(service.getType());
buff.append(" server running on ");
buff.append(service.getURL());
buff.append(" (");
if (service.getAllowOthers()) {
buff.append("others can connect");
} else {
buff.append("only local connections");
}
buff.append(")");
} else {
buff.append("Port is in use, maybe another " + service.getType() + " server already running on ");
buff.append(service.getURL());
}
return buff.toString();
}
/**
* Create a new web server, but does not start it yet. Example:
*
* <pre>
* Server server = Server.createWebServer(
* new String[] { "-log", "true" }).start();
* </pre>
*
* @param args
* @return the server
*/
public static Server createWebServer(String[] args) throws SQLException {
WebServer service = new WebServer();
Server server = new Server(service, args);
service.setShutdownHandler(server);
return server;
}
/**
* Create a new ftp server, but does not start it yet. Example:
*
* <pre>
* Server server = Server.createFtpServer(
* new String[] { "-log", "true" }).start();
* </pre>
*
* @param args
* @return the server
*/
public static Server createFtpServer(String[] args) throws SQLException {
return new Server(new FtpServer(), args);
}
/**
* Create a new TCP server, but does not start it yet. Example:
*
* <pre>
* Server server = Server.createTcpServer(
* new String[] { "-tcpAllowOthers", "true" }).start();
* </pre>
*
* @param args
* @return the server
*/
public static Server createTcpServer(String[] args) throws SQLException {
return new Server(new TcpServer(), args);
}
/**
* Create a new PG server, but does not start it yet.
* Example:
* <pre>
* Server server =
* Server.createPgServer(new String[]{
* "-pgAllowOthers", "true"}).start();
* </pre>
*
* @param args
* @return the server
*/
public static Server createPgServer(String[] args) throws SQLException {
return new Server(new PgServer(), args);
}
/**
* Tries to start the server.
* @return the server if successful
* @throws SQLException if the server could not be started
*/
public Server start() throws SQLException {
service.start();
Thread t = new Thread(this);
t.setName(service.getName() + " (" + service.getURL() + ")");
t.start();
for (int i = 1; i < 64; i += i) {
wait(i);
if (isRunning()) {
return this;
}
}
throw Message.getSQLException(ErrorCode.CONNECTION_BROKEN);
}
private static void wait(int i) {
try {
// sleep at most 4096 ms
long sleep = (long) i * (long) i;
Thread.sleep(sleep);
} catch (InterruptedException e) {
// ignore
}
}
private void stopAll() {
if (web != null && web.isRunning()) {
web.stop();
web = null;
}
if (tcp != null && tcp.isRunning()) {
tcp.stop();
tcp = null;
}
if (pg != null && pg.isRunning()) {
pg.stop();
pg = null;
}
if (ftp != null && ftp.isRunning()) {
ftp.stop();
ftp = null;
}
}
/**
* Checks if the server is running.
*
* @return if the server is running
*/
public boolean isRunning() {
return service.isRunning();
}
/**
* Stops the server.
*/
public void stop() {
service.stop();
}
/**
* Gets the URL of this server.
* @return the url
*/
public String getURL() {
return service.getURL();
}
private Server(Service service, String[] args) throws SQLException {
this.service = service;
try {
service.init(args);
} catch (Exception e) {
throw Message.convert(e);
}
}
/**
* INTERNAL
*/
public void run() {
try {
service.listen();
} catch (Exception e) {
TraceSystem.traceThrowable(e);
}
}
/**
* INTERNAL
*/
public void setShutdownHandler(ShutdownHandler shutdownHandler) {
this.shutdownHandler = shutdownHandler;
}
/**
* INTERNAL
*/
public void shutdown() {
if (shutdownHandler != null) {
shutdownHandler.shutdown();
} else {
stopAll();
}
}
/**
* Get the service attached to this server.
*
* @return the service
*/
public Service getService() {
return service;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -