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 aContextual
type.- 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 C
get()
Get the currently active context, possibly examining the per-thread or global defaults.C
getClassLoaderDefault(java.lang.ClassLoader classLoader)
Get the class loader default instance.C
getGlobalDefault()
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.C
getThreadDefault()
Get the per-thread default context instance.void
setClassLoaderDefault(java.lang.ClassLoader classLoader, C classLoaderDefault)
Set the per-class loader default instance supplier.void
setClassLoaderDefaultSupplier(java.lang.ClassLoader classLoader, java.util.function.Supplier<C> supplier)
Set the per-class loader default instance supplier.void
setGlobalDefault(C globalDefault)
Set the global default instance.void
setGlobalDefaultSupplier(java.util.function.Supplier<C> supplier)
Set the global default instance supplier.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.void
setThreadDefault(C threadDefault)
Set the per-thread default instance.void
setThreadDefaultSupplier(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 benull
or empty)
-
-
Method Detail
-
getGlobalDefault
public C getGlobalDefault()
Get the global default context instance. Note that the global default is determined by way of aSupplier
so the returned value may vary from call to call, depending on the policy of thatSupplier
.- Returns:
- the global default, or
null
if 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, ornull
to 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:
true
if the supplier was set,false
if 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, ornull
to 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 aSupplier
so 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
null
if 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, ornull
to 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, ornull
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 aSupplier
so the returned value may vary from call to call, depending on the policy of thatSupplier
.- Returns:
- the per-thread default, or
null
if 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, ornull
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 togetThreadDefault()
, replacing any previous instance or supplier that was set.- Parameters:
threadDefault
- the per-thread default value, ornull
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 interfacejava.util.function.Supplier<C extends Contextual<C>>
- Returns:
- the current context, or
null
if 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
-
-