Class RemoteDbSessionImpl

java.lang.Object
org.tentackle.dbms.trip.RemoteDbSessionImpl
All Implemented Interfaces:
RemoteDbSession, Remote, RemoteRoot

public abstract class RemoteDbSessionImpl extends Object implements RemoteDbSession, RemoteRoot
User session within the application server.
Author:
harald
  • Constructor Details

    • RemoteDbSessionImpl

      public RemoteDbSessionImpl(RemoteDbConnectionImpl con, SessionInfo clientInfo, SessionInfo serverInfo)
      Creates a session on a given connection.
      Parameters:
      con - the connection
      clientInfo - the SessionInfo from the client
      serverInfo - the SessionInfo to establish the connection to the database server
      Throws:
      PersistenceException - if the session could not be initiated.
  • Method Details

    • startCleanupThread

      public static void startCleanupThread(long checkInterval)
      Starts the optional cleanup thread that will monitor the sessions for database activity.
      Parameters:
      checkInterval - is the interval in ms
    • stopCleanupThread

      public static void stopCleanupThread()
      Stops the cleanup thread.
    • getOpenSessions

      public static Collection<RemoteDbSessionImpl> getOpenSessions()
      Returns a list of all open sessions.
      Returns:
      the sessions, never null
    • isUserLoggedIn

      public static SessionInfo isUserLoggedIn(SessionInfo sessionInfo)
      Checks if the user is already logged in.
      Parameters:
      sessionInfo - the user's session info
      Returns:
      the session info if already logged in, null if not logged in
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • setRegistryCoordinates

      public void setRegistryCoordinates(Registry registry, UUID id)
      Description copied from interface: RemoteRoot
      Sets the registry and the delegate ID this object belongs to.
      Specified by:
      setRegistryCoordinates in interface RemoteRoot
      Parameters:
      registry - the registry
      id - the delegate ID
    • unregisterFromRegistry

      public void unregisterFromRegistry()
      Description copied from interface: RemoteRoot
      Unregisters this target object's delegate from the registry.
      Specified by:
      unregisterFromRegistry in interface RemoteRoot
    • setInvocationFilter

      public void setInvocationFilter(RemoteDbSessionInvocationFilter invocationFilter)
      Sets the invocation filter.
      Parameters:
      invocationFilter - the invocation filter, null for none (default)
    • getInvocationFilter

      public RemoteDbSessionInvocationFilter getInvocationFilter()
      Gets the invocation filter.
      Returns:
      the invocation filter, null for none (default)
    • addMasterSerialEvent

      public void addMasterSerialEvent(MasterSerialEvent masterSerialEvent)
      Adds a master serial event.
      Parameters:
      masterSerialEvent - the event
    • pollMasterSerialEvent

      public MasterSerialEvent pollMasterSerialEvent()
      Gets the next master serial event.
      The method is invoked by the ModificationTracker.
      Returns:
      the next master serial event, null if none
    • verifySessionInfo

      public abstract void verifySessionInfo(SessionInfo sessionInfo)
      Verifies and updates the client's session info.
      Needs to be implemented by the application.
      Checks login credentials and sets the user id.

      Throws LoginFailedException if login is not allowed for whatever reason.

      Parameters:
      sessionInfo - the session info
    • getSessionNumber

      public long getSessionNumber()
      Gets the unique session number.
      Returns:
      the number of this session
    • getTimeout

      public int getTimeout()
      Gets the session timeout.
      Returns:
      the timeout in timeout intervals
    • setTimeout

      public void setTimeout(int timeout)
      Sets the session timeout.
      Parameters:
      timeout - the timeout in timeout intervals
    • getSession

      public Db getSession()
      Gets the session.
      Returns:
      the session
    • getConnection

      public RemoteDbConnectionImpl getConnection()
      Gets the server connection.
      Returns:
      the connection
    • getClientSessionInfo

      public SessionInfo getClientSessionInfo()
      Gets the client session info.
      Specified by:
      getClientSessionInfo in interface RemoteDbSession
      Returns:
      the client session info
    • getServerSessionInfo

      public SessionInfo getServerSessionInfo()
      Gets the server session info.
      Returns:
      the server session info
    • getClosedSince

      public long getClosedSince()
      Gets the epochal time when the session was closed.
      Returns:
      the time when closed, 0 if still open
    • isModificationTrackerSession

      public boolean isModificationTrackerSession()
      Returns whether this is the remote session of the modification tracker.
      Returns:
      true if tracker's session
    • setModificationTrackerSession

      public void setModificationTrackerSession(boolean modificationTrackerSession)
      Sets the modification tracker session flag.
      Parameters:
      modificationTrackerSession - true if tracker's session
    • countMethodInvocation

      public void countMethodInvocation(Method method, Class<?> servicedClass, TimeKeeper timeKeeper)
      Counts the invocation of a delegate method.
      Parameters:
      method - the method invoked
      servicedClass - the serviced class
      timeKeeper - execution duration
    • getClientHostString

      public String getClientHostString()
      Gets the string representation of the client host connected to this session.
      Returns:
      the client host string
    • getOptions

      public String getOptions()
      Returns an application-specific option string.
      For diagnostic purposes only.
      Returns:
      null if no options
    • getMdcInfo

      public String getMdcInfo()
      Gets a string useful for mapped diagnostic context info.
      Returns:
      the MDC info
    • isOpen

      public boolean isOpen()
      Determines whether the session is open.
      Returns:
      true if the session is open
    • close

      public void close()
      Description copied from interface: RemoteDbSession
      Closes a session.
      Specified by:
      close in interface RemoteDbSession
    • log

      public void log(String name, Logger.Level level, String message)
      Description copied from interface: RemoteDbSession
      Sends text based logging infos to the TRIP-Server.
      Specified by:
      log in interface RemoteDbSession
      Parameters:
      name - the logger name, null if predefined
      level - the logging level
      message - the message
    • logStatistics

      public void logStatistics(Logger.Level level, boolean clear)
      Description copied from interface: RemoteDbSession
      Logs the TRIP-statistics.
      Specified by:
      logStatistics in interface RemoteDbSession
      Parameters:
      level - the logging level
      clear - true if clear statistics after dump
    • getRemoteDelegate

      public <T extends Remote> T getRemoteDelegate(String classname)
      Description copied from interface: RemoteDbSession
      Gets the delegate for a given interface name.
      If the implementation isn't annotated with RemoteService, it will be located according to the NamingRules.
      Specified by:
      getRemoteDelegate in interface RemoteDbSession
      Type Parameters:
      T - the delegate type
      Parameters:
      classname - is the name of remote interface
      Returns:
      the delegate
    • getRemoteDelegate

      public DbRemoteDelegate getRemoteDelegate()
      Description copied from interface: RemoteDbSession
      Gets the delegate for the remote session.
      Specified by:
      getRemoteDelegate in interface RemoteDbSession
      Returns:
      the delegate
    • createRemoteDelegateInstance

      public <T extends RemoteDbDelegate, I extends RemoteDbDelegateImpl<?>> T createRemoteDelegateInstance(Class<T> delegateClass, Class<I> delegateImplClass, Class<?> clazz, Class<?> effectiveClass, Object... configArgs) throws InstantiationException, NoSuchMethodException, IllegalAccessException, InvocationTargetException
      Creates a remote delegate for the given class.

      If the optional configuration arguments are given, the delegate must provide a method with the following signature:

       public void configureDelegate(Object... args) {
         // whatever args stands for
       }
      
      The optional configuration parameters allow passing additional objects to the delegate without having to declare the setters in the remote interface.
      Type Parameters:
      T - the delegate class
      I - the delegate implementation class
      Parameters:
      delegateClass - the interface class
      delegateImplClass - the implementing class
      clazz - the serviced class
      effectiveClass - the optional effective serviced class, null if clazz
      configArgs - optional arguments to configure the delegate
      Returns:
      the created delegate
      Throws:
      InstantiationException - if delegate could not be instantiated
      NoSuchMethodException - if the delegate does not provide the necessary constructor
      IllegalAccessException - if access denied
      IllegalArgumentException - if wrong arguments
      InvocationTargetException - if invocation failed
    • createRemoteDelegate

      public <T extends RemoteDbDelegate, I extends RemoteDbDelegateImpl<?>> T createRemoteDelegate(Class<T> delegateClass, Class<I> delegateImplClass, Class<?> clazz, Class<?> effectiveClass, Object... configArgs) throws InstantiationException, NoSuchMethodException, IllegalAccessException, InvocationTargetException
      Creates a remote delegate for the given class.
      Same as createRemoteDelegateInstance(Class, Class, Class, Class, Object...) but with dynamic proxy to allow intercepting.
      Type Parameters:
      T - the remote delegate class
      I - the delegate implementation class
      Parameters:
      delegateClass - the interface class
      delegateImplClass - the implementing class
      clazz - the serviced class
      effectiveClass - the effectively serviced class, null if clazz
      configArgs - optional configuration arguments passed to createRemoteDelegateInstance(Class, Class, Class, Class, Object...)
      Returns:
      the created
      Throws:
      InstantiationException - if delegate could not be instantiated
      NoSuchMethodException - if the delegate does not provide the necessary constructor
      IllegalAccessException - if access denied
      IllegalArgumentException - if wrong arguments
      InvocationTargetException - if invocation failed
    • determineTimeout

      protected int determineTimeout(Session session, SessionInfo clientInfo, SessionInfo serverInfo)
      Determines the session timeout count.
      The default implementation returns TripServer.getSessionTimeout().
      Parameters:
      session - the session attached to the RemoteDbSession
      clientInfo - the session info from the client
      serverInfo - the session info to establish the connection to the database rmiServer
      Returns:
      the timeout
    • openDb

      protected Db openDb()
      Gets an open db session for this remote client session.
      Returns:
      the db session
      Throws:
      LoginFailedException - if opening the session failed
    • cleanup

      protected void cleanup(boolean crashed)
      Clean up the session.

      The method is invoked whenever the session is closed due to an ordinary logout or client crash. The default implementation rolls back any pending transaction.

      Parameters:
      crashed - true if the client crashed, else regular logout
    • doLogStatistics

      protected void doLogStatistics(Logger.Level level, boolean clear)
      Logs the TRIP-statistics.
      Parameters:
      level - the logging level
      clear - true if clear statistics after dump
    • closeDb

      protected void closeDb(boolean cleanup)
      Closes the database connection (and thus rolls back any pending transaction). If the db is pooled, it will be returned to the pool instead of being closed.
      Parameters:
      cleanup - true if db must be physically closed even if pooled, due to session cleanup
    • forceCleanup

      protected void forceCleanup()
      Forces a cleanup if the regular cleanup and closing failed.

      Simply marks the session closed and moves all references to GC.

    • hasTimedOut

      protected boolean hasTimedOut()
      Checks for timeout. Will internally increment a counter until timeout has reached.
      Returns:
      true if timed out
    • getTimeoutCount

      protected int getTimeoutCount()
      Gets the current timeout counter.
      Returns:
      the timeout counter
    • polled

      protected void polled()
      Sets this session as being polled for timeout.