KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 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
23 Initial developer(s): Sylvain Leblanc
24 Contributor(s): _____________________________________.
25
26 ====================================================================*/

27
28 package org.objectweb.openccm.descriptor.componentassembly.ccm.deployer.registrar;
29
30 //Importation of the needed packages
31
import org.objectweb.openccm.descriptor.componentassembly.*;
32
33 import org.objectweb.openccm.corba.TheTradingService;
34 import org.objectweb.openccm.corba.trader.TraderTypeCodeUtils;
35
36 /**
37  * Allows to compute actions on trading service from data
38  * contained in CCM descriptors.
39  *
40  * Provided features:
41  * <ul>
42  * <li>Registration of a home or component in the trading service.</li>
43  * </ul>
44  *
45  * @author <a HREF="mailto:Sylvain.Leblanc@lifl.fr">Sylvain Leblanc</a>
46  *
47  * @version 0.1
48  */

49 public class RegisterwithtraderDeployer extends RegisterwithtraderDeployerContext
50 {
51
52     private static final String JavaDoc header = "Trading Service problem: ";
53
54     // ==================================================================
55
//
56
// Internal state.
57
//
58
// ==================================================================
59

60     /** The CosTrading::OfferId provided by the trading registration. */
61     protected String JavaDoc _oid;
62
63     // ==================================================================
64
//
65
// Constructors.
66
//
67
// ==================================================================
68

69     /**
70      * The default constructor.
71      */

72     public
73     RegisterwithtraderDeployer ()
74     {
75         // Calling the super constructor
76
super();
77
78         // The registered offer id in the trader.
79
this._oid = null;
80     }
81
82     // ==================================================================
83
//
84
// Internal methods.
85
//
86
// ==================================================================
87

88     /**
89      * Resolve the register interface of a named trader.
90      *
91      * @param name The path name of the trader to access. The given
92      * name must stands for a path from the starting trader to the
93      * desired trader following a sequence of links. Here we assume
94      * that the path has the following form:
95      * <code>link_name1/link_name2/link_nameN</code>.
96      *
97      * @return The register interface of the named trader, or the
98      * domain local trader register interface if no name have been
99      * provided, or null if the name doesn't exist or if the named
100      * trader doesn't allows to export offers.
101      */

102     protected org.omg.CosTrading.Register
103     resolveExternalTrader(String JavaDoc name)
104         throws org.omg.CosTrading.RegisterPackage.IllegalTraderName,
105         org.omg.CosTrading.RegisterPackage.UnknownTraderName,
106         org.omg.CosTrading.RegisterPackage.RegisterNotSupported
107     {
108         // Resolving the used trader
109
org.omg.CosTrading.Register iRegister = null;
110         iRegister = TheTradingService.getRegister();
111         if (iRegister == null) return null;
112         
113         if (name != null && !name.trim().equals(""))
114         {
115             // Splitting the given name in a string array
116
java.util.LinkedList JavaDoc path_list = new java.util.LinkedList JavaDoc();
117             int idx = 0;
118             int prev_idx = 0;
119             idx = name.indexOf("/");
120             while (idx != -1) {
121                 String JavaDoc current = name.substring(prev_idx, idx);
122                 if (! current.trim().equals("")) path_list.add(current);
123                 prev_idx = idx+1;
124                 idx = name.indexOf("/", prev_idx);
125             }
126             String JavaDoc current = name.substring(prev_idx);
127             if (! current.trim().equals("")) path_list.add(current);
128             String JavaDoc[] path = (String JavaDoc[])path_list.toArray(new String JavaDoc[0]);
129
130             // Making registration
131
iRegister = iRegister.resolve(path);
132         }
133         return iRegister;
134     }
135
136     /**
137      * Construct a CosTrading property for a given service type using
138      * the property name and value as a string. The Property value
139      * type is checked using the service type repository description
140      * facilities. Here, the <code>fully_describe_type</code> method
141      * of the <code>CosTrading::Register</code> interface is used in
142      * order to retrieve this information.
143      *
144      * @param name The name of the property to create.
145      * @param value The value of the property as a string.
146      *
147      * @return The CosTrading property or null if cannot be constructed.
148      *
149      * @exception org.omg.CosTrading.IllegalServiceType If the service
150      * type name is not valid.
151      * @exception org.omg.CosTrading.UnknownServiceType If the service
152      * type doesn't exist in the service type repository.
153      */

154     protected org.omg.CosTrading.Property
155     constructProperty(String JavaDoc name, String JavaDoc value, String JavaDoc servicetype)
156         throws org.omg.CosTrading.IllegalServiceType,
157         org.omg.CosTrading.UnknownServiceType
158     {
159         org.omg.CORBA.TypeCode JavaDoc tc = null;
160
161         // Accessing to the service type repository in order to
162
// obtain the property type.
163
org.omg.CosTradingRepos.ServiceTypeRepositoryPackage.TypeStruct desc;
164         desc = TheTradingService.getRepository().fully_describe_type(servicetype);
165         for (int i=0 ; i<desc.props.length ; i++)
166         {
167             if (desc.props[i].name.equals(name))
168             {
169                 tc = desc.props[i].value_type;
170                 break;
171             }
172         }
173         if (tc == null) return null;
174
175         // Creating the used any value
176
org.omg.CORBA.Any JavaDoc the_any = TraderTypeCodeUtils.insertInAny(tc, value);
177         if (the_any == null) {
178             return null;
179         }
180
181         org.omg.CosTrading.Property prop = new org.omg.CosTrading.Property();
182         prop.name = name;
183         prop.value = the_any;
184         return prop;
185     }
186
187     /**
188      * Remove a service offer from the OMG Trading Service.
189      *
190      * @param offerId The identifier of the offer to remove.
191      */

192     protected void
193     remove_from_trader(String JavaDoc offerId)
194     {
195         try {
196             TheTradingService.getRegister().withdraw(offerId);
197         } catch (org.omg.CosTrading.IllegalOfferId ex) {
198             System.err.println(RegisterwithtraderDeployer.header +
199                              "cannot withdraw offer '" + offerId + "'.");
200         } catch (org.omg.CosTrading.UnknownOfferId ex) {
201             System.err.println(RegisterwithtraderDeployer.header +
202                              "cannot withdraw offer '" + offerId + "'.");
203         } catch (org.omg.CosTrading.RegisterPackage.ProxyOfferId ex) {
204             System.err.println(RegisterwithtraderDeployer.header +
205                              "cannot withdraw offer '" + offerId + "'.");
206         }
207     }
208
209     /**
210      * Register a CORBA Object in the OMG Trading Service if available.
211      *
212      * @param desc CCM Assembly Descriptor information on trader registration.
213      * @param to_register The CORBA Object to register in the trading service.
214      */

215     protected String JavaDoc
216     register_in_trader(Registerwithtrader desc,
217                        org.omg.CORBA.Object JavaDoc to_register)
218     {
219         String JavaDoc trader_name = desc.getTradername();
220         org.omg.CosTrading.Register registerer = null;
221         try {
222             registerer = resolveExternalTrader(trader_name);
223         } catch (org.omg.CosTrading.RegisterPackage.IllegalTraderName ex) {
224             System.err.println(RegisterwithtraderDeployer.header +
225                              "cannot register in trader " + trader_name + ".");
226             return null;
227         } catch (org.omg.CosTrading.RegisterPackage.UnknownTraderName ex) {
228             System.err.println(RegisterwithtraderDeployer.header +
229                              "cannot register in trader " + trader_name + ".");
230             return null;
231         } catch (org.omg.CosTrading.RegisterPackage.RegisterNotSupported ex) {
232             System.err.println(RegisterwithtraderDeployer.header +
233                              "cannot register in trader " + trader_name + ".");
234             return null;
235         }
236         if (registerer == null) {
237             System.err.println(RegisterwithtraderDeployer.header +
238                              "no trading service found.");
239             return null;
240         }
241         
242         // Retrieves the service type name.
243
Traderexport export_desc = desc.getTraderexport();
244         String JavaDoc st = export_desc.getTraderservicetypename().getValue();
245
246         // Constructing CORBA properties
247
java.util.List JavaDoc properties_desc = export_desc.getTraderproperties().getTraderpropertyList();
248         org.omg.CosTrading.Property[] corba_props = new org.omg.CosTrading.Property[properties_desc.size()];
249         int i = 0;
250         for (java.util.Iterator JavaDoc iter = properties_desc.iterator() ;
251              iter.hasNext() ; i++)
252         {
253             // Values from descriptor
254
Traderproperty xml_prop = (Traderproperty)iter.next();
255             String JavaDoc p_name = xml_prop.getTraderpropertyname().getValue();
256             String JavaDoc p_value = xml_prop.getTraderpropertyvalue().getValue();
257
258             // OMG IDL values
259
org.omg.CosTrading.Property the_prop = null;
260             try {
261                 the_prop = constructProperty(p_name, p_value, st);
262             } catch (org.omg.CosTrading.IllegalServiceType ex) {
263                 System.err.println(RegisterwithtraderDeployer.header +
264                                  "invalid service type.");
265                 return null;
266             } catch (org.omg.CosTrading.UnknownServiceType ex) {
267                 System.err.println(RegisterwithtraderDeployer.header +
268                                  "invalid service type.");
269                 return null;
270             }
271             if (the_prop == null) {
272                 System.err.println(RegisterwithtraderDeployer.header +
273                                  "cannot construct property.");
274                 return null;
275             }
276             corba_props[i] = the_prop;
277         }
278
279         // Proceeding with registration
280
try {
281             return registerer.export(to_register,st,corba_props);
282         } catch (org.omg.CosTrading.RegisterPackage.InvalidObjectRef ex) {
283             System.err.println(RegisterwithtraderDeployer.header +
284                              "Invalid CORBA Object reference.");
285             return null;
286         } catch (org.omg.CosTrading.IllegalServiceType ex) {
287             System.err.println(RegisterwithtraderDeployer.header +
288                              "Bad service type \"" + ex.type + "\"." );
289             return null;
290         } catch (org.omg.CosTrading.UnknownServiceType ex) {
291             System.err.println(RegisterwithtraderDeployer.header +
292                              "Bad service type \"" + ex.type + "\"." );
293             return null;
294         } catch (org.omg.CosTrading.RegisterPackage.InterfaceTypeMismatch ex) {
295             System.err.println(RegisterwithtraderDeployer.header +
296                              "Object interface " +
297                              "mismatch with service type \"" +
298                              ex.type +
299                              "\"." );
300             return null;
301         } catch (org.omg.CosTrading.IllegalPropertyName ex) {
302             System.err.println(RegisterwithtraderDeployer.header +
303                              "Bad property name \"" + ex.name + "\"." );
304             return null;
305         } catch (org.omg.CosTrading.PropertyTypeMismatch ex) {
306             System.err.println(RegisterwithtraderDeployer.header +
307                              "Bad property type of \"" +
308                              ex.prop.name +
309                              "\" in service type \"" +
310                              ex.type +
311                              "\"." );
312             return null;
313         } catch (org.omg.CosTrading.ReadonlyDynamicProperty ex) {
314             System.err.println(RegisterwithtraderDeployer.header +
315                              "Bad property of \"" +
316                              ex.name +
317                              "\" in service type \"" +
318                              ex.type +
319                              "\"." );
320             return null;
321         } catch (org.omg.CosTrading.MissingMandatoryProperty ex) {
322             System.err.println(RegisterwithtraderDeployer.header +
323                              "Require property \"" +
324                              ex.name +
325                              "\" in service type \"" +
326                              ex.type +
327                              "\"." );
328             return null;
329         } catch (org.omg.CosTrading.DuplicatePropertyName ex) {
330             System.err.println(RegisterwithtraderDeployer.header +
331                              "Duplicate property \"" +
332                              ex.name +
333                              "\"." );
334             return null;
335         }
336     }
337
338     // ==================================================================
339
//
340
// Public methods.
341
//
342
// ==================================================================
343

344     public void
345     deploy(org.omg.CORBA.Object JavaDoc objRef)
346     {
347         this._oid = register_in_trader(getRegisterwithtrader(), objRef);
348     }
349     
350     public void
351     tearDown()
352     {
353         if (this._oid != null) remove_from_trader(this._oid);
354         this._oid = null;
355     }
356 }
357
Popular Tags