Nuxeo ECM Projects 5.4.3-SNAPSHOT

org.nuxeo.common.jndi
Class NamingContextFactory

java.lang.Object
  extended by org.nuxeo.common.jndi.NamingContextFactory
All Implemented Interfaces:
InitialContextFactory

public class NamingContextFactory
extends Object
implements InitialContextFactory

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

Author:
Alexander Ananiev, Dimitar Gospodinov

Constructor Summary
NamingContextFactory()
           
 
Method Summary
 Context getInitialContext(Hashtable<?,?> environment)
          Singleton for initial context.
static void install()
          Same as setAsInitial() but it use strong types (avoiding reflection) to install the naming context factory.
static void revertSetAsInitial()
          Restores the properties changed by setAsInitial().
static void setAsInitial()
          Sets the NamingContextFactory as the initial context factory.
static void setDelegateContext(Context ctx)
          Sets the delegate context.
static void setDelegateEnvironment(Hashtable<?,?> env)
          Sets the environment of the delegate JNDI context.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NamingContextFactory

public NamingContextFactory()
Method Detail

getInitialContext

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

Specified by:
getInitialContext in interface InitialContextFactory
Returns:
NamingContext object
Throws:
NamingException
See Also:
InitialContextFactory.getInitialContext(java.util.Hashtable)

setDelegateEnvironment

public static void setDelegateEnvironment(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. NamingContext 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(Context ctx)
Sets the delegate context. Normally, this is the initial context of the application server. NamingContext 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: NamingContextFactory.setDelegateContext(new InitialContext());

Parameters:
ctx - delegate context

setAsInitial

public static void setAsInitial()
Sets the NamingContextFactory 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.nuxeo.common.jndi.NamingContextFactory
  java.naming.factory.url.pkgs=org.nuxeo.common.jndi
 
 

Throws:
NamingException

install

public static void install()
                    throws NamingException
Same as setAsInitial() but it use strong types (avoiding reflection) to install the naming context factory.

This is preferable in frameworks that doesn't work well with the current thread context class loader like OSGi.

Throws:
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.


Nuxeo ECM Projects 5.4.3-SNAPSHOT

Copyright © 2011 Nuxeo SAS. All Rights Reserved.