Class LockingObjectReference<T>

  • Type Parameters:
    T - The type of the contained object
    All Implemented Interfaces:
    AutoCloseable

    public final class LockingObjectReference<T>
    extends Object
    implements AutoCloseable
    An object reference wrapped by a ReentrantLock.

    This allows either directly invoking a function on the locked object or a more procedural lock/unlock pattern. This is fairer and safer than synchronizing on the object, since it can be interrupted (via Thread.interrupt(), e.g. on a task termination) and fairly chooses the longest-waiting thread to execute next.

    In either usage, threads will wait forever for the lock. Threads will emit an INFO level log message every 30 seconds indicating that they are still waiting. Additionally, threads will emit a DEBUG message upon acquiring the lock. Both messages will have a UUID indicating a unique lock name, for tracing purposes.

    If you are calling this from Beanshell, you can directly pass the name of a Beanshell method, along with the ‘this’ reference, such as:

    objReference.lockAndAct(this, “beanshellMethodName”);

    On lock acquisition, the specified Beanshell method in the ‘this’ scope will be invoked, passing the object as the only argument. The object will be automatically freed after the method completes.

    You can also use this class via a try/finally structure, such as:

    Object lockedObject = objReference.lock(); try { // do stuff here } finally { objReference.unlock(); }

    • Method Detail

      • close

        public void close()
        Unlocks the object, allowing use of this class in a try-with-resources context
        Specified by:
        close in interface AutoCloseable
      • lockAndAct

        public void lockAndAct​(bsh.This beanshell,
                               String methodName)
                        throws sailpoint.tools.GeneralException
        Locks the object, then passes it to the given Beanshell method.
        Parameters:
        beanshell - The Beanshell context (‘this’ in a script)
        methodName - The name of a Beanshell method in the current ‘this’ context or a parent
        Throws:
        sailpoint.tools.GeneralException - if any failures occur
      • lockObject

        public T lockObject()
                     throws sailpoint.tools.GeneralException
        Locks the object (waiting forever if necessary), then returns the object
        Returns:
        The object, now exclusive to this thread
        Throws:
        sailpoint.tools.GeneralException - if any failures occur
      • unlockObject

        public void unlockObject()
        Unlocks the object