JADE v6.1

com.dautelle.realtime
Class Context

java.lang.Object
  extended bycom.dautelle.realtime.Context
Direct Known Subclasses:
ConcurrentContext, HeapContext, LocalContext, PoolContext

public abstract class Context
extends java.lang.Object

This class represents a real-time context (thread-based). Applications do not have direct access to instances of this class. Context methods are always static and typically affect the first outer context of appropriate type (as defined by the method's class). In some cases (e.g. LogContext below), context static methods may affect more than one context instance.

The scope of a Context is defined by a try, finally block statement which starts with a static enter call and ends with a static exit call. For example:

     LocalContext.enter();
     try { // Current thread executes in a local context.
         ... 
     } finally {
         LocalContext.exit();
     }
The class of the enter/exit methods identifies the context type.

Context can be nested, they inherit the setting/behaviors of their outer contexts (unless these setting/behaviors are mutually exclusive).

Context-aware applications may extend this base class to address system-wide concerns, such as logging, security, performance, and so forth. For example:

     public class LogContext extends Context {
         public static enter(Logger logger) { ... }
         public static void log(String msg) { ... }
         public static void exit() { ... }
         ...
     }
     ...
     LogContext.enter(logger_A);
     try {
         foo(); // Logs foo with logger_A
         LogContext.enter(logger_B);
         try {
             foo(); // Logs foo with logger_A and logger_B
         } finally {
             LogContext.exit();
         }
     } finally (
         LogContext.exit();
     }
     ...
     void foo() {
          LogContext.log("blah, blah, blah");
     }
     

Note: JADE context programming is somewhat complementary to aspect-oriented programming. Whereas context programming is dynamic by nature (thread based); AOP is typically code based (ref. AspectJ tool/compiler). Both can be used in conjunction to insert custom context code automatically.

Version:
6.0, May 24, 2004
Author:
Jean-Marie Dautelle

Constructor Summary
protected Context()
          Default constructor.
 
Method Summary
protected static Context current()
          Returns the current context for the current thread.
protected  Context getInner()
          Returns the last inner context used by the owner of this context.
protected  Context getOuter()
          Holds the outer context of this Context or null if none (root context).
protected  java.lang.Thread getOwner()
          Returns the thread owner of this Context.
protected static Context pop()
          Pops the current context from the context stack.
protected static Context push(java.lang.Class contextClass)
          Pushes a context of specified type as the current context.
protected static void push(Context ctx)
          Pushes the specified context as the current context.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Context

protected Context()
Default constructor. This constructor should be called from the thread owner of the context.

Method Detail

current

protected static Context current()
Returns the current context for the current thread. The default context is a HeapContext for normal threads and a PoolContext for concurrent threads.

Returns:
the current context (always different from null).

getOwner

protected final java.lang.Thread getOwner()
Returns the thread owner of this Context. The owner of a context is the thread which entered the context. The owner of the current context is always the current thread but the owner of the current outer context might be another thread due to concurrency.

Returns:
the thread owner of this context.

getOuter

protected final Context getOuter()
Holds the outer context of this Context or null if none (root context).

Returns:
the outer context or null if none (root context).

getInner

protected final Context getInner()
Returns the last inner context used by the owner of this context. This method allows for traversing of the contexts which have been used by this context's owner.

Returns:
the outer context or null if none (deepest context).

push

protected static Context push(java.lang.Class contextClass)
Pushes a context of specified type as the current context. This method tries to reuse contexts from the context stack.

Parameters:
contextClass - the class of the context to be pushed.
Returns:
the context pushed or null if none found.

push

protected static void push(Context ctx)
Pushes the specified context as the current context. The previous context becomes the getOuter() of the specified context.

Parameters:
ctx - the new current context.

pop

protected static Context pop()
Pops the current context from the context stack. The getOuter() of the current context becomes the current context.

Returns:
the previous current() context.
Throws:
java.util.EmptyStackException - if the current thread is not the owner of the current context.
See Also:
getOwner()

JADE v6.1

Copyright © 2004 Jean-Marie Dautelle.