KickJava   Java API By Example, From Geeks To Geeks.

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


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  * MemoryContext.java
20  *
21  * This is an in memory version of a context object.
22  */

23
24 // package path
25
package com.rift.coad.lib.naming.cos;
26
27 // imports
28
import java.util.Enumeration JavaDoc;
29 import java.util.Hashtable JavaDoc;
30 import javax.naming.Context JavaDoc;
31 import javax.naming.InvalidNameException JavaDoc;
32 import javax.naming.Name JavaDoc;
33 import javax.naming.NameAlreadyBoundException JavaDoc;
34 import javax.naming.NameParser JavaDoc;
35 import javax.naming.NamingEnumeration JavaDoc;
36 import javax.naming.NamingException JavaDoc;
37 import javax.naming.NameNotFoundException JavaDoc;
38
39 /**
40  * This is an in memory version of a context object.
41  *
42  * @author Brett Chaldecott
43  */

44 public class MemoryContext implements Context JavaDoc {
45     
46     // private member variables
47
private Hashtable JavaDoc env = null;
48     private Hashtable JavaDoc contextEnv = new Hashtable JavaDoc();
49     private Name JavaDoc nameSpace = null;
50     
51     /**
52      * Creates a new instance of MemoryContext
53      *
54      * @param env The environment in which this context is running.
55      */

56     public MemoryContext(Hashtable JavaDoc env,Name JavaDoc nameSpace) {
57         this.env = (Hashtable JavaDoc)env.clone();
58         this.nameSpace = nameSpace;
59     }
60     
61     
62     /**
63      * Adds a new environment property to the environment of this context.
64      *
65      * @return The previous value of the property or null.
66      * @param propName The property to replace or add.
67      * @param propValue The new property value.
68      */

69     public Object JavaDoc addToEnvironment(String JavaDoc propName, Object JavaDoc propVal) throws
70             NamingException JavaDoc {
71         Object JavaDoc original = null;
72         synchronized(env) {
73             if (env.containsKey(propName)) {
74                 original = env.get(propName);
75             }
76             env.put(propName,propVal);
77         }
78         return original;
79     }
80     
81     
82     /**
83      * Binds a name to an object.
84      *
85      * @param name The name of the object to bind.
86      * @param obj The object to bind.
87      * @exception NamingException
88      */

89     public void bind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
90         if (name.size() <= 0) {
91             throw new InvalidNameException JavaDoc("Invalid name has been passed");
92         }
93         String JavaDoc key = name.get(0);
94         MemoryContext subContext = null;
95         synchronized(contextEnv) {
96             boolean contains = contextEnv.containsKey(key);
97             if ((name.size() == 1) && contains) {
98                 throw new NameAlreadyBoundException JavaDoc("The name [" + key
99                         + "] is already bound");
100             } else if (name.size() == 1 ) {
101                 contextEnv.put(key,obj);
102                 return;
103             } else if (contains) {
104                 subContext = (MemoryContext)contextEnv.get(key);
105             } else {
106                 subContext = new MemoryContext(env,composeName(nameSpace,
107                         new NamingParser().parse(key)));
108                 contextEnv.put(key,subContext);
109             }
110         }
111         subContext.bind(name.getSuffix(1),obj);
112     }
113     
114     
115     /**
116      * Binds a name to an object.
117      */

118     public void bind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
119         bind(new NamingParser().parse(name),obj);
120     }
121     
122     
123     /**
124      * Closes this context.
125      */

126     public void close() throws NamingException JavaDoc {
127         
128     }
129     
130     
131     /**
132      * Composes the name of this context with a name relative to this context.
133      *
134      * @return The compisit name.
135      * @param name The name to add to the prefix.
136      * @param prefix The prefix of the current context.
137      * @exception NamingException
138      */

139     public Name JavaDoc composeName(Name JavaDoc name, Name JavaDoc prefix) throws NamingException JavaDoc {
140         Name JavaDoc newName = (Name JavaDoc)prefix.clone();
141         newName.addAll(name);
142         return newName;
143     }
144     
145     
146     /**
147      * Composes the name of this context with a name relative to this context.
148      *
149      * @return The string version of the composed name.
150      * @param name The name to add to the preffix.
151      * @param prefix The prefix for this context.
152      */

153     public String JavaDoc composeName(String JavaDoc name, String JavaDoc prefix) throws
154             NamingException JavaDoc {
155         return composeName(new NamingParser().parse(name),
156                 new NamingParser().parse(prefix)).toString();
157     }
158     
159     
160     /**
161      * Creates and binds a new context to this context.
162      *
163      * @return The newly created context.
164      * @param name The name of the new sub context.
165      * @exception NamingException
166      */

167     public Context JavaDoc createSubcontext(Name JavaDoc name) throws NamingException JavaDoc {
168         if (name.size() <= 0) {
169             throw new InvalidNameException JavaDoc("Invalid name has been passed");
170         }
171         String JavaDoc key = name.get(0);
172         MemoryContext subContext = null;
173         synchronized(contextEnv) {
174             if (contextEnv.containsKey(key)) {
175                 subContext = (MemoryContext)contextEnv.get(key);
176             } else {
177                 subContext = new MemoryContext(env,composeName(
178                         new NamingParser().parse(key),nameSpace));
179                 contextEnv.put(key,subContext);
180             }
181         }
182         if (name.size() > 1) {
183             return subContext.createSubcontext(name.getSuffix(1));
184         }
185         return subContext;
186     }
187     
188     
189     /**
190      * Creates and binds a new context.
191      *
192      * @return The newly create sub context.
193      * @exception name The name of the new sub context.
194      * @exception NamingException
195      */

196     public Context JavaDoc createSubcontext(String JavaDoc name) throws NamingException JavaDoc {
197         return createSubcontext(new NamingParser().parse(name));
198     }
199     
200     
201     /**
202      * Destroys the named context and removes it from the namespace.
203      *
204      * @param name The name of the sub context to remove.
205      * @exception NamingException
206      */

207     public void destroySubcontext(Name JavaDoc name) throws NamingException JavaDoc {
208         if (name.size() <= 0) {
209             throw new InvalidNameException JavaDoc("Invalid name has been passed");
210         }
211         String JavaDoc key = name.get(0);
212         MemoryContext subContext = null;
213         synchronized(contextEnv) {
214             boolean contains = contextEnv.containsKey(key);
215             if ((name.size() == 1) && contains) {
216                 contextEnv.remove(key);
217                 return;
218             } else if (contains) {
219                 subContext = (MemoryContext)contextEnv.get(key);
220             } else {
221                 throw new NameNotFoundException JavaDoc("The name [" + key
222                         + "] not found");
223             }
224         }
225         if (name.size() > 1) {
226             subContext.destroySubcontext(name.getSuffix(1));
227         }
228     }
229     
230     
231     /**
232      * Destroys the named context and removes it from the namespace.
233      *
234      * @param name The name of the context to destroy.
235      */

236     public void destroySubcontext(String JavaDoc name) throws NamingException JavaDoc {
237         destroySubcontext(new NamingParser().parse(name));
238     }
239     
240     
241     /**
242      * Retrieves the environment in effect for this context.
243      *
244      * @return The reference to the hash table.
245      * @exception NamingException
246      */

247     public Hashtable JavaDoc getEnvironment() throws NamingException JavaDoc {
248         return env;
249     }
250     
251     
252     /**
253      * Retrieves the full name of this context within its own namespace.
254      */

255     public String JavaDoc getNameInNamespace() throws NamingException JavaDoc {
256         return nameSpace.toString();
257     }
258     
259     
260     /**
261      * Retrieves the parser associated with the named context.
262      *
263      * @return The reference to the name parser.
264      * @param name The name to return the parser for.
265      * @exception NamingException
266      */

267     public NameParser JavaDoc getNameParser(Name JavaDoc name) throws NamingException JavaDoc {
268         return new NamingParser();
269     }
270     
271     
272     /**
273      * Retrieves the parser associated with the named context.
274      */

275     public NameParser JavaDoc getNameParser(String JavaDoc name) throws NamingException JavaDoc {
276         return new NamingParser();
277     }
278     
279     
280     /**
281      * Enumerates the names bound in the named context, along with the class
282      * names of objects bound to them.
283      */

284     public NamingEnumeration JavaDoc list(Name JavaDoc name) throws NamingException JavaDoc {
285         return new MemoryNamingEnumeration(name.getAll());
286     }
287     
288     
289     /**
290      * Enumerates the names bound in the named context, along with the class
291      * names of objects bound to them.
292      *
293      * @return The list of names bound to this context.
294      * @param name The list of names.
295      * @exception NamingException
296      */

297     public NamingEnumeration JavaDoc list(String JavaDoc name) throws NamingException JavaDoc {
298         return new MemoryNamingEnumeration(new NamingParser().parse(name).
299                 getAll());
300     }
301     
302     
303     /**
304      * Enumerates the names bound in the named context, along with the objects
305      * bound to them.
306      *
307      * @return The list of bindings for the name
308      * @param name The name to perform the search below.
309      * @exception NamingException
310      */

311     public NamingEnumeration JavaDoc listBindings(Name JavaDoc name) throws NamingException JavaDoc {
312         if (name.size() <= 0) {
313             synchronized(contextEnv) {
314                 return new MemoryNamingEnumeration(contextEnv.elements());
315             }
316         }
317         String JavaDoc key = name.get(0);
318         MemoryContext subContext = null;
319         synchronized(contextEnv) {
320             if (contextEnv.containsKey(key)) {
321                 subContext = (MemoryContext)contextEnv.get(key);
322             } else {
323                 throw new NameNotFoundException JavaDoc("The name [" + key
324                         + "] not found");
325             }
326         }
327         return subContext.listBindings(name.getSuffix(1));
328     }
329     
330     
331     /**
332      * Enumerates the names bound in the named context, along with the objects
333      * bound to them.
334      *
335      * @return The list of binding for the name.
336      * @param name The name to perform the search for.
337      * @exception NamingException
338      */

339     public NamingEnumeration JavaDoc listBindings(String JavaDoc name) throws NamingException JavaDoc {
340         return listBindings(new NamingParser().parse(name));
341     }
342     
343     
344     /**
345      * Retrieves the named object.
346      *
347      * @return The named object.
348      * @param name The name to retrieve the object for.
349      * @exception NamingException
350      */

351     public Object JavaDoc lookup(Name JavaDoc name) throws NamingException JavaDoc {
352         if (name.size() <= 0) {
353             throw new InvalidNameException JavaDoc("Invalid name has been passed");
354         }
355         String JavaDoc key = name.get(0);
356         MemoryContext subContext = null;
357         synchronized(contextEnv) {
358             boolean contains = contextEnv.containsKey(key);
359             if ((name.size() == 1) && contains) {
360                 return contextEnv.get(key);
361             } else if (contains) {
362                 subContext = (MemoryContext)contextEnv.get(key);
363             } else {
364                 throw new NameNotFoundException JavaDoc("The name [" + key
365                         + "] not found");
366             }
367         }
368         return subContext.lookup(name.getSuffix(1));
369     }
370     
371     
372     /**
373      * Retrieves the named object.
374      *
375      * @return The object to retrieve by name.
376      * @param name The name of the object to retrieve.
377      * @exception NamingException
378      */

379     public Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc {
380         return lookup(new NamingParser().parse(name));
381     }
382     
383     
384     /**
385      * Retrieves the named object, following links except for the terminal
386      * atomic component of the name.
387      *
388      * @return The object to retrieve.
389      * @param name The name of the object to lookup.
390      * @exception NamingException
391      */

392     public Object JavaDoc lookupLink(Name JavaDoc name) throws NamingException JavaDoc {
393         Name JavaDoc currentName = name;
394         while(true) {
395             Object JavaDoc result = lookup(currentName);
396             if (!(result instanceof Name JavaDoc)) {
397                 return result;
398             }
399             currentName = (Name JavaDoc)result;
400         }
401     }
402     
403     
404     /**
405      * Retrieves the named object, following links except for the terminal
406      * atomic component of the name.
407      *
408      * @return The results of the lookup link.
409      * @param name The name of the object to lookup.
410      * @exception NamingException
411      */

412     public Object JavaDoc lookupLink(String JavaDoc name) throws NamingException JavaDoc {
413         return lookupLink(new NamingParser().parse(name));
414     }
415     
416     
417     /**
418      * Binds a name to an object, overwriting any existing binding.
419      *
420      * @param name The name to rebind.
421      * @param obj The object to rebind.
422      * @exception NamingException
423      */

424     public void rebind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
425         if (name.size() <= 0) {
426             throw new InvalidNameException JavaDoc("Invalid name has been passed");
427         }
428         String JavaDoc key = name.get(0);
429         MemoryContext subContext = null;
430         synchronized(contextEnv) {
431             boolean contains = contextEnv.containsKey(key);
432             if (name.size() == 1 ) {
433                 contextEnv.put(key,obj);
434                 return;
435             } else if (contains) {
436                 subContext = (MemoryContext)contextEnv.get(key);
437             } else {
438                 subContext = new MemoryContext(env,composeName(
439                         new NamingParser().parse(key),nameSpace));
440                 contextEnv.put(key,subContext);
441             }
442         }
443         subContext.rebind(name.getSuffix(1),obj);
444     }
445     
446     
447     /**
448      * Binds a name to an object, overwriting any existing binding.
449      *
450      * @param name The name to rebind.
451      * @param obj The object to rebind.
452      * @exception NamingException
453      */

454     public void rebind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
455         rebind(new NamingParser().parse(name),obj);
456     }
457     
458     
459     /**
460      * Removes an environment property from the environment of this context.
461      *
462      * @return The original value.
463      * @param propName The name of the entry to remove from the environment.
464      * @exception NamingException
465      */

466     public Object JavaDoc removeFromEnvironment(String JavaDoc propName) throws NamingException JavaDoc {
467         Object JavaDoc originalValue = null;
468         synchronized(env) {
469             if (env.containsKey(propName)) {
470                 originalValue = env.get(propName);
471                 env.remove(propName);
472             }
473         }
474         return originalValue;
475     }
476     
477     
478     /**
479      * Binds a new name to the object bound to an old name, and unbinds the old
480      * name.
481      *
482      * @param oldName The old name to rename.
483      * @param newName The name to replace it with.
484      * @exception NamingException
485      */

486     public void rename(Name JavaDoc oldName, Name JavaDoc newName) throws NamingException JavaDoc {
487         Object JavaDoc value = lookup(oldName);
488         unbind(oldName);
489         bind(newName,value);
490     }
491     
492     
493     /**
494      * Binds a new name to the object bound to an old name, and unbinds the old
495      * name.
496      *
497      * @param oldName The old name to rename.
498      * @param newName The name to replace it with.
499      * @exception NamingException
500      */

501     public void rename(String JavaDoc oldName, String JavaDoc newName) throws NamingException JavaDoc {
502         rename(new NamingParser().parse(oldName),new NamingParser().
503                 parse(newName));
504     }
505     
506     
507     /**
508      * Unbinds the named object.
509      *
510      * @param name The name to unbind.
511      * @exception NamingException
512      */

513     public void unbind(Name JavaDoc name) throws NamingException JavaDoc {
514         if (name.size() <= 0) {
515             throw new InvalidNameException JavaDoc("Invalid name has been passed");
516         }
517         String JavaDoc key = name.get(0);
518         MemoryContext subContext = null;
519         synchronized(contextEnv) {
520             boolean contains = contextEnv.containsKey(key);
521             if (contains && (name.size() == 1 )) {
522                 contextEnv.remove(key);
523                 return;
524             } else if (contains) {
525                 subContext = (MemoryContext)contextEnv.get(key);
526             } else {
527                 throw new NameNotFoundException JavaDoc("The name [" + name.toString()
528                         + "] was not found.");
529             }
530         }
531         subContext.unbind(name.getSuffix(1));
532     }
533     
534     
535     /**
536      * Unbinds the named objec.
537      *
538      * @param name The name to unbind.
539      * @exception NamingException
540      */

541     public void unbind(String JavaDoc name) throws NamingException JavaDoc {
542         unbind(new NamingParser().parse(name));
543     }
544 }
545
Popular Tags