public class SingletonInitialContextFactory extends Object implements InitialContextFactory
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 "
" property:
java.naming.factory.initial
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();
Constructor and Description |
---|
SingletonInitialContextFactory() |
Modifier and Type | Method and Description |
---|---|
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 . |
public Context getInitialContext(Hashtable<?,?> environment)
getInitialContext
in interface InitialContextFactory
public static Context getInstance(Hashtable<?,?> environment)
Context
singleton instance. If no such context has been configured, this method will configured the
singletone using the supplied environment.environment
- the environment for the naming context; may be null or emptygetInitialContext(Hashtable)
,
getInstance(Hashtable)
public static Context getInstance()
Context
singleton instance. If no such context has been configured, this method
throws a SystemFailureException
.SystemFailureException
- if the singleton context has not yet been configured.getInitialContext(Hashtable)
,
getInstance(Hashtable)
public static void initialize()
Context.INITIAL_CONTEXT_FACTORY
system property to the name of this context's
factory class
.public static void tearDown()
naming context
. Any subsequent calls to getInstance(Hashtable)
or
getInitialContext(Hashtable)
will return a new singleton instance.Copyright © 2008–2016 JBoss, a division of Red Hat. All rights reserved.