org.springframework.jndi.support
Class SimpleNamingContextBuilder


public class SimpleNamingContextBuilder
implements InitialContextFactoryBuilder

Simple implementation of a JNDI naming context builder.

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 J2EE data access code outside of a J2EE container.

There are various choices for DataSource implementations:

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 it after activation.

Author:
Juergen Hoeller, Rod Johnson
Version: $Id: SimpleNamingContextBuilder.java,v 1.1.1.1 2003/08/14 16:20:35 trisberg Exp $
See Also: bind(java.lang.String,java.lang.Object), activate(), SimpleNamingContext, SingleConnectionDataSource, DriverManagerDataSource

Method Summary
 voidactivate()
          Register the context builder by registering it with the JNDI NamingManager.
 voidbind(String name, Object obj)
          Binds the given object under the given name, for all naming contexts that this context builder will generate.
 voidclear()
          Clears all bindings in this context builder.
 InitialContextFactorycreateInitialContextFactory(Hashtable environment)
          Simple InitialContextFactoryBuilder implementation, creating a new SimpleNamingContext instance.
 static SimpleNamingContextBuilderemptyActivatedContextBuilder()
          If no SimpleNamingContextBuilder is already configuring JNDI, create and activate one.
 static SimpleNamingContextBuildergetCurrentContextBuilder()
          Checks if a SimpleNamingContextBuilder is active.

Method Detail

activate

public void activate()
throws java.lang.IllegalStateException,
javax.naming.NamingException
Register the context builder by registering it with the JNDI NamingManager. Note that once this has been done, 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).
Throws:
IllegalStateException - if there's already a naming context builder registered with the JNDI NamingManager

bind

public void bind(String name, Object obj)
Binds the given object under the given name, for all naming contexts that this context builder will generate.
Parameters:
name - the JNDI name of the object (e.g. "java:comp/env/jdbc/myds")
obj - the object to bind (e.g. a DataSource implementation)

clear

public void clear()
Clears all bindings in this context builder.

createInitialContextFactory

public InitialContextFactory createInitialContextFactory(Hashtable environment)
Simple InitialContextFactoryBuilder implementation, creating a new SimpleNamingContext instance.

emptyActivatedContextBuilder

public static SimpleNamingContextBuilder emptyActivatedContextBuilder()
throws javax.naming.NamingException
If no SimpleNamingContextBuilder is already configuring JNDI, create and activate one. Otherwise take the existing activate SimpleNamingContextBuilder, clear it and return it.

This is mainly intended for test suites that want to reinitialize JNDI bindings from scratch repeatedly.

Returns: an empty SimpleNamingContextBuilder that can be used to control JNDI bindings

getCurrentContextBuilder

public static SimpleNamingContextBuilder getCurrentContextBuilder()
Checks if a SimpleNamingContextBuilder is active.
Returns: the current SimpleNamingContextBuilder instance, or null if none

Association Links

to Class org.springframework.jndi.support.SimpleNamingContextBuilder

Any instance of this class bound to JNDI

to Class java.util.Hashtable