1    //
2    // SwitchRMI  Framework
3    // Copyright (c) 2000-2002 by Michael J. Henderson & Associates.
4    //
5    // Michael Henderson
6    // http://switchrmi.sf.net
7    // mailto:mikehenderson@dunelm.org.uk
8    //
9    // This library is free software.
10   //
11   // You may redistribute it and/or modify it under the terms of the GNU
12   // Lesser General Public License as published by the Free Software Foundation.
13   //
14   // Version 2.1 of the license should be included with this distribution in
15   // the file LICENSE, as well as License.html. If the license is not
16   // included with this distribution, you may find a copy at the FSF web
17   // site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
18   // Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
19   //
20   // This library is distributed in the hope that it will be useful,
21   // but WITHOUT ANY WARRANTY; without even the implied waranty of
22   // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23   // Lesser General Public License for more details.
24   //
25   // $Id: JndiContextImplFactory.java,v 1.1 2002/11/11 22:19:48 mikehenderson Exp $
26   //
27   package com.mjh.switchrmi.jndi;
28   
29   import java.util.*;
30   
31   import javax.naming.*;
32   import javax.naming.spi.*;
33   
34   import org.apache.log4j.*;
35   
36   public class JndiContextImplFactory
37       implements InitialContextFactory
38   {
39       private static final Logger log = 
40               Logger.getLogger(JndiContextImplFactory.class.getName());
41       static Map contextsByName = new HashMap();
42   
43       public JndiContextImplFactory()
44       {
45       }
46   
47       public Context getInitialContext(Hashtable env)
48           throws NamingException
49       {
50           boolean debug = log.isDebugEnabled();
51   
52           if (debug)
53           {
54               log.debug("env = " + env);
55           }
56   
57           JndiContextImpl ctx = null;
58   
59           try
60           {
61               String resourceName = (String) env.get(Context.PROVIDER_URL);
62   
63               if (debug)
64               {
65                   log.debug("From env: resourceName = " + resourceName);
66               }
67   
68               if (resourceName == null)
69               {
70                   resourceName = System.getProperty(Context.PROVIDER_URL);
71   
72                   if (debug)
73                   {
74                       log.debug("From System: resourceName = " + resourceName);
75                   }
76               }
77   
78               String contextName = 
79                       (resourceName == null) ? "default" : resourceName;
80   
81               ctx = (JndiContextImpl) contextsByName.get(contextName);
82   
83               if (ctx == null)
84               {
85                   ctx = new JndiContextImpl();
86   
87                   if (resourceName == null)
88                   {
89                       RmiConfiguration cgf = new RmiConfiguration(ctx);
90                   }
91                   else
92                   {
93                       RmiConfiguration cgf = 
94                               new RmiConfiguration(ctx, resourceName);
95                   }
96   
97                   contextsByName.put(contextName, ctx);
98               }
99   
100              if (debug)
101              {
102                  log.debug("ctx = " + ctx);
103              }
104          }
105          catch (NamingException nex)
106          {
107              throw nex;
108          }
109          catch (Exception ex)
110          {
111              if (debug)
112              {
113                  ex.printStackTrace();
114              }
115  
116              throw new NamingException(ex.toString()); // a better way?
117          }
118  
119          return ctx;
120      }
121  }