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)
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
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
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. */
41 import java
.applet
.Applet
;
42 import java
.io
.IOException
;
43 import java
.io
.InputStream
;
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
63 protected InitialContext (boolean lazy
)
64 throws NamingException
70 public InitialContext ()
71 throws NamingException
77 protected void init (Hashtable environment
)
78 throws NamingException
80 // FIXME: Is this enough?
81 final String
[] properties
= {
83 Context
.INITIAL_CONTEXT_FACTORY
,
84 Context
.OBJECT_FACTORIES
,
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 ();
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
]);
105 o
= napplet
.getParameter (properties
[i
]);
107 o
= System
.getProperty (properties
[i
]);
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 ();
127 catch (IOException e
)
134 catch (IOException e
)
138 String home
= System
.getProperty("gnu.classpath.home.url");
141 String url
= home
+ "/jndi.properties";
142 Properties p
= new Properties ();
146 InputStream is
= new URL(url
).openStream();
150 catch (IOException e
)
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
);
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
192 defaultInitCtx
= NamingManager
.getInitialContext (myProps
);
195 return defaultInitCtx
;
199 protected Context
getURLOrDefaultInitCtx (Name name
)
200 throws NamingException
202 if (name
.size () > 0)
203 return getURLOrDefaultInitCtx (name
.get (0));
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
);
222 NamingManager
.getURLContext(scheme
, myProps
);
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
383 public void close () throws NamingException
386 defaultInitCtx
= null;
389 public String
getNameInNamespace () throws NamingException
391 throw new OperationNotSupportedException ();