1 package org.wamblee.support.jndi;
3 import java.util.HashMap;
4 import java.util.Hashtable;
7 import javax.naming.Context;
8 import javax.naming.InitialContext;
9 import javax.naming.NamingException;
10 import javax.naming.spi.InitialContextFactory;
13 * Test initial context factory used for testing software in a Java SE
14 * environnment that uses JNDI to retrieve objects.
16 * See {@link #bind(String, Object)} to resp. register the initial context.
18 * To bind objects in the JNDI tree simply use the standard JNDI api: <code>
19 * InitialContext context = new InitialContext();
20 * MyClass myObj = ...;
21 * context.bind("a/b", myObj);
24 public class StubInitialContextFactory implements InitialContextFactory {
26 private static Context context;
28 private static void initialize() {
30 context = new StubInitialContext();
31 } catch (NamingException e) { // can't happen.
32 throw new RuntimeException(e);
37 * This method must be called to register this initial context factory as
38 * the default implementation for JNDI.
42 public static void register() {
43 // sets up the InitialContextFactoryForTest as default factory.
44 System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
45 StubInitialContextFactory.class.getName());
46 if (context == null) {
52 * Unregisters the initial context factory
54 public static void unregister() {
55 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "");
59 public Context getInitialContext(Hashtable<?, ?> environment)
60 throws NamingException {