public final class ServletScopes
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
private static class |
ServletScopes.Context |
(package private) static class |
ServletScopes.NullObject
A sentinel attribute value representing null.
|
private static class |
ServletScopes.RequestScope |
private static class |
ServletScopes.SessionScope |
Modifier and Type | Field and Description |
---|---|
static Scope |
REQUEST
HTTP servlet request scope.
|
private static java.lang.ThreadLocal<ServletScopes.Context> |
requestScopeContext
A threadlocal scope map for non-http request scopes.
|
static Scope |
SESSION
HTTP session scope.
|
Modifier | Constructor and Description |
---|---|
private |
ServletScopes() |
Modifier and Type | Method and Description |
---|---|
private static void |
checkScopingState(boolean condition,
java.lang.String msg) |
static <T> java.util.concurrent.Callable<T> |
continueRequest(java.util.concurrent.Callable<T> callable,
java.util.Map<Key<?>,java.lang.Object> seedMap)
Deprecated.
You probably want to use
transferRequest instead |
private static RequestScoper |
continueRequest(java.util.Map<Key<?>,java.lang.Object> seedMap) |
static boolean |
isRequestScoped(Binding<?> binding)
Returns true if
binding is request-scoped. |
static <T> java.util.concurrent.Callable<T> |
scopeRequest(java.util.concurrent.Callable<T> callable,
java.util.Map<Key<?>,java.lang.Object> seedMap)
Scopes the given callable inside a request scope.
|
static RequestScoper |
scopeRequest(java.util.Map<Key<?>,java.lang.Object> seedMap)
Returns an object that will apply request scope to a block of code.
|
private static RequestScoper |
transferHttpRequest() |
private static RequestScoper |
transferNonHttpRequest() |
static RequestScoper |
transferRequest()
Returns an object that "transfers" the request to another thread.
|
static <T> java.util.concurrent.Callable<T> |
transferRequest(java.util.concurrent.Callable<T> callable)
Wraps the given callable in a contextual callable that "transfers" the request to another
thread.
|
private static java.lang.Object |
validateAndCanonicalizeValue(Key<?> key,
java.lang.Object object)
Validates the key and object, ensuring the value matches the key type, and canonicalizing null
objects to the null sentinel.
|
private static <T> java.util.concurrent.Callable<T> |
wrap(java.util.concurrent.Callable<T> delegate,
RequestScoper requestScoper) |
private static final java.lang.ThreadLocal<ServletScopes.Context> requestScopeContext
REQUEST
scope falls back to
this scope map if no http request is available, and requires scopeRequest(java.util.concurrent.Callable<T>, java.util.Map<com.google.inject.Key<?>, java.lang.Object>)
to be called
as an alternative.public static final Scope REQUEST
public static final Scope SESSION
@Deprecated public static <T> java.util.concurrent.Callable<T> continueRequest(java.util.concurrent.Callable<T> callable, java.util.Map<Key<?>,java.lang.Object> seedMap)
transferRequest
insteadThere are some limitations:
The returned callable will throw a ScopingException
when called if the HTTP request
scope is still active on the current thread.
callable
- code to be executed in another thread, which depends on the request scope.seedMap
- the initial set of scoped instances for Guice to seed the request scope with. To
seed a key with null, use null
as the value.OutOfScopeException
- if this method is called from a non-request thread, or if the
request has completed.private static RequestScoper continueRequest(java.util.Map<Key<?>,java.lang.Object> seedMap)
public static <T> java.util.concurrent.Callable<T> transferRequest(java.util.concurrent.Callable<T> callable)
As opposed to continueRequest(java.util.concurrent.Callable<T>, java.util.Map<com.google.inject.Key<?>, java.lang.Object>)
, this method propagates all existing scoped objects.
The primary use case is in server implementations where you can detach the request processing
thread while waiting for data, and reattach to a different thread to finish processing at a
later time.
Because request-scoped objects are not typically thread-safe, the callable returned by this method must not be run on a different thread until the current request scope has terminated. The returned callable will block until the current thread has released the request scope.
callable
- code to be executed in another thread, which depends on the request scope.OutOfScopeException
- if this method is called from a non-request thread, or if the
request has completed.public static RequestScoper transferRequest()
open()
activates the transferred the scope, including propagating any objects scoped at that time.
As opposed to continueRequest(java.util.concurrent.Callable<T>, java.util.Map<com.google.inject.Key<?>, java.lang.Object>)
, this method propagates all existing scoped objects.
The primary use case is in server implementations where you can detach the request processing
thread while waiting for data, and reattach to a different thread to finish processing at a
later time.
Because request-scoped objects are not typically thread-safe, it is important to avoid applying the same request scope concurrently. The returned Scoper will block on open until the current thread has released the request scope.
OutOfScopeException
- if this method is called from a non-request thread, or if the
request has completed.private static RequestScoper transferHttpRequest()
private static RequestScoper transferNonHttpRequest()
public static boolean isRequestScoped(Binding<?> binding)
binding
is request-scoped. If the binding is a linked key binding
and belongs to an injector (i. e. it
was retrieved via Injector.getBinding()
), then this method will
also return true if the target binding is request-scoped.public static <T> java.util.concurrent.Callable<T> scopeRequest(java.util.concurrent.Callable<T> callable, java.util.Map<Key<?>,java.lang.Object> seedMap)
The returned callable will throw a ScopingException
when called if there is a
request scope already active on the current thread.
callable
- code to be executed which depends on the request scope. Typically in another
thread, but not necessarily so.seedMap
- the initial set of scoped instances for Guice to seed the request scope with. To
seed a key with null, use null
as the value.seedMap
as scoped keys.public static RequestScoper scopeRequest(java.util.Map<Key<?>,java.lang.Object> seedMap)
The returned object will throw a ScopingException
when opened if there is a request
scope already active on the current thread.
seedMap
- the initial set of scoped instances for Guice to seed the request scope with. To
seed a key with null, use null
as the value.private static java.lang.Object validateAndCanonicalizeValue(Key<?> key, java.lang.Object object)
private static void checkScopingState(boolean condition, java.lang.String msg)
private static <T> java.util.concurrent.Callable<T> wrap(java.util.concurrent.Callable<T> delegate, RequestScoper requestScoper)