Package org.wildfly.common.context
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:
java.util.function.Supplier<C>
public final class ContextManager<C extends Contextual<C>> extends java.lang.Object implements java.util.function.Supplier<C>A context manager for aContextualtype.- Author:
- David M. Lloyd
-
-
Constructor Summary
Constructors Constructor Description ContextManager(java.lang.Class<C> type)Construct a new instance, with a name matching the class name of the giventype.ContextManager(java.lang.Class<C> type, java.lang.String name)Construct a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Cget()Get the currently active context, possibly examining the per-thread or global defaults.CgetClassLoaderDefault(java.lang.ClassLoader classLoader)Get the class loader default instance.CgetGlobalDefault()Get the global default context instance.java.util.function.Supplier<C>getPrivilegedSupplier()Get a privileged supplier for this context manager which returns the currently active context without a permission check.CgetThreadDefault()Get the per-thread default context instance.voidsetClassLoaderDefault(java.lang.ClassLoader classLoader, C classLoaderDefault)Set the per-class loader default instance supplier.voidsetClassLoaderDefaultSupplier(java.lang.ClassLoader classLoader, java.util.function.Supplier<C> supplier)Set the per-class loader default instance supplier.voidsetGlobalDefault(C globalDefault)Set the global default instance.voidsetGlobalDefaultSupplier(java.util.function.Supplier<C> supplier)Set the global default instance supplier.booleansetGlobalDefaultSupplierIfNotSet(java.util.function.Supplier<java.util.function.Supplier<C>> supplierSupplier)Set the global default instance supplier, but only if it was not already set.voidsetThreadDefault(C threadDefault)Set the per-thread default instance.voidsetThreadDefaultSupplier(java.util.function.Supplier<C> supplier)Set the per-thread default instance supplier.
-
-
-
Constructor Detail
-
ContextManager
public ContextManager(java.lang.Class<C> type)
Construct a new instance, with a name matching the class name of the giventype.- Parameters:
type- the type class of the context object (must not benull)
-
ContextManager
public ContextManager(java.lang.Class<C> type, java.lang.String name)
Construct a new instance.- Parameters:
type- the type class of the context object (must not benull)name- the name to use for permission checks (must not benullor empty)
-
-
Method Detail
-
getGlobalDefault
public C getGlobalDefault()
Get the global default context instance. Note that the global default is determined by way of aSupplierso the returned value may vary from call to call, depending on the policy of thatSupplier.- Returns:
- the global default, or
nullif none is installed or available
-
setGlobalDefaultSupplier
public void setGlobalDefaultSupplier(java.util.function.Supplier<C> supplier)
Set the global default instance supplier. The supplier, if one is given, should have a reasonable policy such that callers ofgetGlobalDefault()will obtain results consistent with a general expectation of stability.- Parameters:
supplier- the supplier, ornullto remove the global default
-
setGlobalDefaultSupplierIfNotSet
public boolean setGlobalDefaultSupplierIfNotSet(java.util.function.Supplier<java.util.function.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 benull)- Returns:
trueif the supplier was set,falseif it was already set to something else- See Also:
setGlobalDefaultSupplier(Supplier)
-
setGlobalDefault
public void setGlobalDefault(C globalDefault)
Set the global default instance. This instance will be returned from all subsequent calls togetGlobalDefault(), replacing any previous instance or supplier that was set.- Parameters:
globalDefault- the global default value, ornullto remove the global default
-
getClassLoaderDefault
public C getClassLoaderDefault(java.lang.ClassLoader classLoader)
Get the class loader default instance. Note that the class loader default is determined by way of aSupplierso the returned value may vary from call to call, depending on the policy of thatSupplier.- Parameters:
classLoader- the class loader- Returns:
- the global default, or
nullif none is installed or available
-
setClassLoaderDefaultSupplier
public void setClassLoaderDefaultSupplier(java.lang.ClassLoader classLoader, java.util.function.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 ofgetClassLoaderDefault(ClassLoader)will obtain results consistent with a general expectation of stability.- Parameters:
classLoader- the class loader (must not benull)supplier- the supplier, ornullto remove the default for this class loader
-
setClassLoaderDefault
public void setClassLoaderDefault(java.lang.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 ofgetClassLoaderDefault(ClassLoader)will obtain results consistent with a general expectation of stability.- Parameters:
classLoader- the class loader (must not benull)classLoaderDefault- the class loader default value, ornullto 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 aSupplierso the returned value may vary from call to call, depending on the policy of thatSupplier.- Returns:
- the per-thread default, or
nullif none is installed or available
-
setThreadDefaultSupplier
public void setThreadDefaultSupplier(java.util.function.Supplier<C> supplier)
Set the per-thread default instance supplier. The supplier, if one is given, should have a reasonable policy such that callers ofgetThreadDefault()will obtain results consistent with a general expectation of stability.- Parameters:
supplier- the supplier, ornullto 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 togetThreadDefault(), replacing any previous instance or supplier that was set.- Parameters:
threadDefault- the per-thread default value, ornullto remove the per-thread default
-
get
public C get()
Get the currently active context, possibly examining the per-thread or global defaults.- Specified by:
getin interfacejava.util.function.Supplier<C extends Contextual<C>>- Returns:
- the current context, or
nullif none is active
-
getPrivilegedSupplier
public java.util.function.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
-
-