J avolution v5.2 (J2SE 1.5+)

javolution.context
Class Context

java.lang.Object
  extended by javolution.context.Context
All Implemented Interfaces:
java.io.Serializable, XMLSerializable
Direct Known Subclasses:
AllocatorContext, ConcurrentContext, LocalContext, LogContext, PersistentContext, SecurityContext

public abstract class Context
extends java.lang.Object
implements XMLSerializable

This class represents an execution context; they can be associated to particular threads or objects.

Context-aware applications may extend the context base class or any predefined contexts in order to facilitate separation of concerns.

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(); // Current thread enter a local context.
     try 
         ModuloInteger.setModulus(m); // No impact on other threads!
         z = x.times(y); // Multiplication modulo m.
     } finally {
         LocalContext.exit();
     }

Version:
5.0, April 15, 2007
Author:
Jean-Marie Dautelle
See Also:
Serialized Form

Field Summary
static Context ROOT
          Holds the root context (top context of all threads).
 
Constructor Summary
protected Context()
          Default constructor.
 
Method Summary
static
<T extends Context>
T
enter(java.lang.Class<T> contextType)
          Enters a factory produced context of specified type to be recycled after exiting.
static
<T extends Context>
T
enter(T context)
          Enters the specified context.
protected abstract  void enterAction()
          The action to be performed after this context becomes the current context.
static Context exit()
          Exits the current context (the outer context becomes the current context).
static void exit(Context ctx)
          Deprecated. exit() should be used.
protected abstract  void exitAction()
          The action to be performed before this context is no more the current context.
static Context getCurrent()
          Returns the current context for the current thread.
 Context getOuter()
          Returns the outer context of this context or null if ROOT.
 java.lang.Thread getOwner()
          Returns the current owner of this context.
protected static void setCurrent(ConcurrentContext context)
          Sets the current context, used by ConcurrentContext exclusively.
 java.lang.String toString()
          Returns the string representation of this context (default "Instance of " + this.getClass().getName()).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ROOT

public static final Context ROOT
Holds the root context (top context of all threads).

Constructor Detail

Context

protected Context()
Default constructor.

Method Detail

getCurrent

public static Context getCurrent()
Returns the current context for the current thread.

Returns:
the current context.

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 and has not yet exited. A context can only have one owner at any given time, although contexts can be shared by concurrent threads.

Returns:
the thread owner of this context or null.

getOuter

public final Context getOuter()
Returns the outer context of this context or null if ROOT.

Returns:
the outer context or null.

toString

public java.lang.String toString()
Returns the string representation of this context (default "Instance of " + this.getClass().getName()).

Overrides:
toString in class java.lang.Object
Returns:
the string representation of this context.

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 final <T extends Context> T enter(T context)
Enters the specified context.

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

enter

public static final <T extends Context> T enter(java.lang.Class<T> contextType)
Enters a factory produced context of specified type to be recycled after exiting. This method is called by the static enter() method of specialized contexts. If the context class has no public no-arg constructor accessible, then the factory for the class should be explicitely set (typically in a static initializer).

Parameters:
contextType - the type of context being entered.
Returns:
the context being entered.
See Also:
ObjectFactory.getInstance(Class)

exit

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

Returns:
the context which has been exited.
Throws:
java.lang.IllegalStateException - if this context is the ROOT context.

setCurrent

protected static void setCurrent(ConcurrentContext context)
Sets the current context, used by ConcurrentContext exclusively.

Parameters:
context - the concurrent context.

exit

public static final void exit(Context ctx)
Deprecated. exit() should be used.


J avolution v5.2 (J2SE 1.5+)

Copyright © 2005 - 2007 Javolution.