KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jms > p2p > naming > InitialContextFactoryImpl


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.jms.p2p.naming;
8
9 import java.util.Hashtable JavaDoc;
10 import java.util.Map JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Iterator JavaDoc;
13
14 import javax.naming.spi.InitialContextFactory JavaDoc;
15 import javax.naming.Context JavaDoc;
16 import javax.naming.NamingException JavaDoc;
17
18 /**
19  * Simple {@link InitialContextFactory} implementation to enable using the JMS/JBoss Pure P2P.
20  *
21  * @author <a HREF="mailto:nathan@jboss.org">Nathan Phelps</a>
22  * @version $Revision: 1.1 $ $Date: 2003/06/30 06:15:37 $
23  */

24 public class InitialContextFactoryImpl implements InitialContextFactory JavaDoc
25 {
26     private static final Map JavaDoc INITIAL_CONTEXT_MAP = new HashMap JavaDoc();
27
28     public synchronized Context JavaDoc getInitialContext(Hashtable JavaDoc environment) throws NamingException JavaDoc
29     {
30         try
31         {
32             Integer JavaDoc key = new Integer JavaDoc(environment.hashCode());
33             if (!INITIAL_CONTEXT_MAP.containsKey(key))
34             {
35                 Context JavaDoc context = new ContextImpl(environment);
36                 INITIAL_CONTEXT_MAP.put(key, context);
37                 return context;
38             }
39             else
40             {
41                 return (Context JavaDoc) INITIAL_CONTEXT_MAP.get(key);
42             }
43         }
44         catch (Exception JavaDoc exception)
45         {
46             throw new NamingException JavaDoc(exception.getMessage());
47         }
48     }
49
50     static synchronized void unregisterInitialContext(Context JavaDoc context)
51     {
52         if (INITIAL_CONTEXT_MAP.containsValue(context))
53         {
54             Iterator JavaDoc iterator = INITIAL_CONTEXT_MAP.keySet().iterator();
55             while (iterator.hasNext())
56             {
57                 Object JavaDoc key = iterator.next();
58                 if (INITIAL_CONTEXT_MAP.get(key) == context)
59                 {
60                     INITIAL_CONTEXT_MAP.remove(key);
61                 }
62             }
63         }
64     }
65 }
66
Popular Tags