📄 server.java
字号:
* This method returns immediately, regardless of current state. In order * to discover the success or failure of this operation, server state must * be polled or a subclass of Server must be used that overrides the * setState method to provide state change notification. * * @return the server state noted at entry to this method * * @jmx.managed-operation * impact="ACTION_INFO" * description="Invokes asynchronous shutdown sequence; returns previous state" */ public int stop() { printWithThread("stop() entered"); int previousState = getState(); if (serverThread == null) { printWithThread("stop() serverThread is null; no action taken"); return previousState; } releaseServerSocket(); printWithThread("stop() exiting"); return previousState; } /** * Retrieves whether the specified socket should be allowed * to make a connection. By default, this method always returns * true, but it can be overidden to implement hosts allow-deny * functionality. * * @param socket the socket to test. */ protected boolean allowConnection(Socket socket) { return true; } /** * Initializes this server, setting the accepted connection protocol. * * @param protocol typically either SC_PROTOCOL_HTTP or SC_PROTOCOL_HSQL */ protected void init(int protocol) { // PRE: This method is only called from the constructor serverState = ServerConstants.SERVER_STATE_SHUTDOWN; serverConnSet = new HashSet(); serverId = toString(); serverId = serverId.substring(serverId.lastIndexOf('.') + 1); serverProtocol = protocol; serverProperties = ServerConfiguration.newDefaultProperties(protocol); logWriter = new PrintWriter(System.out); errWriter = new PrintWriter(System.err); JavaSystem.setLogToSystem(isTrace()); } /** * Sets the server state value. * * @param state the new value */ protected synchronized void setState(int state) { serverState = state; }// Package visibility for related classes and intefaces// that may need to make calls back here. /** * This is called from org.hsqldb.DatabaseManager when a database is * shutdown. This shuts the server down if it is the last database * * @param action a code indicating what has happend */ final void notify(int action, int id) { printWithThread("notifiy(" + action + "," + id + ") entered"); if (action != ServerConstants.SC_DATABASE_SHUTDOWN) { return; } releaseDatabase(id); boolean shutdown = true; for (int i = 0; i < dbID.length; i++) { if (dbAlias[i] != null) { shutdown = false; } } if (!isRemoteOpen && shutdown) { stop(); } } /** * This releases the resources used for a database. * Is called with id 0 multiple times for non-existent databases */ final synchronized void releaseDatabase(int id) { Iterator it; boolean found = false; printWithThread("releaseDatabase(" + id + ") entered"); // check all slots as a database may be opened by multiple aliases for (int i = 0; i < dbID.length; i++) { if (dbID[i] == id && dbAlias[i] != null) { dbID[i] = 0; dbAlias[i] = null; dbPath[i] = null; dbType[i] = null; dbProps[i] = null; } } synchronized (serverConnSet) { it = new WrapperIterator(serverConnSet.toArray(null)); } while (it.hasNext()) { ServerConnection sc = (ServerConnection) it.next(); if (sc.dbID == id) { sc.signalClose(); serverConnSet.remove(sc); } } printWithThread("releaseDatabase(" + id + ") exiting"); } /** * Prints the specified message, s, formatted to identify that the print * operation is against this server instance. * * @param msg The message to print */ protected synchronized void print(String msg) { PrintWriter writer = logWriter; if (writer != null) { writer.println("[" + serverId + "]: " + msg); writer.flush(); } } /** * Prints value from server's resource bundle, formatted to * identify that the print operation is against this server instance. * Value may be localized according to the default JVM locale * * @param key the resource key */ final void printResource(String key) { String resource; StringTokenizer st; if (serverBundleHandle < 0) { return; } resource = BundleHandler.getString(serverBundleHandle, key); if (resource == null) { return; } st = new StringTokenizer(resource, "\n\r"); while (st.hasMoreTokens()) { print(st.nextToken()); } } /** * Prints the stack trace of the Throwable, t, to this Server object's * errWriter. <p> * * @param t the Throwable whose stack trace is to be printed */ protected synchronized void printStackTrace(Throwable t) { if (errWriter != null) { t.printStackTrace(errWriter); errWriter.flush(); } } /** * Prints the specified message, s, prepended with a timestamp representing * the current date and time, formatted to identify that the print * operation is against this server instance. * * @param msg the message to print */ final void printWithTimestamp(String msg) { print(HsqlDateTime.getSytemTimeString() + " " + msg); } /** * Prints a message formatted similarly to print(String), additionally * identifying the current (calling) thread. Replaces old method * trace(String msg). * * @param msg the message to print */ protected void printWithThread(String msg) { if (!isSilent()) { print("[" + Thread.currentThread() + "]: " + msg); } } /** * Prints an error message to this Server object's errWriter. * The message is formatted similarly to print(String), * additionally identifying the current (calling) thread. * * @param msg the message to print */ protected synchronized void printError(String msg) { PrintWriter writer = errWriter; if (writer != null) { writer.print("[" + serverId + "]: "); writer.print("[" + Thread.currentThread() + "]: "); writer.println(msg); writer.flush(); } } /** * Prints a description of the request encapsulated by the * Result argument, r. * * Printing occurs iff isSilent() is false. <p> * * The message is formatted similarly to print(String), additionally * indicating the connection identifier. <p> * * For Server instances, cid is typically the value assigned to each * ServerConnection object that is unique amongst all such identifiers * in each distinct JVM session / class loader * context. <p> * * For WebServer instances, a single logical connection actually spawns * a new physical WebServerConnection object for each request, so the * cid is typically the underlying session id, since that does not * change for the duration of the logical connection. * * @param cid the connection identifier * @param r the request whose description is to be printed */ final void printRequest(int cid, Result r) { if (isSilent()) { return; } StringBuffer sb = new StringBuffer(); sb.append(cid); sb.append(':'); switch (r.mode) { case ResultConstants.SQLPREPARE : { sb.append("SQLCLI:SQLPREPARE "); sb.append(r.getMainString()); break; } case ResultConstants.SQLEXECDIRECT : { if (r.getSize() < 2) { sb.append(r.getMainString()); } else { sb.append("SQLCLI:SQLEXECDIRECT:BATCHMODE\n"); Iterator it = r.iterator(); while (it.hasNext()) { Object[] data = (Object[]) it.next(); sb.append(data[0]).append('\n'); } } break; } case ResultConstants.SQLEXECUTE : { sb.append("SQLCLI:SQLEXECUTE:"); if (r.getSize() > 1) { sb.append("BATCHMODE:"); } sb.append(r.getStatementID());/** * todo - fredt - NOW - fix this without appendStringValueOf *//* if (r.getSize() == 1) { sb.append('\n'); StringUtil.appendStringValueOf(r.getParameterData(), sb, true); }*/ break; } case ResultConstants.SQLFREESTMT : { sb.append("SQLCLI:SQLFREESTMT:"); sb.append(r.getStatementID()); break; } case ResultConstants.GETSESSIONATTR : { sb.append("HSQLCLI:GETSESSIONATTR"); break; } case ResultConstants.SETSESSIONATTR : { sb.append("HSQLCLI:SETSESSIONATTR:"); sb.append("AUTOCOMMIT "); sb.append(r.rRoot.data[Session.INFO_AUTOCOMMIT]); sb.append(" CONNECTION_READONLY "); sb.append(r.rRoot.data[Session.INFO_CONNECTION_READONLY]); break; } case ResultConstants.SQLENDTRAN : { sb.append("SQLCLI:SQLENDTRAN:"); switch (r.getEndTranType()) { case ResultConstants.COMMIT : sb.append("COMMIT"); break; case ResultConstants.ROLLBACK : sb.append("ROLLBACK"); break; case ResultConstants.SAVEPOINT_NAME_RELEASE : sb.append("SAVEPOINT_NAME_RELEASE "); sb.append(r.getMainString()); break; case ResultConstants.SAVEPOINT_NAME_ROLLBACK : sb.append("SAVEPOINT_NAME_ROLLBACK "); sb.append(r.getMainString()); break; default : sb.append(r.getEndTranType()); } break; } case ResultConstants.SQLSTARTTRAN : { sb.append("SQLCLI:SQLSTARTTRAN"); break; } case ResultConstants.SQLDISCONNECT : { sb.append("SQLCLI:SQLDISCONNECT"); break; } case ResultConstants.SQLSETCONNECTATTR : { sb.append("SQLCLI:SQLSETCONNECTATTR:"); switch (r.getConnectionAttrType()) { case ResultConstants.SQL_ATTR_SAVEPOINT_NAME : { sb.append("SQL_ATTR_SAVEPOINT_NAME "); sb.append(r.getMainString()); break; } default : { sb.append(r.getConnectionAttrType()); } } break; } default : { sb.append("SQLCLI:MODE:"); sb.append(r.mode); break; } } print(sb.toString()); } /** * return database ID */ synchronized final int getDBID(String aliasPath) throws HsqlException { int semipos = aliasPath.indexOf(';'); String alias = aliasPath; String filepath = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -