org.glassfish.jersey.process.internal
Class RequestScope

java.lang.Object
  extended by org.glassfish.jersey.process.internal.RequestScope
All Implemented Interfaces:
org.glassfish.hk2.Scope

public class RequestScope
extends java.lang.Object
implements org.glassfish.hk2.Scope

Scopes a single request/response processing execution.

This implementation is derived from Guice Wiki article on Custom Scopes:

Apply this scope with a try…finally block:


   scope.enter();
   try {
     // explicitly seed some seed objects...
     scope.seed(Key.value(SomeObject.class), someObject);
     // create and access scoped objects
   } finally {
     scope.exit();
   }
 
The scope can be initialized with one or more seed instances by calling seed(key, instance) before the injector will be called upon to provide for this key. A typical use is for a Servlet filter to enter/exit the scope, representing a Request Scope, and seed HttpServletRequest and HttpServletResponse. For each key inserted with seed(), it's good practice (since you have to provide some binding anyhow) to include a corresponding binding that will throw an exception if Guice is asked to provide for that key if it was not yet seeded:

   bind(key)
       .toProvider(RequestScope.unseededKeyProvider())
       .in(ScopeAnnotation.class);
 

Author:
Paul Sandoz, Marek Potociar (marek.potociar at oracle.com)

Nested Class Summary
static class RequestScope.Module
           
static class RequestScope.Snapshot
          An opaque RequestScope state holder.
 
Constructor Summary
RequestScope()
           
 
Method Summary
 org.glassfish.hk2.ScopeInstance current()
           
 void enter()
          Enters the new RequestScope scope block in the current thread.
 void enter(RequestScope.Snapshot snapshot)
          Resumes/continues an existing RequestScope scope block in the current thread.
 void exit()
          Exits the active scope block in the current thread.
 boolean isActive()
          Provides information whether the current code is executed in a context of an active request scope.
 RequestScope.Snapshot takeSnapshot()
          Takes snapshot of the state of the active RequestScope scope block in the current thread.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RequestScope

public RequestScope()
Method Detail

current

public org.glassfish.hk2.ScopeInstance current()
Specified by:
current in interface org.glassfish.hk2.Scope

takeSnapshot

public RequestScope.Snapshot takeSnapshot()
                                   throws java.lang.IllegalStateException
Takes snapshot of the state of the active RequestScope scope block in the current thread.

Returns:
currently active RequestScope scope block state snapshot
Throws:
java.lang.IllegalStateException - in case there is no active RequestScope scope block in the current thread.

isActive

public boolean isActive()
Provides information whether the current code is executed in a context of an active request scope.

Returns:
true if the current code runs in a context of an active request scope, false otherwise.

enter

public void enter()
           throws java.lang.IllegalStateException
Enters the new RequestScope scope block in the current thread.

NOTE: This method must not be called from within an active RequestScope scope block in the same thread.

Throws:
java.lang.IllegalStateException - in case the method is called from an already active RequestScope scope block in the same thread.

enter

public void enter(RequestScope.Snapshot snapshot)
           throws java.lang.IllegalStateException
Resumes/continues an existing RequestScope scope block in the current thread. All scope data are initialized from the provided scope snapshot.

NOTE: This method must not be called from within an active RequestScope scope block in the same thread otherwise an exception will be thrown.

Parameters:
snapshot - snapshot of the scope block that should be resumed in the current thread
Throws:
java.lang.IllegalStateException - in case the method is called from an already active RequestScope scope block in the same thread.

exit

public void exit()
          throws java.lang.IllegalStateException
Exits the active scope block in the current thread. All scoped instances are discarded.

NOTE: This method must be called only from within an active RequestScope scope block in the current thread.

Throws:
java.lang.IllegalStateException - in case there is no active RequestScope scope block to exit in the current thread.


Copyright © 2007-2012 Oracle Corporation. All Rights Reserved. Use is subject to license terms.