1 /* InitialContext.java --
2    Copyright (C) 2000, 2002, 2003 Free Software Foundation, Inc.
3 
4 This file is part of GNU Classpath.
5 
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10 
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
20 
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25 
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37 
38 
39 package javax.naming;
40 
41 import java.applet.Applet;
42 import java.io.File;
43 import java.io.FileInputStream;
44 import java.io.IOException;
45 import java.io.InputStream;
46 import java.net.URL;
47 import java.util.Enumeration;
48 import java.util.Hashtable;
49 import java.util.Properties;
50 import javax.naming.spi.NamingManager;
51 
52 public class InitialContext implements Context
53 {
54   protected Context defaultInitCtx;
55   protected boolean gotDefault = false;
56   protected Hashtable myProps;
57 
InitialContext(Hashtable environment)58   public InitialContext (Hashtable environment)
59     throws NamingException
60   {
61     init (environment);
62   }
63 
InitialContext(boolean lazy)64   protected InitialContext (boolean lazy)
65     throws NamingException
66   {
67     if (! lazy)
68       init (null);
69   }
70 
InitialContext()71   public InitialContext ()
72     throws NamingException
73   {
74     init (null);
75   }
76 
77   /** @since 1.3 */
init(Hashtable environment)78   protected void init (Hashtable environment)
79     throws NamingException
80   {
81     // FIXME: Is this enough?
82     final String[] properties = {
83       Context.DNS_URL,
84       Context.INITIAL_CONTEXT_FACTORY,
85       Context.OBJECT_FACTORIES,
86       Context.PROVIDER_URL,
87       Context.STATE_FACTORIES,
88       Context.URL_PKG_PREFIXES,
89     };
90 
91     // Create myProps, cloning environment if needed.
92     if (environment != null)
93       myProps = (Hashtable) environment.clone ();
94     else
95       myProps = new Hashtable ();
96 
97     Applet napplet = (Applet) myProps.get (Context.APPLET);
98 
99     for (int i = properties.length - 1; i >= 0; i--)
100       {
101 	Object o = myProps.get (properties[i]);
102 
103 	if (o == null)
104 	  {
105 	    if (napplet != null)
106 	      o = napplet.getParameter (properties[i]);
107 	    if (o == null)
108 	      o = System.getProperty (properties[i]);
109 	    if (o != null)
110 	      myProps.put (properties[i], o);
111 	  }
112       }
113 
114     try
115       {
116 	Enumeration ep = Thread.currentThread().getContextClassLoader().getResources("jndi.naming");
117 	while (ep.hasMoreElements ())
118 	  {
119 	    URL url = (URL) ep.nextElement ();
120 	    Properties p = new Properties ();
121 
122 	    try
123 	      {
124 		InputStream is = url.openStream ();
125 		p.load (is);
126 		is.close ();
127 	      }
128 	    catch (IOException e)
129 	      {
130 	      }
131 
132 	    merge (myProps, p);
133 	  }
134       }
135     catch (IOException e)
136       {
137       }
138 
139     String home = System.getProperty("gnu.classpath.home.url");
140     if (home != null)
141       {
142 	String url = home + "/jndi.properties";
143 	Properties p = new Properties ();
144 
145 	try
146 	  {
147 	    InputStream is = new URL(url).openStream();
148 	    p.load (is);
149 	    is.close ();
150 	  }
151 	catch (IOException e)
152 	  {
153 	    // Ignore.
154 	  }
155 
156 	merge (myProps, p);
157       }
158   }
159 
160   // FIXME: Is this enough?
161   private static final String[] colon_list =
162     {
163       Context.OBJECT_FACTORIES,
164       Context.URL_PKG_PREFIXES,
165       Context.STATE_FACTORIES
166     };
167 
merge(Hashtable h1, Hashtable h2)168   private static void merge (Hashtable h1, Hashtable h2)
169   {
170     Enumeration e2 = h2.keys();
171 
172     while (e2.hasMoreElements())
173       {
174 	String key2 = (String) e2.nextElement();
175 	Object value1 = h1.get(key2);
176 	if (value1 == null)
177 	  h1.put(key2, h2.get(key2));
178 	else if (key2.compareTo(colon_list[0]) == 0
179 		 || key2.compareTo(colon_list[1]) == 0
180 		 || key2.compareTo(colon_list[2]) == 0
181 		 || key2.compareTo(colon_list[3]) == 0)
182 	  {
183 	    String value2 = (String) h2.get(key2);
184 	    h1.put(key2, (String) value1 + ":" + value2);
185 	  }
186       }
187   }
188 
getDefaultInitCtx()189   protected Context getDefaultInitCtx () throws NamingException
190   {
191     if (! gotDefault)
192       {
193 	defaultInitCtx = NamingManager.getInitialContext (myProps);
194 	gotDefault = true;
195       }
196     return defaultInitCtx;
197   }
198 
199 
getURLOrDefaultInitCtx(Name name)200   protected Context getURLOrDefaultInitCtx (Name name)
201     throws NamingException
202   {
203     if (name.size () > 0)
204       return getURLOrDefaultInitCtx (name.get (0));
205     else
206       return getDefaultInitCtx ();
207   }
208 
getURLOrDefaultInitCtx(String name)209   protected Context getURLOrDefaultInitCtx (String name)
210     throws NamingException
211   {
212     String scheme = null;
213 
214     if (NamingManager.hasInitialContextFactoryBuilder())
215       return getDefaultInitCtx();
216     int colon = name.indexOf(':');
217     int slash = name.indexOf('/');
218     if (colon > 0 && (slash == -1 || colon < slash))
219       scheme = name.substring(0, colon);
220     if (scheme != null)
221       {
222 	Context context =
223 	  NamingManager.getURLContext(scheme, myProps);
224 	if (context != null)
225 	  return context;
226       }
227 
228     return getDefaultInitCtx();
229   }
230 
bind(Name name, Object obj)231   public void bind (Name name, Object obj) throws NamingException
232   {
233     getURLOrDefaultInitCtx (name).bind (name, obj);
234   }
235 
bind(String name, Object obj)236   public void bind (String name, Object obj) throws NamingException
237   {
238     getURLOrDefaultInitCtx (name).bind (name, obj);
239   }
240 
lookup(Name name)241   public Object lookup (Name name) throws NamingException
242   {
243     try
244       {
245 	return getURLOrDefaultInitCtx (name).lookup (name);
246       }
247     catch (CannotProceedException cpe)
248       {
249 	Context ctx = NamingManager.getContinuationContext (cpe);
250 	return ctx.lookup (cpe.getRemainingName());
251       }
252   }
253 
lookup(String name)254   public Object lookup (String name) throws NamingException
255   {
256       try
257 	{
258 	  return getURLOrDefaultInitCtx (name).lookup (name);
259 	}
260       catch (CannotProceedException cpe)
261 	{
262 	  Context ctx = NamingManager.getContinuationContext (cpe);
263 	  return ctx.lookup (cpe.getRemainingName());
264 	}
265   }
266 
rebind(Name name, Object obj)267   public void rebind (Name name, Object obj) throws NamingException
268   {
269     getURLOrDefaultInitCtx (name).rebind (name, obj);
270   }
271 
rebind(String name, Object obj)272   public void rebind (String name, Object obj) throws NamingException
273   {
274     getURLOrDefaultInitCtx (name).rebind (name, obj);
275   }
276 
unbind(Name name)277   public void unbind (Name name) throws NamingException
278   {
279     getURLOrDefaultInitCtx (name).unbind (name);
280   }
281 
unbind(String name)282   public void unbind (String name) throws NamingException
283   {
284     getURLOrDefaultInitCtx (name).unbind (name);
285   }
286 
rename(Name oldName, Name newName)287   public void rename (Name oldName, Name newName) throws NamingException
288   {
289     getURLOrDefaultInitCtx (oldName).rename (oldName, newName);
290   }
291 
rename(String oldName, String newName)292   public void rename (String oldName, String newName) throws NamingException
293   {
294     getURLOrDefaultInitCtx (oldName).rename (oldName, newName);
295   }
296 
list(Name name)297   public NamingEnumeration list (Name name) throws NamingException
298   {
299     return getURLOrDefaultInitCtx (name).list (name);
300   }
301 
list(String name)302   public NamingEnumeration list (String name) throws NamingException
303   {
304     return getURLOrDefaultInitCtx (name).list (name);
305   }
306 
listBindings(Name name)307   public NamingEnumeration listBindings (Name name) throws NamingException
308   {
309     return getURLOrDefaultInitCtx (name).listBindings (name);
310   }
311 
listBindings(String name)312   public NamingEnumeration listBindings (String name) throws NamingException
313   {
314     return getURLOrDefaultInitCtx (name).listBindings (name);
315   }
316 
destroySubcontext(Name name)317   public void destroySubcontext (Name name) throws NamingException
318   {
319     getURLOrDefaultInitCtx (name).destroySubcontext (name);
320   }
321 
destroySubcontext(String name)322   public void destroySubcontext (String name) throws NamingException
323   {
324     getURLOrDefaultInitCtx (name).destroySubcontext (name);
325   }
326 
createSubcontext(Name name)327   public Context createSubcontext (Name name) throws NamingException
328   {
329     return getURLOrDefaultInitCtx (name).createSubcontext (name);
330   }
331 
createSubcontext(String name)332   public Context createSubcontext (String name) throws NamingException
333   {
334     return getURLOrDefaultInitCtx (name).createSubcontext (name);
335   }
336 
lookupLink(Name name)337   public Object lookupLink (Name name) throws NamingException
338   {
339     return getURLOrDefaultInitCtx (name).lookupLink (name);
340   }
341 
lookupLink(String name)342   public Object lookupLink (String name) throws NamingException
343   {
344     return getURLOrDefaultInitCtx (name).lookupLink (name);
345   }
346 
getNameParser(Name name)347   public NameParser getNameParser (Name name) throws NamingException
348   {
349     return getURLOrDefaultInitCtx (name).getNameParser (name);
350   }
351 
getNameParser(String name)352   public NameParser getNameParser (String name) throws NamingException
353   {
354     return getURLOrDefaultInitCtx (name).getNameParser (name);
355   }
356 
composeName(Name name, Name prefix)357   public Name composeName (Name name, Name prefix) throws NamingException
358   {
359     return getURLOrDefaultInitCtx (name).composeName (name, prefix);
360   }
361 
composeName(String name, String prefix)362   public String composeName (String name,
363 			     String prefix) throws NamingException
364   {
365     return getURLOrDefaultInitCtx (name).composeName (name, prefix);
366   }
367 
addToEnvironment(String propName, Object propVal)368   public Object addToEnvironment (String propName,
369 				  Object propVal) throws NamingException
370   {
371     return myProps.put (propName, propVal);
372   }
373 
removeFromEnvironment(String propName)374   public Object removeFromEnvironment (String propName) throws NamingException
375   {
376     return myProps.remove (propName);
377   }
378 
getEnvironment()379   public Hashtable getEnvironment () throws NamingException
380   {
381     return myProps;
382   }
383 
close()384   public void close () throws NamingException
385   {
386     myProps = null;
387     defaultInitCtx = null;
388   }
389 
getNameInNamespace()390   public String getNameInNamespace () throws NamingException
391   {
392     throw new OperationNotSupportedException ();
393   }
394 }
395