public class SimpleNamingContextBuilder
extends java.lang.Object
implements javax.naming.spi.InitialContextFactoryBuilder
Mainly targeted at test environments, where each test case can
configure JNDI appropriately, so that new InitialContext()
will expose the required objects. Also usable for standalone applications,
e.g. for binding a JDBC DataSource to a well-known JNDI location, to be
able to use traditional J2EE data access code outside of a J2EE container.
There are various choices for DataSource implementations:
SingleConnectionDataSource
(using the same Connection for all getConnection calls)
DriverManagerDataSource
(creating a new Connection on each getConnection call)
org.apache.commons.dbcp.BasicDataSource
(a real pool)
Typical usage in bootstrap code:
SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder(); DataSource ds = new DriverManagerDataSource(...); builder.bind("java:comp/env/jdbc/myds", ds); builder.activate();Note that it's impossible to activate multiple builders within the same JVM, due to JNDI restrictions. Thus to configure a fresh builder repeatedly, use the following code to get a reference to either an already activated builder or a newly activated one:
SimpleNamingContextBuilder builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder(); DataSource ds = new DriverManagerDataSource(...); builder.bind("java:comp/env/jdbc/myds", ds);Note that you should not call
activate()
on a builder from
this factory method, as there will already be an activated one in any case.
An instance of this class is only necessary at setup time. An application does not need to keep a reference to it after activation.
emptyActivatedContextBuilder()
,
bind(String, Object)
,
activate()
,
SimpleNamingContext
,
SingleConnectionDataSource
,
DriverManagerDataSource
Constructor and Description |
---|
SimpleNamingContextBuilder() |
Modifier and Type | Method and Description |
---|---|
void |
activate()
Register the context builder by registering it with the JNDI NamingManager.
|
void |
bind(java.lang.String name,
java.lang.Object obj)
Bind the given object under the given name, for all naming contexts
that this context builder will generate.
|
void |
clear()
Clear all bindings in this context builder, while keeping it active.
|
javax.naming.spi.InitialContextFactory |
createInitialContextFactory(java.util.Hashtable<?,?> environment)
Simple InitialContextFactoryBuilder implementation,
creating a new SimpleNamingContext instance.
|
void |
deactivate()
Temporarily deactivate this context builder.
|
static SimpleNamingContextBuilder |
emptyActivatedContextBuilder()
If no SimpleNamingContextBuilder is already configuring JNDI,
create and activate one.
|
static SimpleNamingContextBuilder |
getCurrentContextBuilder()
Checks if a SimpleNamingContextBuilder is active.
|
public static SimpleNamingContextBuilder getCurrentContextBuilder()
null
if nonepublic static SimpleNamingContextBuilder emptyActivatedContextBuilder() throws javax.naming.NamingException
This is mainly intended for test suites that want to reinitialize JNDI bindings from scratch repeatedly.
javax.naming.NamingException
public void activate() throws java.lang.IllegalStateException, javax.naming.NamingException
new InitialContext()
will always
return a context from this factory. Use the emptyActivatedContextBuilder()
static method to get an empty context (for example, in test methods).java.lang.IllegalStateException
- if there's already a naming context builder
registered with the JNDI NamingManagerjavax.naming.NamingException
public void deactivate()
Call activate()
again in order to expose this context builder's own
bound objects again. Such activate/deactivate sequences can be applied any number
of times (e.g. within a larger integration test suite running in the same VM).
activate()
public void clear()
public void bind(java.lang.String name, java.lang.Object obj)
name
- the JNDI name of the object (e.g. "java:comp/env/jdbc/myds")obj
- the object to bind (e.g. a DataSource implementation)public javax.naming.spi.InitialContextFactory createInitialContextFactory(java.util.Hashtable<?,?> environment)
createInitialContextFactory
in interface javax.naming.spi.InitialContextFactoryBuilder
SimpleNamingContext