Class SessionLock


  • public class SessionLock
    extends ReentrantLock<SessionToken>
    Represents a reentrant read/write lock or exclusive lock based on session token.

    This session lock supports the consideration of subsessions. They may either be considered as equal to a parent session or not with regard to a lock. If you do not want to treat subsessions as equal, attend particularly to deadlocks!

    See Also:
    ReentrantReadWriteLock
    • Constructor Detail

      • SessionLock

        public SessionLock​(Class<? extends LockCountManager<SessionToken>> lockCountManager,
                           boolean exclusive,
                           Logger logger)
                    throws NoSuchMethodException,
                           InstantiationException,
                           IllegalAccessException,
                           InvocationTargetException
        Creates either a new exclusive lock or a new read/write-lock and using the designated LockCountManager for counting locks as well as comparing sessions.
        Parameters:
        lockCountManager - The manager counting locks for sessions. The provided class needs an accessible (public) constructor without parameters.
        exclusive - Whether to create an exclusive lock or a read/write-lock.
        logger - The logger for log messages.
        Throws:
        NoSuchMethodException - If the designated lock count manager has no constructor without parameters, a NouSuchMethodException will be thrown.
        InstantiationException - If the designated lock count manager class cannot be instantiated or does not have a parameterless constructor, an InstantiationException will be thrown.
        IllegalAccessException - If the designated lock count manager class or its constructor is not accessible, an IllegalAccessException will be thrown.
        InvocationTargetException - If the designated lock count manager throws an exception, this will be wrapped by an InvocationTargetException.
      • SessionLock

        public SessionLock​(Class<? extends LockCountManager<SessionToken>> lockCountManager,
                           boolean exclusive,
                           LockNotification<SessionToken> notification,
                           Logger logger)
                    throws NoSuchMethodException,
                           InstantiationException,
                           IllegalAccessException,
                           InvocationTargetException
        Creates either a new exclusive lock or a new read/write-lock and using the designated LockCountManager for counting locks as well as comparing objects. The designated notification will be used for notifying a successful lock or unlock while locking and unlocking, that is, the notification will be atomically with respect to locking/unlocking.
        Parameters:
        lockCountManager - The manager counting locks for objects. The provided class needs an accessible (public) constructor without parameters.
        exclusive - Whether to create an exclusive lock or a read/write-lock.
        notification - The notification to call directly but atomically after locking and unlocking. If this is null, no notification will be sent.
        logger - The logger for log messages.
        Throws:
        NoSuchMethodException - If the designated lock count manager has no constructor without parameters, a NouSuchMethodException will be thrown.
        InstantiationException - If the designated lock count manager class cannot be instantiated or does not have a parameterless constructor, an InstantiationException will be thrown.
        InvocationTargetException - If the designated lock count manager throws an exception, this will be wrapped by an InvocationTargetException.
        IllegalAccessException
      • SessionLock

        public SessionLock​(LockCountManager<SessionToken> lockCountManager,
                           Logger logger)
        Creates a new exclusive lock using the designated LockCountManager for counting locks as well as comparing objects.
        Parameters:
        lockCountManager - The manager counting locks for objects.
        logger - The logger for log messages.
      • SessionLock

        public SessionLock​(LockCountManager<SessionToken> lockCountManager,
                           LockNotification<SessionToken> notification,
                           Logger logger)
        Creates a new exclusive lock using the designated LockCountManager for counting locks as well as comparing objects. The designated notification will be used for notifying a successful lock or unlock while locking and unlocking, that is, the notification will be atomically with respect to locking/unlocking.
        Parameters:
        lockCountManager - The manager counting locks for objects.
        notification - The notification to call directly but atomically after locking and unlocking. If this is null, no notification will be sent.
        logger - The logger for log messages.
      • SessionLock

        public SessionLock​(LockCountManager<SessionToken> writeLockCountManager,
                           LockCountManager<SessionToken> readLockCountManager,
                           LockNotification<SessionToken> notification,
                           Logger logger)
        Creates either a new exclusive lock or a new read/write-lock and using the LockCountManager for counting locks as well as comparing objects. If there is no lock count manager for the read lock or it equals the lock count manager for the write lock, an exclusive lock will be created, otherwise this will be read/write-lock.
        The designated notification will be used for notifying a successful lock or unlock while locking and unlocking, that is, the notification will be atomically with respect to locking/unlocking.
        Parameters:
        writeLockCountManager - The manager counting write locks for objects.
        readLockCountManager - The manager counting read locks for objects. If this is null or equals the writeLockCountManager, an exclusive lock will be created.
        notification - The notification to call directly but atomically after locking and unlocking. If this is null, no notification will be sent.
        logger - The logger for log messages.
    • Method Detail

      • isAncestorOf

        protected static boolean isAncestorOf​(UUID ancestorId,
                                              int ancestorLevel,
                                              SessionToken session)
        Gets whether the designated ID belongs to a session token that is an ancestor of the designated session token.
        Parameters:
        ancestorId - The ID which to check for whether it belongs to an ancestor session token.
        ancestorLevel - The level of the ancestor.
        session - The session token to check for whether it is a descendant of the session token with the designated (child) ID.
        Returns:
        Whether the designated ID belongs to a session token that is an ancestor of the designated session token.
      • getNearestCommonAncestor

        protected static SessionToken getNearestCommonAncestor​(SessionToken first,
                                                               SessionToken second)
        Gets the session token that is the nearest common ancestor of the designated session tokens. This is the session token with the highest child level of which both session tokens are descendants of. If no such session token exists, null will be returned.
        Parameters:
        first - The first session token to find the nearest common ancestor.
        second - The second session token to find the nearest common ancestor.
        Returns:
        The nearest common ancestor session token of the designated session token or null if no such session token exists.