KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > descriptor > componentassembly > ccm > deployer > binder > TraderqueryDeployer


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2003 USTL - LIFL - GOAL
5 Contact: openccm-team@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): Sylvain Leblanc
23 Contributor(s): _____________________________________.
24
25 ====================================================================*/

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

47 public class TraderqueryDeployer
48 {
49
50     private static final String JavaDoc header = "Trading Service query problem: ";
51
52     // ==================================================================
53
//
54
// Internal state.
55
//
56
// ==================================================================
57

58     /** The registered Offer Id */
59     protected String JavaDoc _oid;
60
61     // ==================================================================
62
//
63
// Constructors.
64
//
65
// ==================================================================
66

67     /**
68      * The default constructor.
69      */

70     public
71     TraderqueryDeployer ()
72     {
73         // The registered offer id in the trader.
74
this._oid = null;
75     }
76
77     // ==================================================================
78
//
79
// Internal methods.
80
//
81
// ==================================================================
82

83     /**
84      * Checks if a value has been defined for the "return_card" policy.
85      *
86      * @param policies A list of policies defined in XML.
87      *
88      * @return True if the "return_card" policy has been defined, false otherwise.
89      */

90     protected boolean
91     isReturnCardPolicyDefined(java.util.List JavaDoc policies)
92     {
93         for (java.util.Iterator JavaDoc iter = policies.iterator() ; iter.hasNext() ; )
94         {
95             Traderpolicy tp = (Traderpolicy)iter.next();
96             String JavaDoc policy_name = tp.getTraderpolicyname().getValue().trim().toLowerCase();
97             if (policy_name.equals("return_card")) return true;
98         }
99         return false;
100     }
101
102     /**
103      * Lookup an offer in the OMG Trading Service. The reference of
104      * the service of the first matching offer.
105      *
106      * @param desc The query description in the CCM ComponentAssembly
107      * descriptor.
108      *
109      * @return The first result of the query or null if the query
110      * has no result or cannot be completed.
111      */

112     public org.omg.CORBA.Object JavaDoc
113     lookup(Traderquery desc)
114     {
115         // Retrieve simple parameters
116
String JavaDoc st = desc.getTraderservicetypename().getValue();
117         String JavaDoc constraint = desc.getTraderconstraint().getValue();
118         String JavaDoc pref = "first"; // as the default preference (see
119
// Trading ObjectService spec)
120
if (desc.getTraderpreference() != null)
121             pref = desc.getTraderpreference().getValue();
122
123         // Construct a policies sequence
124
java.util.List JavaDoc policies = desc.getTraderpolicyList();
125         java.util.List JavaDoc cos_policies = new java.util.LinkedList JavaDoc();
126         for (java.util.Iterator JavaDoc iter = policies.iterator() ; iter.hasNext() ; )
127         {
128             Traderpolicy tp = (Traderpolicy)iter.next();
129             org.omg.CosTrading.Policy cos_policy = construct_policy(tp);
130             if (cos_policy != null) cos_policies.add(cos_policy);
131         }
132         // If no "match_return_card" policy has been defined, setting the match return card to 1
133
if (! isReturnCardPolicyDefined(policies)) {
134             org.omg.CosTrading.Policy return_card_policy = QueryPoliciesHelper.construct_policy("return_card", "1");
135             cos_policies.add(return_card_policy);
136         }
137         org.omg.CosTrading.Policy[] cos_policies_arr = (org.omg.CosTrading.Policy[])cos_policies.toArray(new org.omg.CosTrading.Policy[0]);
138
139         // Construct a SpecifedProp
140
java.util.List JavaDoc props = desc.getTraderspecifiedpropList();
141         org.omg.CosTrading.LookupPackage.SpecifiedProps cos_props = construct_props(props);
142
143
144         // Construct the used holders
145
org.omg.CosTrading.OfferSeqHolder offerSeq;
146         offerSeq = new org.omg.CosTrading.OfferSeqHolder();
147         org.omg.CosTrading.OfferIteratorHolder offerIter;
148         offerIter = new org.omg.CosTrading.OfferIteratorHolder();
149         org.omg.CosTrading.PolicyNameSeqHolder limits;
150         limits = new org.omg.CosTrading.PolicyNameSeqHolder();
151
152         // The CORBA reference to return
153
org.omg.CORBA.Object JavaDoc result = null;
154         try {
155             // perform the query
156
TheTradingService.getLookup().query(st, constraint,
157                                                 pref, cos_policies_arr,
158                                                 cos_props, 1,
159                                                 offerSeq, offerIter,
160                                                 limits);
161             // Then processing results
162
if (offerSeq.value.length != 0) {
163                 // there is a matching offer in the sequence
164
result = offerSeq.value[0].reference;
165             }
166             if (offerIter.value != null) {
167                 // there could be a matching offer in the iterator
168
// if no result already set, set it
169
if (result == null) {
170                     org.omg.CosTrading.OfferSeqHolder offerSeqIter = new org.omg.CosTrading.OfferSeqHolder();
171                     offerIter.value.next_n(1, offerSeqIter);
172                     if (offerSeqIter.value.length != 0) result = (offerSeqIter.value)[0].reference;
173                 }
174                 // anyways, the iterator must be destroyed
175
offerIter.value.destroy();
176             }
177             // Return the resulting offer
178
return result;
179         } catch (org.omg.CosTrading.IllegalServiceType ex) {
180             System.err.println(TraderqueryDeployer.header +
181                                "bad service type name '" + st + "'.");
182         } catch (org.omg.CosTrading.UnknownServiceType ex) {
183             System.err.println(TraderqueryDeployer.header +
184                                "bad service type name '" + st + "'.");
185         } catch (org.omg.CosTrading.IllegalConstraint ex) {
186             System.err.println(TraderqueryDeployer.header +
187                                "bad constraint '" + constraint + "'.");
188         } catch (org.omg.CosTrading.LookupPackage.IllegalPreference ex) {
189             System.err.println(TraderqueryDeployer.header +
190                                "bad preference '" + pref + "'.");
191         } catch (org.omg.CosTrading.LookupPackage.IllegalPolicyName ex) {
192             System.err.println(TraderqueryDeployer.header +
193                                "bad policy name '" + ex.name + "'.");
194         } catch (org.omg.CosTrading.LookupPackage.PolicyTypeMismatch ex) {
195             System.err.println(TraderqueryDeployer.header +
196                                "bad policy type for '" + ex.the_policy.name + "'.");
197         } catch (org.omg.CosTrading.LookupPackage.InvalidPolicyValue ex) {
198             System.err.println(TraderqueryDeployer.header +
199                                "bad policy value for '" + ex.the_policy.name + "'.");
200         } catch (org.omg.CosTrading.IllegalPropertyName ex) {
201             System.err.println(TraderqueryDeployer.header +
202                                "bad property name '" + ex.name + "'.");
203         } catch (org.omg.CosTrading.DuplicatePropertyName ex) {
204             System.err.println(TraderqueryDeployer.header +
205                                "duplicated property name '" + ex.name + "'.");
206         } catch (org.omg.CosTrading.DuplicatePolicyName ex) {
207             System.err.println(TraderqueryDeployer.header +
208                                "duplicated policy name " + ex.name + ".");
209         }
210         return null;
211     }
212
213     /**
214      * Construct the CosTrading::Policy corresponding to the trader
215      * policy specified in the XML. As the given value is not
216      * associated to a type code, this method assumes that the policy
217      * must be an importer policy as specified in the Trading Object
218      * Service specification. Otherwise, the CosTrading::Property
219      * cannot be constructed and the <code>null</code> value is
220      * returned.
221      *
222      * @param tp The specified trader policy.
223      *
224      * @return The CosTrading::Policy corresponding structure or null
225      * if this policy cannot be constructed.
226      */

227     protected org.omg.CosTrading.Policy
228     construct_policy(Traderpolicy tp)
229     {
230         String JavaDoc name = tp.getTraderpolicyname().getValue();
231         String JavaDoc value = tp.getTraderpolicyvalue().getValue();
232         return QueryPoliciesHelper.construct_policy(name, value);
233     }
234
235     /**
236      * Construct the CosTrading::Lookup::SpecifiedProps corresponding
237      * to the list of props specified in the XML.
238      *
239      * @param trader_props The list of trader specified properties.
240      *
241      * @return The specified props corresponding union.
242      */

243     protected org.omg.CosTrading.LookupPackage.SpecifiedProps
244     construct_props(java.util.List JavaDoc trader_props)
245     {
246         // If no specified props return the "none specified props"
247
if (trader_props.size() == 0) {
248             return org.objectweb.openccm.corba.trader.SpecifiedPropsWrapper.create_none();
249         }
250         
251         // else construct the list of prop names for the "some
252
// required props"
253
java.util.List JavaDoc cos_props = new java.util.LinkedList JavaDoc();
254         for (java.util.Iterator JavaDoc iter = trader_props.iterator() ; iter.hasNext() ; )
255         {
256             Traderspecifiedprop tsp = (Traderspecifiedprop)iter.next();
257             String JavaDoc prop_name = tsp.getValue();
258             cos_props.add(prop_name);
259         }
260         String JavaDoc[] names = (String JavaDoc[])cos_props.toArray(new String JavaDoc[0]);
261         return org.objectweb.openccm.corba.trader.SpecifiedPropsWrapper.create_some(names);
262     }
263
264     // ==================================================================
265
//
266
// Public methods.
267
//
268
// ==================================================================
269

270     /**
271      * Returns a CORBA::Object if the query match or null otherwise.
272      *
273      * @param reg The trader query as specified in the XML.
274      *
275      * @return The reference of a CORBA Object if the query match an offer.
276      */

277     public org.omg.CORBA.Object JavaDoc
278     deploy(Traderquery reg)
279     {
280         return lookup(reg);
281     }
282 }
283
Popular Tags