KickJava   Java API By Example, From Geeks To Geeks.

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


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  * CosContext.java
20  *
21  * This object wrapps the complexity of speaking to the Cos Naming Server.
22  */

23
24 // package path
25
package com.rift.coad.lib.naming.cos;
26
27 // java imports
28
import java.io.Serializable JavaDoc;
29 import java.rmi.Remote JavaDoc;
30 import java.util.Hashtable JavaDoc;
31 import javax.naming.Context JavaDoc;
32 import javax.naming.InitialContext JavaDoc;
33 import javax.naming.Name JavaDoc;
34 import javax.naming.NameParser JavaDoc;
35 import javax.naming.NamingEnumeration JavaDoc;
36 import javax.naming.NamingException JavaDoc;
37 import javax.naming.OperationNotSupportedException JavaDoc;
38 import javax.naming.Reference JavaDoc;
39 import javax.naming.Referenceable JavaDoc;
40 import javax.rmi.PortableRemoteObject JavaDoc;
41 import org.omg.CORBA.Any JavaDoc;
42 import org.omg.CORBA.TypeCode JavaDoc;
43 import org.omg.CORBA.portable.ObjectImpl JavaDoc;
44 import org.omg.PortableServer.POA JavaDoc;
45 import org.omg.DynamicAny.DynAny JavaDoc;
46 import org.omg.DynamicAny.DynAnyFactory JavaDoc;
47 import org.omg.DynamicAny.DynAnyFactoryHelper JavaDoc;
48 import org.omg.CosNaming.NamingContext JavaDoc;
49 import org.omg.CosNaming.NamingContextHelper JavaDoc;
50 import org.omg.PortableServer.Servant JavaDoc;
51 import org.omg.PortableServer.ForwardRequest JavaDoc;
52 import org.omg.PortableServer.IdAssignmentPolicyValue JavaDoc;
53 import org.omg.PortableServer.LifespanPolicyValue JavaDoc;
54 import org.omg.PortableServer.RequestProcessingPolicyValue JavaDoc;
55 import javax.rmi.CORBA.Util JavaDoc;
56
57 // logging import
58
import org.apache.log4j.Logger;
59
60 // carol imports
61
// We use the carol objects to store none org.omg.CORBA.Object values in the
62
// Cos naming server. It is not recommened that information other than corba
63
// objects get stored in the cos naming server but this gives us a way to do it
64
// if someone requires this functionality.
65
import org.objectweb.carol.jndi.wrapping.JNDIRemoteResource;
66 import org.objectweb.carol.jndi.wrapping.JNDIReferenceWrapper;
67 import org.objectweb.carol.jndi.wrapping.JNDIResourceWrapper;
68
69 // coadunation imports
70
import com.rift.coad.lib.thread.CoadunationThreadGroup;
71 import com.rift.coad.lib.configuration.Configuration;
72 import com.rift.coad.lib.configuration.ConfigurationFactory;
73 import com.rift.coad.lib.naming.NamingConstants;
74 import com.rift.coad.lib.naming.OrbManager;
75 import com.rift.coad.lib.thread.BasicThread;
76 import com.rift.coad.lib.thread.ThreadStateMonitor;
77 import com.rift.coad.lib.naming.NamingDirector;
78
79 /**
80  * This object wrapps the complexity of speaking to the Cos Naming Server.
81  *
82  * @author Brett Chaldecott
83  */

84 public class CosContext implements Context JavaDoc {
85     
86     /**
87      * The class responsible for binding this nameserver context to the master
88      * nameserver context.
89      */

90     public class InstanceBinder extends BasicThread {
91         
92         // the classes private member variables
93
private ThreadStateMonitor stateMonitor = new
94                 ThreadStateMonitor(60 * 1000);
95         
96         /**
97          * The constructor of the instance binder thread.
98          *
99          * @exception Exception
100          */

101         public InstanceBinder() throws Exception JavaDoc {
102             
103         }
104         
105         
106         /**
107          * This method is responsible for performing the processing in the cos
108          * context.
109          */

110         public void process() {
111             while(!stateMonitor.isTerminated()) {
112                 try {
113                     log.info("Making a connection to the name server context");
114                     NamingContext JavaDoc namingContext = NamingContextHelper.narrow(
115                             orbManager.getORB().string_to_object(instanceCosURL));
116                     log.info("Binding to the primary context [" + masterCosURL
117                             + "]");
118                     Context JavaDoc context = getInitialContext(instanceURL);
119                     
120                     log.info("Loop through the instance url and add [" +
121                             instanceURL.toString() + "]");
122                     for (int index = 0; index < instanceURL.size() - 1; index++){
123                         try {
124                             context = (Context JavaDoc)context.lookup(
125                                     instanceURL.get(index));
126                         } catch (javax.naming.NameNotFoundException JavaDoc ex) {
127                             context = context.createSubcontext(
128                                     instanceURL.get(index));
129                         }
130                     }
131                     log.info("Add entry to the context");
132                     context.rebind(instanceURL.get(instanceURL.size() -1),
133                             namingContext);
134                     log.info("Registered with master Cos Naming service");
135                     break;
136                 } catch (Exception JavaDoc ex) {
137                     log.error("Failed to bind to the mater cos name server : " +
138                             ex.getMessage(),ex);
139                 }
140                 stateMonitor.monitor();
141             }
142         }
143         
144         
145         /**
146          * The terminate method.
147          */

148         public void terminate() {
149             stateMonitor.terminate(true);
150         }
151     }
152             
153     
154     // the class log variable
155
protected Logger log =
156         Logger.getLogger(CosContext.class.getName());
157     
158     // class constants
159
private final static String JavaDoc SUN_COS_CONTEXT_FACTORY =
160             "com.sun.jndi.cosnaming.CNCtxFactory";
161     private final static String JavaDoc PRIMARY = "primary";
162     private final static String JavaDoc INSTANCE_COS_URL = "instance_cos_url";
163     private final static String JavaDoc MASTER_COS_URL = "master_cos_url";
164     private final static String JavaDoc USER = "cos_user";
165             
166     // class private member variables
167
private Hashtable JavaDoc env = null;
168     private CoadunationThreadGroup threadGroup = null;
169     private OrbManager orbManager = null;
170     private Name JavaDoc base = null;
171     private Name JavaDoc instanceURL = null;
172     private String JavaDoc instanceCosURL = null;
173     private Context JavaDoc instanceContext = null;
174     private boolean primary = false;
175     private String JavaDoc masterCosURL = null;
176     private Context JavaDoc masterContext = null;
177     private InstanceBinder instanceBinder = null;
178     
179     
180     /**
181      * Creates a new instance of CosContext
182      *
183      * @param env The environment of the cos context.
184      * @param threadGroup The reference to the coadunation thread group.
185      * @param instanceId The id of the coadunation instance.
186      * @exception NamingException
187      */

188     public CosContext(Hashtable JavaDoc env, CoadunationThreadGroup threadGroup,
189             OrbManager orbManager,String JavaDoc instanceId) throws NamingException JavaDoc {
190         try {
191             this.env = env;
192             this.threadGroup = threadGroup.createThreadGroup();
193             this.orbManager = orbManager;
194             Configuration config = ConfigurationFactory.getInstance().
195                     getConfig(this.getClass());
196             
197             instanceCosURL = config.getString(INSTANCE_COS_URL);
198             if (config.getBoolean(PRIMARY)) {
199                 instanceURL = new NamingParser().parse(config.getString(
200                         NamingDirector.PRIMARY_URL));
201                 base = instanceURL;
202                 primary = true;
203                 log.info("Running as primary");
204             } else {
205                 instanceURL = new NamingParser().parse(config.getString(
206                         NamingDirector.PRIMARY_URL))
207                         .add(NamingConstants.SUBCONTEXT)
208                         .add(instanceId);
209             
210                 base = new NamingParser().parse("");
211                 masterCosURL = config.getString(MASTER_COS_URL);
212                 
213                 // instanciate the new instance binder thread thread.
214
instanceBinder = new InstanceBinder();
215                 threadGroup.addThread(instanceBinder,config.getString(USER));
216                 instanceBinder.start();
217                 log.info("Running as secondary");
218                 
219             }
220             
221         } catch (Exception JavaDoc ex) {
222             log.error("Failed to create a new URL context : " +
223                     ex.getMessage());
224             throw new NamingException JavaDoc("Failed to create a new URL context : " +
225                     ex.getMessage());
226         }
227     }
228     
229     /**
230      * Adds a new environment property to the environment of this context.
231      *
232      * @return The previous value of the property or null.
233      * @param propName The property to replace or add.
234      * @param propValue The new property value.
235      */

236     public Object JavaDoc addToEnvironment(String JavaDoc propName, Object JavaDoc propVal) throws
237             NamingException JavaDoc {
238         return null;
239     }
240     
241     
242     /**
243      * Binds a name to an object.
244      *
245      * @param name The name of the object to bind.
246      * @param obj The object to bind.
247      * @exception NamingException
248      */

249     public void bind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
250         if (name.get(0).equals(NamingConstants.JNDI_NETWORK_PREFIX)) {
251             throw new NamingException JavaDoc(
252                     "Not allowed to bind to the base context. " +
253                     "Must use relative binding");
254         }
255         Context JavaDoc context = getInitialContext(name);
256         try {
257             Name JavaDoc composedName = composeName(name, base);
258             for (int index = 0; index < (composedName.size() -1); index++) {
259                 try {
260                     context = (Context JavaDoc)context.lookup(composedName.get(index));
261                 } catch (javax.naming.NameNotFoundException JavaDoc ex) {
262                     context = context.createSubcontext(composedName.get(index));
263                 }
264             }
265             log.info("Binding object [" + composedName.toString() + "] [" +
266                     name.get(0) + "][" + composedName.get(
267                     composedName.size() -1) + "] object [" +
268                     obj.getClass().getName() + "]");
269             context.bind(composedName.get(composedName.size() -1),
270                     wrapBindingValue(obj));
271         } catch (NamingException JavaDoc ex) {
272             resetContext(context);
273             log.error("Failed to bind the object to the context :" +
274                     ex.getMessage(),ex);
275             throw ex;
276         } catch (Exception JavaDoc ex) {
277             resetContext(context);
278             log.error("Failed to bind the object to the context :" +
279                     ex.getMessage(),ex);
280             throw new NamingException JavaDoc(
281                     "Failed to bind the object to the context :" +
282                     ex.getMessage());
283         }
284     }
285     
286     
287     /**
288      * Binds a name to an object.
289      */

290     public void bind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
291         bind(new NamingParser().parse(name),obj);
292     }
293     
294     
295     /**
296      * Closes this context.
297      */

298     public void close() throws NamingException JavaDoc {
299         if (instanceContext != null) {
300             instanceContext.close();
301         }
302         if (masterContext != null) {
303             masterContext.close();
304         }
305     }
306     
307     
308     /**
309      * Composes the name of this context with a name relative to this context.
310      *
311      * @return The compisit name.
312      * @param name The name to add to the prefix.
313      * @param prefix The prefix of the current context.
314      * @exception NamingException
315      */

316     public Name JavaDoc composeName(Name JavaDoc name, Name JavaDoc prefix) throws NamingException JavaDoc {
317         Name JavaDoc tmp = (Name JavaDoc)prefix.clone();
318         tmp.addAll(name);
319         return tmp;
320     }
321     
322     
323     /**
324      * Composes the name of this context with a name relative to this context.
325      *
326      * @return The string version of the composed name.
327      * @param name The name to add to the preffix.
328      * @param prefix The prefix for this context.
329      */

330     public String JavaDoc composeName(String JavaDoc name, String JavaDoc prefix) throws
331             NamingException JavaDoc {
332         return composeName(new NamingParser().parse(name),
333                 new NamingParser().parse(prefix)).toString();
334     }
335     
336     
337     /**
338      * Creates and binds a new context to this context.
339      *
340      * @return The newly created context.
341      * @param name The name of the new sub context.
342      * @exception NamingException
343      */

344     public Context JavaDoc createSubcontext(Name JavaDoc name) throws NamingException JavaDoc {
345         if (name.get(0).equals(NamingConstants.JNDI_NETWORK_PREFIX)) {
346             throw new NamingException JavaDoc(
347                     "Must use relative bindings.");
348         }
349         Context JavaDoc context = getInitialContext(name);
350         try {
351             Name JavaDoc composedName = composeName(name, base);
352             for (int index = 0; index < (composedName.size()); index++) {
353                 try {
354                     context = (Context JavaDoc)context.lookup(composedName.get(index));
355                 } catch (javax.naming.NameNotFoundException JavaDoc ex) {
356                     context = context.createSubcontext(composedName.get(index));
357                 }
358             }
359             return context;
360         } catch (NamingException JavaDoc ex) {
361             resetContext(context);
362             log.error("Failed to create the sub context :" +
363                     ex.getMessage(),ex);
364             throw ex;
365         } catch (Exception JavaDoc ex) {
366             resetContext(context);
367             log.error("Failed to create the sub context :" +
368                     ex.getMessage(),ex);
369             throw new NamingException JavaDoc(
370                     "Failed to create the sub context :" +
371                     ex.getMessage());
372         }
373     }
374     
375     
376     /**
377      * Creates and binds a new context.
378      *
379      * @return The newly create sub context.
380      * @exception name The name of the new sub context.
381      * @exception NamingException
382      */

383     public Context JavaDoc createSubcontext(String JavaDoc name) throws NamingException JavaDoc {
384         return createSubcontext(new NamingParser().parse(name));
385     }
386     
387     
388     /**
389      * Destroys the named context and removes it from the namespace.
390      *
391      * @param name The name of the sub context to remove.
392      * @exception NamingException
393      */

394     public void destroySubcontext(Name JavaDoc name) throws NamingException JavaDoc {
395         if (name.get(0).equals(NamingConstants.JNDI_NETWORK_PREFIX)) {
396             throw new NamingException JavaDoc(
397                     "Must use relative bindings.");
398         }
399         Context JavaDoc context = getInitialContext(name);
400         try {
401             Name JavaDoc composedName = composeName(name, base);
402             context.destroySubcontext(composedName);
403         } catch (NamingException JavaDoc ex) {
404             resetContext(context);
405             log.error("Failed to destroy the sub context :" +
406                     ex.getMessage(),ex);
407             throw ex;
408         } catch (Exception JavaDoc ex) {
409             resetContext(context);
410             log.error("Failed to destroy the sub context :" +
411                     ex.getMessage(),ex);
412             throw new NamingException JavaDoc(
413                     "Failed to destroy the sub context :" +
414                     ex.getMessage());
415         }
416     }
417     
418     
419     /**
420      * Destroys the named context and removes it from the namespace.
421      *
422      * @param name The name of the context to destroy.
423      */

424     public void destroySubcontext(String JavaDoc name) throws NamingException JavaDoc {
425         destroySubcontext(new NamingParser().parse(name));
426     }
427     
428     
429     /**
430      * Retrieves the environment in effect for this context.
431      *
432      * @return The reference to the hash table.
433      * @exception NamingException
434      */

435     public Hashtable JavaDoc getEnvironment() throws NamingException JavaDoc {
436         return null;
437     }
438     
439     
440     /**
441      * Retrieves the full name of this context within its own namespace.
442      */

443     public String JavaDoc getNameInNamespace() throws NamingException JavaDoc {
444         return this.instanceURL.toString();
445     }
446     
447     
448     /**
449      * Retrieves the parser associated with the named context.
450      *
451      * @return The reference to the name parser.
452      * @param name The name to return the parser for.
453      * @exception NamingException
454      */

455     public NameParser JavaDoc getNameParser(Name JavaDoc name) throws NamingException JavaDoc {
456         return new NamingParser();
457     }
458     
459     
460     /**
461      * Retrieves the parser associated with the named context.
462      */

463     public NameParser JavaDoc getNameParser(String JavaDoc name) throws NamingException JavaDoc {
464         return new NamingParser();
465     }
466     
467     
468     /**
469      * Enumerates the names bound in the named context, along with the class
470      * names of objects bound to them.
471      */

472     public NamingEnumeration JavaDoc list(Name JavaDoc name) throws NamingException JavaDoc {
473         throw new OperationNotSupportedException JavaDoc(
474                 "This operation is currently not supported");
475     }
476     
477     
478     /**
479      * Enumerates the names bound in the named context, along with the class
480      * names of objects bound to them.
481      *
482      * @return The list of names bound to this context.
483      * @param name The list of names.
484      * @exception NamingException
485      */

486     public NamingEnumeration JavaDoc list(String JavaDoc name) throws NamingException JavaDoc {
487         throw new OperationNotSupportedException JavaDoc(
488                 "This operation is currently not supported");
489     }
490     
491     
492     /**
493      * Enumerates the names bound in the named context, along with the objects
494      * bound to them.
495      *
496      * @return The list of bindings for the name
497      * @param name The name to perform the search below.
498      * @exception NamingException
499      */

500     public NamingEnumeration JavaDoc listBindings(Name JavaDoc name) throws NamingException JavaDoc {
501         throw new OperationNotSupportedException JavaDoc(
502                 "This operation is currently not supported");
503     }
504     
505     
506     /**
507      * Enumerates the names bound in the named context, along with the objects
508      * bound to them.
509      *
510      * @return The list of binding for the name.
511      * @param name The name to perform the search for.
512      * @exception NamingException
513      */

514     public NamingEnumeration JavaDoc listBindings(String JavaDoc name) throws NamingException JavaDoc {
515         throw new OperationNotSupportedException JavaDoc(
516                 "This operation is currently not supported");
517     }
518     
519     
520     /**
521      * Retrieves the named object.
522      *
523      * @return The named object.
524      * @param name The name to retrieve the object for.
525      * @exception NamingException
526      */

527     public Object JavaDoc lookup(Name JavaDoc name) throws NamingException JavaDoc {
528         Context JavaDoc context = getInitialContext(name);
529         String JavaDoc currentName = null;
530         try {
531             Name JavaDoc composedName = null;
532             if (name.get(0).equals(NamingConstants.JNDI_NETWORK_PREFIX)) {
533                 composedName = name;
534             } else {
535                 composedName = composeName(name, base);
536             }
537             for (int index =0; index < (composedName.size() - 1); index++ ) {
538                 currentName = composedName.get(index);
539                 context = (Context JavaDoc)context.lookup(currentName);
540             }
541             currentName = composedName.get(composedName.size() - 1);
542             return unwrapBindingValue(
543                     context.lookup(currentName));
544         } catch (NamingException JavaDoc ex) {
545             resetContext(context);
546             log.error("Failed to lookup the object [" + currentName + "] :" +
547                     ex.getMessage(),ex);
548             throw ex;
549         } catch (Exception JavaDoc ex) {
550             resetContext(context);
551             log.error("Failed to lookup the object [" + currentName + "] :" +
552                     ex.getMessage(),ex);
553             throw new NamingException JavaDoc(
554                     "Failed to lookup the object [" + currentName + "] :" +
555                     ex.getMessage());
556         }
557     }
558     
559     
560     /**
561      * Retrieves the named object.
562      *
563      * @return The object to retrieve by name.
564      * @param name The name of the object to retrieve.
565      * @exception NamingException
566      */

567     public Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc {
568         return lookup(new NamingParser().parse(name));
569     }
570     
571     
572     /**
573      * Retrieves the named object, following links except for the terminal
574      * atomic component of the name.
575      *
576      * @return The object to retrieve.
577      * @param name The name of the object to lookup.
578      * @exception NamingException
579      */

580     public Object JavaDoc lookupLink(Name JavaDoc name) throws NamingException JavaDoc {
581         throw new OperationNotSupportedException JavaDoc(
582                 "This method is not currently supported");
583     }
584     
585     
586     /**
587      * Retrieves the named object, following links except for the terminal
588      * atomic component of the name.
589      *
590      * @return The results of the lookup link.
591      * @param name The name of the object to lookup.
592      * @exception NamingException
593      */

594     public Object JavaDoc lookupLink(String JavaDoc name) throws NamingException JavaDoc {
595         return lookupLink(new NamingParser().parse(name));
596     }
597     
598     
599     /**
600      * Binds a name to an object, overwriting any existing binding.
601      *
602      * @param name The name to rebind.
603      * @param obj The object to rebind.
604      * @exception NamingException
605      */

606     public void rebind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
607         if (name.get(0).equals(NamingConstants.JNDI_NETWORK_PREFIX)) {
608             throw new NamingException JavaDoc(
609                     "Not allowed to bind to the base context. " +
610                     "Must use relative binding");
611         }
612         Context JavaDoc context = getInitialContext(name);
613         try {
614             Name JavaDoc composedName = composeName(name, base);
615             for (int index = 0; index < (composedName.size() -1); index++) {
616                 try {
617                     context = (Context JavaDoc)context.lookup(composedName.get(index));
618                 } catch (javax.naming.NameNotFoundException JavaDoc ex) {
619                     context = context.createSubcontext(composedName.get(index));
620                 }
621             }
622             log.info("Rebinding object [" + composedName.toString() + "] [" +
623                     name.get(0) + "][" + composedName.get(
624                     composedName.size() -1) + "] object [" +
625                     obj.getClass().getName() + "]");
626             context.rebind(composedName.get(composedName.size() -1),
627                     wrapBindingValue(obj));
628         } catch (NamingException JavaDoc ex) {
629             resetContext(context);
630             log.error("Failed to rebind the object to the context :" +
631                     ex.getMessage(),ex);
632             throw ex;
633         } catch (Exception JavaDoc ex) {
634             resetContext(context);
635             log.error("Failed to rebind the object to the context :" +
636                     ex.getMessage(),ex);
637             throw new NamingException JavaDoc(
638                     "Failed to rebind the object to the context :" +
639                     ex.getMessage());
640         }
641     }
642     
643     
644     /**
645      * Binds a name to an object, overwriting any existing binding.
646      *
647      * @param name The name to rebind.
648      * @param obj The object to rebind.
649      * @exception NamingException
650      */

651     public void rebind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
652         rebind(new NamingParser().parse(name),obj);
653     }
654     
655     
656     /**
657      * Removes an environment property from the environment of this context.
658      *
659      * @param propName The name of the entry to remove from the environment.
660      * @exception NamingException
661      */

662     public Object JavaDoc removeFromEnvironment(String JavaDoc propName) throws NamingException JavaDoc {
663         return null;
664     }
665     
666     
667     /**
668      * Binds a new name to the object bound to an old name, and unbinds the old
669      * name.
670      *
671      * @param oldName The old name to rename.
672      * @param newName The name to replace it with.
673      * @exception NamingException
674      */

675     public void rename(Name JavaDoc oldName, Name JavaDoc newName) throws NamingException JavaDoc {
676         if (oldName.get(0).equals(NamingConstants.JNDI_NETWORK_PREFIX) ||
677                 newName.get(0).equals(NamingConstants.JNDI_NETWORK_PREFIX)) {
678             throw new NamingException JavaDoc(
679                     "Must use relative binding");
680         }
681         Context JavaDoc context = getInitialContext(oldName);
682         try {
683             Object JavaDoc value = lookup(oldName);
684             bind(newName,value);
685             unbind(oldName);
686         } catch (NamingException JavaDoc ex) {
687             resetContext(context);
688             log.error("Failed to rename the object in the context :" +
689                     ex.getMessage(),ex);
690             throw ex;
691         } catch (Exception JavaDoc ex) {
692             resetContext(context);
693             log.error("Failed to rename the object in the context :" +
694                     ex.getMessage(),ex);
695             throw new NamingException JavaDoc(
696                     "Failed to rename the object in the context :" +
697                     ex.getMessage());
698         }
699     }
700     
701     
702     /**
703      * Binds a new name to the object bound to an old name, and unbinds the old
704      * name.
705      *
706      * @param oldName The old name to rename.
707      * @param newName The name to replace it with.
708      * @exception NamingException
709      */

710     public void rename(String JavaDoc oldName, String JavaDoc newName) throws NamingException JavaDoc {
711         rename(new NamingParser().parse(oldName),
712                 new NamingParser().parse(newName));
713     }
714     
715     
716     /**
717      * Unbinds the named object.
718      *
719      * @param name The name to unbind.
720      * @exception NamingException
721      */

722     public void unbind(Name JavaDoc name) throws NamingException JavaDoc {
723         if (name.get(0).equals(NamingConstants.JNDI_NETWORK_PREFIX)) {
724             throw new NamingException JavaDoc(
725                     "Not allowed to use the base context. " +
726                     "Must use relative binding");
727         }
728         Context JavaDoc context = getInitialContext(name);
729         try {
730             Name JavaDoc composedName = composeName(name, base);
731             context.unbind(composedName);
732         } catch (NamingException JavaDoc ex) {
733             resetContext(context);
734             log.error("Failed to unbind the object to the context :" +
735                     ex.getMessage(),ex);
736             throw ex;
737         } catch (Exception JavaDoc ex) {
738             resetContext(context);
739             log.error("Failed to unbind the object to the context :" +
740                     ex.getMessage(),ex);
741             throw new NamingException JavaDoc(
742                     "Failed to unbind the object to the context :" +
743                     ex.getMessage());
744         }
745     }
746     
747     
748     /**
749      * Unbinds the named object.
750      *
751      * @param name The name to unbind.
752      * @exception NamingException
753      */

754     public void unbind(String JavaDoc name) throws NamingException JavaDoc {
755         unbind(new NamingParser().parse(name));
756     }
757     
758     
759     /**
760      * This method is responsible for terminating the processing of this cos
761      * context.
762      */

763     public void terminate() {
764         threadGroup.terminate();
765         try {
766             close();
767         } catch (Exception JavaDoc ex) {
768             log.error("Failed to close the contexts : " + ex.getMessage(),ex);
769         }
770         if (instanceBinder != null) {
771             instanceBinder.terminate();
772         }
773     }
774     
775     
776     /**
777      * This method returns the initial context that matches the given name.
778      *
779      * @return The context that matches the name.
780      * @param name The name to retrieve the context for.
781      * @exception NamingException
782      */

783     private Context JavaDoc getInitialContext(Name JavaDoc name) throws NamingException JavaDoc {
784         try {
785             if (primary || !(name.get(0).equals(this.instanceURL.get(0)))) {
786                 synchronized(this) {
787                     if (instanceContext != null) {
788                         return instanceContext;
789                     }
790                     Hashtable JavaDoc env = new Hashtable JavaDoc();
791                     env.put(Context.INITIAL_CONTEXT_FACTORY,
792                             SUN_COS_CONTEXT_FACTORY);
793                     env.put(Context.PROVIDER_URL,
794                             instanceCosURL);
795                     env.put("java.naming.corba.orb",orbManager.getORB());
796                     return instanceContext = new InitialContext JavaDoc(env);
797                 }
798             }
799             synchronized(this) {
800                 if (masterContext != null) {
801                     return masterContext;
802                 }
803                 Hashtable JavaDoc env = new Hashtable JavaDoc();
804                 env.put(Context.INITIAL_CONTEXT_FACTORY,
805                         SUN_COS_CONTEXT_FACTORY);
806                 env.put(Context.PROVIDER_URL,
807                         masterCosURL);
808                 env.put("java.naming.corba.orb",orbManager.getORB());
809                 log.info("Retrieve the master context url");
810                 return masterContext = new InitialContext JavaDoc(env);
811             }
812         } catch (Exception JavaDoc ex) {
813             log.error("Failed to retrieve an initial context for the name [" +
814                     name.toString() + "] because : " + ex.getMessage(),ex);
815             throw new NamingException JavaDoc("Failed to retrieve an initial context " +
816                     "for the name [" + name.toString() + "] because : " +
817                     ex.getMessage());
818         }
819     }
820     
821     
822     /**
823      * This method is used to reset contexts that are not working correctly.
824      *
825      * @param context The reference to the context that needs resetting
826      */

827     private void resetContext(Context JavaDoc context) {
828         if (context == instanceContext) {
829             instanceContext = null;
830         }
831         if (context == masterContext) {
832             masterContext = null;
833         }
834     }
835     
836     
837     /**
838      * This method is responsible for wrapping the binding value. It uses the
839      * dynamic any value stub code to perform this task.
840      *
841      * @return The wrapped object or Remote.
842      * @param value The object to wrap if remote it will just be returned.
843      * @exception NamingException
844      */

845     public Object JavaDoc wrapBindingValue(Object JavaDoc value) throws NamingException JavaDoc {
846         if (value instanceof Remote JavaDoc) {
847             return value;
848         } else if (!(value instanceof Serializable JavaDoc)) {
849             throw new NamingException JavaDoc("binding value is not instance of " +
850                     "Remote or Serializable cannot be stored in Cos Naming");
851         }
852         JNDIResourceWrapper resourceWrapper = null;
853         if (value instanceof Referenceable JavaDoc) {
854             resourceWrapper = new JNDIResourceWrapper((Serializable JavaDoc)((
855                     Referenceable JavaDoc)value).
856                     getReference());
857         } else {
858             resourceWrapper = new JNDIResourceWrapper((Serializable JavaDoc)value);
859         }
860         try {
861             PortableRemoteObject.exportObject(resourceWrapper);
862         } catch (Exception JavaDoc ex) {
863             log.error("Failed to export wrapper :" + ex.getMessage(),ex);
864             throw new NamingException JavaDoc("Failed to export wrapper :" +
865                     ex.getMessage());
866         }
867         return resourceWrapper;
868     }
869     
870     
871     /**
872      * This method unwrapps the retrieved value. If it is an instance of remote
873      * than the value just gets returned.
874      *
875      * @return The unwrapped object.
876      * @param value The value to unwrapp.
877      * @param name The name of the bound object used for Referenceble objects.
878      * @exception NamingException
879      */

880     public Object JavaDoc unwrapBindingValue(Object JavaDoc value) throws
881             NamingException JavaDoc {
882         try {
883             ObjectImpl JavaDoc objectImpl = (ObjectImpl JavaDoc)PortableRemoteObject.narrow(
884                     value,ObjectImpl JavaDoc.class);
885             String JavaDoc[] ids = objectImpl._ids();
886             String JavaDoc itf = ids[0];
887             
888             if (itf.indexOf(":org.objectweb.carol.jndi.wrapping." +
889                     "JNDIRemoteResource:") != -1) {
890                 JNDIRemoteResource jndiRemoteResource = (JNDIRemoteResource)
891                     PortableRemoteObject.narrow(value,JNDIRemoteResource.class);
892                 return jndiRemoteResource.getResource();
893             }
894             
895             return value;
896         } catch (Exception JavaDoc ex) {
897             log.error("Failed to wrapp the object :" + ex.getMessage(),ex);
898             throw new NamingException JavaDoc("Failed to wrapp the object :" +
899                     ex.getMessage());
900         }
901     }
902 }
903
Popular Tags