J avolution v3.7 (J2SE 1.5+)

javolution.realtime
Class Context

java.lang.Object
  extended by javolution.realtime.Context
Direct Known Subclasses:
ConcurrentContext, HeapContext, LocalContext, LogContext, PoolContext

public abstract class Context
extends java.lang.Object

This class represents a real-time context; they are typically thread-local but they can also be associated to particular objects.

This package provides few predefined contexts:

Context-aware applications may extend the context base class or any predefined contexts in order to facilitate separation of concern (e.g. logging, security, performance and so forth).

The scope of a Context should be surrounded by a try, finally block statement to ensure correct behavior in case of exceptions being raised. For example:

     LocalContext.enter();
     try 
         Length.showAs(Unit.FOOT); // Thread-Local setting (no impact on other threads)
         ... 
     } finally {
         LocalContext.exit();
     }
     
     public class Calculator {  
         private PoolContext _pool = new PoolContext();
         public void execute(Runnable logic) {
             PoolContext.enter(_pool); 
             try {
                 logic.run(); // Executes the logic using this calculator pool.
             } finally {
                 PoolContext.exit(_pool); // Recycles used objects all at once.    
             }
         }
     }

Finally, context instances can be serialized/deserialized in xml format. For example, applications may want to load pool contexts at start-up to limit the number of object creation at run-time (and therefore reducing the worst-case execution time).

Version:
3.7, January 1, 2006
Author:
Jean-Marie Dautelle

Constructor Summary
protected Context()
          Default constructor.
 
Method Summary
 void clear()
          Clears this context and releases any associated resource.
static Context current()
          Returns the current context for the current thread.
protected static Context enter(java.lang.Class contextClass)
          Enters a context of specified class.
static void enter(Context context)
          Enters the specified context.
protected abstract  void enterAction()
          The action to be performed after this context becomes the current context.
protected static Context exit(java.lang.Class contextClass)
          Exits the current context which must be of specified type.
static void exit(Context context)
          Exits the specified context.
protected abstract  void exitAction()
          The action to be performed before this context is no more the current context.
 Context getOuter()
          Holds the outer context of this context or null if none (root context).
 java.lang.Thread getOwner()
          Returns the current owner of this 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.

Method Detail

current

public 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

public final java.lang.Thread getOwner()
Returns the current owner of this context. The owner of a context is the thread which entered the context.

Returns:
the thread owner of this context.

getOuter

public 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).

clear

public void clear()
Clears this context and releases any associated resource. Dead threads contexts are automatically cleared before finalization.


enterAction

protected abstract void enterAction()
The action to be performed after this context becomes the current context.


exitAction

protected abstract void exitAction()
The action to be performed before this context is no more the current context.


enter

public static void enter(Context context)
Enters the specified context.

Parameters:
context - the context being entered.
Throws:
java.lang.IllegalStateException - if this context is currently in use.

exit

public static void exit(Context context)
Exits the specified context. The outer context becomes the current context.

Parameters:
context - the context being entered.
Throws:
java.lang.IllegalStateException - if this context is not the current context.

enter

protected static Context enter(java.lang.Class contextClass)
Enters a context of specified class. This method searches the current context for a matching context to reuse (previously used context of same class); if none found a new instance is created using the specified class no-arg constructor from the same memory area as the current context.

Parameters:
contextClass - the type of context to be entered.
Returns:
the context being entered.

exit

protected static Context exit(java.lang.Class contextClass)
Exits the current context which must be of specified type.

Parameters:
contextClass - the type of context to be exited.
Returns:
the context being exited.
Throws:
java.lang.IllegalStateException - if the current context is not an instance of the specified class.

J avolution v3.7 (J2SE 1.5+)

Copyright © 2006 Javolution.