Class InjectionContext
- java.lang.Object
-
- pl.morgwai.base.guice.scopes.InjectionContext
-
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
TrackableContext
public abstract class InjectionContext extends Object implements Serializable
Stores objectsscoped
to the context of some processing/call/request/session, such as an RPC, a servlet request processing, a session combining several calls etc. Each concrete subclass corresponds to a specific type of events and each instance corresponds to a single such event. For example an instance ofHttpRequestContext
may correspond to the processing of a single HTTP request. Creation of instances must be hooked at the beginning of a given processing: for example in Java Servlet environment, aHttpRequestContext
may be created in aFilter
.Note: most
Context
classes should rather extendTrackableContext
subclass instead of this one. The main exception areContext
types that areinduced by other Contexts
.Subclasses usually add properties and methods specific to their types, like their call's arguments, a reference to their event objects etc.
Multiple threads may run within the same
Context
and access or remove the same scoped objects as the state is backed by aConcurrentMap
. Nevertheless, concurrently accessed scoped objects themself must be thread-safe or accessing them must be properly synchronized in such case.During the standard
Java serialization
, non-serializable scoped objects will be filtered out and the remaining part will be properly serialized.
MethodsprepareForSerialization()
andrestoreAfterDeserialization()
are provided for other serialization mechanisms.
The serialization is not thread-safe, so aContext
that is being serialized must not be accessed by other threads in any way during the process.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description InjectionContext()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
prepareForSerialization()
protected <T> T
produceIfAbsent(Key<T> key, Provider<T> producer)
Provides the scoped object given bykey
.void
removeScopedObject(Key<?> key)
Removes the object given bykey
from thisContext
.protected void
restoreAfterDeserialization()
Restores the state ofscopedObjects
from the deserialized data from the privateList
that was filled before serialization withprepareForSerialization()
.
-
-
-
Method Detail
-
removeScopedObject
public void removeScopedObject(Key<?> key)
Removes the object given bykey
from thisContext
. This forces production of a new instance during thenext provisioning
within theScope of this Context
. This is useful if the currently stored instance is not usable anymore (for example a timed-out connection, expired token, etc).
If there's no object stored underkey
in this (@code Context}, this method has no effect.Note: If multiple threads run within the same context, care must be taken to prevent some of them from retaining the old stale instances.
-
produceIfAbsent
protected <T> T produceIfAbsent(Key<T> key, Provider<T> producer)
Provides the scoped object given bykey
. If there already is an instance scoped to thisContext
, it is returned immediately. Otherwise, a new instance is first obtained fromproducer
, stored for subsequent calls and returned.
-
prepareForSerialization
protected void prepareForSerialization()
StoresSerializable
entries fromscopedObjects
into a fullySerializable
privateList
. This method is called automatically during the standard Java serialization. It may be called manually if some other serialization mechanism is used.This method is idempotent between the most recent modification of this
Context
's state and the actual serialization, so it is safe to call it manually if it is unknown whether the standard Java serialization or some other mechanism will be used.It must be ensured, that no other threads may access a given
Context
between the call to this method and the actual serialization.
-
restoreAfterDeserialization
protected void restoreAfterDeserialization() throws ClassNotFoundException
Restores the state ofscopedObjects
from the deserialized data from the privateList
that was filled before serialization withprepareForSerialization()
. This method is called automatically during the standard Java deserialization. It may be called manually if some other deserialization mechanism is used.This method is idempotent between the actual deserialization and the next invocation of
prepareForSerialization()
, so it is safe to call it manually right after deserialization if it is unknown whether the standard Java deserialization or some other mechanism was used.- Throws:
ClassNotFoundException
-
-