Interface ContextManagerProvider
-
public interface ContextManagerProvider
A provider implementation supplied by the container, which creates and caches instances of
ContextManager
per class loader, which in turn create new instances ofManagedExecutor.Builder
andThreadContext.Builder
.The container must register its
ContextManagerProvider
implementation via theregister
method, or by providing an implementation via the standardServiceLoader
mechanism.ContextManagerProvider
implementations that wish to use theServiceLoader
registration mechanism must include a file of the following name and location in their jar:META-INF/services/org.eclipse.microprofile.context.spi.ContextManagerProvider
The content of the aforementioned file must be exactly one line, specifying the fully qualified name of a
ContextManagerProvider
implementation that is provided within the JAR file.If there is no manually registered
ContextManagerProvider
(viaregister(ContextManagerProvider)
), any call toinstance()
will look up anyContextManagerProvider
implementation via the aforementionedServiceLoader
mechanism. If there are more than one such implementation registered, theinstance()
method will throw an exception as documented
-
-
Field Summary
Fields Modifier and Type Field Description static java.util.concurrent.atomic.AtomicReference<ContextManagerProvider>
INSTANCE
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default ContextManager
getContextManager()
Gets aContextManager
for the current thread-contextClassLoader
.ContextManager
getContextManager(java.lang.ClassLoader classloader)
Gets aContextManager
for the givenClassLoader
.default ContextManager.Builder
getContextManagerBuilder()
Returns a newContextManager.Builder
to create newContextManager
instances.static ContextManagerProvider
instance()
Obtains theContextManagerProvider
instance that has been previously registered, or usesServiceLoader
to load and register aContextManagerProvider
from the current context class loader.static ContextManagerProviderRegistration
register(ContextManagerProvider provider)
Allows the container to register theContextManagerProvider
implementation.default void
registerContextManager(ContextManager manager, java.lang.ClassLoader classLoader)
Registers the givenContextManager
for the givenClassLoader
, so that further calls togetContextManager(ClassLoader)
for the sameClassLoader
will return this instance instead of creating a new one.default void
releaseContextManager(ContextManager manager)
Releases aContextManager
that was previously registered withregisterContextManager(ContextManager, ClassLoader)
.
-
-
-
Field Detail
-
INSTANCE
static final java.util.concurrent.atomic.AtomicReference<ContextManagerProvider> INSTANCE
-
-
Method Detail
-
instance
static ContextManagerProvider instance()
Obtains theContextManagerProvider
instance that has been previously registered, or usesServiceLoader
to load and register aContextManagerProvider
from the current context class loader.- Returns:
- the registered
ContextManagerProvider
instance. - Throws:
java.lang.IllegalStateException
- if there are no registeredContextManagerProvider
and we could not discover any viaServiceLoader
, or if there are more than oneServiceLoader
results.
-
register
static ContextManagerProviderRegistration register(ContextManagerProvider provider) throws java.lang.IllegalStateException
Allows the container to register theContextManagerProvider
implementation. At most one implementation can be registered at any given point in time. In order to register a different implementation, the container must first unregister its previous implementation.- Parameters:
provider
- the provider implementation to register.- Returns:
- registration instance that gives the caller control over unregistering.
- Throws:
java.lang.IllegalStateException
- if an implementation is already registered.
-
getContextManager
default ContextManager getContextManager()
Gets aContextManager
for the current thread-contextClassLoader
. This is equivalent to callinggetContextManager(Thread.currentThread().getContextClassLoader())
, which is the default implementation of this method.- Returns:
- a
ContextManager
for the current thread-contextClassLoader
. - Throws:
java.lang.IllegalStateException
- if more than oneThreadContextProvider
provides the same thread contexttype
- See Also:
getContextManager(ClassLoader)
-
getContextManager
ContextManager getContextManager(java.lang.ClassLoader classloader)
Gets aContextManager
for the givenClassLoader
. If there is already aContextManager
registered for the givenClassLoader
or the context manager provider uses a single fixed set ofThreadContextProvider
regardless of the class loader, the existing instance will be returned. If not, one will be created, either by provider-specific mechanisms ifContextManager.Builder
is not supported, or with aContextManager.Builder
using the specifiedClassLoader
(withContextManager.Builder.forClassLoader(ClassLoader)
) and withContextManager.Builder.addDiscoveredThreadContextProviders()
called in order to load allThreadContextProvider
discoverable from the givenClassLoader
. If created, the newContextManager
will then be registered for the givenClassLoader
withregisterContextManager(ContextManager, ClassLoader)
.- Parameters:
classloader
- the class loader for which to obtain the context manager.- Returns:
- a
ContextManager
for the givenClassLoader
. - Throws:
java.lang.IllegalStateException
- if more than oneThreadContextProvider
provides the same thread contexttype
- See Also:
ContextManager.Builder.addDiscoveredThreadContextProviders()
,ContextManager.Builder.build()
,registerContextManager(ContextManager, ClassLoader)
-
getContextManagerBuilder
default ContextManager.Builder getContextManagerBuilder()
Returns a newContextManager.Builder
to create newContextManager
instances. Watch out that instances created this way will not be automatically registered here, so you need to callregisterContextManager(ContextManager, ClassLoader)
yourself if you need to.- Returns:
- a new
ContextManager.Builder
- Throws:
java.lang.UnsupportedOperationException
- if theContextManagerProvider
always uses the same set ofThreadContextProvider
or is inseparable from the container.
-
registerContextManager
default void registerContextManager(ContextManager manager, java.lang.ClassLoader classLoader)
Registers the givenContextManager
for the givenClassLoader
, so that further calls togetContextManager(ClassLoader)
for the sameClassLoader
will return this instance instead of creating a new one.- Parameters:
manager
- TheContextManager
to registerclassLoader
- TheClassLoader
to register it for- Throws:
java.lang.UnsupportedOperationException
- if theContextManagerProvider
always uses the same set ofThreadContextProvider
or is inseparable from the container.- See Also:
getContextManager(ClassLoader)
,releaseContextManager(ContextManager)
-
releaseContextManager
default void releaseContextManager(ContextManager manager)
Releases aContextManager
that was previously registered withregisterContextManager(ContextManager, ClassLoader)
.- Parameters:
manager
- TheContextManager
to release- Throws:
java.lang.UnsupportedOperationException
- if theContextManagerProvider
always uses the same set ofThreadContextProvider
or is inseparable from the container.- See Also:
registerContextManager(ContextManager, ClassLoader)
-
-