Package org.rapidcontext.core.ctx
Class Context
java.lang.Object
org.rapidcontext.core.ctx.Context
- Direct Known Subclasses:
ApplicationContext,ThreadContext
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 Summary
FieldsModifier and TypeFieldDescriptionThe currently active threads with contexts.static final StringThe base directory context attribute.static final StringThe environment context attribute.static final StringThe scheduler context attribute.static final StringThe storage context attribute.protected StringThe context identifier (for stack traces, etc).protected ContextThe parent context (if available).protected static ContextThe shared root context at the end of the chain. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Contextactive()Returns the currently active context.static <T extends Context>
TReturns the currently active context of a specified type.static ContextReturns the currently active context for a specified thread.Returns a read-only set of active context threads.baseDir()Returns the base directory.voidclose()Closes this context if and only if it is active for the thread.<T extends Context>
TReturns the closest context of the specified type.<T extends Context>
intReturns the context depth of the specified type.Returns the context connectivity environment.<T> TReturns an attribute value of a specified type.<T> TReturns or sets an attribute value of a specified type.booleanChecks if an attribute value is set.booleanhasMatchingId(Pattern pattern) Checks if any parent context has an identifier matching the pattern.id()Returns the context identifier.protected voidopen()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.parent()Returns the parent context.<T extends Context>
TReturns the first parent context of the specified type.voidRemoves an attribute value.Returns the context task scheduler.<T> TSets an attribute value.storage()Returns the context data store.
-
Field Details
-
CX_DIRECTORY
The base directory context attribute.- See Also:
-
CX_ENVIRONMENT
The environment context attribute.- See Also:
-
CX_STORAGE
The storage context attribute.- See Also:
-
CX_SCHEDULER
The scheduler context attribute.- See Also:
-
root
The shared root context at the end of the chain. -
actives
The currently active threads with contexts. -
id
The context identifier (for stack traces, etc). -
parent
The parent context (if available).
-
-
Constructor Details
-
Context
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
Returns the currently active context. If no thread-local context is available, the root context is returned.- Returns:
- the currently active context
-
active
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
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
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
Returns the context identifier. This is used to identify the current context in a stack trace or similar.- Returns:
- the context identifier
-
parent
Returns the parent context.- Returns:
- the parent context, or null if this is the root context
-
parent
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
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
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
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
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
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 nameclazz- the value class required- Returns:
- the attribute value, or null if not defined or mismatching type
-
getOrSet
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 nameclazz- the value class requiredinit- the initializer if not set- Returns:
- the attribute value found or created
-
set
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 namevalue- the attribute value- Returns:
- the attribute value set
-
remove
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
Returns the base directory.- Returns:
- the base directory
-
environment
Returns the context connectivity environment.- Returns:
- the context connectivity environment
-
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
Returns the context task scheduler. This may be used for scheduling delayed or periodic background tasks.- Returns:
- the context task scheduler
-