KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > naming > cos > URLContext


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * URLContext.java
20  *
21  * This object is responsible for implementing the JNDI context within
22  * Coadunation.
23  */

24
25 // package path
26
package com.rift.coad.lib.naming.cos;
27
28 // logging import
29
import org.apache.log4j.Logger;
30
31 // imports
32
import java.util.Hashtable JavaDoc;
33 import javax.naming.Context JavaDoc;
34 import javax.naming.Name JavaDoc;
35 import javax.naming.NameParser JavaDoc;
36 import javax.naming.NamingEnumeration JavaDoc;
37 import javax.naming.NamingException JavaDoc;
38
39 /**
40  * This object is responsible for implementing the JNDI context within
41  * Coadunation.
42  *
43  * @author Brett Chaldecott
44  */

45 public class URLContext implements Context JavaDoc {
46     
47     // the class log variable
48
protected Logger log =
49         Logger.getLogger(URLContext.class.getName());
50     
51     // class private member variables
52
private Hashtable JavaDoc env = null;
53     private MasterContext masterContext = null;
54     private Name JavaDoc prefix = null;
55     
56     /**
57      * Creates a new instance of URLContext
58      *
59      * @param env The environment of the cos context.
60      */

61     public URLContext(Hashtable JavaDoc env) throws NamingException JavaDoc {
62         try {
63             this.env = env;
64             masterContext = CosNamingContextManager.getInstance().
65                     getMasterContext();
66             prefix = new NamingParser().parse("");
67         } catch (Exception JavaDoc ex) {
68             throw new NamingException JavaDoc("Failed to create a new URL context");
69         }
70     }
71     
72     /**
73      * Creates a new instance of URLContext
74      *
75      * @param name The prefix name of this context.
76      * @param env The environment of the cos context.
77      * @exception NamingException
78      */

79     public URLContext(Name JavaDoc name, Hashtable JavaDoc env) throws NamingException JavaDoc {
80         try {
81             this.env = env;
82             masterContext = CosNamingContextManager.getInstance().
83                     getMasterContext();
84             this.prefix = name;
85         } catch (Exception JavaDoc ex) {
86             throw new NamingException JavaDoc("Failed to create a new URL context");
87         }
88     }
89     
90     
91     /**
92      *
93      * Creates a new instance of URLContext
94      *
95      *
96      * @param env The environment of the cos context.
97      */

98     public URLContext(Hashtable JavaDoc env,MasterContext masterContext,Name JavaDoc prefix)
99             throws NamingException JavaDoc {
100         this.env = env;
101         this.masterContext = masterContext;
102         this.prefix = prefix;
103     }
104     
105     
106     
107     /**
108      * Adds a new environment property to the environment of this context.
109      *
110      * @return The previous value of the property or null.
111      * @param propName The property to replace or add.
112      * @param propValue The new property value.
113      */

114     public Object JavaDoc addToEnvironment(String JavaDoc propName, Object JavaDoc propVal) throws
115             NamingException JavaDoc {
116         return masterContext.addToEnvironment(propName,propVal);
117     }
118     
119     
120     /**
121      * Binds a name to an object.
122      *
123      * @param name The name of the object to bind.
124      * @param obj The object to bind.
125      * @exception NamingException
126      */

127     public void bind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
128         Name JavaDoc composedName = masterContext.composeName(name,prefix);
129         masterContext.bind(composedName,obj);
130     }
131     
132     
133     /**
134      * Binds a name to an object.
135      */

136     public void bind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
137         Name JavaDoc composedName = masterContext.composeName(
138                 new NamingParser().parse(name),prefix);
139         masterContext.bind(composedName,obj);
140     }
141     
142     
143     /**
144      * Closes this context.
145      */

146     public void close() throws NamingException JavaDoc {
147         masterContext.close();
148     }
149     
150     
151     /**
152      * Composes the name of this context with a name relative to this context.
153      *
154      * @return The compisit name.
155      * @param name The name to add to the prefix.
156      * @param prefix The prefix of the current context.
157      * @exception NamingException
158      */

159     public Name JavaDoc composeName(Name JavaDoc name, Name JavaDoc prefix) throws NamingException JavaDoc {
160         return masterContext.composeName(name,prefix);
161     }
162     
163     
164     /**
165      * Composes the name of this context with a name relative to this context.
166      *
167      * @return The string version of the composed name.
168      * @param name The name to add to the preffix.
169      * @param prefix The prefix for this context.
170      */

171     public String JavaDoc composeName(String JavaDoc name, String JavaDoc prefix) throws
172             NamingException JavaDoc {
173         return masterContext.composeName(name,prefix);
174     }
175     
176     
177     /**
178      * Creates and binds a new context to this context.
179      *
180      * @return The newly created context.
181      * @param name The name of the new sub context.
182      * @exception NamingException
183      */

184     public Context JavaDoc createSubcontext(Name JavaDoc name) throws NamingException JavaDoc {
185         Name JavaDoc composedName = masterContext.composeName(name,prefix);
186         masterContext.createSubcontext(composedName);
187         return new URLContext(env,masterContext,composedName);
188     }
189     
190     
191     /**
192      * Creates and binds a new context.
193      *
194      * @return The newly create sub context.
195      * @exception name The name of the new sub context.
196      * @exception NamingException
197      */

198     public Context JavaDoc createSubcontext(String JavaDoc name) throws NamingException JavaDoc {
199         Name JavaDoc composedName = masterContext.composeName(
200                 new NamingParser().parse(name),prefix);
201         masterContext.createSubcontext(composedName);
202         return new URLContext(env,masterContext,composedName);
203     }
204     
205     
206     /**
207      * Destroys the named context and removes it from the namespace.
208      *
209      * @param name The name of the sub context to remove.
210      * @exception NamingException
211      */

212     public void destroySubcontext(Name JavaDoc name) throws NamingException JavaDoc {
213         Name JavaDoc composedName = masterContext.composeName(name,prefix);
214         masterContext.destroySubcontext(composedName);
215     }
216     
217     
218     /**
219      * Destroys the named context and removes it from the namespace.
220      *
221      * @param name The name of the context to destroy.
222      */

223     public void destroySubcontext(String JavaDoc name) throws NamingException JavaDoc {
224         Name JavaDoc composedName = masterContext.composeName(
225                 new NamingParser().parse(name),prefix);
226         masterContext.destroySubcontext(composedName);
227     }
228     
229     
230     /**
231      * Retrieves the environment in effect for this context.
232      *
233      * @return The reference to the hash table.
234      * @exception NamingException
235      */

236     public Hashtable JavaDoc getEnvironment() throws NamingException JavaDoc {
237         return masterContext.getEnvironment();
238     }
239     
240     
241     /**
242      * Retrieves the full name of this context within its own namespace.
243      */

244     public String JavaDoc getNameInNamespace() throws NamingException JavaDoc {
245         return masterContext.getNameInNamespace();
246     }
247     
248     
249     /**
250      * Retrieves the parser associated with the named context.
251      *
252      * @return The reference to the name parser.
253      * @param name The name to return the parser for.
254      * @exception NamingException
255      */

256     public NameParser JavaDoc getNameParser(Name JavaDoc name) throws NamingException JavaDoc {
257         return masterContext.getNameParser(name);
258     }
259     
260     
261     /**
262      * Retrieves the parser associated with the named context.
263      */

264     public NameParser JavaDoc getNameParser(String JavaDoc name) throws NamingException JavaDoc {
265         return masterContext.getNameParser(name);
266     }
267     
268     
269     /**
270      * Enumerates the names bound in the named context, along with the class
271      * names of objects bound to them.
272      */

273     public NamingEnumeration JavaDoc list(Name JavaDoc name) throws NamingException JavaDoc {
274         Name JavaDoc composedName = masterContext.composeName(name,prefix);
275         return masterContext.list(composedName);
276     }
277     
278     
279     /**
280      * Enumerates the names bound in the named context, along with the class
281      * names of objects bound to them.
282      *
283      * @return The list of names bound to this context.
284      * @param name The list of names.
285      * @exception NamingException
286      */

287     public NamingEnumeration JavaDoc list(String JavaDoc name) throws NamingException JavaDoc {
288         Name JavaDoc composedName = masterContext.composeName(
289                 new NamingParser().parse(name),prefix);
290         return masterContext.list(composedName);
291     }
292     
293     
294     /**
295      * Enumerates the names bound in the named context, along with the objects
296      * bound to them.
297      *
298      * @return The list of bindings for the name
299      * @param name The name to perform the search below.
300      * @exception NamingException
301      */

302     public NamingEnumeration JavaDoc listBindings(Name JavaDoc name) throws NamingException JavaDoc {
303         Name JavaDoc composedName = masterContext.composeName(name,prefix);
304         return masterContext.listBindings(composedName);
305     }
306     
307     
308     /**
309      * Enumerates the names bound in the named context, along with the objects
310      * bound to them.
311      *
312      * @return The list of binding for the name.
313      * @param name The name to perform the search for.
314      * @exception NamingException
315      */

316     public NamingEnumeration JavaDoc listBindings(String JavaDoc name) throws NamingException JavaDoc {
317         Name JavaDoc composedName = masterContext.composeName(
318                 new NamingParser().parse(name),prefix);
319         return masterContext.listBindings(composedName);
320     }
321     
322     
323     /**
324      * Retrieves the named object.
325      *
326      * @return The named object.
327      * @param name The name to retrieve the object for.
328      * @exception NamingException
329      */

330     public Object JavaDoc lookup(Name JavaDoc name) throws NamingException JavaDoc {
331         Name JavaDoc composedName = masterContext.composeName(name,prefix);
332         Object JavaDoc result = masterContext.lookup(composedName);
333         if (result instanceof Context JavaDoc) {
334             return new URLContext(env,masterContext,composedName);
335         }
336         return result;
337     }
338     
339     
340     /**
341      * Retrieves the named object.
342      *
343      * @return The object to retrieve by name.
344      * @param name The name of the object to retrieve.
345      * @exception NamingException
346      */

347     public Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc {
348         Name JavaDoc composedName = masterContext.composeName(
349                 new NamingParser().parse(name),prefix);
350         Object JavaDoc result = masterContext.lookup(composedName);
351         if (result instanceof Context JavaDoc) {
352             return new URLContext(env,masterContext,composedName);
353         }
354         return result;
355     }
356     
357     
358     /**
359      * Retrieves the named object, following links except for the terminal
360      * atomic component of the name.
361      *
362      * @return The object to retrieve.
363      * @param name The name of the object to lookup.
364      * @exception NamingException
365      */

366     public Object JavaDoc lookupLink(Name JavaDoc name) throws NamingException JavaDoc {
367         Name JavaDoc composedName = masterContext.composeName(name,prefix);
368         Object JavaDoc result = masterContext.lookupLink(composedName);
369         if (result instanceof Context JavaDoc) {
370             return new URLContext(env,masterContext,composedName);
371         }
372         return result;
373     }
374     
375     
376     /**
377      * Retrieves the named object, following links except for the terminal
378      * atomic component of the name.
379      *
380      * @return The results of the lookup link.
381      * @param name The name of the object to lookup.
382      * @exception NamingException
383      */

384     public Object JavaDoc lookupLink(String JavaDoc name) throws NamingException JavaDoc {
385         Name JavaDoc composedName = masterContext.composeName(
386                 new NamingParser().parse(name),prefix);
387         Object JavaDoc result = masterContext.lookupLink(composedName);
388         if (result instanceof Context JavaDoc) {
389             return new URLContext(env,masterContext,composedName);
390         }
391         return result;
392     }
393     
394     
395     /**
396      * Binds a name to an object, overwriting any existing binding.
397      *
398      * @param name The name to rebind.
399      * @param obj The object to rebind.
400      * @exception NamingException
401      */

402     public void rebind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
403         Name JavaDoc composedName = masterContext.composeName(name,prefix);
404         masterContext.rebind(composedName,obj);
405     }
406     
407     
408     /**
409      * Binds a name to an object, overwriting any existing binding.
410      *
411      * @param name The name to rebind.
412      * @param obj The object to rebind.
413      * @exception NamingException
414      */

415     public void rebind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
416         Name JavaDoc composedName = masterContext.composeName(
417                 new NamingParser().parse(name),prefix);
418         masterContext.rebind(composedName,obj);
419     }
420     
421     
422     /**
423      * Removes an environment property from the environment of this context.
424      *
425      * @param propName The name of the entry to remove from the environment.
426      * @exception NamingException
427      */

428     public Object JavaDoc removeFromEnvironment(String JavaDoc propName) throws NamingException JavaDoc {
429         return masterContext.removeFromEnvironment(propName);
430     }
431     
432     
433     /**
434      * Binds a new name to the object bound to an old name, and unbinds the old
435      * name.
436      *
437      * @param oldName The old name to rename.
438      * @param newName The name to replace it with.
439      * @exception NamingException
440      */

441     public void rename(Name JavaDoc oldName, Name JavaDoc newName) throws NamingException JavaDoc {
442         Name JavaDoc oldComposedName = masterContext.composeName(oldName,prefix);
443         Name JavaDoc newComposedName = masterContext.composeName(newName,prefix);
444         masterContext.rename(oldComposedName,newComposedName);
445     }
446     
447     
448     /**
449      * Binds a new name to the object bound to an old name, and unbinds the old
450      * name.
451      *
452      * @param oldName The old name to rename.
453      * @param newName The name to replace it with.
454      * @exception NamingException
455      */

456     public void rename(String JavaDoc oldName, String JavaDoc newName) throws NamingException JavaDoc {
457         Name JavaDoc oldComposedName = masterContext.composeName(
458                 new NamingParser().parse(oldName),prefix);
459         Name JavaDoc newComposedName = masterContext.composeName(
460                 new NamingParser().parse(newName),prefix);
461         masterContext.rename(oldComposedName,newComposedName);
462     }
463     
464     
465     /**
466      * Unbinds the named object.
467      *
468      * @param name The name to unbind.
469      * @exception NamingException
470      */

471     public void unbind(Name JavaDoc name) throws NamingException JavaDoc {
472         Name JavaDoc composedName = masterContext.composeName(name,prefix);
473         masterContext.unbind(composedName);
474     }
475     
476     
477     /**
478      * Unbinds the named objec.
479      *
480      * @param name The name to unbind.
481      * @exception NamingException
482      */

483     public void unbind(String JavaDoc name) throws NamingException JavaDoc {
484         Name JavaDoc composedName = masterContext.composeName(
485                 new NamingParser().parse(name),prefix);
486         masterContext.unbind(composedName);
487     }
488           
489     
490 }
491
Popular Tags