Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / javax / naming / InitialContext.java
blob1a9ee5a2738928933e6a063b28377cd825a4d1cd
1 /* InitialContext.java --
2 Copyright (C) 2000, 2002, 2003, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
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.
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. */
39 package javax.naming;
41 import java.applet.Applet;
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.net.URL;
45 import java.util.Enumeration;
46 import java.util.Hashtable;
47 import java.util.Properties;
49 import javax.naming.spi.NamingManager;
51 public class InitialContext implements Context
53 protected Context defaultInitCtx;
54 protected boolean gotDefault = false;
55 protected Hashtable myProps;
57 public InitialContext (Hashtable environment)
58 throws NamingException
60 init (environment);
63 protected InitialContext (boolean lazy)
64 throws NamingException
66 if (! lazy)
67 init (null);
70 public InitialContext ()
71 throws NamingException
73 init (null);
76 /** @since 1.3 */
77 protected void init (Hashtable environment)
78 throws NamingException
80 // FIXME: Is this enough?
81 final String[] properties = {
82 Context.DNS_URL,
83 Context.INITIAL_CONTEXT_FACTORY,
84 Context.OBJECT_FACTORIES,
85 Context.PROVIDER_URL,
86 Context.STATE_FACTORIES,
87 Context.URL_PKG_PREFIXES,
90 // Create myProps, cloning environment if needed.
91 if (environment != null)
92 myProps = (Hashtable) environment.clone ();
93 else
94 myProps = new Hashtable ();
96 Applet napplet = (Applet) myProps.get (Context.APPLET);
98 for (int i = properties.length - 1; i >= 0; i--)
100 Object o = myProps.get (properties[i]);
102 if (o == null)
104 if (napplet != null)
105 o = napplet.getParameter (properties[i]);
106 if (o == null)
107 o = System.getProperty (properties[i]);
108 if (o != null)
109 myProps.put (properties[i], o);
115 Enumeration ep = Thread.currentThread().getContextClassLoader().getResources("jndi.naming");
116 while (ep.hasMoreElements ())
118 URL url = (URL) ep.nextElement ();
119 Properties p = new Properties ();
123 InputStream is = url.openStream ();
124 p.load (is);
125 is.close ();
127 catch (IOException e)
131 merge (myProps, p);
134 catch (IOException e)
138 String home = System.getProperty("gnu.classpath.home.url");
139 if (home != null)
141 String url = home + "/jndi.properties";
142 Properties p = new Properties ();
146 InputStream is = new URL(url).openStream();
147 p.load (is);
148 is.close ();
150 catch (IOException e)
152 // Ignore.
155 merge (myProps, p);
159 // FIXME: Is this enough?
160 private static final String[] colon_list =
162 Context.OBJECT_FACTORIES,
163 Context.URL_PKG_PREFIXES,
164 Context.STATE_FACTORIES
167 private static void merge (Hashtable h1, Hashtable h2)
169 Enumeration e2 = h2.keys();
171 while (e2.hasMoreElements())
173 String key2 = (String) e2.nextElement();
174 Object value1 = h1.get(key2);
175 if (value1 == null)
176 h1.put(key2, h2.get(key2));
177 else if (key2.compareTo(colon_list[0]) == 0
178 || key2.compareTo(colon_list[1]) == 0
179 || key2.compareTo(colon_list[2]) == 0
180 || key2.compareTo(colon_list[3]) == 0)
182 String value2 = (String) h2.get(key2);
183 h1.put(key2, (String) value1 + ":" + value2);
188 protected Context getDefaultInitCtx () throws NamingException
190 if (! gotDefault)
192 defaultInitCtx = NamingManager.getInitialContext (myProps);
193 gotDefault = true;
195 return defaultInitCtx;
199 protected Context getURLOrDefaultInitCtx (Name name)
200 throws NamingException
202 if (name.size () > 0)
203 return getURLOrDefaultInitCtx (name.get (0));
204 else
205 return getDefaultInitCtx ();
208 protected Context getURLOrDefaultInitCtx (String name)
209 throws NamingException
211 String scheme = null;
213 if (NamingManager.hasInitialContextFactoryBuilder())
214 return getDefaultInitCtx();
215 int colon = name.indexOf(':');
216 int slash = name.indexOf('/');
217 if (colon > 0 && (slash == -1 || colon < slash))
218 scheme = name.substring(0, colon);
219 if (scheme != null)
221 Context context =
222 NamingManager.getURLContext(scheme, myProps);
223 if (context != null)
224 return context;
227 return getDefaultInitCtx();
230 public void bind (Name name, Object obj) throws NamingException
232 getURLOrDefaultInitCtx (name).bind (name, obj);
235 public void bind (String name, Object obj) throws NamingException
237 getURLOrDefaultInitCtx (name).bind (name, obj);
240 public Object lookup (Name name) throws NamingException
244 return getURLOrDefaultInitCtx (name).lookup (name);
246 catch (CannotProceedException cpe)
248 Context ctx = NamingManager.getContinuationContext (cpe);
249 return ctx.lookup (cpe.getRemainingName());
253 public Object lookup (String name) throws NamingException
257 return getURLOrDefaultInitCtx (name).lookup (name);
259 catch (CannotProceedException cpe)
261 Context ctx = NamingManager.getContinuationContext (cpe);
262 return ctx.lookup (cpe.getRemainingName());
266 public void rebind (Name name, Object obj) throws NamingException
268 getURLOrDefaultInitCtx (name).rebind (name, obj);
271 public void rebind (String name, Object obj) throws NamingException
273 getURLOrDefaultInitCtx (name).rebind (name, obj);
276 public void unbind (Name name) throws NamingException
278 getURLOrDefaultInitCtx (name).unbind (name);
281 public void unbind (String name) throws NamingException
283 getURLOrDefaultInitCtx (name).unbind (name);
286 public void rename (Name oldName, Name newName) throws NamingException
288 getURLOrDefaultInitCtx (oldName).rename (oldName, newName);
291 public void rename (String oldName, String newName) throws NamingException
293 getURLOrDefaultInitCtx (oldName).rename (oldName, newName);
296 public NamingEnumeration list (Name name) throws NamingException
298 return getURLOrDefaultInitCtx (name).list (name);
301 public NamingEnumeration list (String name) throws NamingException
303 return getURLOrDefaultInitCtx (name).list (name);
306 public NamingEnumeration listBindings (Name name) throws NamingException
308 return getURLOrDefaultInitCtx (name).listBindings (name);
311 public NamingEnumeration listBindings (String name) throws NamingException
313 return getURLOrDefaultInitCtx (name).listBindings (name);
316 public void destroySubcontext (Name name) throws NamingException
318 getURLOrDefaultInitCtx (name).destroySubcontext (name);
321 public void destroySubcontext (String name) throws NamingException
323 getURLOrDefaultInitCtx (name).destroySubcontext (name);
326 public Context createSubcontext (Name name) throws NamingException
328 return getURLOrDefaultInitCtx (name).createSubcontext (name);
331 public Context createSubcontext (String name) throws NamingException
333 return getURLOrDefaultInitCtx (name).createSubcontext (name);
336 public Object lookupLink (Name name) throws NamingException
338 return getURLOrDefaultInitCtx (name).lookupLink (name);
341 public Object lookupLink (String name) throws NamingException
343 return getURLOrDefaultInitCtx (name).lookupLink (name);
346 public NameParser getNameParser (Name name) throws NamingException
348 return getURLOrDefaultInitCtx (name).getNameParser (name);
351 public NameParser getNameParser (String name) throws NamingException
353 return getURLOrDefaultInitCtx (name).getNameParser (name);
356 public Name composeName (Name name, Name prefix) throws NamingException
358 return getURLOrDefaultInitCtx (name).composeName (name, prefix);
361 public String composeName (String name,
362 String prefix) throws NamingException
364 return getURLOrDefaultInitCtx (name).composeName (name, prefix);
367 public Object addToEnvironment (String propName,
368 Object propVal) throws NamingException
370 return myProps.put (propName, propVal);
373 public Object removeFromEnvironment (String propName) throws NamingException
375 return myProps.remove (propName);
378 public Hashtable getEnvironment () throws NamingException
380 return myProps;
383 public void close () throws NamingException
385 myProps = null;
386 defaultInitCtx = null;
389 public String getNameInNamespace () throws NamingException
391 throw new OperationNotSupportedException ();