/** * Scopes the given callable inside a request scope. This is not the same as the HTTP request * scope, but is used if no HTTP request scope is in progress. In this way, keys can be scoped * as @RequestScoped and exist 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 request scope with. To * seed a key with null, use {@code null} as the value. * @return a callable that when called will run inside the a request scope that exposes the * instances in the {@code seedMap} as scoped keys. * @since 3.0 */ public static <T> Callable<T> scopeRequest(Callable<T> callable, Map<Key<?>, Object> seedMap) { return wrap(callable, scopeRequest(seedMap)); }
public void testTransferNonHttpRequest_concurrentUseSameThreadOk() throws Exception { Callable<Boolean> callable = new Callable<Boolean>() { @Override public Boolean call() throws Exception { return ServletScopes.transferRequest(FALSE_CALLABLE).call(); } }; ImmutableMap<Key<?>, Object> seedMap = ImmutableMap.of(); assertFalse(ServletScopes.scopeRequest(callable, seedMap).call()); }
public void testTransferNonHttpRequest_concurrentUseSameThreadOk_closeable() throws Exception { Callable<Boolean> callable = new Callable<Boolean>() { @Override public Boolean call() throws Exception { RequestScoper.CloseableScope scope = ServletScopes.transferRequest().open(); try { return false; } finally { scope.close(); } } }; ImmutableMap<Key<?>, Object> seedMap = ImmutableMap.of(); assertFalse(ServletScopes.scopeRequest(callable, seedMap).call()); } }
public void testTransferNonHttpRequest_concurrentUseBlocks() throws Exception { Callable<Boolean> callable = new Callable<Boolean>() { @Override public Boolean call() throws Exception { ExecutorService executor = Executors.newSingleThreadExecutor(); try { Future<Boolean> future = executor.submit(ServletScopes.transferRequest(FALSE_CALLABLE)); try { return future.get(100, TimeUnit.MILLISECONDS); } catch (TimeoutException e) { return true; } } finally { executor.shutdownNow(); } } }; ImmutableMap<Key<?>, Object> seedMap = ImmutableMap.of(); assertTrue(ServletScopes.scopeRequest(callable, seedMap).call()); }
assertTrue(ServletScopes.scopeRequest(callable, seedMap).call());
public final void testWrongValueClasses() throws Exception { Injector injector = Guice.createInjector( new ServletModule() { @Override protected void configureServlets() { bindConstant().annotatedWith(Names.named(SomeObject.INVALID)).to(SHOULDNEVERBESEEN); bind(SomeObject.class).in(RequestScoped.class); } }); OffRequestCallable offRequestCallable = injector.getInstance(OffRequestCallable.class); try { ServletScopes.scopeRequest( offRequestCallable, ImmutableMap.<Key<?>, Object>of(Key.get(SomeObject.class), "Boo!")); fail(); } catch (IllegalArgumentException iae) { assertEquals( "Value[Boo!] of type[java.lang.String] is not compatible with key[" + Key.get(SomeObject.class) + "]", iae.getMessage()); } }
Callable<Boolean> transfer = ServletScopes.scopeRequest(callable, seedMap).call();
Data data = ServletScopes.scopeRequest(callable, seedMap).call();
public final void testNullReplacement() throws Exception { Injector injector = Guice.createInjector( new ServletModule() { @Override protected void configureServlets() { bindConstant().annotatedWith(Names.named(SomeObject.INVALID)).to(SHOULDNEVERBESEEN); bind(SomeObject.class).in(RequestScoped.class); } }); Callable<SomeObject> callable = injector.getInstance(Caller.class); try { assertNotNull(callable.call()); fail(); } catch (ProvisionException pe) { assertTrue(pe.getCause() instanceof OutOfScopeException); } // Validate that an actual null entry in the map results in a null injected object. Map<Key<?>, Object> map = Maps.newHashMap(); map.put(Key.get(SomeObject.class), null); callable = ServletScopes.scopeRequest(injector.getInstance(Caller.class), map); assertNull(callable.call()); }
executor .submit( ServletScopes.scopeRequest( offRequestCallable, ImmutableMap.<Key<?>, Object>of(Key.get(SomeObject.class), someObject))) executor .submit( ServletScopes.scopeRequest( offRequestCallable, ImmutableMap.<Key<?>, Object>of(Key.get(SomeObject.class), someObject)))
@Override public void evaluate() throws Throwable { ServletScopes.scopeRequest(new Callable<Object>() { @Override public Object call() throws Exception { try { base.evaluate(); } catch (Throwable e) { throw new RuntimeException(e); } return null; } }, new HashMap<Key<?>, Object>()).call(); } };
@Override public void evaluate() throws Throwable { ServletScopes.scopeRequest(new Callable<Object>() { @Override public Object call() throws Exception { try { base.evaluate(); } catch (RuntimeException | Error e) { // don't wrap RuntimeExceptions // don't wrap Errors either, JUnit throws this throw e; } catch (Throwable e) { throw new RuntimeException(e); } return null; } }, new HashMap<Key<?>, Object>()).call(); } };
/** * Scopes the given callable inside a request scope. This is not the same as the HTTP request * scope, but is used if no HTTP request scope is in progress. In this way, keys can be scoped * as @RequestScoped and exist 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 request scope with. To * seed a key with null, use {@code null} as the value. * @return a callable that when called will run inside the a request scope that exposes the * instances in the {@code seedMap} as scoped keys. * @since 3.0 */ public static <T> Callable<T> scopeRequest(Callable<T> callable, Map<Key<?>, Object> seedMap) { return wrap(callable, scopeRequest(seedMap)); }
/** * Scopes the given callable inside a request scope. This is not the same as the HTTP request * scope, but is used if no HTTP request scope is in progress. In this way, keys can be scoped * as @RequestScoped and exist 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 request scope with. To * seed a key with null, use {@code null} as the value. * @return a callable that when called will run inside the a request scope that exposes the * instances in the {@code seedMap} as scoped keys. * @since 3.0 */ public static <T> Callable<T> scopeRequest(Callable<T> callable, Map<Key<?>, Object> seedMap) { return wrap(callable, scopeRequest(seedMap)); }