KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > descriptor > componentassembly > ccm > deployer > registrar > RegisterwithnamingDeployer


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2005 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Briclet Frédéric.
23 Contributor(s): Philippe Merle.
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.descriptor.componentassembly.ccm.deployer.registrar;
28
29 /**
30  * Specific deployer allowing to register a corba object and to unbind it
31  * @author <a HREF="mailto:frederic.briclet@lifl.fr">Briclet Frederic</a>
32  *
33  * @version 0.1
34  */

35
36 public class RegisterwithnamingDeployer
37        extends RegisterwithnamingDeployerContext
38     {
39     // ==================================================================
40
//
41
// Internal state
42
//
43
// ==================================================================
44

45     /** Namecomponent use to found object*/
46     private org.omg.CosNaming.NameComponent JavaDoc [] ncomp;
47     /* Boolean indocating if the referencement have failed*/
48     private boolean failed;
49     /*reference on naming context*/
50     private org.omg.CosNaming.NamingContextExt JavaDoc rootContext;
51       
52     // ==================================================================
53
//
54
// Constructor.
55
//
56
// ==================================================================
57

58     // ==================================================================
59
//
60
// Internal methods.
61
//
62
// ==================================================================
63

64     // ==================================================================
65
//
66
// Public methods.
67
//
68
// ==================================================================
69

70     /**
71      * Rebind method in charge to register the object give in parameter
72      * with the name service.
73      *
74      * @param objectToRebind the object to register with the name service
75      * @throws NameserviceRegistrationFailureException if registration failed
76      */

77     public void
78     rebind(org.omg.CORBA.Object JavaDoc objectToRebind)
79     throws NameserviceRegistrationFailureException
80     {
81
82         /*Retrieve a NamingContextExt*/
83         rootContext=getNamingcontextExt();
84         try{
85             //Invoque the rebind
86
ncomp=rebind(rootContext,getRegisterwithnaming().getName(),objectToRebind);
87         }
88         catch(Exception JavaDoc e)
89             {
90                 failed=true;
91                 throw new NameserviceRegistrationFailureException(this,e," TO complete");
92             }
93         getLifeCycleManager().stepEndedSuccessFully();
94     }
95
96     /**
97      * Static method with parameter available out the component assembly.
98      * This method can be invoke by another object that deployer
99      *
100      * @param rootContextExt the naming context to use for rebinding
101      * @param name The rebinding name
102      * @param objectToRebind The corba object to rebind
103      */

104     public static org.omg.CosNaming.NameComponent JavaDoc []
105     rebind(org.omg.CosNaming.NamingContextExt JavaDoc rootContextExt,
106            String JavaDoc name,
107            org.omg.CORBA.Object JavaDoc objectToRebind)
108     throws Exception JavaDoc
109     {
110         String JavaDoc pathError="" ;
111         org.omg.CosNaming.NameComponent JavaDoc [] nameComp=null;
112         
113         nameComp=rootContextExt.to_name(name);
114         String JavaDoc nameTmp="";
115         //ADD a loop to find a valid name based on the given name
116
try{
117             rootContextExt.resolve(nameComp);
118             for(int i=0;true;i++)
119             {
120                 nameTmp=name+"("+i+")";
121                 nameComp=rootContextExt.to_name(nameTmp);
122                 rootContextExt.resolve(nameComp);
123             }
124                 
125         }
126         catch(org.omg.CosNaming.NamingContextPackage.NotFound JavaDoc nf )
127         {
128             
129         }
130         
131         try{
132             
133             rootContextExt.rebind(nameComp,objectToRebind);
134         }
135         catch(org.omg.CosNaming.NamingContextPackage.NotFound JavaDoc nf )
136             {
137                 org.omg.CosNaming.NamingContext JavaDoc context=rootContextExt;
138
139                 org.omg.CosNaming.NameComponent JavaDoc [] ncompTmp=
140                     new org.omg.CosNaming.NameComponent JavaDoc[1];
141                
142                 //All the context required doesn't exist
143

144                 for(int i=0; i<nameComp.length-1; i++)
145                     {
146                         pathError+=nameComp[i].id+"/";
147                         ncompTmp[0]=nameComp[i];
148                     
149                     try{
150                         context=org.omg.CosNaming.
151                             NamingContextHelper.narrow(context.resolve(ncompTmp));
152                     }
153                     catch(org.omg.CORBA.BAD_PARAM JavaDoc bp)
154                         {
155                             System.err.println("Invalid registeration name,"+pathError+
156                                                " is already used as full path to register a corba object ");
157                             System.err.println
158                                 ("The corba object cannot be registered, take care some connection can fail");
159                             throw new org.omg.CosNaming.NamingContextPackage.CannotProceed JavaDoc();
160                         }
161                     catch(Exception JavaDoc e)
162                     {
163                         try
164                         {
165                             context=context.bind_new_context(ncompTmp);
166                         }
167                         // AlreadyBound could be thrown when another application
168
// creates the naming context in parallel.
169
catch(org.omg.CosNaming.NamingContextPackage.AlreadyBound JavaDoc ab)
170                         {
171                              // If AlreadyBound thrown then just resolve the naming context
172
// once again as it is now created.
173
context=org.omg.CosNaming.NamingContextHelper.narrow(context.resolve(ncompTmp));
174                         }
175                     }
176                 }
177                 ncompTmp[0]=nameComp[nameComp.length-1];
178                 context.rebind(ncompTmp,objectToRebind);
179             }
180         return nameComp;
181     }
182
183     /**
184      * Unbind method which unregister from the name service the previous
185      * registered object
186      */

187     public void
188     unbind()
189     throws NameserviceRegistrationFailureException
190     {
191         if(failed)
192             return;
193         try{
194             org.objectweb.openccm.corba
195                 .TheNameService.getNamingContext()
196                 .getNamingContext().unbind(ncomp);
197         }
198         catch(Exception JavaDoc e){
199                 throw new NameserviceRegistrationFailureException(this,e,"Error occur during unbinding");
200         }
201         getLifeCycleManager().stepEndedSuccessFully();
202     }
203 }
204     
205
Popular Tags