public interface Context
A context resource is an object instance identified by a String key.
Resources are organized in ContextScopes, each of them characterized
by a resource data source and a resource retrieval strategy. A scope can be
manageable, i.e. supporting resource addition and removal, or not.
Context scope can be registered either by using standard Java extension
service loading using ContextScope class name files under
META-INF/services, or by direct registration using
ContextManager.registerScope(ClassLoader, ContextScope).
When asking for a context resource using resource(String, Class) and
similar methods, all registered context scopes are invoked, in the order
defined by ContextScope.getOrder() precedence value. The first not
null resource instance obtained from a scope is the one returned to caller,
if any.
Every Context resource set is bound to a ClassLoader. If not
specified, the default classloader is obtained from
ClassUtils.getDefaultClassLoader().
CLASSLOADER_SCOPE_NAME,
registering and providing resources as singleton instances for the
reference ClassLoader. This scope is registered with a low precedence
order;THREAD_SCOPE_NAME, using current
Thread local data set to register and provide context resources. This scope
is registered with a high precedence order.| Modifier and Type | Field and Description |
|---|---|
static String |
CLASSLOADER_SCOPE_NAME
Default classloader-bound scope name
|
static String |
THREAD_SCOPE_NAME
Default thread-bound scope name
|
| Modifier and Type | Method and Description |
|---|---|
default Optional<ContextScope> |
classLoaderScope()
Shortcut method to obtain the ClassLoader-bound context scope, i.e.
|
default Optional<ContextScope> |
classLoaderScope(ClassLoader classLoader)
Shortcut method to obtain the ClassLoader-bound context scope, i.e.
|
default <V> V |
executeThreadBound(String resourceKey,
Object resource,
Callable<V> operation)
|
default void |
executeThreadBound(String resourceKey,
Object resource,
Runnable operation)
|
static Context |
get()
Obtain the default
Context implementation instance. |
default <T> Optional<T> |
resource(Class<T> resourceType)
Shortcut method to obtain a
Context resource using the
resourceType class name as resource key. |
default <T> Optional<T> |
resource(Class<T> resourceType,
ClassLoader classLoader)
Shortcut method to obtain a
Context resource using the
resourceType class name as resource key and given
classLoader. |
default <T> Optional<T> |
resource(String resourceKey,
Class<T> resourceType)
Lookup a context resource of
resourceType class using given
resourceKey and the default ClassLoader. |
<T> Optional<T> |
resource(String resourceKey,
Class<T> resourceType,
ClassLoader classLoader)
Lookup a context resource of
resourceType class using given
resourceKey. |
default Optional<ContextScope> |
scope(String name)
|
Optional<ContextScope> |
scope(String name,
ClassLoader classLoader)
|
default Optional<ContextScope> |
threadScope()
Shortcut method to obtain the thread-bound context scope, i.e.
|
default Optional<ContextScope> |
threadScope(ClassLoader classLoader)
Shortcut method to obtain the thread-bound context scope, i.e.
|
static final String THREAD_SCOPE_NAME
static final String CLASSLOADER_SCOPE_NAME
<T> Optional<T> resource(String resourceKey, Class<T> resourceType, ClassLoader classLoader)
resourceType class using given
resourceKey.
All registered context scopes are invoked, in the order defined by
ContextScope.getOrder() precedence value. The first not
null resource instance obtained from a scope is the one returned
to caller, if any.
T - Resource typeresourceKey - Resource key (not null)resourceType - Required resource type (not null)classLoader - ClassLoader to use. If null, the default
ClassLoader is used.TypeMismatchException - If a context resource with given key was found
but it is not of required typedefault <T> Optional<T> resource(String resourceKey, Class<T> resourceType)
resourceType class using given
resourceKey and the default ClassLoader.
All registered context scopes are invoked, in the order defined by
ContextScope.getOrder() precedence value. The first not
null resource instance obtained from a scope is the one returned
to caller, if any.
T - Resource typeresourceKey - Resource key (not null)resourceType - Required resource type (not null)TypeMismatchException - If a context resource with given key was found
but it is not of required typedefault <T> Optional<T> resource(Class<T> resourceType)
Context resource using the
resourceType class name as resource key.T - Resource typeresourceType - Resource type (not null)default <T> Optional<T> resource(Class<T> resourceType, ClassLoader classLoader)
Context resource using the
resourceType class name as resource key and given
classLoader.T - Resource typeresourceType - Resource type (not null)classLoader - ClassLoader to use. If null, the default
ClassLoader is used.Optional<ContextScope> scope(String name, ClassLoader classLoader)
name - Scope name (not null)classLoader - ClassLoader, if null, the default ClassLoader
is usedContextScope with given name, or an empty
Optional if a scope with such name is not registered in contextdefault Optional<ContextScope> scope(String name)
name - Scope name (not null)ContextScope with given name, or an empty
Optional if a scope with such name is not registered in contextdefault Optional<ContextScope> classLoaderScope(ClassLoader classLoader)
ContextScope named CLASSLOADER_SCOPE_NAME.classLoader - ClassLoader, if null, the default ClassLoader
is useddefault Optional<ContextScope> classLoaderScope()
ContextScope named CLASSLOADER_SCOPE_NAME, using the
default ClassLoader.default Optional<ContextScope> threadScope(ClassLoader classLoader)
ContextScope named THREAD_SCOPE_NAME.classLoader - ClassLoader, if null, the default ClassLoader
is useddefault Optional<ContextScope> threadScope()
ContextScope named THREAD_SCOPE_NAME, using the default
ClassLoader.default void executeThreadBound(String resourceKey, Object resource, Runnable operation) throws RuntimeException
Runnable operation, binding given
Context resource key and value to current Thread, and removing the
binding after operation execution.
The THREAD_SCOPE_NAME context scope is used.
resourceKey - Context resource key (not null)resource - Context resource value to bind to current Thread (not
null)operation - Operation to execute (not null)RuntimeException - Exception during operation executiondefault <V> V executeThreadBound(String resourceKey, Object resource, Callable<V> operation) throws RuntimeException
Callable operation, binding given
Context resource key and value to current Thread, and removing the
binding after operation execution.
The THREAD_SCOPE_NAME context scope is used.
V - Operation result typeresourceKey - Context resource key (not null)resource - Context resource value to bind to current Thread (not
null)operation - Operation to executeRuntimeException - Exception during operation executionCopyright © 2020 The Holon Platform. All rights reserved.