Update the ThreadLocal based scopes to use RequestContext.

Bound the ThreadLocalRequestContext module, which makes the CurrentUser
available in the Global module. Removed any binding in the scopes that
provided the CurrentUser. Updated all of the scopes to propagate the
RequestContext. The PerThreadRequestScope.Propagator allows scoping
callables to enter a request context.

Change-Id: Idf682ed1d7485cf8c9cdd22cd89bfe1ad5296880
This commit is contained in:
Colby Ranger
2012-05-09 10:39:48 -07:00
parent b3a402124f
commit cfd994548e
20 changed files with 222 additions and 258 deletions

View File

@@ -38,17 +38,15 @@ public class GuiceRequestScopePropagator extends RequestScopePropagator {
private final String url;
private final SocketAddress peer;
private final CurrentUser user;
@Inject
GuiceRequestScopePropagator(
@CanonicalWebUrl @Nullable Provider<String> urlProvider,
@RemotePeer Provider<SocketAddress> remotePeerProvider,
Provider<CurrentUser> currentUserProvider) {
super(ServletScopes.REQUEST);
ThreadLocalRequestContext local) {
super(ServletScopes.REQUEST, local);
this.url = urlProvider != null ? urlProvider.get() : null;
this.peer = remotePeerProvider.get();
this.user = currentUserProvider.get();
}
/**
@@ -69,9 +67,6 @@ public class GuiceRequestScopePropagator extends RequestScopePropagator {
Providers.of(peer));
seedMap.put(Key.get(SocketAddress.class, RemotePeer.class), peer);
seedMap.put(Key.get(typeOfProvider(CurrentUser.class)), Providers.of(user));
seedMap.put(Key.get(CurrentUser.class), user);
return ServletScopes.continueRequest(callable, seedMap);
}

View File

@@ -42,9 +42,12 @@ import java.util.concurrent.Executors;
public abstract class RequestScopePropagator {
private final Scope scope;
private final ThreadLocalRequestContext local;
protected RequestScopePropagator(Scope scope) {
protected RequestScopePropagator(Scope scope,
ThreadLocalRequestContext local) {
this.scope = scope;
this.local = local;
}
/**
@@ -70,26 +73,8 @@ public abstract class RequestScopePropagator {
* @return a new Callable which will execute in the current request scope.
*/
public final <T> Callable<T> wrap(final Callable<T> callable) {
final Callable<T> wrapped = wrapImpl(new Callable<T>() {
@Override
public T call() throws Exception {
RequestCleanup cleanup = scope.scope(
Key.get(RequestCleanup.class),
new Provider<RequestCleanup>() {
@Override
public RequestCleanup get() {
return new RequestCleanup();
}
}).get();
try {
return callable.call();
} finally {
cleanup.run();
}
}
});
final Callable<T> wrapped =
wrapImpl(context(local.getContext(), cleanup(callable)));
return new Callable<T>() {
@Override
public T call() throws Exception {
@@ -178,4 +163,41 @@ public abstract class RequestScopePropagator {
* @see #wrap(Callable)
*/
protected abstract <T> Callable<T> wrapImpl(final Callable<T> callable);
protected <T> Callable<T> context(final RequestContext context,
final Callable<T> callable) {
return new Callable<T>() {
@Override
public T call() throws Exception {
RequestContext old = local.setContext(context);
try {
return callable.call();
} finally {
local.setContext(old);
}
}
};
}
protected <T> Callable<T> cleanup(final Callable<T> callable) {
return new Callable<T>() {
@Override
public T call() throws Exception {
RequestCleanup cleanup = scope.scope(
Key.get(RequestCleanup.class),
new Provider<RequestCleanup>() {
@Override
public RequestCleanup get() {
return new RequestCleanup();
}
}).get();
try {
return callable.call();
} finally {
cleanup.run();
}
}
};
}
}

View File

@@ -31,8 +31,8 @@ public abstract class ThreadLocalRequestScopePropagator<C>
private final ThreadLocal<C> threadLocal;
protected ThreadLocalRequestScopePropagator(Scope scope,
ThreadLocal<C> threadLocal) {
super(scope);
ThreadLocal<C> threadLocal, ThreadLocalRequestContext local) {
super(scope, local);
this.threadLocal = threadLocal;
}