KickJava   Java API By Example, From Geeks To Geeks.

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


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 is the master context responsible for managing both the URL in memory
22  * contexts and the Cos contexts.
23  */

24
25 // package path
26
package com.rift.coad.lib.naming.cos;
27
28 // imports
29
import java.util.Hashtable JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Map JavaDoc;
32 import javax.naming.Context JavaDoc;
33 import javax.naming.CompositeName 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 import javax.naming.NameAlreadyBoundException JavaDoc;
39 import javax.naming.NameNotFoundException JavaDoc;
40 import javax.naming.Reference JavaDoc;
41 import javax.naming.OperationNotSupportedException JavaDoc;
42 import javax.naming.spi.ObjectFactory JavaDoc;
43
44 // logging import
45
import org.apache.log4j.Logger;
46
47 // coadunation imports
48
import com.rift.coad.lib.naming.*;
49 import com.rift.coad.lib.thread.CoadunationThreadGroup;
50
51 /**
52  * This is the master context responsible for managing both the URL in memory
53  * contexts and the Cos contexts.
54  *
55  * @author Brett Chaldecott
56  */

57 public class MasterContext implements Context JavaDoc {
58     
59     // the class log variable
60
protected Logger log =
61         Logger.getLogger(MasterContext.class.getName());
62     
63     // class private member variables
64
private Hashtable JavaDoc env = null;
65     private OrbManager orbManager = null;
66     private CosContext cosContext = null;
67     private MemoryContext masterMemoryContext = null;
68     private Map JavaDoc classLoaderMemoryContexts = new HashMap JavaDoc();
69     
70     
71     /**
72      * Creates a new instance of MasterContext
73      *
74      * @param env The environment of the cos context.
75      *
76      * @param env The global has table for this environment.
77      * @param threadGroup The thread group for this object.
78      * @param orbManager The reference to the orb manager.
79      * @param instanceId The id of the coadunation server
80      */

81     public MasterContext(Hashtable JavaDoc env, CoadunationThreadGroup threadGroup,
82             OrbManager orbManager, String JavaDoc instanceId) throws
83             com.rift.coad.lib.naming.NamingException {
84         try {
85             this.env = (Hashtable JavaDoc)env.clone();
86             this.orbManager = orbManager;
87             this.cosContext = new CosContext(env,threadGroup,orbManager,
88                     instanceId);
89             this.masterMemoryContext = new MemoryContext(env,new NamingParser().
90                     parse(""));
91         } catch (javax.naming.NamingException JavaDoc ex) {
92             log.error("Failed to instanciate the Master Context because : " +
93                     ex.getMessage(),ex);
94             throw new com.rift.coad.lib.naming.NamingException("Failed to " +
95                     "instanciate the Master Context because : " +
96                     ex.getMessage(),ex);
97         }
98     }
99     
100     
101     /**
102      * Adds a new environment property to the environment of this context.
103      *
104      * @return The previous value of the property or null.
105      * @param propName The property to replace or add.
106      * @param propValue The new property value.
107      */

108     public Object JavaDoc addToEnvironment(String JavaDoc propName, Object JavaDoc propVal) throws
109             NamingException JavaDoc {
110         synchronized(env) {
111             Object JavaDoc origValue = null;
112             if (env.containsKey(propName)) {
113                 origValue = env.get(propName);
114             }
115             env.put(propName,propVal);
116             return origValue;
117         }
118     }
119     
120     
121     /**
122      * Binds a name to an object.
123      *
124      * @param name The name of the object to bind.
125      * @param obj The object to bind.
126      * @exception NamingException
127      */

128     public void bind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
129         Context JavaDoc context = getContext(name);
130         context.bind(name,obj);
131     }
132     
133     
134     /**
135      * Binds a name to an object.
136      *
137      * @param name The name to bind.
138      * @param obj The object value to bind to the name.
139      * @exception NamingException
140      */

141     public void bind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
142         Context JavaDoc context = getContext(name);
143         context.bind(name,obj);
144     }
145     
146     
147     /**
148      * Closes this context.
149      */

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

163     public Name JavaDoc composeName(Name JavaDoc name, Name JavaDoc prefix) throws NamingException JavaDoc {
164         Name JavaDoc newName = (Name JavaDoc)prefix.clone();
165         newName.addAll(name);
166         return newName;
167     }
168     
169     
170     /**
171      * Composes the name of this context with a name relative to this context.
172      *
173      * @return The string version of the composed name.
174      * @param name The name to add to the preffix.
175      * @param prefix The prefix for this context.
176      */

177     public String JavaDoc composeName(String JavaDoc name, String JavaDoc prefix) throws
178             NamingException JavaDoc {
179         return composeName(new NamingParser().parse(name),new NamingParser().
180                 parse(prefix)).toString();
181     }
182     
183     
184     /**
185      * Creates and binds a new context to this context.
186      *
187      * @return The newly created context.
188      * @param name The name of the new sub context.
189      * @exception NamingException
190      */

191     public Context JavaDoc createSubcontext(Name JavaDoc name) throws NamingException JavaDoc {
192         Context JavaDoc context = getContext(name);
193         return context.createSubcontext(name);
194     }
195     
196     
197     /**
198      * Creates and binds a new context.
199      *
200      * @return The newly create sub context.
201      * @exception name The name of the new sub context.
202      * @exception NamingException
203      */

204     public Context JavaDoc createSubcontext(String JavaDoc name) throws NamingException JavaDoc {
205         Context JavaDoc context = getContext(name);
206         return context.createSubcontext(name);
207     }
208     
209     
210     /**
211      * Destroys the named context and removes it from the namespace.
212      *
213      * @param name The name of the sub context to remove.
214      * @exception NamingException
215      */

216     public void destroySubcontext(Name JavaDoc name) throws NamingException JavaDoc {
217         Context JavaDoc context = getContext(name);
218         context.destroySubcontext(name);
219     }
220     
221     
222     /**
223      * Destroys the named context and removes it from the namespace.
224      *
225      * @param name The name of the context to destroy.
226      */

227     public void destroySubcontext(String JavaDoc name) throws NamingException JavaDoc {
228         Context JavaDoc context = getContext(name);
229         context.destroySubcontext(name);
230     }
231     
232     
233     /**
234      * Retrieves the environment in effect for this context.
235      *
236      * @return The reference to the hash table.
237      * @exception NamingException
238      */

239     public Hashtable JavaDoc getEnvironment() throws NamingException JavaDoc {
240         return env;
241     }
242     
243     
244     /**
245      * Retrieves the full name of this context within its own namespace.
246      */

247     public String JavaDoc getNameInNamespace() throws NamingException JavaDoc {
248         return new NamingParser().parse("").toString();
249     }
250     
251     
252     /**
253      * Retrieves the parser associated with the named context.
254      *
255      * @return The reference to the name parser.
256      * @param name The name to return the parser for.
257      * @exception NamingException
258      */

259     public NameParser JavaDoc getNameParser(Name JavaDoc name) throws NamingException JavaDoc {
260         Context JavaDoc context = getContext(name);
261         return context.getNameParser(name);
262     }
263     
264     
265     /**
266      * Retrieves the parser associated with the named context.
267      */

268     public NameParser JavaDoc getNameParser(String JavaDoc name) throws NamingException JavaDoc {
269         Context JavaDoc context = getContext(name);
270         return context.getNameParser(name);
271     }
272     
273     
274     /**
275      * Enumerates the names bound in the named context, along with the class
276      * names of objects bound to them.
277      */

278     public NamingEnumeration JavaDoc list(Name JavaDoc name) throws NamingException JavaDoc {
279         Context JavaDoc context = getContext(name);
280         return context.list(name);
281     }
282     
283     
284     /**
285      * Enumerates the names bound in the named context, along with the class
286      * names of objects bound to them.
287      *
288      * @return The list of names bound to this context.
289      * @param name The list of names.
290      * @exception NamingException
291      */

292     public NamingEnumeration JavaDoc list(String JavaDoc name) throws NamingException JavaDoc {
293         Context JavaDoc context = getContext(name);
294         return context.list(name);
295     }
296     
297     
298     /**
299      * Enumerates the names bound in the named context, along with the objects
300      * bound to them.
301      *
302      * @return The list of bindings for the name
303      * @param name The name to perform the search below.
304      * @exception NamingException
305      */

306     public NamingEnumeration JavaDoc listBindings(Name JavaDoc name) throws NamingException JavaDoc {
307         Context JavaDoc context = getContext(name);
308         return context.listBindings(name);
309     }
310     
311     
312     /**
313      * Enumerates the names bound in the named context, along with the objects
314      * bound to them.
315      *
316      * @return The list of binding for the name.
317      * @param name The name to perform the search for.
318      * @exception NamingException
319      */

320     public NamingEnumeration JavaDoc listBindings(String JavaDoc name) throws NamingException JavaDoc {
321         Context JavaDoc context = getContext(name);
322         return context.listBindings(name);
323     }
324     
325     
326     /**
327      * Retrieves the named object.
328      *
329      * @return The named object.
330      * @param name The name to retrieve the object for.
331      * @exception NamingException
332      */

333     public Object JavaDoc lookup(Name JavaDoc name) throws NamingException JavaDoc {
334         Context JavaDoc context = getContext(name);
335         try {
336             return processResult(context.lookup(name),name);
337         } catch (javax.naming.NameNotFoundException JavaDoc ex) {
338             if (context != masterMemoryContext) {
339                 return processResult(masterMemoryContext.lookup(name),name);
340             }
341             throw ex;
342         }
343     }
344     
345     
346     /**
347      * Retrieves the named object.
348      *
349      * @return The object to retrieve by name.
350      * @param name The name of the object to retrieve.
351      * @exception NamingException
352      */

353     public Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc {
354         Context JavaDoc context = getContext(name);
355         try {
356             return processResult(context.lookup(name),name);
357         } catch (javax.naming.NameNotFoundException JavaDoc ex) {
358             if (context != masterMemoryContext) {
359                 return processResult(masterMemoryContext.lookup(name),name);
360             }
361             throw ex;
362         }
363     }
364     
365     
366     /**
367      * Retrieves the named object, following links except for the terminal
368      * atomic component of the name.
369      *
370      * @return The object to retrieve.
371      * @param name The name of the object to lookup.
372      * @exception NamingException
373      */

374     public Object JavaDoc lookupLink(Name JavaDoc name) throws NamingException JavaDoc {
375         Context JavaDoc context = getContext(name);
376         try {
377             return processResult(context.lookupLink(name),name);
378         } catch (javax.naming.NameNotFoundException JavaDoc ex) {
379             if (context != masterMemoryContext) {
380                 return processResult(masterMemoryContext.lookupLink(name),name);
381             }
382             throw ex;
383         }
384     }
385     
386     
387     /**
388      * Retrieves the named object, following links except for the terminal
389      * atomic component of the name.
390      *
391      * @return The results of the lookup link.
392      * @param name The name of the object to lookup.
393      * @exception NamingException
394      */

395     public Object JavaDoc lookupLink(String JavaDoc name) throws NamingException JavaDoc {
396         Context JavaDoc context = getContext(name);
397         try {
398             return processResult(context.lookupLink(name),name);
399         } catch (javax.naming.NameNotFoundException JavaDoc ex) {
400             if (context != masterMemoryContext) {
401                 return processResult(masterMemoryContext.lookupLink(name),name);
402             }
403             throw ex;
404         }
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(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
416         Context JavaDoc context = getContext(name);
417         context.rebind(name,obj);
418     }
419     
420     
421     /**
422      * Binds a name to an object, overwriting any existing binding.
423      *
424      * @param name The name to rebind.
425      * @param obj The object to rebind.
426      * @exception NamingException
427      */

428     public void rebind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
429         Context JavaDoc context = getContext(name);
430         context.rebind(name,obj);
431     }
432     
433     
434     /**
435      * Removes an environment property from the environment of this context.
436      *
437      * @param propName The name of the entry to remove from the environment.
438      * @exception NamingException
439      */

440     public Object JavaDoc removeFromEnvironment(String JavaDoc propName) throws NamingException JavaDoc {
441         synchronized(env) {
442             Object JavaDoc original = null;
443             if (env.containsKey(propName)) {
444                 original = env.get(propName);
445                 env.remove(propName);
446             }
447             return original;
448         }
449     }
450     
451     
452     /**
453      * Binds a new name to the object bound to an old name, and unbinds the old
454      * name.
455      *
456      * @param oldName The old name to rename.
457      * @param newName The name to replace it with.
458      * @exception NamingException
459      */

460     public void rename(Name JavaDoc oldName, Name JavaDoc newName) throws NamingException JavaDoc {
461         Context JavaDoc oldContext = getContext(oldName);
462         Context JavaDoc newContext = getContext(newName);
463         Object JavaDoc ref = oldContext.lookup(oldName);
464         newContext.bind(newName,ref);
465         oldContext.unbind(oldName);
466     }
467     
468     
469     /**
470      * Binds a new name to the object bound to an old name, and unbinds the old
471      * name.
472      *
473      * @param oldName The old name to rename.
474      * @param newName The name to replace it with.
475      * @exception NamingException
476      */

477     public void rename(String JavaDoc oldName, String JavaDoc newName) throws NamingException JavaDoc {
478         Context JavaDoc oldContext = getContext(oldName);
479         Context JavaDoc newContext = getContext(newName);
480         Object JavaDoc ref = oldContext.lookup(oldName);
481         newContext.bind(newName,ref);
482         oldContext.unbind(oldName);
483     }
484     
485     
486     /**
487      * Unbinds the named object.
488      *
489      * @param name The name to unbind.
490      * @exception NamingException
491      */

492     public void unbind(Name JavaDoc name) throws NamingException JavaDoc {
493         Context JavaDoc context = getContext(name);
494         context.unbind(name);
495     }
496     
497     
498     /**
499      * Unbinds the named objec.
500      *
501      * @param name The name to unbind.
502      * @exception NamingException
503      */

504     public void unbind(String JavaDoc name) throws NamingException JavaDoc {
505         Context JavaDoc context = getContext(name);
506         context.unbind(name);
507     }
508     
509     
510     /**
511      * This method is called to init the context for a class loader.
512      *
513      * @exception NamingException
514      */

515     public void initContext() throws com.rift.coad.lib.naming.NamingException {
516         try {
517             ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
518             synchronized(classLoaderMemoryContexts) {
519                 classLoaderMemoryContexts.put(loader,new MemoryContext(env,
520                         new NamingParser().parse("")));
521             }
522         } catch (Exception JavaDoc ex) {
523             log.error("Failed to init the context for a class loader : " +
524                     ex.getMessage(),ex);
525             throw new com.rift.coad.lib.naming.NamingException(
526                     "Failed to init the context for a class loader : " +
527                     ex.getMessage(),ex);
528         }
529     }
530     
531     
532     /**
533      * This method is called to release the context for class loader.
534      */

535     public void releaseContext() {
536         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
537         synchronized(classLoaderMemoryContexts) {
538             classLoaderMemoryContexts.remove(loader);
539         }
540     }
541     
542     
543     /**
544      * This method terminates the processing of the MasterContext by terminating
545      * the processing of its sub contexts
546      */

547     public void terminate() {
548         cosContext.terminate();
549     }
550     
551     
552     /**
553      * This method returns the context object responsible for managing the
554      * given name.
555      */

556     private Context JavaDoc getContext(String JavaDoc name) throws NamingException JavaDoc {
557         return getContext(new NamingParser().parse(name));
558     }
559     
560     
561     /**
562      * This method returns the context object responsible for managing the
563      * given name.
564      */

565     private Context JavaDoc getContext(Name JavaDoc name) {
566         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
567         // check if the naming being supplied is a JNDI standard url or if
568
// it should go into the COS naming service
569
if (!name.get(0).equals(NamingConstants.JAVA_JNDI_PREFIX))
570         {
571             return cosContext;
572         }
573         // look for a local context to support the lookup
574
synchronized(classLoaderMemoryContexts) {
575             if (classLoaderMemoryContexts.containsKey(loader)) {
576                 return (Context JavaDoc)classLoaderMemoryContexts.get(loader);
577             }
578         }
579         // return the master memory context
580
return masterMemoryContext;
581     }
582     
583     
584     /**
585      * This method process the result of the call
586      *
587      * @return The result of the processing.
588      * @param result The result to process.
589      */

590     private Object JavaDoc processResult(Object JavaDoc result,String JavaDoc name) throws NamingException JavaDoc {
591         return processResult(result, new NamingParser().parse(name));
592     }
593     
594     /**
595      * This method process the result of the object lookup.
596      *
597      * @return The result of the processing.
598      * @param result The result to process.
599      */

600     private Object JavaDoc processResult(Object JavaDoc result,Name JavaDoc name) throws NamingException JavaDoc {
601         try {
602             if (result instanceof Reference JavaDoc) {
603                 Reference JavaDoc ref = (Reference JavaDoc)result;
604                 ObjectFactory JavaDoc objFactory = (ObjectFactory JavaDoc)(Class.forName(
605                         ref.getFactoryClassName())).newInstance();
606                 return objFactory.getObjectInstance(ref,name,this,
607                         this.getEnvironment());
608             }
609             return result;
610         } catch (Exception JavaDoc ex) {
611             log.error("Failed to process the result : " +
612                     ex.getMessage(),ex);
613             throw new NamingException JavaDoc("Failed to process the result : " +
614                     ex.getMessage());
615         }
616     }
617 }
618
Popular Tags