Class ContextManager<C extends Contextual<C>>

java.lang.Object
org.wildfly.common.context.ContextManager<C>
Type Parameters:
C - the public type of the contextual object
All Implemented Interfaces:
Supplier<C>

public final class ContextManager<C extends Contextual<C>> extends Object implements Supplier<C>
A context manager for a Contextual type.
Author:
David M. Lloyd
  • Constructor Details

    • ContextManager

      public ContextManager(Class<C> type)
      Construct a new instance, with a name matching the class name of the given type.
      Parameters:
      type - the type class of the context object (must not be null)
    • ContextManager

      public ContextManager(Class<C> type, String name)
      Construct a new instance.
      Parameters:
      type - the type class of the context object (must not be null)
      name - the name to use for permission checks (must not be null or empty)
  • Method Details

    • getGlobalDefault

      public C getGlobalDefault()
      Get the global default context instance. Note that the global default is determined by way of a Supplier so the returned value may vary from call to call, depending on the policy of that Supplier.
      Returns:
      the global default, or null if none is installed or available
    • getGlobalDefaultSupplier

      public Supplier<C> getGlobalDefaultSupplier()
      Get the global default supplier instance.
      Returns:
      the global default supplier, or null if none is installed or available
    • setGlobalDefaultSupplier

      public void setGlobalDefaultSupplier(Supplier<C> supplier)
      Set the global default instance supplier. The supplier, if one is given, should have a reasonable policy such that callers of getGlobalDefault() will obtain results consistent with a general expectation of stability.
      Parameters:
      supplier - the supplier, or null to remove the global default
    • setGlobalDefaultSupplierIfNotSet

      public boolean setGlobalDefaultSupplierIfNotSet(Supplier<Supplier<C>> supplierSupplier)
      Set the global default instance supplier, but only if it was not already set. If no supplier is set, the given supplier supplier is queried to get the new value to set.
      Parameters:
      supplierSupplier - the supplier supplier (must not be null)
      Returns:
      true if the supplier was set, false if it was already set to something else
      See Also:
    • setGlobalDefault

      public void setGlobalDefault(C globalDefault)
      Set the global default instance. This instance will be returned from all subsequent calls to getGlobalDefault(), replacing any previous instance or supplier that was set.
      Parameters:
      globalDefault - the global default value, or null to remove the global default
    • getClassLoaderDefault

      public C getClassLoaderDefault(ClassLoader classLoader)
      Get the class loader default instance. Note that the class loader default is determined by way of a Supplier so the returned value may vary from call to call, depending on the policy of that Supplier.
      Parameters:
      classLoader - the class loader
      Returns:
      the global default, or null if none is installed or available
    • getClassLoaderDefaultSupplier

      public Supplier<C> getClassLoaderDefaultSupplier(ClassLoader classLoader)
      Get the class loader default supplier.\
      Parameters:
      classLoader - the class loader
      Returns:
      the global default, or null if none is installed or available
    • setClassLoaderDefaultSupplier

      public void setClassLoaderDefaultSupplier(ClassLoader classLoader, Supplier<C> supplier)
      Set the per-class loader default instance supplier. The supplier, if one is given, should have a reasonable policy such that callers of getClassLoaderDefault(ClassLoader) will obtain results consistent with a general expectation of stability.
      Parameters:
      classLoader - the class loader (must not be null)
      supplier - the supplier, or null to remove the default for this class loader
    • setClassLoaderDefault

      public void setClassLoaderDefault(ClassLoader classLoader, C classLoaderDefault)
      Set the per-class loader default instance supplier. The supplier, if one is given, should have a reasonable policy such that callers of getClassLoaderDefault(ClassLoader) will obtain results consistent with a general expectation of stability.
      Parameters:
      classLoader - the class loader (must not be null)
      classLoaderDefault - the class loader default value, or null to remove the default
    • getThreadDefault

      public C getThreadDefault()
      Get the per-thread default context instance. Note that the per-thread default is determined by way of a Supplier so the returned value may vary from call to call, depending on the policy of that Supplier.
      Returns:
      the per-thread default, or null if none is installed or available
    • getThreadDefaultSupplier

      public Supplier<C> getThreadDefaultSupplier()
      Get the per-thread default context instance.
      Returns:
      the per-thread default supplier, or null if none is installed or available
    • setThreadDefaultSupplier

      public void setThreadDefaultSupplier(Supplier<C> supplier)
      Set the per-thread default instance supplier. The supplier, if one is given, should have a reasonable policy such that callers of getThreadDefault() will obtain results consistent with a general expectation of stability.
      Parameters:
      supplier - the supplier, or null to remove the per-thread default
    • setThreadDefault

      public void setThreadDefault(C threadDefault)
      Set the per-thread default instance. This instance will be returned from all subsequent calls to getThreadDefault(), replacing any previous instance or supplier that was set.
      Parameters:
      threadDefault - the per-thread default value, or null to remove the per-thread default
    • get

      public C get()
      Get the currently active context, possibly examining the per-thread or global defaults.
      Specified by:
      get in interface Supplier<C extends Contextual<C>>
      Returns:
      the current context, or null if none is active
    • getPrivilegedSupplier

      public Supplier<C> getPrivilegedSupplier()
      Get a privileged supplier for this context manager which returns the currently active context without a permission check.
      Returns:
      the privileged supplier