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<Object,Object> myProps;
77:
78:
83: static final HashSet<String> colon_list;
84: static
85: {
86: colon_list = new HashSet<String>();
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 = (Hashtable<Object, Object>) environment;
174: else
175: myProps = new Hashtable<Object, Object>();
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<Object, Object> primary,
266: Hashtable<Object, Object> additional)
267: {
268: Enumeration en = additional.keys();
269:
270: while (en.hasMoreElements())
271: {
272: String key2 = (String) en.nextElement();
273: Object value1 = primary.get(key2);
274: if (value1 == null)
275: primary.put(key2, additional.get(key2));
276: else if (colon_list.contains(key2))
277: {
278: String value2 = (String) additional.get(key2);
279: primary.put(key2, (String) value1 + ":" + value2);
280: }
281: }
282: }
283:
284:
293: protected Context getDefaultInitCtx() throws NamingException
294: {
295: if (! gotDefault)
296: {
297: defaultInitCtx = NamingManager.getInitialContext(myProps);
298: gotDefault = true;
299: }
300: return defaultInitCtx;
301: }
302:
303:
313: protected Context getURLOrDefaultInitCtx(Name name) throws NamingException
314: {
315: if (name.size() > 0)
316: return getURLOrDefaultInitCtx(name.get(0));
317: else
318: return getDefaultInitCtx();
319: }
320:
321:
331: protected Context getURLOrDefaultInitCtx(String name) throws NamingException
332: {
333: String scheme = null;
334:
335: if (NamingManager.hasInitialContextFactoryBuilder())
336: return getDefaultInitCtx();
337: int colon = name.indexOf(':');
338: int slash = name.indexOf('/');
339: if (colon > 0 && (slash == - 1 || colon < slash))
340: scheme = name.substring(0, colon);
341: if (scheme != null)
342: {
343: Context context = NamingManager.getURLContext(scheme, myProps);
344: if (context != null)
345: return context;
346: }
347:
348: return getDefaultInitCtx();
349: }
350:
351:
352: public void bind (Name name, Object obj) throws NamingException
353: {
354: getURLOrDefaultInitCtx (name).bind (name, obj);
355: }
356:
357:
358: public void bind (String name, Object obj) throws NamingException
359: {
360: getURLOrDefaultInitCtx (name).bind (name, obj);
361: }
362:
363:
364: public Object lookup (Name name) throws NamingException
365: {
366: try
367: {
368: return getURLOrDefaultInitCtx (name).lookup (name);
369: }
370: catch (CannotProceedException cpe)
371: {
372: Context ctx = NamingManager.getContinuationContext (cpe);
373: return ctx.lookup (cpe.getRemainingName());
374: }
375: }
376:
377:
378: public Object lookup (String name) throws NamingException
379: {
380: try
381: {
382: return getURLOrDefaultInitCtx (name).lookup (name);
383: }
384: catch (CannotProceedException cpe)
385: {
386: Context ctx = NamingManager.getContinuationContext (cpe);
387: return ctx.lookup (cpe.getRemainingName());
388: }
389: }
390:
391:
392: public void rebind (Name name, Object obj) throws NamingException
393: {
394: getURLOrDefaultInitCtx (name).rebind (name, obj);
395: }
396:
397:
398: public void rebind (String name, Object obj) throws NamingException
399: {
400: getURLOrDefaultInitCtx (name).rebind (name, obj);
401: }
402:
403:
404: public void unbind (Name name) throws NamingException
405: {
406: getURLOrDefaultInitCtx (name).unbind (name);
407: }
408:
409:
410: public void unbind (String name) throws NamingException
411: {
412: getURLOrDefaultInitCtx (name).unbind (name);
413: }
414:
415:
416: public void rename (Name oldName, Name newName) throws NamingException
417: {
418: getURLOrDefaultInitCtx (oldName).rename (oldName, newName);
419: }
420:
421:
422: public void rename (String oldName, String newName) throws NamingException
423: {
424: getURLOrDefaultInitCtx (oldName).rename (oldName, newName);
425: }
426:
427:
428: public NamingEnumeration<NameClassPair> list (Name name) throws NamingException
429: {
430: return getURLOrDefaultInitCtx (name).list (name);
431: }
432:
433:
434: public NamingEnumeration<NameClassPair> list (String name) throws NamingException
435: {
436: return getURLOrDefaultInitCtx (name).list (name);
437: }
438:
439:
440: public NamingEnumeration<Binding> listBindings (Name name) throws NamingException
441: {
442: return getURLOrDefaultInitCtx (name).listBindings (name);
443: }
444:
445:
446: public NamingEnumeration<Binding> listBindings (String name) throws NamingException
447: {
448: return getURLOrDefaultInitCtx (name).listBindings (name);
449: }
450:
451:
452: public void destroySubcontext (Name name) throws NamingException
453: {
454: getURLOrDefaultInitCtx (name).destroySubcontext (name);
455: }
456:
457:
458: public void destroySubcontext (String name) throws NamingException
459: {
460: getURLOrDefaultInitCtx (name).destroySubcontext (name);
461: }
462:
463:
464: public Context createSubcontext (Name name) throws NamingException
465: {
466: return getURLOrDefaultInitCtx (name).createSubcontext (name);
467: }
468:
469:
470: public Context createSubcontext (String name) throws NamingException
471: {
472: return getURLOrDefaultInitCtx (name).createSubcontext (name);
473: }
474:
475:
476: public Object lookupLink (Name name) throws NamingException
477: {
478: return getURLOrDefaultInitCtx (name).lookupLink (name);
479: }
480:
481:
482: public Object lookupLink (String name) throws NamingException
483: {
484: return getURLOrDefaultInitCtx (name).lookupLink (name);
485: }
486:
487:
488: public NameParser getNameParser (Name name) throws NamingException
489: {
490: return getURLOrDefaultInitCtx (name).getNameParser (name);
491: }
492:
493:
494: public NameParser getNameParser (String name) throws NamingException
495: {
496: return getURLOrDefaultInitCtx (name).getNameParser (name);
497: }
498:
499:
500: public Name composeName (Name name, Name prefix) throws NamingException
501: {
502: return getURLOrDefaultInitCtx (name).composeName (name, prefix);
503: }
504:
505:
506: public String composeName (String name,
507: String prefix) throws NamingException
508: {
509: return getURLOrDefaultInitCtx (name).composeName (name, prefix);
510: }
511:
512:
513: public Object addToEnvironment (String propName,
514: Object propVal) throws NamingException
515: {
516: return myProps.put (propName, propVal);
517: }
518:
519:
520: public Object removeFromEnvironment (String propName) throws NamingException
521: {
522: return myProps.remove (propName);
523: }
524:
525:
526: public Hashtable<?,?> getEnvironment () throws NamingException
527: {
528: return myProps;
529: }
530:
531:
532: public void close () throws NamingException
533: {
534: myProps = null;
535: defaultInitCtx = null;
536: }
537:
538:
544: public String getNameInNamespace () throws NamingException
545: {
546: throw new OperationNotSupportedException ();
547: }
548: }