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 event/call/request/session, such as an RPC, a servlet request processing, a session combining several events 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 event: 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 sameContext
and access or remove the same scoped objects as the state is backed by aConcurrentMap
. Nevertheless, concurrently accessed scoped objects themself must either be thread-safe or accessing them must be properly synchronized.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()
PicksSerializable
objects scoped to thisContext
from itstransient
state and stores them into a fullySerializable
privateList
of entries.protected <T> T
produceIfAbsent(Key<T> key, Provider<T> producer)
Provides an object scoped to thisContext
given bykey
.void
removeScopedObject(Key<?> key)
Removes the object given bykey
from thisContext
.protected void
restoreAfterDeserialization()
Restores the state of thisContext
from the deserialized data in the privateList
filled usingprepareForSerialization()
.
-
-
-
Method Detail
-
produceIfAbsent
protected <T> T produceIfAbsent(Key<T> key, Provider<T> producer)
Provides an object scoped to thisContext
given bykey
. If there is already an object scoped to thisContext
underkey
, it is returned immediately. Otherwise, a new instance is first obtained fromproducer
, stored for subsequent calls and then returned.
-
removeScopedObject
public void removeScopedObject(Key<?> key)
Removes the object given bykey
from thisContext
. This forces production of a new instance during the nextprovisioning
within theScope of this Context
. This is useful if the currently stored instance is no longer usable (for example a broken connection, expired token, etc).
If there's no object stored underkey
in thisContext
, 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.
-
prepareForSerialization
protected void prepareForSerialization()
PicksSerializable
objects scoped to thisContext
from itstransient
state and stores them into a fullySerializable
privateList
of entries. After a deserialization, the state can restored usingrestoreAfterDeserialization()
.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 safe to call several times between the most recent modification of this
Context
's state and the actual serialization in case 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 givenContext
between the call to this method and the actual serialization.
-
restoreAfterDeserialization
protected void restoreAfterDeserialization() throws ClassNotFoundException
Restores the state of thisContext
from the deserialized data in the privateList
filled usingprepareForSerialization()
.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 modification of this
Context
's state or an invocation ofprepareForSerialization()
, 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
-
-