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