Top Description Inners Fields Constructors Methods
org.hsqldb.server

public Class Server

extends Object
implements HsqlSocketRequestHandler, Notified
Class Inheritance
All Implemented Interfaces
org.hsqldb.lib.Notified, org.hsqldb.server.HsqlSocketRequestHandler
Known Direct Subclasses
org.hsqldb.server.WebServer, org.hsqldb.Server
Imports
java.io.File, .IOException, .PrintWriter, java.net.ServerSocket, .Socket, .UnknownHostException, java.sql.DriverManager, java.util.Enumeration, .Properties, .StringTokenizer, org.hsqldb.Database, .DatabaseManager, .DatabaseURL, .HsqlException, org.hsqldb.error.Error, .ErrorCode, org.hsqldb.lib.ArrayUtil, .FileUtil, .HashSet, .IntKeyHashMap, .Iterator, .Notified, .StopWatch, .StringUtil, org.hsqldb.persist.HsqlDatabaseProperties, .HsqlProperties, org.hsqldb.resources.ResourceBundleHandler, org.hsqldb.result.Result, .ResultConstants, org.hsqldb.types.HsqlDateTime.SystemTimeString

The HyperSQL hsql: and hsqls: protocol network database server.

A Server object acts as a network database server and is one way of using the client-server mode of HSQLDB Database Engine. Instances of this class handle native HSQL protocol connections exclusively, allowing database queries to be performed efficiently across the network. Server's direct descendant, WebServer, handles HTTP protocol connections exclusively, allowing HSQL protocol to be tunnelled over HTTP to avoid sandbox and firewall issues, albeit less efficiently.

There are a number of ways to configure and start a Server instance.

When started from the command line or programatically via the main(String[]) method, configuration occurs in three phases, with later phases overriding properties set by previous phases:

  1. Upon construction, a Server object is assigned a set of default properties.
  2. If it exists, properties are loaded from a file named 'server.properties' in the present working directory.
  3. The command line arguments (alternatively, the String[] passed to main()) are parsed and used to further configure the Server's properties.

From the command line, the options are as follows:

+-----------------+-------------+----------+------------------------------+
|    OPTION       |    TYPE     | DEFAULT  |         DESCRIPTION          |
+-----------------+-------------+----------+------------------------------|
| --help          |             |          | prints this message          |
| --address       | name|number | any      | server inet address          |
| --port          | number      | 9001/544 | port at which server listens |
| --database.i    | [type]spec  | 0=test   | path of database i           |
| --dbname.i      | alias       |          | url alias for database i     |
| --silent        | true|false  | true     | false => display all queries |
| --trace         | true|false  | false    | display JDBC trace messages  |
| --tls           | true|false  | false    | TLS/SSL (secure) sockets     |
| --no_system_exit| true|false  | false    | do not issue System.exit()   |
| --remote_open   | true|false  | false    | can open databases remotely  |
| --props         | filepath    |          | file path of properties file |
+-----------------+-------------+----------+------------------------------+
The database.i and dbname.i options need further explanation: From the 'server.properties' file, options can be set similarly, using a slightly different format.

Here is an example 'server.properties' file:

server.port=9001
server.database.0=test
server.dbname.0=...
...
server.database.n=...
server.dbname.n=...
server.silent=true
Starting with 1.7.2, Server has been refactored to become a simple JavaBean with non-blocking start() and stop() service methods. It is possible to configure a Server instance through the JavaBean API as well, but this part of the public interface is still under review and will not be finalized or documented fully until the final 1.7.2 release.

Note:

The 'no_system_exit' property is of particular interest.

If a Server instance is to run embedded in, say, an application server, such as when the JDBCDataSource or HsqlServerFactory classes are used, it is typically necessary to avoid calling System.exit() when the Server instance shuts down.

By default, 'no_system_exit' is set:

  1. true when a Server is started directly from the start() method.
  2. false when a Server is started from the main(String[]) method.
These values are natural to their context because the first case allows the JVM to exit by default on Server shutdown when a Server instance is started from a command line environment, whereas the second case prevents a typically unwanted JVM exit on Server shutdown when a Server instance is started as part of a larger framework.
Author
Fred Toussi (fredt@users dot sourceforge.net)
Since
1.7.2
Version
2.7.4

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
Server.ServerThread

A specialized Thread inner class in which the run() method of this server executes.

Field Summary

Modifier and TypeField and Description
private ServerAcl
pack-priv volatile long
pack-priv HashSet<String>
protected long[]
protected String[]
protected int[]
protected String[]
protected HsqlProperties[]
protected String[]
private PrintWriter
protected boolean
protected volatile boolean
private volatile boolean
private volatile boolean
private PrintWriter
protected int
protected int
protected static final int
protected ThreadGroup
pack-priv HashSet<ServerConnection>
pack-priv final Object
private Throwable
protected String
pack-priv ServerProperties
protected int
private volatile int
private Thread
protected volatile ServerSocket
protected HsqlSocketFactory
private HsqlDateTime.SystemTimeString

Constructor Summary

AccessConstructor and Description
public
Server()

Creates a new Server instance handling HSQL protocol connections.

protected
Server(int
the ServerConstants code indicating which connection protocol to handle
protocol
)

Creates a new Server instance handling the specified connection protocol.

Method Summary

Modifier and TypeMethod and Description
protected boolean

Returns:

boolean
allowConnection
(Socket
the socket to test.
socket
)

Retrieves whether the specified socket should be allowed to make a connection.

public void
checkRunning(boolean
if true, ensure the server is running, else ensure the server is not running
running
)

Checks if this Server object is or is not running and throws if the current state does not match the specified value.

protected int

Returns:

int
closeOldestDatabase
()

Feature is turned off by, pending a property to allow it.

public String

Returns:

this server's host address
getAddress
()

Retrieves, in string form, this server's host address.

public String

Returns:

the url alias component of the i'th database that this Server hosts, or null if no such name exists.
getDatabaseName
(int
the index of the url alias upon which to report
index
,
boolean
if true, report the configured value, else the live value
asconfigured
)

Retrieves the url alias (network name) of the i'th database that this Server hosts.

public String

Returns:

the HSQLDB database path descriptor of the i'th database that this Server hosts, or null if no such path descriptor exists
getDatabasePath
(int
the index of the uri upon which to report
index
,
boolean
if true, report the configured value, else the live value
asconfigured
)

Retrieves the HSQLDB path descriptor (uri) of the i'th Database that this Server hosts.

public String
getDatabaseType(int index)

pack-priv final synchronized int

Returns:

int
getDBIndex
(String
String
aliasPath
)

return database ID

private IntKeyHashMap<String>

Returns:

IntKeyHashMap
getDBNameArray
()

Returns a map of n values from server.dbname.n values to database names from the properties object.

public String

Returns:

the name of the web page served when no page is specified
getDefaultWebPage
()

Retrieves the name of the web page served when no page is specified.

public PrintWriter

Returns:

the PrintWriter to which server errors are printed.
getErrWriter
()

Retrieves the PrintWriter to which server errors are printed.

pack-priv final int
public String

Returns:

the command line and properties options help for this Server
getHelpString
()

Retrieves a String object describing the command line and properties options for this Server.

public int

Returns:

this server socket's actual host port
getLocalPort
()

Retrieves this server socket's actual host port.

public PrintWriter

Returns:

the PrintWriter to which server messages are printed.
getLogWriter
()

Retrieves the PrintWriter to which server messages are printed.

public int

Returns:

this server socket's max wait time
getMaxWait
()

Retrieves this server socket's max wait time for connection handshake in milliseconds.

public int

Returns:

this server's host port as configured
getPort
()

Retrieves this server's host port as configured.

public String

Returns:

the product name of this server
getProductName
()

Retrieves this server's product name.

public String

Returns:

the product version of the server
getProductVersion
()

Retrieves the server's product version, as a String.

public String

Returns:

string representation of this server's protocol
getProtocol
()

Retrieves a string resresentaion of the network protocol this server offers, typically one of 'HTTP', HTTPS', 'HSQL' or 'HSQLS'.

public Throwable

Returns:

a Throwable indicating the last server error
getServerError
()

Retrieves a Throwable indicating the last server error, if any.

public String

Returns:

a String identifying this Server object
getServerId
()

Retrieves a String identifying this Server object.

public Thread

Returns:

thread
getServerThread
()

Returns thread object for "HSQLDB Server" thread

public int

Returns:

this server's state code.
getState
()

Retrieves current state of this server in numerically coded form.

public String

Returns:

this server's state represented as a character sequence.
getStateDescriptor
()

Retrieves a character sequence describing this server's current state, including the message of the last exception, if there is one, and it is still in context.

public String

Returns:

the root context (directory) from which web content is served
getWebRoot
()

Retrieves the root context (directory) from which web content is served.

public void
handleConnection(Socket
the socket to connect
s
)

Implements org.hsqldb.server.HsqlSocketRequestHandler.handleConnection.

Assigns the specified socket to a new connection handler and starts the handler in a new Thread.

protected void
init(int
typically either SC_PROTOCOL_HTTP or SC_PROTOCOL_HSQL
protocol
)

Initializes this server, setting the accepted connection protocol.

public boolean

Returns:

true if this server does not call System.exit()
isNoSystemExit
()

Retrieves whether this server calls System.exit() when shutdown.

public boolean

Returns:

boolean
isNotRunning
()

Returns true if this server is not running

public boolean

Returns:

true this server restarts on shutdown
isRestartOnShutdown
()

Retrieves whether this server restarts on shutdown.

public boolean

Returns:

if true, silent mode was requested, else trace messages are to be printed
isSilent
()

Retrieves whether silent mode operation was requested in the server properties.

public boolean

Returns:

if true, secure sockets are requested, else not
isTls
()

Retrieves whether the use of secure sockets was requested in the server properties.

public boolean

Returns:

true if tracing is on (JDBC trace messages to system out)
isTrace
()

Retrieves whether JDBC trace messages are to go to System.out or the DriverManger PrintStream/PrintWriter, if any.

public static void
main(String[]
the command line arguments for the Server instance
args
)

Creates and starts a new Server.

public final void
notify(int
database id
id
)

Implements org.hsqldb.lib.Notified.notify.

This is called from org.hsqldb.DatabaseManager when a database is shutdown.

pack-priv final int

Returns:

int
openDatabase
(String
String
alias
,
String
String
datapath
)

Open and return database index

pack-priv final boolean

Returns:

boolean
openDatabases
()

Opens this server's database instances.

private void
openServerSocket()

Constructs and installs a new ServerSocket instance for this server.

protected void
printError(String
the message to print
msg
)

Prints an error message to this Server object's errWriter.

protected static void
printHelp(String
for message
key
)

Prints message for the specified key, without any special formatting.

protected void
printProperties()

Prints a description of the server properties iff !isSilent().

pack-priv final void
printRequest(long
the connection identifier
cid
,
Result
the request whose description is to be printed
r
)

Prints a description of the request encapsulated by the Result argument, r. Printing occurs iff isSilent() is false.

pack-priv final void
printResource(String
the resource key
key
)

Prints value from server's resource bundle, formatted to identify that the print operation is against this server instance.

private void
printServerOnlineMessage()

Prints a timestamped message indicating that this server is online

protected void
printStackTrace(Throwable
the Throwable whose stack trace is to be printed
t
)

Prints the stack trace of the Throwable, t, to this Server object's errWriter.

protected void
printWithThread(String
the message to print
msg
)

Prints a message formatted similarly to print(String), additionally identifying the current (calling) thread.

pack-priv final void
printWithTimestamp(String
the message to print
msg
)

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.

public boolean

Returns:

true if the indicated file was read successfully, else false
putPropertiesFromFile
(String
the path of the desired properties file, without the '.properties' file extension
path
)

Attempts to put properties from the file with the specified path.

public boolean

Returns:

true if the indicated file was read successfully, else false
putPropertiesFromFile
(String
the path of the desired properties file.
path
,
String
extension to add to path
extension
)

Attempts to put properties from the file with given extension.

public void
putPropertiesFromString(String
semicolon-delimited key=value pair string, e.g. silent=false;port=8080;...
s
)

Puts properties from the supplied string argument.

pack-priv final synchronized void
releaseDatabase(int
int
id
)

This releases the resources used for a database.

private synchronized void
releaseServerSocket()

Puts this server into the SERVER_CLOSING state, closes the ServerSocket and nullifies the reference to it.

private void
run()

Attempts to bring this server fully online by opening a new ServerSocket, obtaining the hosted databases, notifying the status waiter thread (if any) and finally entering the listen loop if all else succeeds.

pack-priv synchronized void
setActionSequence(int
int
dbIndex
)

Used by Connection object

public void
setAddress(String
A string representing the desired InetAddress as would be retrieved by InetAddres.getByName(), or a null or empty string or "0.0.0.0" to signify that the server socket should be constructed using the signature that does not specify the InetAddress.
address
)

Sets the InetAddress with which this server's ServerSocket will be constructed.

public void
setDaemon(boolean
ignored
daemon
)

No-op as deprecated.

public void
setDatabaseName(int
int
index
,
String
external name (url alias) of the i'th HSQLDB database instance this server is to host.
name
)

Sets the external name (url alias) of the i'th hosted database.

public void
setDatabasePath(int
int
index
,
String
The path of the i'th HSQLDB database instance this server is to host.
path
)

Sets the path of the hosted database.

private void
setDBInfoArrays()

Initialises the database attributes lists from the server properties object.

public void
setDefaultWebPage(String
the name of the web page served when no page is specified
file
)

Sets the name of the web page served when no page is specified.

public void
setErrWriter(PrintWriter
the PrintWriter to which server messages are logged
pw
)

Sets the PrintWriter to which server errors are logged.

private static void
setLogToSystem(boolean value)

public void
setLogWriter(PrintWriter
the PrintWriter to which server messages are logged
pw
)

Sets the PrintWriter to which server messages are logged.

public void
setMaxWait(int
an integer between 0 and 30000
maxWait
)

Sets the max wait time for connection handshake in milliseconds.

public void
setNoSystemExit(boolean
if true, System.exit() will not be called.
noExit
)

Sets whether this server calls System.exit() when shutdown.

public void
setPort(int
the port at which this server listens
port
)

Sets the server listen port.

public void
setProperties(Properties
The object containing properties to set
props
)

Sets server properties using the specified properties object

public void
setProperties(HsqlProperties
The HsqlProperties object containing properties to set
props
)

Sets server properties using the specified HsqlProperties object

public void
setRestartOnShutdown(boolean
if true, this server restarts on shutdown
restart
)

Sets whether this server restarts on shutdown.

protected void
setServerError(Throwable
The new value for the server error
t
)

Sets this Server's last encountered error state.

public void
setSilent(boolean
if true, then silent mode, else trace messages are to be printed
silent
)

Sets silent mode operation

protected synchronized void
setState(int
the new value
state
)

Sets the server state value.

public void
setTls(boolean
true for secure sockets, else false
tls
)

Sets whether to use secure sockets

public void
setTrace(boolean
if true, route JDBC trace messages to System.out
trace
)

Sets whether trace messages go to System.out or the DriverManger PrintStream/PrintWriter, if any.

public void
setWebRoot(String
the root (context) directory from which web content is served
root
)

Sets the path of the root directory from which web content is served.

public void
shutdown()

External method to shut down this server.

protected synchronized void
shutdown(boolean
true if shutdown is in response to an error state, else false
error
)

Shuts down this server.

public void
shutdownCatalogs(int
a value between 1-4, usually 2 for NORMAL.
shutdownMode
)

Shuts down all the database served by this server.

public void
shutdownWithCatalogs(int
a value between 1-4, usually 2 for NORMAL.
shutdownMode
)

Shuts down this server and all the database served by this server.

public synchronized void
public int

Returns:

the server state noted at entry to this method
start
()

Starts this server synchronously.

public int

Returns:

the server state noted at entry to this method
stop
()

Stops this server asynchronously.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait