Add a ISE exception subclass for scoping exceptions
and document when it will be thrown for all the various
methods in ServletScopes.java
------------
Manually synced.
COMMIT=32471733
diff --git a/extensions/servlet/src/com/google/inject/servlet/ScopingException.java b/extensions/servlet/src/com/google/inject/servlet/ScopingException.java
new file mode 100644
index 0000000..22f90f1
--- /dev/null
+++ b/extensions/servlet/src/com/google/inject/servlet/ScopingException.java
@@ -0,0 +1,28 @@
+/**
+ * Copyright (C) 2012 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.inject.servlet;
+
+/**
+ * Exception thrown when there was a failure entering request scope.
+ *
+ * @author Chris Nokleberg
+ */
+public final class ScopingException extends IllegalStateException {
+ public ScopingException(String message) {
+ super(message);
+ }
+}
diff --git a/extensions/servlet/src/com/google/inject/servlet/ServletScopes.java b/extensions/servlet/src/com/google/inject/servlet/ServletScopes.java
index 33b04c0..e3f8e0c 100644
--- a/extensions/servlet/src/com/google/inject/servlet/ServletScopes.java
+++ b/extensions/servlet/src/com/google/inject/servlet/ServletScopes.java
@@ -194,6 +194,9 @@
* are not available.</li>
* </ul>
*
+ * <p>The returned callable will throw a {@link ScopingException} when called
+ * if the HTTP request scope is still active on the current thread.
+ *
* @param callable code to be executed in another thread, which depends on
* the request scope.
* @param seedMap the initial set of scoped instances for Guice to seed the
@@ -221,7 +224,7 @@
return new Callable<T>() {
public T call() throws Exception {
- Preconditions.checkState(null == GuiceFilter.localContext.get(),
+ checkScopingState(null == GuiceFilter.localContext.get(),
"Cannot continue request in the same thread as a HTTP request!");
return new GuiceFilter.Context(continuingRequest, continuingRequest, null)
.call(callable);
@@ -245,6 +248,9 @@
* words, do not use this method to propagate the current request scope to
* worker threads that may run concurrently with the current thread.
*
+ * <p>The returned callable will throw a {@link ScopingException} when called
+ * if the request scope being transferred is still active on a different
+ * thread.
*
* @param callable code to be executed in another thread, which depends on
* the request scope.
@@ -301,6 +307,9 @@
* in non-HTTP requests (for example: RPC requests) as well as in HTTP
* request threads.
*
+ * <p>The returned callable will throw a {@link ScopingException} when called
+ * if there is a request scope already active on the current thread.
+ *
* @param callable code to be executed which depends on the request scope.
* Typically in another thread, but not necessarily so.
* @param seedMap the initial set of scoped instances for Guice to seed the
@@ -324,9 +333,9 @@
return new Callable<T>() {
public T call() throws Exception {
- Preconditions.checkState(null == GuiceFilter.localContext.get(),
+ checkScopingState(null == GuiceFilter.localContext.get(),
"An HTTP request is already in progress, cannot scope a new request in this thread.");
- Preconditions.checkState(null == requestScopeContext.get(),
+ checkScopingState(null == requestScopeContext.get(),
"A request scope is already in progress, cannot scope a new request in this thread.");
return context.call(callable);
}
@@ -357,7 +366,7 @@
<T> T call(Callable<T> callable) throws Exception {
Thread oldOwner = owner;
Thread newOwner = Thread.currentThread();
- Preconditions.checkState(oldOwner == null || oldOwner == newOwner,
+ checkScopingState(oldOwner == null || oldOwner == newOwner,
"Trying to transfer request scope but original scope is still active");
owner = newOwner;
Context previous = requestScopeContext.get();
@@ -370,4 +379,10 @@
}
}
}
+
+ private static void checkScopingState(boolean condition, String msg) {
+ if (!condition) {
+ throw new ScopingException(msg);
+ }
+ }
}