Top Description Fields Constructors Methods
org.mortbay.naming

public Class ContextFactory

extends Object
implements ObjectFactory
Class Inheritance
All Implemented Interfaces
javax.naming.spi.ObjectFactory
Imports
java.util.Hashtable, .WeakHashMap, javax.naming.Context, .Name, .NameParser, .Reference, .StringRefAddr, javax.naming.spi.ObjectFactory, org.mortbay.jetty.handler.ContextHandler, org.mortbay.log.Log

ContextFactory.java This is an object factory that produces a jndi naming context based on a classloader. It is used for the java:comp context. This object factory is bound at java:comp. When a lookup arrives for java:comp, this object factory is invoked and will return a context specific to the caller's environment (so producing the java:comp/env specific to a webapp). The context selected is based on classloaders. First we try looking in at the classloader that is associated with the current webapp context (if there is one). If not, we use the thread context classloader. Created: Fri Jun 27 09:26:40 2003
Author
Jan Bartel

Field Summary

Modifier and TypeField and Description
private static WeakHashMap<K, V>
_contextMap

Map of classloaders to contexts.

private static ThreadLocal<T>
_threadContext

Threadlocal for injecting a context to use instead of looking up the map.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Object
getObjectInstance(Object
The possibly null object containing location or reference information that can be used in creating an object.
obj
,
Name
The name of this object relative to nameCtx, or null if no name is specified.
name
,
Context
The context relative to which the name parameter is specified, or null if name is relative to the default initial context.
nameCtx
,
Hashtable<K, V>
The possibly null environment that is used in creating the object.
env
)

Implements javax.naming.spi.ObjectFactory.getObjectInstance.

Find or create a context which pertains to a classloader.
public Context
getParentClassLoaderContext(ClassLoader loader)

Keep trying ancestors of the given classloader to find one to which the context is bound.

public static void
resetComponentContext(final Context
the context to associate to the current thread.
ctx
)

Set back the context with the given value.

public static Context

Returns:

the previous context associated on the thread (can be null)
setComponentContext
(final Context
the context to associate to the current thread.
ctx
)

Associate the given Context with the current thread.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

_contextMapback to summary
private static WeakHashMap<K, V> _contextMap

Map of classloaders to contexts.

_threadContextback to summary
private static ThreadLocal<T> _threadContext

Threadlocal for injecting a context to use instead of looking up the map.

Constructor Detail

ContextFactoryback to summary
public ContextFactory()

Method Detail

getObjectInstanceback to summary
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<K, V> env) throws Exception

Implements javax.naming.spi.ObjectFactory.getObjectInstance.

Find or create a context which pertains to a classloader. We use either the classloader for the current ContextHandler if we are handling a request, OR we use the thread context classloader if we are not processing a request.

Parameters
obj:Object

Doc from javax.naming.spi.ObjectFactory.getObjectInstance.

The possibly null object containing location or reference information that can be used in creating an object.

name:Name

Doc from javax.naming.spi.ObjectFactory.getObjectInstance.

The name of this object relative to nameCtx, or null if no name is specified.

nameCtx:Context

Doc from javax.naming.spi.ObjectFactory.getObjectInstance.

The context relative to which the name parameter is specified, or null if name is relative to the default initial context.

env:Hashtable<K, V>

Doc from javax.naming.spi.ObjectFactory.getObjectInstance.

The possibly null environment that is used in creating the object.

Returns:Object

Doc from javax.naming.spi.ObjectFactory.getObjectInstance.

The object created; null if an object cannot be created.

Exceptions
Exception:

Doc from javax.naming.spi.ObjectFactory.getObjectInstance.

if this object factory encountered an exception while attempting to create an object, and no other object factories are to be tried.

See Also
javax.naming.spi.ObjectFactory#getObjectInstance(java.lang.Object, javax.naming.Name, javax.naming.Context, java.util.Hashtable)
getParentClassLoaderContextback to summary
public Context getParentClassLoaderContext(ClassLoader loader)

Keep trying ancestors of the given classloader to find one to which the context is bound.

resetComponentContextback to summary
public static void resetComponentContext(final Context ctx)

Set back the context with the given value. Don't return the previous context, use setComponentContext() method for this.

Parameters
ctx:Context

the context to associate to the current thread.

setComponentContextback to summary
public static Context setComponentContext(final Context ctx)

Associate the given Context with the current thread. resetComponentContext method should be called to reset the context.

Parameters
ctx:Context

the context to associate to the current thread.

Returns:Context

the previous context associated on the thread (can be null)