Class SimpleResourceHolder


  • public final class SimpleResourceHolder
    extends java.lang.Object
    Central helper that manages resources per thread to be used by resource management code.

    bind(Object, Object) supports one resource per key without overwriting, that is, a resource needs to be removed before a new one can be set for the same key. But see push(Object, Object) and pop(Object).

    Resource management code should check for thread-bound resources via has(Object).

    This helper isn't designed for transaction synchronization cases. Use TransactionSynchronizationManager and ResourceHolder instead.

    Since:
    1.3
    Author:
    Artem Bilan, Gary Russell
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void bind​(java.lang.Object key, java.lang.Object value)
      Bind the given resource for the given key to the current thread.
      static void clear()
      Clear resources for the current thread.
      static java.lang.Object get​(java.lang.Object key)
      Retrieve a resource for the given key that is bound to the current thread.
      static java.util.Map<java.lang.Object,​java.lang.Object> getResources()
      Return all resources that are bound to the current thread.
      static boolean has​(java.lang.Object key)
      Check if there is a resource for the given key bound to the current thread.
      static java.lang.Object pop​(java.lang.Object key)
      Unbind the current value and bind the head of the stack if present.
      static void push​(java.lang.Object key, java.lang.Object value)
      Set the value for this key and push any existing value onto a stack.
      static java.lang.Object unbind​(java.lang.Object key)
      Unbind a resource for the given key from the current thread.
      static java.lang.Object unbindIfPossible​(java.lang.Object key)
      Unbind a resource for the given key from the current thread.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getResources

        public static java.util.Map<java.lang.Object,​java.lang.Object> getResources()
        Return all resources that are bound to the current thread.

        Mainly for debugging purposes. Resource managers should always invoke hasResource for a specific resource key that they are interested in.

        Returns:
        a Map with resource keys (usually the resource factory) and resource values (usually the active resource object), or an empty Map if there are currently no resources bound
        See Also:
        has(java.lang.Object)
      • has

        public static boolean has​(java.lang.Object key)
        Check if there is a resource for the given key bound to the current thread.
        Parameters:
        key - the key to check (usually the resource factory)
        Returns:
        if there is a value bound to the current thread
      • get

        @Nullable
        public static java.lang.Object get​(java.lang.Object key)
        Retrieve a resource for the given key that is bound to the current thread.
        Parameters:
        key - the key to check (usually the resource factory)
        Returns:
        a value bound to the current thread (usually the active resource object), or null if none
      • bind

        public static void bind​(java.lang.Object key,
                                java.lang.Object value)
        Bind the given resource for the given key to the current thread.
        Parameters:
        key - the key to bind the value to (usually the resource factory)
        value - the value to bind (usually the active resource object)
        Throws:
        java.lang.IllegalStateException - if there is already a value bound to the thread
      • push

        public static void push​(java.lang.Object key,
                                java.lang.Object value)
        Set the value for this key and push any existing value onto a stack.
        Parameters:
        key - the key.
        value - the value.
        Since:
        2.1.11
      • pop

        @Nullable
        public static java.lang.Object pop​(java.lang.Object key)
        Unbind the current value and bind the head of the stack if present.
        Parameters:
        key - the key.
        Returns:
        the popped value.
        Since:
        2.1.11
      • unbind

        public static java.lang.Object unbind​(java.lang.Object key)
                                       throws java.lang.IllegalStateException
        Unbind a resource for the given key from the current thread.
        Parameters:
        key - the key to unbind (usually the resource factory)
        Returns:
        the previously bound value (usually the active resource object)
        Throws:
        java.lang.IllegalStateException - if there is no value bound to the thread
      • unbindIfPossible

        @Nullable
        public static java.lang.Object unbindIfPossible​(java.lang.Object key)
        Unbind a resource for the given key from the current thread.
        Parameters:
        key - the key to unbind (usually the resource factory)
        Returns:
        the previously bound value, or null if none bound
      • clear

        public static void clear()
        Clear resources for the current thread.