Class VaadinSessionTracker

java.lang.Object
org.dellroad.stuff.vaadin24.util.VaadinSessionTracker

public class VaadinSessionTracker extends Object
Tracks and limits the open VaadinSession's in a Vaadin application and provides a thread-safe way to poll them for information.

The application is responsible for registering new sessions via registerCurrentSession(). This should happen as soon as possible, e.g., on a login screen. If that method returns false, then the maximum number of allowed sessions has already been reached, and so the application should take appropriate action, e.g., display an error message and invalidate the new session.

Sessions are automatically unregistered when they close.

All of the currently registered sessions can be surveyed via surveySessions().

This class does not care if the registered sessions come from multiple Vaadin servlets; if there are multiple servlets, all of the registered sessions will end up being tracked and counted together in one common pool.

  • Field Details

    • log

      protected final Logger log
  • Constructor Details

    • VaadinSessionTracker

      public VaadinSessionTracker()
      Default constructor.

      This instance will be configured with no limit on the number of sessions.

    • VaadinSessionTracker

      public VaadinSessionTracker(int maxSessions)
      Constructor.
      Parameters:
      maxSessions - maximum number of allowed sessions, or zero for infinity
      Throws:
      IllegalArgumentException - if maxSessions is negative
  • Method Details

    • setMaxSessions

      public void setMaxSessions(int maxSessions)
      Configure the maximum number of allowed sessions.

      Default is zero, which means no limit.

      Parameters:
      maxSessions - maximum number of allowed sessions, or zero for infinity
      Throws:
      IllegalArgumentException - if maxSessions is negative
    • getNumSessions

      public int getNumSessions()
      Get the number of currently registered sessions.
      Returns:
      number of registered sessions
    • registerCurrentSession

      public boolean registerCurrentSession()
      Register the current Vaadin session.

      The current thread must have an associated VaadinSession and be holding its lock.

      If the session is already registered, or in the VaadinSessionState.CLOSING state, nothing happens and this method returns true. Otherwise the session is added if there is room, otherwise false is returned.

      Returns:
      true if the new total number of sessions does not exceed the maximum number allowed, otherwise false
      Throws:
      IllegalStateException - if there is no VaadinSession associated with the current thread
      IllegalStateException - if the current VaadinSession is not locked
      IllegalArgumentException - the VaadinSession is in state VaadinSessionState.CLOSED
    • unregisterSession

      public boolean unregisterSession(VaadinSession session)
      Manually unregister a session.

      It is not necessary to explicitly call this method; sessions are unregistered automatically on close.

      Parameters:
      session -
      Returns:
      true if session was unregistered, false if session was already not registered
      Throws:
      IllegalArgumentException - if session is null
    • surveySessions

      public <T> Map<VaadinSession,T> surveySessions(Function<? super VaadinSession,? extends T> extractor) throws InterruptedException
      Survey all registered VaadinSessions and extract some info from each one.

      This method guarantees that each session is locked while extractor is extracting from it.

      This method must not be invoked with a current and locked VaadinSession.

      Parameters:
      extractor - returns info about a session (may be null)
      Returns:
      mapping from VaadinSession to the corresponding extracted information
      Throws:
      IllegalArgumentException - if extractor is null
      IllegalStateException - if there is a locked VaadinSession associated with the current thread
      InterruptedException - if interrupted while waiting to lock a session