Class DerbyImpl


  • public class DerbyImpl
    extends java.lang.Object
    Class containing Derby client data and optionally to start/stop the database server.
    Author:
    Christopher Mindus
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String DERBY_CLIENT_DRIVER_CLASS
      The Derby Client driver class name.
      static int DERBY_DEFAULT_PORT
      The default Derby server network port: 1527.
    • Constructor Summary

      Constructors 
      Constructor Description
      DerbyImpl​(java.io.File jdbcDriversDir, java.io.File dir, boolean doStartDBServer, java.lang.String host, int port, java.lang.String database, boolean doCreateDB, java.lang.String username, java.lang.String password)
      Constructor.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean addServerStateListener​(java.lang.Runnable listener)
      Adds a new listener for the server state.
      static boolean canStopServer()
      Checks if the Derby network server can be stopped, or if it running outside of our reach.
      static int getServerPort()
      Gets the server port being used.
      static DBServerState getServerState()
      Gets the state of the Derby network server.
      static DBServerState isSocketConnectable​(java.lang.String host, int port, int timeout, IWebServiceEngine wsEngine)
      Checks if a host is Socket connectable on the specified host name and port.
      static boolean removeServerStateListener​(java.lang.Runnable listener)
      Removes a listener for the server state.
      static DBServerState startServer​(java.io.File jdbcDriversDir, java.io.File dbDir, int port)
      Starts the Derby network server with configured port if running on "localhost".
      static DBServerState stopServer​(java.io.File jdbcDriversDir)
      Stops the Derby network server if started from this instance.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DERBY_DEFAULT_PORT

        public static final int DERBY_DEFAULT_PORT
        The default Derby server network port: 1527.
        See Also:
        Constant Field Values
      • DERBY_CLIENT_DRIVER_CLASS

        public static final java.lang.String DERBY_CLIENT_DRIVER_CLASS
        The Derby Client driver class name.
        See Also:
        Constant Field Values
    • Constructor Detail

      • DerbyImpl

        public DerbyImpl​(java.io.File jdbcDriversDir,
                         java.io.File dir,
                         boolean doStartDBServer,
                         java.lang.String host,
                         int port,
                         java.lang.String database,
                         boolean doCreateDB,
                         java.lang.String username,
                         java.lang.String password)
        Constructor.
        Parameters:
        dir - The directory to the derby Database server current directory. If the directory does not exist, it will be created. null means that the database server is not started.
        doStartDBServer - Flag to start the Derby network database server.
        host - The host name of the Derby network database server. The server will not be started, regardless of doStartDBSeerver if host is not "localhost", "127.0.0.1" or null.
        port - The port number to use, -1 or zero for default.
        database - The database name.
        doCreateDB - Flag to create database if not existing.
        username - The user name.
        password - The password.
        Throws:
        java.lang.NullPointerException - If jdbcDriversDir is null.
        java.lang.IllegalArgumentException - If port is not 1 to 65535, or the directory jdbcDriversDir or dir does not exist.
    • Method Detail

      • getServerState

        public static DBServerState getServerState()
        Gets the state of the Derby network server.
        Returns:
        The state.
      • addServerStateListener

        public static boolean addServerStateListener​(java.lang.Runnable listener)
        Adds a new listener for the server state. Calling this method multiple times with the same listener will have no effect (but to return false).
        Parameters:
        listener - The listener.
        Returns:
        true if the listener was added, false otherwise.
        Throws:
        java.lang.NullPointerException - If the listener is null.
      • removeServerStateListener

        public static boolean removeServerStateListener​(java.lang.Runnable listener)
        Removes a listener for the server state.
        Parameters:
        listener - The listener.
        Returns:
        true if listener is successfully removed, false otherwise.
        Throws:
        java.lang.NullPointerException - If the listener is null.
      • canStopServer

        public static boolean canStopServer()
        Checks if the Derby network server can be stopped, or if it running outside of our reach.
        Returns:
        true if it can probably be stopped, false if not, or already stopped.
      • getServerPort

        public static int getServerPort()
        Gets the server port being used.
        Returns:
        The port number, zero if not started/starting.
      • isSocketConnectable

        public static DBServerState isSocketConnectable​(java.lang.String host,
                                                        int port,
                                                        int timeout,
                                                        IWebServiceEngine wsEngine)
        Checks if a host is Socket connectable on the specified host name and port. This call does just tries to access the Socket with the specified host and port, nothing else.
        Parameters:
        host - The host name or address.
        port - The port name.
        timeout - Specifies the timeout value, in milliseconds, to be used when opening a communications link to the specified host and port. A timeout of zero is interpreted as an infinite timeout and is NOT recommended.
        wsEngine - The web service engine used to resolve a proxy in case it's not a "localhost". Set to null to use Proxy.NO_PROXY.
        Returns:
        One of:
      • startServer

        public static DBServerState startServer​(java.io.File jdbcDriversDir,
                                                java.io.File dbDir,
                                                int port)
        Starts the Derby network server with configured port if running on "localhost". Otherwise nothing is done.

        If network server process failed starting, the error will be logged as SEVERE.

        Parameters:
        jdbcDriversDir - The directory for the JDBC drivers.
        dbDir - The directory for the databases.
        port - The port number.
        Returns:
        One of:
        Throws:
        java.lang.NullPointerException - If dir is null.
        java.lang.IllegalArgumentException - If port is not 1 to 65535, or the directory dir does not exist.
      • stopServer

        public static DBServerState stopServer​(java.io.File jdbcDriversDir)
        Stops the Derby network server if started from this instance.
        Parameters:
        jdbcDriversDir - The directory for the JDBC drivers.
        Returns:
        The state of the server, one of:
        • STOPPED,
        • CONNECTION_FAILED,
        • START_FAILED,
        • UNKNOWN.