Class AbstractServer<T extends Remote,A extends ServerAdmin>

java.lang.Object
is.codion.common.rmi.server.AbstractServer<T,A>
Type Parameters:
T - the type of remote interface served by this server
A - the type of the admin interface this server provides
All Implemented Interfaces:
Server<T,A>, Remote
Direct Known Subclasses:
EntityServer

public abstract class AbstractServer<T extends Remote,A extends ServerAdmin> extends Object implements Server<T,A>
A base Server implementation, constructed and then started.
  • Constructor Details

    • AbstractServer

      protected AbstractServer(ServerConfiguration configuration)
      Instantiates a new AbstractServer, which publishes nothing until started.
      Parameters:
      configuration - the configuration
  • Method Details

    • connections

      public final Map<RemoteSession,T> connections()
      Returns:
      a map containing the current connections
    • connection

      public final T connection(UUID connectionId)
      Parameters:
      connectionId - the connection id
      Returns:
      the connection with the given id
      Throws:
      IllegalArgumentException - in case no such connection exists
    • connectionCount

      public final int connectionCount()
      Returns:
      the current number of connections
    • connectionLimit

      public final int connectionLimit()
      Returns:
      the maximum number of concurrent connections accepted by this server, a negative number means no limit while 0 means the server is closed.
    • connectionLimit

      public final void connectionLimit(int connectionLimit)
      Parameters:
      connectionLimit - the maximum number of concurrent connections accepted by this server, a negative number means no limit while 0 means the server is closed.
    • maintenanceInterval

      public final int maintenanceInterval()
      Returns:
      the maintenance check interval in ms
    • maintenanceInterval

      public final void maintenanceInterval(int maintenanceInterval)
      Parameters:
      maintenanceInterval - the new maintenance interval in ms
    • information

      public final ServerInformation information()
      Specified by:
      information in interface Server<T extends Remote,A extends ServerAdmin>
      Returns:
      static information about this server
    • connectionsAvailable

      public final boolean connectionsAvailable()
      Specified by:
      connectionsAvailable in interface Server<T extends Remote,A extends ServerAdmin>
      Returns:
      true if there are connections available
    • connect

      public final T connect(ConnectionRequest connectionRequest) throws RemoteException, ConnectionNotAvailableException, LoginException
      Description copied from interface: Server
      Establishes a connection to this Server.

      If a connection already exists for the request's connectionId, that existing connection is returned after re-validating the supplied credentials (username case-insensitively, password exactly), rather than establishing a new one.

      Specified by:
      connect in interface Server<T extends Remote,A extends ServerAdmin>
      Parameters:
      connectionRequest - the information required for establishing a connection
      Returns:
      a remote connection instance
      Throws:
      RemoteException - in case of a communication error
      ConnectionNotAvailableException - in case the server isn't accepting more connections, its connection limit reached, or it shutting down
      LoginException - in case the login fails
    • disconnect

      public final void disconnect(UUID connectionId)
      Description copied from interface: Server
      Disconnects the connection identified by the given id.
      Specified by:
      disconnect in interface Server<T extends Remote,A extends ServerAdmin>
      Parameters:
      connectionId - the UUID identifying the connection that should be disconnected
    • start

      public final void start() throws RemoteException
      Starts this server, publishing it: exports it, when serving RMI connections or the admin interface, starts the auxiliary servers, binds it in the registry, when exported, starts the connection maintenance and registers a JVM shutdown hook.

      Nothing is published before this is called, so a subclass is fully constructed, its own subclasses included, before a connection request can reach it. A server failing to start is shut down.

      Throws:
      RemoteException - in case of an exception
      IllegalStateException - in case this server has already been started or has been shut down
    • shutdown

      public final void shutdown()
      Shuts down this server, also one not started.
    • setAdmin

      protected final void setAdmin(A admin)
      Sets the admin instance for this server
      Parameters:
      admin - the admin instance
      Throws:
      IllegalStateException - in case an admin instance has already been set
    • getAdmin

      protected final A getAdmin()
      Returns:
      the admin instance associated with this server
      Throws:
      IllegalStateException - in case no admin instance has been set
      See Also:
    • addShutdownListener

      protected final void addShutdownListener(Runnable listener)
      Parameters:
      listener - a listener notified when this server is shutting down.
    • connect

      protected abstract T connect(RemoteSession session) throws RemoteException, LoginException
      Establishes the actual connection.
      Parameters:
      session - the remote session
      Returns:
      a connection for the given session
      Throws:
      RemoteException - in case of an exception
      LoginException - in case of an error during the login
    • disconnect

      protected abstract void disconnect(T connection) throws RemoteException
      Disconnects the given connection.
      Parameters:
      connection - the connection to disconnect
      Throws:
      RemoteException - in case of an exception
    • maintainConnections

      protected abstract void maintainConnections(Collection<AbstractServer.SessionConnection<T>> connections) throws RemoteException
      Maintains the given connections, that is, disconnects inactive or invalid connections, if required.
      Parameters:
      connections - all current connections
      Throws:
      RemoteException - in case of an exception
    • registry

      protected final Registry registry() throws RemoteException
      Returns:
      the Registry instance
      Throws:
      RemoteException - in case of an exception
    • logShutdownAndReturn

      protected final <T extends Throwable> T logShutdownAndReturn(T exception)
      Logs the given exception and shuts down this server
      Type Parameters:
      T - the exception type
      Parameters:
      exception - the exception
      Returns:
      the exception
    • auxiliaryServers

      protected final Collection<AuxiliaryServer> auxiliaryServers()
      Returns:
      an unmodifiable view of the auxiliary servers running alongside this server
    • validateUserCredentials

      protected static void validateUserCredentials(User userToCheck, User requiredUser) throws ServerAuthenticationException
      Validates the given user credentials.

      Usernames are compared case-insensitively, passwords byte-exactly.

      Parameters:
      userToCheck - the credentials to check
      requiredUser - the required credentials
      Throws:
      ServerAuthenticationException - in case either User instance is null or if the username or password do not match