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 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 of HttpRequestContext 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, 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 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.
    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

      • produceIfAbsent

        protected <T> T produceIfAbsent​(Key<T> key,
                                        Provider<T> producer)
        Provides an object scoped to this Context given by key. If there is already an object scoped to this Context under key, it is returned immediately. Otherwise, a new instance is first obtained from producer, stored for subsequent calls and then returned.
      • 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 no longer usable (for example a broken connection, expired token, etc).
        If there's no object stored under key in this 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.

      • prepareForSerialization

        protected void prepareForSerialization()
        Picks Serializable objects scoped to this Context from its transient state and stores them into a fully Serializable private List of entries. After a deserialization, the state can restored using restoreAfterDeserialization().

        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 given Context between the call to this method and the actual serialization.

      • restoreAfterDeserialization

        protected void restoreAfterDeserialization()
                                            throws ClassNotFoundException
        Restores the state of this Context from the deserialized data in the private List filled using 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 modification of this Context's state or an 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