org.modeshape.common.naming
Class SingletonInitialContextFactory

java.lang.Object
  extended by org.modeshape.common.naming.SingletonInitialContextFactory
All Implemented Interfaces:
InitialContextFactory

public class SingletonInitialContextFactory
extends Object
implements InitialContextFactory

An InitialContextFactory that provides a singleton JNDI naming context. Because it is a singleton, it is useful in unit tests that looks up objects where it can be used within service registration logic while also being easily accessible in the test case itself.

For example, the following code sample shows how this InitialContextFactory implementation can be specified via the standard " java.naming.factory.initial" property:

 Hashtable<String, Object> jndiContext = new Hashtable<String, Object>();
 jndiContext.put("java.naming.factory.initial", "org.modeshape.common.naming.SingleonInitialContextFactory");
 jndiContext.put("java.naming.provider.url", "localhost");
 InitialContext initialContext = new InitialContext(jndiContext);
 
while the following sample shows how the same Context instance will be subsequently returned from accessed within other code (e.g., a test case):
 Context context = SingletoneInitialContextFactory.create();
 

Author:
Luca Stancapiano, Randall Hauch

Constructor Summary
SingletonInitialContextFactory()
          No-arg constructor
 
Method Summary
 Context getInitialContext(Hashtable<?,?> environment)
          
static Context getInstance()
          Return the previously-configured Context singleton instance.
static Context getInstance(Hashtable<?,?> environment)
          Return the Context singleton instance.
static void initialize()
          Set the Context.INITIAL_CONTEXT_FACTORY system property to the name of this context's factory class.
static void tearDown()
          Release any existing singleton naming context.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SingletonInitialContextFactory

public SingletonInitialContextFactory()
No-arg constructor

Method Detail

getInitialContext

public Context getInitialContext(Hashtable<?,?> environment)

Specified by:
getInitialContext in interface InitialContextFactory

getInstance

public static Context getInstance(Hashtable<?,?> environment)
Return the Context singleton instance. If no such context has been configured, this method will configured the singletone using the supplied environment.

Parameters:
environment - the environment for the naming context; may be null or empty
Returns:
the singleton context; never null
See Also:
getInitialContext(Hashtable), getInstance(Hashtable)

getInstance

public static Context getInstance()
Return the previously-configured Context singleton instance. If no such context has been configured, this method throws a SystemFailureException.

Returns:
the singleton context; never null
Throws:
SystemFailureException - if the singleton context has not yet been configured.
See Also:
getInitialContext(Hashtable), getInstance(Hashtable)

initialize

public static void initialize()
Set the Context.INITIAL_CONTEXT_FACTORY system property to the name of this context's factory class.


tearDown

public static void tearDown()
Release any existing singleton naming context. Any subsequent calls to getInstance(Hashtable) or getInitialContext(Hashtable) will return a new singleton instance.



Copyright © 2008-2011 JBoss, a division of Red Hat. All Rights Reserved.