1 /* InitialContext.java --
2 Copyright (C) 2000, 2002 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. */
42 import java
.io
.FileInputStream
;
43 import java
.io
.IOException
;
44 import java
.io
.InputStream
;
46 import java
.util
.Enumeration
;
47 import java
.util
.Hashtable
;
48 import java
.util
.Properties
;
49 import java
.applet
.Applet
;
50 import java
.util
.Hashtable
;
51 import javax
.naming
.spi
.NamingManager
;
53 public class InitialContext
implements Context
55 protected Context defaultInitCtx
;
56 protected boolean gotDefault
= false;
57 protected Hashtable myProps
;
59 public InitialContext (Hashtable environment
)
64 protected InitialContext (boolean lazy
)
70 public InitialContext ()
75 protected void init (Hashtable environment
)
77 // FIXME: Is this enough?
78 final String
[] properties
= {
80 Context
.INITIAL_CONTEXT_FACTORY
,
81 Context
.OBJECT_FACTORIES
,
83 Context
.STATE_FACTORIES
,
84 Context
.URL_PKG_PREFIXES
,
87 // Create myProps, cloning environment if needed.
88 if (environment
!= null)
89 myProps
= (Hashtable
) environment
.clone ();
91 myProps
= new Hashtable ();
93 Applet napplet
= (Applet
) myProps
.get (Context
.APPLET
);
95 for (int i
= properties
.length
- 1; i
>= 0; i
--)
97 Object o
= myProps
.get (properties
[i
]);
102 o
= napplet
.getParameter (properties
[i
]);
104 o
= System
.getProperty (properties
[i
]);
106 myProps
.put (properties
[i
], o
);
112 Enumeration ep
= Thread
.currentThread().getContextClassLoader().getResources("jndi.naming");
113 while (ep
.hasMoreElements ())
115 URL url
= (URL
) ep
.nextElement ();
116 Properties p
= new Properties ();
119 InputStream is
= url
.openStream ();
122 } catch (IOException e
) {}
127 catch (IOException e
) {}
129 String home
= System
.getProperty("gnu.classpath.home.url");
132 String url
= home
+ "/jndi.properties";
133 Properties p
= new Properties ();
137 InputStream is
= new URL(url
).openStream();
141 catch (IOException e
)
150 // FIXME: Is this enough?
151 private static final String
[] colon_list
=
153 Context
.OBJECT_FACTORIES
,
154 Context
.URL_PKG_PREFIXES
,
155 Context
.STATE_FACTORIES
158 private static void merge (Hashtable h1
, Hashtable h2
)
160 Enumeration e2
= h2
.keys();
162 while (e2
.hasMoreElements())
164 String key2
= (String
) e2
.nextElement();
165 Object value1
= h1
.get(key2
);
167 h1
.put(key2
, h2
.get(key2
));
168 else if (key2
.compareTo(colon_list
[0]) == 0
169 || key2
.compareTo(colon_list
[1]) == 0
170 || key2
.compareTo(colon_list
[2]) == 0
171 || key2
.compareTo(colon_list
[3]) == 0)
173 String value2
= (String
) h2
.get(key2
);
174 h1
.put(key2
, (String
) value1
+ ":" + value2
);
179 protected Context
getDefaultInitCtx () throws NamingException
183 defaultInitCtx
= NamingManager
.getInitialContext (myProps
);
186 return defaultInitCtx
;
190 protected Context
getURLOrDefaultInitCtx (Name name
)
191 throws NamingException
193 if (name
.size () > 0)
194 return getURLOrDefaultInitCtx (name
.get (0));
196 return getDefaultInitCtx ();
199 protected Context
getURLOrDefaultInitCtx (String name
)
200 throws NamingException
202 String scheme
= null;
204 if (NamingManager
.hasInitialContextFactoryBuilder())
205 return getDefaultInitCtx();
206 int colon
= name
.indexOf(':');
207 int slash
= name
.indexOf('/');
208 if (colon
> 0 && (slash
== -1 || colon
< slash
))
209 scheme
= name
.substring(0, colon
);
213 NamingManager
.getURLContext(scheme
, myProps
);
218 return getDefaultInitCtx();
221 public void bind (Name name
, Object obj
) throws NamingException
223 getURLOrDefaultInitCtx (name
).bind (name
, obj
);
226 public void bind (String name
, Object obj
) throws NamingException
228 getURLOrDefaultInitCtx (name
).bind (name
, obj
);
231 public Object
lookup (Name name
) throws NamingException
233 return getURLOrDefaultInitCtx (name
).lookup (name
);
236 public Object
lookup (String name
) throws NamingException
238 return getURLOrDefaultInitCtx (name
).lookup (name
);
241 public void rebind (Name name
, Object obj
) throws NamingException
243 getURLOrDefaultInitCtx (name
).rebind (name
, obj
);
246 public void rebind (String name
, Object obj
) throws NamingException
248 getURLOrDefaultInitCtx (name
).rebind (name
, obj
);
251 public void unbind (Name name
) throws NamingException
253 getURLOrDefaultInitCtx (name
).unbind (name
);
256 public void unbind (String name
) throws NamingException
258 getURLOrDefaultInitCtx (name
).unbind (name
);
261 public void rename (Name oldName
, Name newName
) throws NamingException
263 getURLOrDefaultInitCtx (oldName
).rename (oldName
, newName
);
266 public void rename (String oldName
, String newName
) throws NamingException
268 getURLOrDefaultInitCtx (oldName
).rename (oldName
, newName
);
271 public NamingEnumeration
list (Name name
) throws NamingException
273 return getURLOrDefaultInitCtx (name
).list (name
);
276 public NamingEnumeration
list (String name
) throws NamingException
278 return getURLOrDefaultInitCtx (name
).list (name
);
281 public NamingEnumeration
listBindings (Name name
) throws NamingException
283 return getURLOrDefaultInitCtx (name
).listBindings (name
);
286 public NamingEnumeration
listBindings (String name
) throws NamingException
288 return getURLOrDefaultInitCtx (name
).listBindings (name
);
291 public void destroySubcontext (Name name
) throws NamingException
293 getURLOrDefaultInitCtx (name
).destroySubcontext (name
);
296 public void destroySubcontext (String name
) throws NamingException
298 getURLOrDefaultInitCtx (name
).destroySubcontext (name
);
301 public Context
createSubcontext (Name name
) throws NamingException
303 return getURLOrDefaultInitCtx (name
).createSubcontext (name
);
306 public Context
createSubcontext (String name
) throws NamingException
308 return getURLOrDefaultInitCtx (name
).createSubcontext (name
);
311 public Object
lookupLink (Name name
) throws NamingException
313 return getURLOrDefaultInitCtx (name
).lookupLink (name
);
316 public Object
lookupLink (String name
) throws NamingException
318 return getURLOrDefaultInitCtx (name
).lookupLink (name
);
321 public NameParser
getNameParser (Name name
) throws NamingException
323 return getURLOrDefaultInitCtx (name
).getNameParser (name
);
326 public NameParser
getNameParser (String name
) throws NamingException
328 return getURLOrDefaultInitCtx (name
).getNameParser (name
);
331 public Name
composeName (Name name
, Name prefix
) throws NamingException
333 return getURLOrDefaultInitCtx (name
).composeName (name
, prefix
);
336 public String
composeName (String name
,
337 String prefix
) throws NamingException
339 return getURLOrDefaultInitCtx (name
).composeName (name
, prefix
);
342 public Object
addToEnvironment (String propName
,
343 Object propVal
) throws NamingException
345 return myProps
.put (propName
, propVal
);
348 public Object
removeFromEnvironment (String propName
) throws NamingException
350 return myProps
.remove (propName
);
353 public Hashtable
getEnvironment () throws NamingException
358 public void close () throws NamingException
360 throw new OperationNotSupportedException ();
363 public String
getNameInNamespace () throws NamingException
365 throw new OperationNotSupportedException ();