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 of
ManagedExecutor.Builder
and
ThreadContext.Builder
.
The container must register its ContextManagerProvider
implementation via the register
method, or by providing
an implementation via the standard ServiceLoader
mechanism.
ContextManagerProvider
implementations that wish to use
the ServiceLoader
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
(via
register(ContextManagerProvider)
), any call to instance()
will
look up any ContextManagerProvider
implementation via the aforementioned
ServiceLoader
mechanism. If there are more than one such implementation
registered, the instance()
method will throw an exception as documented
-
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptiondefault ContextManager
Gets aContextManager
for the current thread-contextClassLoader
.getContextManager
(ClassLoader classloader) Gets aContextManager
for the givenClassLoader
.default ContextManager.Builder
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.register
(ContextManagerProvider provider) Allows the container to register theContextManagerProvider
implementation.default void
registerContextManager
(ContextManager manager, 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 Details
-
INSTANCE
-
-
Method Details
-
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:
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 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:
IllegalStateException
- if an implementation is already registered.
-
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:
IllegalStateException
- if more than oneThreadContextProvider
provides the same thread contexttype
- See Also:
-
getContextManager
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:
IllegalStateException
- if more than oneThreadContextProvider
provides the same thread contexttype
- See Also:
-
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:
UnsupportedOperationException
- if theContextManagerProvider
always uses the same set ofThreadContextProvider
or is inseparable from the container.
-
registerContextManager
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:
UnsupportedOperationException
- if theContextManagerProvider
always uses the same set ofThreadContextProvider
or is inseparable from the container.- See Also:
-
releaseContextManager
Releases aContextManager
that was previously registered withregisterContextManager(ContextManager, ClassLoader)
.- Parameters:
manager
- TheContextManager
to release- Throws:
UnsupportedOperationException
- if theContextManagerProvider
always uses the same set ofThreadContextProvider
or is inseparable from the container.- See Also:
-