1   /*
2    * Copyright (C) The Spice Group. All rights reserved.
3    *
4    * This software is published under the terms of the Spice
5    * Software License version 1.1, a copy of which has been included
6    * with this distribution in the LICENSE.txt file.
7    */
8   
9   package org.apache.commons.configuration;
10  
11  import java.util.Hashtable;
12  
13  import javax.naming.Context;
14  import javax.naming.NamingException;
15  import javax.naming.spi.InitialContextFactory;
16  
17  import org.codehaus.spice.jndikit.DefaultNameParser;
18  import org.codehaus.spice.jndikit.DefaultNamespace;
19  import org.codehaus.spice.jndikit.memory.MemoryContext;
20  
21  /***
22   * Initial context factory for memorycontext. This factory will
23   * retrieve the {@link MemoryContext} from a static variable.
24   * Thus this factory will always return the same instance of
25   * memory context.
26   *
27   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
28   * @version $Revision$
29   */
30  public class MockStaticMemoryInitialContextFactory
31      implements InitialContextFactory
32  {
33      private static final MemoryContext MEMORY_CONTEXT = createMemoryContext();
34  
35      public Context getInitialContext(final Hashtable environment)
36          throws NamingException
37      {
38          return MEMORY_CONTEXT;
39      }
40  
41      /***
42       * Method to create the inital {@link MemoryContext}.
43       *
44       * @return the new {@link MemoryContext}.
45       */
46      private static final MemoryContext createMemoryContext()
47      {
48          final DefaultNamespace namespace =
49              new DefaultNamespace(new DefaultNameParser());
50          MemoryContext me = new MemoryContext(namespace, new Hashtable(), null);
51          
52          try
53          {
54              Context testContext = me.createSubcontext("test");
55              testContext.bind("key", "jndivalue");
56              testContext.bind("key2","jndivalue2");
57              testContext.bind("short","1");
58              testContext.bind("boolean","true");
59              testContext.bind("byte","10");
60              testContext.bind("double","10.25");
61              testContext.bind("float","20.25");
62              testContext.bind("integer","10");
63              testContext.bind("long","1000000");
64              testContext.bind("onlyinjndi","true");
65          }
66          catch (NamingException ne)
67          {
68              throw new RuntimeException(ne.getMessage());
69          }
70          return me;
71      }
72  }