Class VersionedThreadLocal<T>

  • Type Parameters:
    T - the type of the value held in this ThreadLocal
    All Implemented Interfaces:
    Supplier<T>

    public class VersionedThreadLocal<T>
    extends Object
    implements Supplier<T>
    A thread-local container that is associated with the plugin cache version.

    If a new plugin is installed, the version increments, and the entire ThreadLocal will be replaced. This means that running processes relying on plugin-provided classes can retrieve new versions of those objects.

    Your Supplier code should always assume that the plugin cache has been refreshed since the last invocation and never cache plugin objects.

    The object type T, of course, cannot be the actual implementation in the plugin classloader, as that class will have become invalid when the plugin cache was reset. The type T should be either a JDK class or a custom interface implemented at the webapp layer.

    • Constructor Detail

      • VersionedThreadLocal

        public VersionedThreadLocal()
        Creates a new VersionedThreadLocal with the default initial value of null.
    • Method Detail

      • withInitial

        public static <U> VersionedThreadLocal<U> withInitial​(Supplier<? extends U> supplier)
        Creates a new VersionedThreadLocal with the given initial value.
        Type Parameters:
        U - the type of the value held in this ThreadLocal
        Parameters:
        supplier - the initial value supplier
        Returns:
        a new VersionedThreadLocal
        See Also:
        ThreadLocal.withInitial(Supplier)
      • compute

        public T compute​(Supplier<? extends T> supplier)
        Computes the value for this ThreadLocal using the given supplier.

        If the value is already set, it will be returned. Otherwise, the supplier will be called to compute the value and set it in this ThreadLocal.

        Parameters:
        supplier - the supplier to compute the value
        Returns:
        the current or computed value
      • get

        public T get()
        Returns the current value in this ThreadLocal.

        If the version has changed, a new ThreadLocal will be created and the value returned from that.

        Specified by:
        get in interface Supplier<T>
        Returns:
        the current value in this ThreadLocal
        See Also:
        ThreadLocal.get()
      • set

        public void set​(T value)
        Sets the value in this ThreadLocal.

        If the version has changed, a new ThreadLocal will be created and the value set in that.

        Parameters:
        value - the new value to set
        See Also:
        ThreadLocal.set(Object)