/** * Get a new reference for to currently running request scope context. This call * prevents automatic {@link RequestContext#release() release} of the scope * context once the task that runs in the scope has finished. * <p> * The returned scope context may be used to run additional task(s) in the * same request scope using one of the {@code #runInScope(RequestContext, ...)} methods. * </p> * <p> * Note that the returned context must be {@link RequestContext#release() * released} manually once not needed anymore to prevent memory leaks. * </p> * * @return currently active {@link RequestContext request scope context}. * @throws IllegalStateException in case there is no active request scope associated * with the current thread or if the request scope has * been already shut down. * @see #suspendCurrent() */ public RequestContext referenceCurrent() throws IllegalStateException { return current().getReference(); }
/** * Create new scoped client response. * * @param context jersey client response context. * @param scope request scope instance. */ public InboundJaxrsResponse(final ClientResponse context, final RequestScope scope) { this.context = context; this.scope = scope; if (this.scope != null) { this.requestContext = scope.referenceCurrent(); } else { this.requestContext = null; } }
@Override public void run() { responder.runtime.requestScope.runInScope(requestContext, new Runnable() { @Override public void run() { try { requestScopeListener.resume(foreignScopeInstance, responder.runtime.injectionManager); final Response response = producer.call(); if (response != null) { resume(response); } } catch (final Throwable t) { resume(t); } } }); } });
/** * Runs the {@link Runnable task} in the new request scope. The current thread might * be already in any request scope and in that case the scope will be changed to the * scope defined by the {@link RequestContext scope context}. At the end of * the method the request scope is returned to its original state. The newly created * {@link RequestContext scope context} will be implicitly released at the end * of the method call except the task will call * {@link RequestScope#suspendCurrent}. * * @param task Task to be executed. */ public void runInScope(Runnable task) { final RequestContext oldContext = retrieveCurrent(); final RequestContext context = createContext(); try { activate(context, oldContext); Errors.process(task); } finally { release(context); resume(oldContext); } }
final RequestContext context = retrieveCurrent(); if (context == null) { return null; suspend(referencedContext); return referencedContext; } finally {
/** * Runs the {@link Runnable task} in the new request scope. The current thread might * be already in any request scope and in that case the scope will be changed to the * scope defined by the {@link RequestScope.Instance scope instance}. At the end of * the method the request scope is returned to its original state. The newly created * {@link RequestScope.Instance scope instance} will be implicitly released at the end * of the method call except the task will call * {@link RequestScope#suspendCurrent}. * * @param task Task to be executed. */ public void runInScope(Runnable task) { final Instance oldInstance = retrieveCurrent(); final Instance instance = createInstance(); try { setCurrent(instance); Errors.process(task); } finally { instance.release(); resumeCurrent(oldInstance); } }
final RequestContext requestScopeInstance = requestScope.createContext(); context.initAsyncContext(asyncResponderHolder); requestScope.runInScope(requestScopeInstance, new Runnable() { @Override public void run() {
final RequestScope.Instance requestScopeInstance = requestScope.createInstance(); final AsyncResponderHolder asyncResponderHolder = new AsyncResponderHolder(responder, externalRequestScope, context.initAsyncContext(asyncResponderHolder); requestScope.runInScope(requestScopeInstance, new Runnable() { @Override public void run() {
beanFactory.registerScope(WebApplicationContext.SCOPE_REQUEST, new RequestScope());
@Override protected void resume(RequestContext context) { super.resume(context); if (context != null) { CdiRequestContext cdiRequestContext = (CdiRequestContext) context; requestContextController.associate(cdiRequestContext.getStore()); requestContextController.activate(); } }
@Override protected void release(RequestContext context) { super.release(context); CdiRequestContext cdiRequestContext = (CdiRequestContext) context; requestContextController.invalidate(); requestContextController.deactivate(); requestContextController.dissociate(cdiRequestContext.getStore()); }
@Override protected void activate(RequestContext context, RequestContext oldContext) { super.activate(context, oldContext); if (oldContext != null) { CdiRequestContext oldRequestContext = (CdiRequestContext) oldContext; requestContextController.deactivate(); requestContextController.dissociate(oldRequestContext.getStore()); } CdiRequestContext cdiRequestContext = (CdiRequestContext) context; requestContextController.associate(cdiRequestContext.getStore()); requestContextController.activate(); }
/** * Runs the {@link Runnable task} in the new request scope. The current thread might * be already in any request scope and in that case the scope will be changed to the * scope defined by the {@link RequestContext scope context}. At the end of * the method the request scope is returned to its original state. The newly created * {@link RequestContext scope context} will be implicitly released at the end * of the method call except the task will call * {@link RequestScope#suspendCurrent}. * * @param task Task to be executed. */ public void runInScope(Runnable task) { final RequestContext oldContext = retrieveCurrent(); final RequestContext context = createContext(); try { activate(context, oldContext); Errors.process(task); } finally { release(context); resume(oldContext); } }
/** * Runs the {@link Runnable task} in the new request scope. The current thread might * be already in any request scope and in that case the scope will be changed to the * scope defined by the {@link RequestScope.Instance scope instance}. At the end of * the method the request scope is returned to its original state. The newly created * {@link RequestScope.Instance scope instance} will be implicitly released at the end * of the method call except the task will call * {@link RequestScope#suspendCurrent}. * * @param task Task to be executed. */ public void runInScope(Runnable task) { final Instance oldInstance = retrieveCurrent(); final Instance instance = createInstance(); try { setCurrent(instance); Errors.process(task); } finally { instance.release(); resumeCurrent(oldInstance); } }
final RequestContext requestScopeInstance = requestScope.createContext(); context.initAsyncContext(asyncResponderHolder); requestScope.runInScope(requestScopeInstance, new Runnable() { @Override public void run() {
final RequestScope.Instance requestScopeInstance = requestScope.createInstance(); final AsyncResponderHolder asyncResponderHolder = new AsyncResponderHolder(responder, externalRequestScope, context.initAsyncContext(asyncResponderHolder); requestScope.runInScope(requestScopeInstance, new Runnable() { @Override public void run() {
final RequestContext context = retrieveCurrent(); if (context == null) { return null; suspend(referencedContext); return referencedContext; } finally {
@BeforeClass(dependsOnMethod = "springTestContextPrepareTestInstance") public void registerWebScopes() { ConfigurableBeanFactory factory = (ConfigurableBeanFactory) this.applicationContext.getAutowireCapableBeanFactory(); factory.registerScope("session", new SessionScope()); factory.registerScope("request", new RequestScope()); }
/** * Runs the {@link org.glassfish.jersey.internal.util.Producer task} in the new request scope. The * current thread might be already in any request scope and in that case the scope * will be changed to the scope defined by the {@link RequestContext scope * instance}. At the end of the method the request scope is returned to its original * state. The newly created {@link RequestContext scope context} will be * implicitly released at the end of the method call except the task will call * {@link RequestScope#suspendCurrent}. * * @param task Task to be executed. * @param <T> {@code task} result type. * @return result returned by the {@code task}. */ public <T> T runInScope(Producer<T> task) { final RequestContext oldContext = retrieveCurrent(); final RequestContext context = createContext(); try { activate(context, oldContext); return Errors.process(task); } finally { release(context); resume(oldContext); } }
/** * Runs the {@link Runnable task} in the new request scope. The current thread might * be already in any request scope and in that case the scope will be changed to the * scope defined by the {@link RequestScope.Instance scope instance}. At the end of * the method the request scope is returned to its original state. The newly created * {@link RequestScope.Instance scope instance} will be implicitly released at the end * of the method call except the task will call * {@link RequestScope#suspendCurrent}. * * @param task Task to be executed. */ public void runInScope(Runnable task) { final Instance oldInstance = retrieveCurrent(); final Instance instance = createInstance(); try { setCurrent(instance); Errors.process(task); } finally { instance.release(); resumeCurrent(oldInstance); } }