KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > explorer > CosTrading > Query


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 Initial developer(s): Sylvain Leblanc.
23 Contributor(s): ______________________________________.
24
25 ===========================================================================*/

26
27 package org.objectweb.openccm.explorer.CosTrading;
28
29 /** OMG CosTrading imports. */
30 import org.objectweb.openccm.corba.trader.SpecifiedPropsWrapper;
31 import org.omg.CosTrading.Policy;
32 import org.omg.CosTrading.Lookup;
33 import org.omg.CosTrading.Offer;
34
35 import javax.swing.JOptionPane JavaDoc;
36
37 /**
38  * Wrapper class used to provide a node in the administration console
39  * tree which children are <code>org.omg.CosTrading.Offer</code>
40  * instances which results of the query execution.
41  *
42  * @author <a HREF="mailto:Sylvain.Leblanc@lifl.fr">Sylvain Leblanc</a>
43  * @version 0.1
44  */

45 public class Query {
46
47     // ==================================================================
48
//
49
// Internal state.
50
//
51
// ==================================================================
52

53     /** The label of the query. */
54     protected String JavaDoc label_;
55
56     /**
57      * The CosTrading Lookup interface used to execute the query.
58      */

59     protected Lookup lookup_;
60
61     /** The service type to address. */
62     protected String JavaDoc service_type_;
63
64     /** The constraint to use. */
65     protected String JavaDoc constraint_;
66
67     /** The preference to use in the query. */
68     protected String JavaDoc pref_;
69
70     /** The policies to use in the query. */
71     protected Policy[] policies_;
72
73     /** The parent queries wrapper object. */
74     protected Queries parent_;
75
76     private boolean removed = false;
77
78     // ==================================================================
79
//
80
// Constructors.
81
//
82
// ==================================================================
83

84     /**
85      * Default constructor.
86      *
87      * @param query_label The label of the query.
88      * @param lookup The Lookup reference used to do the query.
89      * @param service_type The service type name to address in the query.
90      * @param constraint The constraint to use in the query.
91      * @param pref The preference to use in the query.
92      * @param policies The policies to apply in the query.
93      */

94     public Query(String JavaDoc query_label, Queries parent, String JavaDoc service_type,
95                  String JavaDoc constraint, String JavaDoc pref, Policy[] policies)
96     {
97         label_ = query_label;
98         parent_ = parent;
99         lookup_ = parent.getLookup();
100         service_type_ = service_type;
101         constraint_ = constraint;
102         pref_ = pref;
103         policies_ = policies;
104     }
105
106     // ==================================================================
107
//
108
// Internal methods.
109
//
110
// ==================================================================
111

112     /**
113      * Used to treat an error while evaluating the query. Common
114      * action is to display an error message and to remove this query
115      * from his parent.
116      *
117      * @param message The error message to display.
118      */

119     protected void fatalError(String JavaDoc message) {
120         removed = true;
121         JOptionPane.showMessageDialog(null, message, "Error on query '" + label_ + "'", JOptionPane.ERROR_MESSAGE);
122         parent_.removeQuery(this);
123     }
124
125     // ==================================================================
126
//
127
// Public methods.
128
//
129
// ==================================================================
130

131     /**
132      * Returns the CosTrading Offers selected by the query.
133      *
134      * @return The CosTrading Offers selected by the query.
135      */

136     public Offer[] getOffers() {
137         return getOffers(100);
138     }
139
140     /**
141      * Returns the CosTrading Offers selected by the query.
142      *
143      * @param how_many_step The step used to retrieve offers.
144      * @return The CosTrading Offers selected by the query.
145      */

146     public Offer[] getOffers(int how_many_step) {
147
148         if (removed) return new Offer[0];
149
150         // Construct a SpecifedProp
151
org.omg.CosTrading.LookupPackage.SpecifiedProps cos_props;
152         cos_props = SpecifiedPropsWrapper.create_all();
153
154         // Construct the used holders
155
org.omg.CosTrading.OfferSeqHolder offerSeq;
156         offerSeq = new org.omg.CosTrading.OfferSeqHolder();
157         org.omg.CosTrading.OfferIteratorHolder offerIter;
158         offerIter = new org.omg.CosTrading.OfferIteratorHolder();
159         org.omg.CosTrading.PolicyNameSeqHolder limits;
160         limits = new org.omg.CosTrading.PolicyNameSeqHolder();
161
162         // Perform the query
163
try {
164             lookup_.query(service_type_, constraint_, pref_, policies_,
165                          cos_props, how_many_step, offerSeq, offerIter, limits);
166
167             // Compute the results of the query
168
java.util.Vector JavaDoc offerAnswer = new java.util.Vector JavaDoc();
169             for (int i = 0 ; i < offerSeq.value.length ; i++) {
170                 org.omg.CosTrading.Offer offer = (offerSeq.value)[i];
171                 offerAnswer.add(offer);
172             }
173       
174             // Checking the iterator
175
if (offerIter.value != null) {
176                 org.omg.CosTrading.OfferSeqHolder offerSeqIter = new org.omg.CosTrading.OfferSeqHolder();
177                 boolean more;
178                 do {
179                     more = offerIter.value.next_n(how_many_step, offerSeqIter);
180                     for (int i = 0 ; i < offerSeqIter.value.length ; i++) {
181                         org.omg.CosTrading.Offer offer = (offerSeqIter.value)[i];
182                         offerAnswer.add(offer);
183                     }
184                 } while(more);
185                 offerIter.value.destroy();
186             }
187             return (Offer[]) offerAnswer.toArray(new Offer[0]);
188
189         } catch (org.omg.CosTrading.IllegalServiceType ex) {
190             fatalError("bad service type name '" + service_type_ + "'.");
191         } catch (org.omg.CosTrading.UnknownServiceType ex) {
192             fatalError("bad service type name '" + service_type_ + "'.");
193         } catch (org.omg.CosTrading.IllegalConstraint ex) {
194             fatalError("bad constraint '" + constraint_ + "'.");
195         } catch (org.omg.CosTrading.LookupPackage.IllegalPreference ex) {
196             fatalError("bad preference '" + pref_ + "'.");
197         } catch (org.omg.CosTrading.LookupPackage.IllegalPolicyName ex) {
198             fatalError("bad policy name '" + ex.name + "'.");
199         } catch (org.omg.CosTrading.LookupPackage.PolicyTypeMismatch ex) {
200             fatalError("bad policy type for '" + ex.the_policy.name + "'.");
201         } catch (org.omg.CosTrading.LookupPackage.InvalidPolicyValue ex) {
202             fatalError("bad policy value for '" + ex.the_policy.name + "'.");
203         } catch (org.omg.CosTrading.IllegalPropertyName ex) {
204             fatalError("bad property name '" + ex.name + "'.");
205         } catch (org.omg.CosTrading.DuplicatePropertyName ex) {
206             fatalError("duplicated property name '" + ex.name + "'.");
207         } catch (org.omg.CosTrading.DuplicatePolicyName ex) {
208             fatalError("duplicated policy name " + ex.name + ".");
209         }
210         return new Offer[0];
211     }
212
213     /**
214      * Returns the Lookup reference to use to do the query.
215      *
216      * @return The Lookup reference to use to do the query.
217      */

218     public Lookup getLookup() {
219         return lookup_;
220     }
221
222     /**
223      * Returns the label of the query.
224      *
225      * @return The label of the query.
226      */

227     public String JavaDoc getQueryLabel() {
228         return label_;
229     }
230
231     /**
232      * Returns the service type name used in the query.
233      *
234      * @return The service type name used in the query.
235      */

236     public String JavaDoc getServiceTypeName() {
237         return service_type_;
238     }
239
240     /**
241      * Returns the constraint used in the query.
242       *
243      * @return The constraint used in the query.
244     */

245     public String JavaDoc getConstraint() {
246         return constraint_;
247     }
248
249     /**
250      * Returns the preference used in the query.
251      *
252      * @return The preference used in the query.
253      */

254     public String JavaDoc getPref() {
255         return pref_;
256     }
257
258     /**
259      * Returns the policies used in the query.
260      *
261      * @return The policies used in the query.
262      */

263     public Policy[] getPolicies() {
264         return policies_;
265     }
266
267 }
268
Popular Tags