Class Context

java.lang.Object
org.rapidcontext.core.ctx.Context
Direct Known Subclasses:
ApplicationContext, ThreadContext

public abstract class Context extends Object
The base execution context. The context provides access to settings, storage, current user, arguments, etc. Each context links to a parent context, creating a chain that allows adding or updating attributes in a way that is only visible in a particular context (and its children). All execution contexts, except the root (global) context, are bound to a single execution thread. Each thread has (at most) a single active context that holds data related to a request, procedure call, etc. The shared root (global) context holds data that pertains to the whole system.
Version:
1.0
See Also:
  • Field Details

    • CX_DIRECTORY

      public static final String CX_DIRECTORY
      The base directory context attribute.
      See Also:
    • CX_ENVIRONMENT

      public static final String CX_ENVIRONMENT
      The environment context attribute.
      See Also:
    • CX_STORAGE

      public static final String CX_STORAGE
      The storage context attribute.
      See Also:
    • CX_SCHEDULER

      public static final String CX_SCHEDULER
      The scheduler context attribute.
      See Also:
    • root

      protected static Context root
      The shared root context at the end of the chain.
    • actives

      protected static Map<Thread,Context> actives
      The currently active threads with contexts.
    • id

      protected String id
      The context identifier (for stack traces, etc).
    • parent

      protected Context parent
      The parent context (if available).
  • Constructor Details

    • Context

      protected Context(String id)
      Creates a new context. The previously active (or root) context is set as the parent of this context.
      Parameters:
      id - the context identifier (name)
      See Also:
  • Method Details

    • active

      public static Context active()
      Returns the currently active context. If no thread-local context is available, the root context is returned.
      Returns:
      the currently active context
    • active

      public static <T extends Context> T active(Class<T> clazz)
      Returns the currently active context of a specified type. If no thread-local context is available, the root context is checked.
      Type Parameters:
      T - the context type
      Parameters:
      clazz - the context class
      Returns:
      the currently active context of the specified type, or null if not found
    • activeFor

      public static Context activeFor(Thread thread)
      Returns the currently active context for a specified thread.
      Parameters:
      thread - the thread to fetch for
      Returns:
      the thread-local context, or null if not set
    • activeThreads

      public static Set<Thread> activeThreads()
      Returns a read-only set of active context threads.
      Returns:
      the active context threads
    • open

      protected void open()
      Opens this context and sets it either as the root context (if no root previously set), or as the active context for the currently executing thread.
      See Also:
    • close

      public void close()
      Closes this context if and only if it is active for the thread. If not called from the same thread that created the context, no changes will be made. The parent context will be set as the new active context for the thread. All parent and attribute references in this object are cleared to facilitate garbage collection.
    • id

      public String id()
      Returns the context identifier. This is used to identify the current context in a stack trace or similar.
      Returns:
      the context identifier
    • parent

      public Context parent()
      Returns the parent context.
      Returns:
      the parent context, or null if this is the root context
    • parent

      public <T extends Context> T parent(Class<T> clazz)
      Returns the first parent context of the specified type. The search starts from the immediate parent and searches up the context chain.
      Type Parameters:
      T - the context type
      Parameters:
      clazz - the context class
      Returns:
      the first parent context of the specified type, or null if not found
    • closest

      public <T extends Context> T closest(Class<T> clazz)
      Returns the closest context of the specified type. The search starts with this context and searches up the context chain.
      Type Parameters:
      T - the context type
      Parameters:
      clazz - the context class
      Returns:
      the closest context of the specified type (this context if it matches, otherwise the first matching parent), or null if not found
    • depthOf

      public <T extends Context> int depthOf(Class<T> clazz)
      Returns the context depth of the specified type. The depth includes this context if it matches, otherwise zero (0) is returned.
      Type Parameters:
      T - the context type
      Parameters:
      clazz - the context class
      Returns:
      the context depth, zero (0) or higher
    • hasMatchingId

      public boolean hasMatchingId(Pattern pattern)
      Checks if any parent context has an identifier matching the pattern.
      Parameters:
      pattern - the pattern to match
      Returns:
      true if a parent context id matches, or false otherwise
    • has

      public boolean has(String key)
      Checks if an attribute value is set. If the attribute isn't set in this context, the parent context is checked.
      Parameters:
      key - the attribute name
      Returns:
      true if the attribute is set, or false otherwise
    • get

      public <T> T get(String key, Class<T> clazz)
      Returns an attribute value of a specified type. If the attribute isn't set in this context, the parent context is searched. If the attribute type doesn't match, null is returned.
      Type Parameters:
      T - the attribute type
      Parameters:
      key - the attribute name
      clazz - the value class required
      Returns:
      the attribute value, or null if not defined or mismatching type
    • getOrSet

      public <T> T getOrSet(String key, Class<T> clazz, Supplier<T> init)
      Returns or sets an attribute value of a specified type. If the attribute isn't set in this or any parent context, it is created using the provided supplier.
      Type Parameters:
      T - the attribute type
      Parameters:
      key - the attribute name
      clazz - the value class required
      init - the initializer if not set
      Returns:
      the attribute value found or created
    • set

      public <T> T set(String key, T value)
      Sets an attribute value. The value is set in this context, regardless if it is also defined in a parent context.
      Type Parameters:
      T - the attribute type
      Parameters:
      key - the attribute name
      value - the attribute value
      Returns:
      the attribute value set
    • remove

      public void remove(String key)
      Removes an attribute value. The value is removed in this context, but may still be defined in a parent context.
      Parameters:
      key - the attribute name
    • baseDir

      public File baseDir()
      Returns the base directory.
      Returns:
      the base directory
    • environment

      public Environment environment()
      Returns the context connectivity environment.
      Returns:
      the context connectivity environment
    • storage

      public Storage storage()
      Returns the context data store. This may be either the root data storage or one that restricts or modifies access.
      Returns:
      the context data store
    • scheduler

      public ScheduledExecutorService scheduler()
      Returns the context task scheduler. This may be used for scheduling delayed or periodic background tasks.
      Returns:
      the context task scheduler