1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49:
50: import ;
51:
52:
57: public class InitialContext implements Context
58: {
59:
65: protected Context defaultInitCtx;
66:
67:
71: protected boolean gotDefault = false;
72:
73:
76: protected Hashtable myProps;
77:
78:
83: static final HashSet colon_list;
84: static
85: {
86: colon_list = new HashSet();
87: colon_list.add(Context.OBJECT_FACTORIES);
88: colon_list.add(Context.URL_PKG_PREFIXES);
89: colon_list.add(Context.STATE_FACTORIES);
90: };
91:
92:
96: static final String[] use_properties =
97: {
98: Context.DNS_URL,
99: Context.INITIAL_CONTEXT_FACTORY,
100: Context.OBJECT_FACTORIES,
101: Context.PROVIDER_URL,
102: Context.STATE_FACTORIES,
103: Context.URL_PKG_PREFIXES,
104: };
105:
106:
107:
114: public InitialContext(Hashtable environment) throws NamingException
115: {
116: init(environment);
117: }
118:
119:
128: protected InitialContext(boolean lazy) throws NamingException
129: {
130: if (! lazy)
131: init(null);
132: }
133:
134:
140: public InitialContext() throws NamingException
141: {
142: init(null);
143: }
144:
145:
169: protected void init(Hashtable environment) throws NamingException
170: {
171:
172: if (environment != null)
173: myProps = environment;
174: else
175: myProps = new Hashtable();
176:
177: Applet napplet = (Applet) myProps.get(Context.APPLET);
178:
179: Properties pApplet = null;
180: if (napplet != null)
181: pApplet = new Properties();
182: Properties pSystem = new Properties();
183: Object value;
184:
185: for (int i = use_properties.length - 1; i >= 0; i--)
186: {
187: String key = use_properties[i];
188: if (napplet != null)
189: {
190: value = napplet.getParameter(key);
191: if (value != null)
192: pApplet.put(key, value);
193: }
194:
195: value = System.getProperty(key);
196: if (value != null)
197: pSystem.put(key, value);
198: }
199:
200: merge(myProps, pSystem);
201: if (pApplet != null)
202: merge(myProps, pApplet);
203:
204: try
205: {
206: Enumeration ep = Thread.currentThread().
207: getContextClassLoader().getResources("jndi.properties");
208: while (ep.hasMoreElements())
209: {
210: URL url = (URL) ep.nextElement();
211: Properties p = new Properties();
212:
213: try
214: {
215: InputStream is = url.openStream();
216: p.load(is);
217: is.close();
218: }
219: catch (IOException e)
220: {
221:
222: }
223:
224: merge(myProps, p);
225: }
226: }
227: catch (IOException e)
228: {
229:
230: }
231:
232: String home = System.getProperty("gnu.classpath.home.url");
233: if (home != null)
234: {
235: String url = home + "/jndi.properties";
236: Properties p = new Properties();
237:
238: try
239: {
240: InputStream is = new URL(url).openStream();
241: p.load(is);
242: is.close();
243: }
244: catch (IOException e)
245: {
246:
247: }
248:
249: merge(myProps, p);
250: }
251: }
252:
253:
265: static void merge (Hashtable primary, Hashtable additional)
266: {
267: Enumeration en = additional.keys();
268:
269: while (en.hasMoreElements())
270: {
271: String key2 = (String) en.nextElement();
272: Object value1 = primary.get(key2);
273: if (value1 == null)
274: primary.put(key2, additional.get(key2));
275: else if (colon_list.contains(key2))
276: {
277: String value2 = (String) additional.get(key2);
278: primary.put(key2, (String) value1 + ":" + value2);
279: }
280: }
281: }
282:
283:
292: protected Context getDefaultInitCtx() throws NamingException
293: {
294: if (! gotDefault)
295: {
296: defaultInitCtx = NamingManager.getInitialContext(myProps);
297: gotDefault = true;
298: }
299: return defaultInitCtx;
300: }
301:
302:
312: protected Context getURLOrDefaultInitCtx(Name name) throws NamingException
313: {
314: if (name.size() > 0)
315: return getURLOrDefaultInitCtx(name.get(0));
316: else
317: return getDefaultInitCtx();
318: }
319:
320:
330: protected Context getURLOrDefaultInitCtx(String name) throws NamingException
331: {
332: String scheme = null;
333:
334: if (NamingManager.hasInitialContextFactoryBuilder())
335: return getDefaultInitCtx();
336: int colon = name.indexOf(':');
337: int slash = name.indexOf('/');
338: if (colon > 0 && (slash == - 1 || colon < slash))
339: scheme = name.substring(0, colon);
340: if (scheme != null)
341: {
342: Context context = NamingManager.getURLContext(scheme, myProps);
343: if (context != null)
344: return context;
345: }
346:
347: return getDefaultInitCtx();
348: }
349:
350:
351: public void bind (Name name, Object obj) throws NamingException
352: {
353: getURLOrDefaultInitCtx (name).bind (name, obj);
354: }
355:
356:
357: public void bind (String name, Object obj) throws NamingException
358: {
359: getURLOrDefaultInitCtx (name).bind (name, obj);
360: }
361:
362:
363: public Object lookup (Name name) throws NamingException
364: {
365: try
366: {
367: return getURLOrDefaultInitCtx (name).lookup (name);
368: }
369: catch (CannotProceedException cpe)
370: {
371: Context ctx = NamingManager.getContinuationContext (cpe);
372: return ctx.lookup (cpe.getRemainingName());
373: }
374: }
375:
376:
377: public Object lookup (String name) throws NamingException
378: {
379: try
380: {
381: return getURLOrDefaultInitCtx (name).lookup (name);
382: }
383: catch (CannotProceedException cpe)
384: {
385: Context ctx = NamingManager.getContinuationContext (cpe);
386: return ctx.lookup (cpe.getRemainingName());
387: }
388: }
389:
390:
391: public void rebind (Name name, Object obj) throws NamingException
392: {
393: getURLOrDefaultInitCtx (name).rebind (name, obj);
394: }
395:
396:
397: public void rebind (String name, Object obj) throws NamingException
398: {
399: getURLOrDefaultInitCtx (name).rebind (name, obj);
400: }
401:
402:
403: public void unbind (Name name) throws NamingException
404: {
405: getURLOrDefaultInitCtx (name).unbind (name);
406: }
407:
408:
409: public void unbind (String name) throws NamingException
410: {
411: getURLOrDefaultInitCtx (name).unbind (name);
412: }
413:
414:
415: public void rename (Name oldName, Name newName) throws NamingException
416: {
417: getURLOrDefaultInitCtx (oldName).rename (oldName, newName);
418: }
419:
420:
421: public void rename (String oldName, String newName) throws NamingException
422: {
423: getURLOrDefaultInitCtx (oldName).rename (oldName, newName);
424: }
425:
426:
427: public NamingEnumeration list (Name name) throws NamingException
428: {
429: return getURLOrDefaultInitCtx (name).list (name);
430: }
431:
432:
433: public NamingEnumeration list (String name) throws NamingException
434: {
435: return getURLOrDefaultInitCtx (name).list (name);
436: }
437:
438:
439: public NamingEnumeration listBindings (Name name) throws NamingException
440: {
441: return getURLOrDefaultInitCtx (name).listBindings (name);
442: }
443:
444:
445: public NamingEnumeration listBindings (String name) throws NamingException
446: {
447: return getURLOrDefaultInitCtx (name).listBindings (name);
448: }
449:
450:
451: public void destroySubcontext (Name name) throws NamingException
452: {
453: getURLOrDefaultInitCtx (name).destroySubcontext (name);
454: }
455:
456:
457: public void destroySubcontext (String name) throws NamingException
458: {
459: getURLOrDefaultInitCtx (name).destroySubcontext (name);
460: }
461:
462:
463: public Context createSubcontext (Name name) throws NamingException
464: {
465: return getURLOrDefaultInitCtx (name).createSubcontext (name);
466: }
467:
468:
469: public Context createSubcontext (String name) throws NamingException
470: {
471: return getURLOrDefaultInitCtx (name).createSubcontext (name);
472: }
473:
474:
475: public Object lookupLink (Name name) throws NamingException
476: {
477: return getURLOrDefaultInitCtx (name).lookupLink (name);
478: }
479:
480:
481: public Object lookupLink (String name) throws NamingException
482: {
483: return getURLOrDefaultInitCtx (name).lookupLink (name);
484: }
485:
486:
487: public NameParser getNameParser (Name name) throws NamingException
488: {
489: return getURLOrDefaultInitCtx (name).getNameParser (name);
490: }
491:
492:
493: public NameParser getNameParser (String name) throws NamingException
494: {
495: return getURLOrDefaultInitCtx (name).getNameParser (name);
496: }
497:
498:
499: public Name composeName (Name name, Name prefix) throws NamingException
500: {
501: return getURLOrDefaultInitCtx (name).composeName (name, prefix);
502: }
503:
504:
505: public String composeName (String name,
506: String prefix) throws NamingException
507: {
508: return getURLOrDefaultInitCtx (name).composeName (name, prefix);
509: }
510:
511:
512: public Object addToEnvironment (String propName,
513: Object propVal) throws NamingException
514: {
515: return myProps.put (propName, propVal);
516: }
517:
518:
519: public Object removeFromEnvironment (String propName) throws NamingException
520: {
521: return myProps.remove (propName);
522: }
523:
524:
525: public Hashtable getEnvironment () throws NamingException
526: {
527: return myProps;
528: }
529:
530:
531: public void close () throws NamingException
532: {
533: myProps = null;
534: defaultInitCtx = null;
535: }
536:
537:
543: public String getNameInNamespace () throws NamingException
544: {
545: throw new OperationNotSupportedException ();
546: }
547: }