Class Context

java.lang.Object
org.jpos.transaction.Context
All Implemented Interfaces:
Externalizable, Serializable, Cloneable, Pausable, Loggeable

public class Context extends Object implements Externalizable, Loggeable, Cloneable, Pausable
Transaction context carrying typed key-value pairs that flow through participant pipelines.
See Also:
  • Constructor Details

    • Context

      public Context()
      Default constructor.
  • Method Details

    • put

      public void put(Object key, Object value)
      Puts an Object in the transient Map.
      Parameters:
      key - the map key
      value - the value to store
    • put

      public void put(Object key, Object value, boolean persist)
      Puts an Object in the transient or persistent Map.
      Parameters:
      key - the map key
      value - the value to store
      persist - true to also store in the persistent map
    • persist

      public void persist(Object key)
      Persists a transient entry
      Parameters:
      key - the key
    • evict

      public void evict(Object key)
      Evicts a persistent entry
      Parameters:
      key - the key
    • get

      public <T> T get(Object key)
      Get object instance from transaction context.
      Type Parameters:
      T - desired type of object instance
      Parameters:
      key - the key of object instance
      Returns:
      object instance if exist in context or null otherwise
    • hasKey

      public boolean hasKey(Object key)
      Check if key present
      Parameters:
      key - the key
      Returns:
      true if present
    • hasKeys

      public boolean hasKeys(Object... keys)
      Determines whether the specified keys are all present in the map. This method accepts a variable number of key arguments and supports both Object[] and String keys. When the key is a String, it can contain multiple keys separated by a '|' character, and the method will return true if any of those keys is present in the map. The method does not support nested arrays of keys.
      Parameters:
      keys - A variable-length array of keys to check for in the map. These keys can be of any Object type or String containing multiple keys separated by '|'.
      Returns:
      true if all specified keys (or any of the '|' separated keys within a String key) are present in the map, false otherwise.
    • keysNotPresent

      public String keysNotPresent(Object... keys)
      Returns a comma-separated string of keys that are not present in the map. This method accepts a variable number of key arguments and supports both Object[] and String keys. When the key is a String, it can contain multiple keys separated by a '|' character, and the method will return the keys not present in the map. The method does not support nested arrays of keys.
      Parameters:
      keys - A variable-length array of keys to check for their absence in the map. These keys can be of any Object type or String containing multiple keys separated by '|'.
      Returns:
      A comma-separated string of keys that are not present in the map. If all the specified keys (or any of the '|' separated keys within a String key) are present in the map, an empty string is returned.
    • hasPersistedKey

      public boolean hasPersistedKey(Object key)
      Check key exists present persisted map
      Parameters:
      key - the key
      Returns:
      true if present
    • move

      public <T> T move(Object from, Object to)
      Move entry to new key name Moves the value from one key to another.
      Type Parameters:
      T - the expected type
      Parameters:
      from - source key
      to - destination key
      Returns:
      the moved value (or null if source key not present)
    • get

      public <T> T get(Object key, T defValue)
      Get object instance from transaction context.
      Type Parameters:
      T - desired type of object instance
      Parameters:
      key - the key of object instance
      defValue - default value returned if there is no value in context
      Returns:
      object instance if exist in context or defValue otherwise
    • remove

      public <T> T remove(Object key)
      Removes the value associated with the given key from both transient and persistent maps.
      Type Parameters:
      T - the expected type
      Parameters:
      key - the key to remove
      Returns:
      the removed value, or null
    • getString

      public String getString(Object key)
      Returns the value as a String, converting it if necessary.
      Parameters:
      key - the map key
      Returns:
      the value as a String, or null
    • getString

      public String getString(Object key, String defValue)
      Returns the value as a String, or a default if not found.
      Parameters:
      key - the map key
      defValue - default if not found
      Returns:
      the value as String, or defValue
    • dump

      public void dump(PrintStream p, String indent)
      Description copied from interface: Loggeable
      Dumps a human-readable representation of this object to the print stream.
      Specified by:
      dump in interface Loggeable
      Parameters:
      p - the output stream
      indent - indentation prefix
    • get

      public <T> T get(Object key, long timeout)
      Retrieves a persistent value by key, waiting up to timeout milliseconds.
      Type Parameters:
      T - the expected return type
      Parameters:
      key - the key
      timeout - maximum wait time in milliseconds
      Returns:
      the value, or null on timeout
    • writeExternal

      public void writeExternal(ObjectOutput out) throws IOException
      Specified by:
      writeExternal in interface Externalizable
      Throws:
      IOException
    • readExternal

    • clone

      public Context clone()
      Creates a copy of the current Context object.

      This method clones the Context object, creating new synchronized map containers that are independent of the original. However, the keys and values themselves are not cloned - both Context instances will share references to the same key/value objects. Structural changes (add/remove operations) to one Context's maps will not affect the other, but modifications to mutable key/value objects will be visible in both Contexts.

      The cloned Context preserves the thread-safety characteristics through Collections.synchronizedMap wrappers.

      Overrides:
      clone in class Object
      Returns:
      a copy of the current Context object with independent map containers
      Throws:
      AssertionError - if cloning is not supported, which should not happen
    • clone

      public Context clone(Object... keys)
      Creates a clone of the current Context instance, including only the specified keys. This method accepts a variable number of key arguments and supports both Object[] and String keys. When the key is a String, it can contain multiple keys separated by a '|' character. The method does not support nested arrays of keys.
      Parameters:
      keys - A variable-length array of keys to include in the cloned context. These keys can be of any Object type or String containing multiple keys separated by '|'.
      Returns:
      A cloned Context instance containing only the specified keys and their associated values from the original context. If none of the specified keys are present in the original context, an empty Context instance is returned.
    • merge

      public void merge(Context c)
      Merges the entries from the provided Context object into the current Context.

      This method iterates over the entries in the given Context object 'c' and adds or updates the entries in the current Context. If an entry already exists in the current Context, its value will be updated. If an entry is marked as persisted in the given Context object, it will also be marked as persisted in the current Context.

      Parameters:
      c - the Context object whose entries should be merged into the current Context
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • getMap

      public Map<Object,Object> getMap()
      Returns the transient map, creating it lazily.
      Returns:
      transient map
    • getMapClone

      Returns a snapshot copy of the transient map.
      Returns:
      a new map containing all current transient entries
    • dumpMap

      protected void dumpMap(PrintStream p, String indent)
      Dumps all map entries to the output stream.
      Parameters:
      p - output stream
      indent - indentation prefix
    • dumpEntry

      protected void dumpEntry(PrintStream p, String indent, Map.Entry<Object,Object> entry)
      Dumps a single map entry to the output stream.
      Parameters:
      p - output stream
      indent - indentation prefix
      entry - the map entry to dump
    • getLogEvent

      return a LogEvent used to store trace information about this transaction. If there's no LogEvent there, it creates one.
      Returns:
      LogEvent
    • getProfiler

      return (or creates) a Profiler object
      Returns:
      Profiler object
    • getResult

      public Result getResult()
      return (or creates) a Resultr object
      Returns:
      Profiler object
    • log

      public void log(Object msg)
      Adds a trace message to the context log event.
      Parameters:
      msg - trace information
    • checkPoint

      public void checkPoint(String detail)
      Adds a checkpoint to the context profiler.
      Parameters:
      detail - descriptive label for this checkpoint
    • isTrace

      public boolean isTrace()
      Returns whether tracing is enabled for this context.
      Returns:
      true if tracing is active
    • setTrace

      public void setTrace(boolean trace)
      Enables or disables tracing for this context.
      Parameters:
      trace - true to enable tracing
    • pause

      public Future<Integer> pause()
      Description copied from interface: Pausable
      Pauses execution and returns a Future that resolves to the resume result.
      Specified by:
      pause in interface Pausable
      Returns:
      a Future resolving to the result code passed to Pausable.resume(int)
    • resume

      public void resume(int result)
      Description copied from interface: Pausable
      Resumes a paused execution with the given result code.
      Specified by:
      resume in interface Pausable
      Parameters:
      result - the result code to deliver to the waiting Future
    • reset

      public void reset()
      Description copied from interface: Pausable
      Resets the pause state so this object can be reused.
      Specified by:
      reset in interface Pausable
    • getTimeout

      public long getTimeout()
      Description copied from interface: Pausable
      Returns the configured pause timeout.
      Specified by:
      getTimeout in interface Pausable
      Returns:
      timeout in milliseconds
    • setTimeout

      public void setTimeout(long timeout)
      Description copied from interface: Pausable
      Sets the maximum pause duration.
      Specified by:
      setTimeout in interface Pausable
      Parameters:
      timeout - timeout in milliseconds