1 | /* InitialContext.java --
|
---|
2 | Copyright (C) 2000, 2002 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.io.File;
|
---|
42 | import java.io.FileInputStream;
|
---|
43 | import java.io.IOException;
|
---|
44 | import java.io.InputStream;
|
---|
45 | import java.net.URL;
|
---|
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;
|
---|
52 |
|
---|
53 | public class InitialContext implements Context
|
---|
54 | {
|
---|
55 | protected Context defaultInitCtx;
|
---|
56 | protected boolean gotDefault = false;
|
---|
57 | protected Hashtable myProps;
|
---|
58 |
|
---|
59 | public InitialContext (Hashtable environment)
|
---|
60 | {
|
---|
61 | init (environment);
|
---|
62 | }
|
---|
63 |
|
---|
64 | protected InitialContext (boolean lazy)
|
---|
65 | {
|
---|
66 | if (! lazy)
|
---|
67 | init (null);
|
---|
68 | }
|
---|
69 |
|
---|
70 | public InitialContext ()
|
---|
71 | {
|
---|
72 | init (null);
|
---|
73 | }
|
---|
74 |
|
---|
75 | protected void init (Hashtable environment)
|
---|
76 | {
|
---|
77 | // FIXME: Is this enough?
|
---|
78 | final String[] properties = {
|
---|
79 | Context.DNS_URL,
|
---|
80 | Context.INITIAL_CONTEXT_FACTORY,
|
---|
81 | Context.OBJECT_FACTORIES,
|
---|
82 | Context.PROVIDER_URL,
|
---|
83 | Context.STATE_FACTORIES,
|
---|
84 | Context.URL_PKG_PREFIXES,
|
---|
85 | };
|
---|
86 |
|
---|
87 | // Create myProps, cloning environment if needed.
|
---|
88 | if (environment != null)
|
---|
89 | myProps = (Hashtable) environment.clone ();
|
---|
90 | else
|
---|
91 | myProps = new Hashtable ();
|
---|
92 |
|
---|
93 | Applet napplet = (Applet) myProps.get (Context.APPLET);
|
---|
94 |
|
---|
95 | for (int i = properties.length - 1; i >= 0; i--)
|
---|
96 | {
|
---|
97 | Object o = myProps.get (properties[i]);
|
---|
98 |
|
---|
99 | if (o == null)
|
---|
100 | {
|
---|
101 | if (napplet != null)
|
---|
102 | o = napplet.getParameter (properties[i]);
|
---|
103 | if (o == null)
|
---|
104 | o = System.getProperty (properties[i]);
|
---|
105 | if (o != null)
|
---|
106 | myProps.put (properties[i], o);
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | try
|
---|
111 | {
|
---|
112 | Enumeration ep = Thread.currentThread().getContextClassLoader().getResources("jndi.naming");
|
---|
113 | while (ep.hasMoreElements ())
|
---|
114 | {
|
---|
115 | URL url = (URL) ep.nextElement ();
|
---|
116 | Properties p = new Properties ();
|
---|
117 |
|
---|
118 | try {
|
---|
119 | InputStream is = url.openStream ();
|
---|
120 | p.load (is);
|
---|
121 | is.close ();
|
---|
122 | } catch (IOException e) {}
|
---|
123 |
|
---|
124 | merge (myProps, p);
|
---|
125 | }
|
---|
126 | }
|
---|
127 | catch (IOException e) {}
|
---|
128 |
|
---|
129 | String home = System.getProperty("gnu.classpath.home.url");
|
---|
130 | if (home != null)
|
---|
131 | {
|
---|
132 | String url = home + "/jndi.properties";
|
---|
133 | Properties p = new Properties ();
|
---|
134 |
|
---|
135 | try
|
---|
136 | {
|
---|
137 | InputStream is = new URL(url).openStream();
|
---|
138 | p.load (is);
|
---|
139 | is.close ();
|
---|
140 | }
|
---|
141 | catch (IOException e)
|
---|
142 | {
|
---|
143 | // Ignore.
|
---|
144 | }
|
---|
145 |
|
---|
146 | merge (myProps, p);
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | // FIXME: Is this enough?
|
---|
151 | private static final String[] colon_list =
|
---|
152 | {
|
---|
153 | Context.OBJECT_FACTORIES,
|
---|
154 | Context.URL_PKG_PREFIXES,
|
---|
155 | Context.STATE_FACTORIES
|
---|
156 | };
|
---|
157 |
|
---|
158 | private static void merge (Hashtable h1, Hashtable h2)
|
---|
159 | {
|
---|
160 | Enumeration e2 = h2.keys();
|
---|
161 |
|
---|
162 | while (e2.hasMoreElements())
|
---|
163 | {
|
---|
164 | String key2 = (String) e2.nextElement();
|
---|
165 | Object value1 = h1.get(key2);
|
---|
166 | if (value1 == null)
|
---|
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)
|
---|
172 | {
|
---|
173 | String value2 = (String) h2.get(key2);
|
---|
174 | h1.put(key2, (String) value1 + ":" + value2);
|
---|
175 | }
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | protected Context getDefaultInitCtx () throws NamingException
|
---|
180 | {
|
---|
181 | if (! gotDefault)
|
---|
182 | {
|
---|
183 | defaultInitCtx = NamingManager.getInitialContext (myProps);
|
---|
184 | gotDefault = true;
|
---|
185 | }
|
---|
186 | return defaultInitCtx;
|
---|
187 | }
|
---|
188 |
|
---|
189 |
|
---|
190 | protected Context getURLOrDefaultInitCtx (Name name)
|
---|
191 | throws NamingException
|
---|
192 | {
|
---|
193 | if (name.size () > 0)
|
---|
194 | return getURLOrDefaultInitCtx (name.get (0));
|
---|
195 | else
|
---|
196 | return getDefaultInitCtx ();
|
---|
197 | }
|
---|
198 |
|
---|
199 | protected Context getURLOrDefaultInitCtx (String name)
|
---|
200 | throws NamingException
|
---|
201 | {
|
---|
202 | String scheme = null;
|
---|
203 |
|
---|
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);
|
---|
210 | if (scheme != null)
|
---|
211 | {
|
---|
212 | Context context =
|
---|
213 | NamingManager.getURLContext(scheme, myProps);
|
---|
214 | if (context != null)
|
---|
215 | return context;
|
---|
216 | }
|
---|
217 |
|
---|
218 | return getDefaultInitCtx();
|
---|
219 | }
|
---|
220 |
|
---|
221 | public void bind (Name name, Object obj) throws NamingException
|
---|
222 | {
|
---|
223 | getURLOrDefaultInitCtx (name).bind (name, obj);
|
---|
224 | }
|
---|
225 |
|
---|
226 | public void bind (String name, Object obj) throws NamingException
|
---|
227 | {
|
---|
228 | getURLOrDefaultInitCtx (name).bind (name, obj);
|
---|
229 | }
|
---|
230 |
|
---|
231 | public Object lookup (Name name) throws NamingException
|
---|
232 | {
|
---|
233 | return getURLOrDefaultInitCtx (name).lookup (name);
|
---|
234 | }
|
---|
235 |
|
---|
236 | public Object lookup (String name) throws NamingException
|
---|
237 | {
|
---|
238 | return getURLOrDefaultInitCtx (name).lookup (name);
|
---|
239 | }
|
---|
240 |
|
---|
241 | public void rebind (Name name, Object obj) throws NamingException
|
---|
242 | {
|
---|
243 | getURLOrDefaultInitCtx (name).rebind (name, obj);
|
---|
244 | }
|
---|
245 |
|
---|
246 | public void rebind (String name, Object obj) throws NamingException
|
---|
247 | {
|
---|
248 | getURLOrDefaultInitCtx (name).rebind (name, obj);
|
---|
249 | }
|
---|
250 |
|
---|
251 | public void unbind (Name name) throws NamingException
|
---|
252 | {
|
---|
253 | getURLOrDefaultInitCtx (name).unbind (name);
|
---|
254 | }
|
---|
255 |
|
---|
256 | public void unbind (String name) throws NamingException
|
---|
257 | {
|
---|
258 | getURLOrDefaultInitCtx (name).unbind (name);
|
---|
259 | }
|
---|
260 |
|
---|
261 | public void rename (Name oldName, Name newName) throws NamingException
|
---|
262 | {
|
---|
263 | getURLOrDefaultInitCtx (oldName).rename (oldName, newName);
|
---|
264 | }
|
---|
265 |
|
---|
266 | public void rename (String oldName, String newName) throws NamingException
|
---|
267 | {
|
---|
268 | getURLOrDefaultInitCtx (oldName).rename (oldName, newName);
|
---|
269 | }
|
---|
270 |
|
---|
271 | public NamingEnumeration list (Name name) throws NamingException
|
---|
272 | {
|
---|
273 | return getURLOrDefaultInitCtx (name).list (name);
|
---|
274 | }
|
---|
275 |
|
---|
276 | public NamingEnumeration list (String name) throws NamingException
|
---|
277 | {
|
---|
278 | return getURLOrDefaultInitCtx (name).list (name);
|
---|
279 | }
|
---|
280 |
|
---|
281 | public NamingEnumeration listBindings (Name name) throws NamingException
|
---|
282 | {
|
---|
283 | return getURLOrDefaultInitCtx (name).listBindings (name);
|
---|
284 | }
|
---|
285 |
|
---|
286 | public NamingEnumeration listBindings (String name) throws NamingException
|
---|
287 | {
|
---|
288 | return getURLOrDefaultInitCtx (name).listBindings (name);
|
---|
289 | }
|
---|
290 |
|
---|
291 | public void destroySubcontext (Name name) throws NamingException
|
---|
292 | {
|
---|
293 | getURLOrDefaultInitCtx (name).destroySubcontext (name);
|
---|
294 | }
|
---|
295 |
|
---|
296 | public void destroySubcontext (String name) throws NamingException
|
---|
297 | {
|
---|
298 | getURLOrDefaultInitCtx (name).destroySubcontext (name);
|
---|
299 | }
|
---|
300 |
|
---|
301 | public Context createSubcontext (Name name) throws NamingException
|
---|
302 | {
|
---|
303 | return getURLOrDefaultInitCtx (name).createSubcontext (name);
|
---|
304 | }
|
---|
305 |
|
---|
306 | public Context createSubcontext (String name) throws NamingException
|
---|
307 | {
|
---|
308 | return getURLOrDefaultInitCtx (name).createSubcontext (name);
|
---|
309 | }
|
---|
310 |
|
---|
311 | public Object lookupLink (Name name) throws NamingException
|
---|
312 | {
|
---|
313 | return getURLOrDefaultInitCtx (name).lookupLink (name);
|
---|
314 | }
|
---|
315 |
|
---|
316 | public Object lookupLink (String name) throws NamingException
|
---|
317 | {
|
---|
318 | return getURLOrDefaultInitCtx (name).lookupLink (name);
|
---|
319 | }
|
---|
320 |
|
---|
321 | public NameParser getNameParser (Name name) throws NamingException
|
---|
322 | {
|
---|
323 | return getURLOrDefaultInitCtx (name).getNameParser (name);
|
---|
324 | }
|
---|
325 |
|
---|
326 | public NameParser getNameParser (String name) throws NamingException
|
---|
327 | {
|
---|
328 | return getURLOrDefaultInitCtx (name).getNameParser (name);
|
---|
329 | }
|
---|
330 |
|
---|
331 | public Name composeName (Name name, Name prefix) throws NamingException
|
---|
332 | {
|
---|
333 | return getURLOrDefaultInitCtx (name).composeName (name, prefix);
|
---|
334 | }
|
---|
335 |
|
---|
336 | public String composeName (String name,
|
---|
337 | String prefix) throws NamingException
|
---|
338 | {
|
---|
339 | return getURLOrDefaultInitCtx (name).composeName (name, prefix);
|
---|
340 | }
|
---|
341 |
|
---|
342 | public Object addToEnvironment (String propName,
|
---|
343 | Object propVal) throws NamingException
|
---|
344 | {
|
---|
345 | return myProps.put (propName, propVal);
|
---|
346 | }
|
---|
347 |
|
---|
348 | public Object removeFromEnvironment (String propName) throws NamingException
|
---|
349 | {
|
---|
350 | return myProps.remove (propName);
|
---|
351 | }
|
---|
352 |
|
---|
353 | public Hashtable getEnvironment () throws NamingException
|
---|
354 | {
|
---|
355 | return myProps;
|
---|
356 | }
|
---|
357 |
|
---|
358 | public void close () throws NamingException
|
---|
359 | {
|
---|
360 | throw new OperationNotSupportedException ();
|
---|
361 | }
|
---|
362 |
|
---|
363 | public String getNameInNamespace () throws NamingException
|
---|
364 | {
|
---|
365 | throw new OperationNotSupportedException ();
|
---|
366 | }
|
---|
367 | }
|
---|