Class InjectionContext

  • All Implemented Interfaces:
    Serializable
    Direct Known Subclasses:
    TrackableContext

    public abstract class InjectionContext
    extends Object
    implements Serializable
    Stores objects scoped 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 of HttpRequestContext 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, a HttpRequestContext may be created in a Filter.

    Note: most Context classes should rather extend TrackableContext subclass instead of this one. The main exception are Context types that are induced 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 a ConcurrentMap. 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.
    Methods prepareForSerialization() and restoreAfterDeserialization() are provided for other serialization mechanisms.
    The serialization is not thread-safe, so a Context that is being serialized must not be accessed by other threads in any way during the process.

    See Also:
    Serialized Form
    • Constructor Detail

      • InjectionContext

        public InjectionContext()
    • Method Detail

      • removeScopedObject

        public void removeScopedObject​(Key<?> key)
        Removes the object given by key from this Context. This forces production of a new instance during the next provisioning within the Scope 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 under key 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 by key. If there already is an instance scoped to this Context, it is returned immediately. Otherwise, a new instance is first obtained from producer, stored for subsequent calls and returned.
      • prepareForSerialization

        protected void prepareForSerialization()
        Stores Serializable entries from scopedObjects into a fully Serializable private List. 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 of scopedObjects from the deserialized data from the private List that was filled before serialization with prepareForSerialization(). 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