@Realtime @Parallelizable(comment="Sequential configuration, parallel use") public abstract class AbstractContext extends Object
The parent class for all contexts. Contexts allow for cross cutting concerns (performance, logging, security, ...) to be addressed at run-time through OSGi published services without polluting the application code ( Separation of Concerns).
Context configuration is performed in a try, finally
block statement and impacts only the current thread (although inherited
by inner ConcurrentContext
threads).
AnyContext ctx = AnyContext.enter(); // Enters a context scope. try { ctx.configure(...); // Local configuration (optional). ... // Current thread executes using the configured context. } finally { ctx.exit(); }
Modifier | Constructor and Description |
---|---|
protected |
AbstractContext()
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
static AbstractContext |
current()
Returns the current context for the current thread or
null
if this thread has no context (default). |
protected static <T extends AbstractContext> |
current(Class<T> type)
Returns the current context of specified type or
null if none. |
static <T extends AbstractContext> |
enter(Class<T> custom)
Enters the scope of a custom context.
|
protected AbstractContext |
enterInner()
Enters the scope of an inner context which becomes the current context;
the previous current context becomes the outer of this context.
|
void |
exit()
Exits the scope of this context; the outer of this context becomes
the current context.
|
protected AbstractContext |
getOuter()
Returns the outer context of this context or
null if this
context has no outer context. |
static void |
inherit(AbstractContext ctx)
Inherits the specified context which becomes the context of the current
thread.
|
protected abstract AbstractContext |
inner()
Returns a new inner instance of this context inheriting the properties
of this context.
|
public static AbstractContext current()
null
if this thread has no context (default).protected static <T extends AbstractContext> T current(Class<T> type)
null
if none.public static <T extends AbstractContext> T enter(Class<T> custom)
Enters the scope of a custom context. This method raises a
SecurityException
if the permission to enter contexts of
the specified class is not granted. For example, the following
disallow entering any custom context.
SecurityContext ctx = SecurityContext.enter(); try { ctx.revoke(new SecurityContext.Permission(AbstractContext.class, "enter")); ... // Cannot enter any custom context. } finally { ctx.exit(); // Back to previous security settings. }
custom
- the custom context to enter.IllegalArgumentException
- if the specified class default constructor
cannot be instantiated.SecurityException
- if SecurityContext.Permission(custom, "enter")
is not granted.SecurityContext.Permission
public static void inherit(AbstractContext ctx)
//Spawns a new thread inheriting the context of the current thread. MyThread myThread = new MyThread(); myThread.inherited = AbstractContext.current(); myThread.start(); ... class MyThread extends Thread { AbstractContext inherited; public void run() { AbstractContext.inherit(inherited); // Sets current context. ... } }
protected AbstractContext enterInner()
public void exit()
IllegalStateException
- if this context is not the current
context.protected AbstractContext getOuter()
null
if this
context has no outer context.protected abstract AbstractContext inner()
Copyright © 2005-2013 Javolution. All Rights Reserved.