1 /* InitialContext.java --
2 Copyright (C) 2000, 2002, 2003 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., 59 Temple Place, Suite 330, 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
;
43 import java
.io
.FileInputStream
;
44 import java
.io
.IOException
;
45 import java
.io
.InputStream
;
47 import java
.util
.Enumeration
;
48 import java
.util
.Hashtable
;
49 import java
.util
.Properties
;
50 import javax
.naming
.spi
.NamingManager
;
52 public class InitialContext
implements Context
54 protected Context defaultInitCtx
;
55 protected boolean gotDefault
= false;
56 protected Hashtable myProps
;
58 public InitialContext (Hashtable environment
)
59 throws NamingException
64 protected InitialContext (boolean lazy
)
65 throws NamingException
71 public InitialContext ()
72 throws NamingException
78 protected void init (Hashtable environment
)
79 throws NamingException
81 // FIXME: Is this enough?
82 final String
[] properties
= {
84 Context
.INITIAL_CONTEXT_FACTORY
,
85 Context
.OBJECT_FACTORIES
,
87 Context
.STATE_FACTORIES
,
88 Context
.URL_PKG_PREFIXES
,
91 // Create myProps, cloning environment if needed.
92 if (environment
!= null)
93 myProps
= (Hashtable
) environment
.clone ();
95 myProps
= new Hashtable ();
97 Applet napplet
= (Applet
) myProps
.get (Context
.APPLET
);
99 for (int i
= properties
.length
- 1; i
>= 0; i
--)
101 Object o
= myProps
.get (properties
[i
]);
106 o
= napplet
.getParameter (properties
[i
]);
108 o
= System
.getProperty (properties
[i
]);
110 myProps
.put (properties
[i
], o
);
116 Enumeration ep
= Thread
.currentThread().getContextClassLoader().getResources("jndi.naming");
117 while (ep
.hasMoreElements ())
119 URL url
= (URL
) ep
.nextElement ();
120 Properties p
= new Properties ();
124 InputStream is
= url
.openStream ();
128 catch (IOException e
)
135 catch (IOException e
)
139 String home
= System
.getProperty("gnu.classpath.home.url");
142 String url
= home
+ "/jndi.properties";
143 Properties p
= new Properties ();
147 InputStream is
= new URL(url
).openStream();
151 catch (IOException e
)
160 // FIXME: Is this enough?
161 private static final String
[] colon_list
=
163 Context
.OBJECT_FACTORIES
,
164 Context
.URL_PKG_PREFIXES
,
165 Context
.STATE_FACTORIES
168 private static void merge (Hashtable h1
, Hashtable h2
)
170 Enumeration e2
= h2
.keys();
172 while (e2
.hasMoreElements())
174 String key2
= (String
) e2
.nextElement();
175 Object value1
= h1
.get(key2
);
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)
183 String value2
= (String
) h2
.get(key2
);
184 h1
.put(key2
, (String
) value1
+ ":" + value2
);
189 protected Context
getDefaultInitCtx () throws NamingException
193 defaultInitCtx
= NamingManager
.getInitialContext (myProps
);
196 return defaultInitCtx
;
200 protected Context
getURLOrDefaultInitCtx (Name name
)
201 throws NamingException
203 if (name
.size () > 0)
204 return getURLOrDefaultInitCtx (name
.get (0));
206 return getDefaultInitCtx ();
209 protected Context
getURLOrDefaultInitCtx (String name
)
210 throws NamingException
212 String scheme
= null;
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
);
223 NamingManager
.getURLContext(scheme
, myProps
);
228 return getDefaultInitCtx();
231 public void bind (Name name
, Object obj
) throws NamingException
233 getURLOrDefaultInitCtx (name
).bind (name
, obj
);
236 public void bind (String name
, Object obj
) throws NamingException
238 getURLOrDefaultInitCtx (name
).bind (name
, obj
);
241 public Object
lookup (Name name
) throws NamingException
245 return getURLOrDefaultInitCtx (name
).lookup (name
);
247 catch (CannotProceedException cpe
)
249 Context ctx
= NamingManager
.getContinuationContext (cpe
);
250 return ctx
.lookup (cpe
.getRemainingName());
254 public Object
lookup (String name
) throws NamingException
258 return getURLOrDefaultInitCtx (name
).lookup (name
);
260 catch (CannotProceedException cpe
)
262 Context ctx
= NamingManager
.getContinuationContext (cpe
);
263 return ctx
.lookup (cpe
.getRemainingName());
267 public void rebind (Name name
, Object obj
) throws NamingException
269 getURLOrDefaultInitCtx (name
).rebind (name
, obj
);
272 public void rebind (String name
, Object obj
) throws NamingException
274 getURLOrDefaultInitCtx (name
).rebind (name
, obj
);
277 public void unbind (Name name
) throws NamingException
279 getURLOrDefaultInitCtx (name
).unbind (name
);
282 public void unbind (String name
) throws NamingException
284 getURLOrDefaultInitCtx (name
).unbind (name
);
287 public void rename (Name oldName
, Name newName
) throws NamingException
289 getURLOrDefaultInitCtx (oldName
).rename (oldName
, newName
);
292 public void rename (String oldName
, String newName
) throws NamingException
294 getURLOrDefaultInitCtx (oldName
).rename (oldName
, newName
);
297 public NamingEnumeration
list (Name name
) throws NamingException
299 return getURLOrDefaultInitCtx (name
).list (name
);
302 public NamingEnumeration
list (String name
) throws NamingException
304 return getURLOrDefaultInitCtx (name
).list (name
);
307 public NamingEnumeration
listBindings (Name name
) throws NamingException
309 return getURLOrDefaultInitCtx (name
).listBindings (name
);
312 public NamingEnumeration
listBindings (String name
) throws NamingException
314 return getURLOrDefaultInitCtx (name
).listBindings (name
);
317 public void destroySubcontext (Name name
) throws NamingException
319 getURLOrDefaultInitCtx (name
).destroySubcontext (name
);
322 public void destroySubcontext (String name
) throws NamingException
324 getURLOrDefaultInitCtx (name
).destroySubcontext (name
);
327 public Context
createSubcontext (Name name
) throws NamingException
329 return getURLOrDefaultInitCtx (name
).createSubcontext (name
);
332 public Context
createSubcontext (String name
) throws NamingException
334 return getURLOrDefaultInitCtx (name
).createSubcontext (name
);
337 public Object
lookupLink (Name name
) throws NamingException
339 return getURLOrDefaultInitCtx (name
).lookupLink (name
);
342 public Object
lookupLink (String name
) throws NamingException
344 return getURLOrDefaultInitCtx (name
).lookupLink (name
);
347 public NameParser
getNameParser (Name name
) throws NamingException
349 return getURLOrDefaultInitCtx (name
).getNameParser (name
);
352 public NameParser
getNameParser (String name
) throws NamingException
354 return getURLOrDefaultInitCtx (name
).getNameParser (name
);
357 public Name
composeName (Name name
, Name prefix
) throws NamingException
359 return getURLOrDefaultInitCtx (name
).composeName (name
, prefix
);
362 public String
composeName (String name
,
363 String prefix
) throws NamingException
365 return getURLOrDefaultInitCtx (name
).composeName (name
, prefix
);
368 public Object
addToEnvironment (String propName
,
369 Object propVal
) throws NamingException
371 return myProps
.put (propName
, propVal
);
374 public Object
removeFromEnvironment (String propName
) throws NamingException
376 return myProps
.remove (propName
);
379 public Hashtable
getEnvironment () throws NamingException
384 public void close () throws NamingException
387 defaultInitCtx
= null;
390 public String
getNameInNamespace () throws NamingException
392 throw new OperationNotSupportedException ();