public interface ContextScope
Context
scope to hold, manage and access context resources. Every resource is identified by a
String
key.
Each scope is identified by a name which must be unique among all registered context scopes and may implement a specific strategy to store and provide resources.
Every concrete scope must provide the get(String, Class)
operation. Resource management operations
(put/remove) are optional and may not be available, throwing an UnsupportedOperationException
.
A ContextScope must be registered in ContextManager
to be accessibile from Context
façade and join
generic context resource retrieval chain. Scope registration can be performed using default Java extension through
ServiceLoader
or directly using ContextManager.registerScope(ClassLoader, ContextScope)
.
The getOrder()
method can be used to assign a order to the scope within the scopes chain for context
resource resolution. Lower order values mean highest precedence.
Modifier and Type | Method and Description |
---|---|
<T> Optional<T> |
get(String resourceKey,
Class<T> resourceType)
Try to obtain the resource identified by given
resourceKey and with expected
resourceType . |
String |
getName()
Gets the scope name.
|
int |
getOrder()
Gets the scope order.
|
<T> Optional<T> |
put(String resourceKey,
T value)
Stores a resource reference identified by given
resourceKey . |
<T> Optional<T> |
putIfAbsent(String resourceKey,
T value)
Stores a resource reference identified by given
resourceKey , only if there is not a resource
instance already bound to given key. |
boolean |
remove(String resourceKey)
Removes the resource reference bound to the given
resourceKey from scope, if any. |
String getName()
int getOrder()
<T> Optional<T> get(String resourceKey, Class<T> resourceType) throws TypeMismatchException
resourceKey
and with expected
resourceType
.T
- Resource typeresourceKey
- Resource key (not null)resourceType
- Expected resource type (not null)TypeMismatchException
- If a resource is available but the expected type is not compatible with the
resource type<T> Optional<T> put(String resourceKey, T value) throws UnsupportedOperationException
resourceKey
.T
- Resource instance typeresourceKey
- Resource key (not null)value
- Resource instance. If null
, if a previous resource was bound to given resource key, it
will be removed from scope.UnsupportedOperationException
- If the concrete scope implementation does not support this operation<T> Optional<T> putIfAbsent(String resourceKey, T value) throws UnsupportedOperationException
resourceKey
, only if there is not a resource
instance already bound to given key.T
- Resource instance typeresourceKey
- Resource key (not null)value
- Resource instance, if null
, this method should do nothing.value
has been associated to the
resource key.UnsupportedOperationException
- If the concrete scope implementation does not support this operationboolean remove(String resourceKey) throws UnsupportedOperationException
resourceKey
from scope, if any.resourceKey
- Resource key (not null)true
if a resource instance was bound to the given key and it's been removed,
false
otherwise.UnsupportedOperationException
- If the concrete scope implementation does not support this operationCopyright © 2019 The Holon Platform. All rights reserved.