org.mockejb.jndi
Class MockContextFactory

java.lang.Object
  extended byorg.mockejb.jndi.MockContextFactory
All Implemented Interfaces:
javax.naming.spi.InitialContextFactory

public class MockContextFactory
extends java.lang.Object
implements javax.naming.spi.InitialContextFactory

Creates MockContext. In case if delegate environment was provided, obtains delegate InitialContext. Delegate context is used by MockContext for unresolved lookups.

Author:
Alexander Ananiev, Dimitar Gospodinov

Constructor Summary
MockContextFactory()
           
 
Method Summary
 javax.naming.Context getInitialContext(java.util.Hashtable environment)
          Singleton for initial context.
static void revertSetAsInitial()
          Restores the properties changed by setAsInitial() This method should be called in tearDown() to clean up all changes to the environment in case if the test is running in the app server.
static void setAsInitial()
          Sets the MockContextFactory as the initial context factory.
static void setDelegateContext(javax.naming.Context ctx)
          Sets the delegate context.
static void setDelegateEnvironment(java.util.Hashtable env)
          Sets the environment of the delegate JNDI context.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MockContextFactory

public MockContextFactory()
Method Detail

getInitialContext

public javax.naming.Context getInitialContext(java.util.Hashtable environment)
                                       throws javax.naming.NamingException
Singleton for initial context. Instantiates and returns root/initial MockContext object that will be used as starting point for all naming operations. MockContext is then used by javax.naming.InitialContext object. It also creates the delegate context if the delegate environment is set. MockContextFactory caches the delegate context once it's created.

Specified by:
getInitialContext in interface javax.naming.spi.InitialContextFactory
Returns:
MockContext object
Throws:
javax.naming.NamingException
See Also:
InitialContextFactory.getInitialContext(java.util.Hashtable)

setDelegateEnvironment

public static void setDelegateEnvironment(java.util.Hashtable env)
Sets the environment of the delegate JNDI context. Normally, this is the environment of the application server. At the very minimum, the environment includes PROVIDER_URL and INITIAL_CONTEXT_FACTORY. MockContext first tries to look up the object in its local tree. If the object is not found, it will look in the delegate context.

Parameters:
env - JNDI properties of the delegate environment

setDelegateContext

public static void setDelegateContext(javax.naming.Context ctx)
Sets the delegate context. Normally, this is the initial context of the application server. MockContext first tries to look up the object in its local tree. If the object is not found, it will look in the delegate context. Example: MockContextFactory.setDelegateContext( new InitialContext() );

Parameters:
ctx - delegate context

setAsInitial

public static void setAsInitial()
                         throws javax.naming.NamingException
Sets the MockContextFactory as the initial context factory. This helper method sets the Context.INITIAL_CONTEXT_FACTORY and Context.URL_PKG_PREFIXES system properties. The second one is needed to be able to handle java:comp context correctly. The method also saves the current values of these properties so they can be restored later on using revertSetAsInitial. This method is normally called from setUp

You can also set these properties directly:

 
  java.naming.factory.initial=org.mockejb.jndi.MockContextFactory
  java.naming.factory.url.pkgs=org.mockejb.jndi
 
 

Throws:
javax.naming.NamingException

revertSetAsInitial

public static void revertSetAsInitial()
Restores the properties changed by setAsInitial() This method should be called in tearDown() to clean up all changes to the environment in case if the test is running in the app server.

This method also cleans the initial context.