1 4 13 package org.jacorb.trading.client.query; 14 15 import java.awt.*; 16 import java.awt.event.*; 17 import java.io.*; 18 import java.util.*; 19 import org.omg.CORBA.ORB ; 20 import org.omg.CosTrading.*; 21 import org.omg.CosTrading.LookupPackage.*; 22 import org.omg.CosTradingRepos.*; 23 import org.omg.CosTradingRepos.ServiceTypeRepositoryPackage.*; 24 import org.jacorb.trading.client.util.*; 25 26 public class Query 27 extends Frame 28 implements ActionListener, ItemListener, Runnable 29 { 30 private Choice m_types; 31 private TextField m_constraint; 32 private TextField m_preference; 33 private Checkbox m_exactType; 34 private Checkbox m_useDynamic; 35 private Checkbox m_useProxy; 36 private Checkbox m_useProps; 37 private TextField m_props; 38 private Button m_query; 39 private TextArea m_results; 40 private Label m_status; 41 private Lookup m_lookup; 42 private ServiceTypeRepository m_repos; 43 private static ORB s_orb; 44 45 46 public Query(Lookup lookup) 47 { 48 super("Query"); 49 50 setFont(new Font("Helvetica", Font.PLAIN, 12)); 51 52 m_lookup = lookup; 53 org.omg.CORBA.Object obj = lookup.type_repos(); 54 m_repos = ServiceTypeRepositoryHelper.narrow(obj); 55 56 createContents(); 57 } 58 59 60 protected void createContents() 61 { 62 Panel panel = new Panel(); 63 panel.setLayout(new GridBagLayout()); 64 65 Panel typesPanel = new Panel(); 66 typesPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); 67 typesPanel.add(new Label("Service type", Label.LEFT)); 68 69 m_types = new Choice(); 70 loadTypes(m_types); 71 typesPanel.add(m_types); 72 73 Constrain.constrain(panel, typesPanel, 0, 0, 2, 1, 74 GridBagConstraints.HORIZONTAL, GridBagConstraints.NORTHWEST, 75 1.0, 0.0, 5, 10, 0, 10); 76 77 Constrain.constrain(panel, new Label("Constraint", Label.LEFT), 0, 1, 2, 1, 78 GridBagConstraints.NONE, GridBagConstraints.NORTHWEST, 0.0, 0.0, 79 5, 10, 0, 10); 80 81 m_constraint = new TextField(50); 82 m_constraint.setEditable(true); 83 Constrain.constrain(panel, m_constraint, 0, 2, 2, 1, 84 GridBagConstraints.HORIZONTAL, GridBagConstraints.NORTHWEST, 1.0, 0.0, 85 0, 10, 0, 10); 86 87 Constrain.constrain(panel, new Label("Preference", Label.LEFT), 0, 3, 2, 1, 88 GridBagConstraints.NONE, GridBagConstraints.NORTHWEST, 0.0, 0.0, 89 5, 10, 0, 10); 90 91 m_preference = new TextField(50); 92 m_preference.setEditable(true); 93 Constrain.constrain(panel, m_preference, 0, 4, 2, 1, 94 GridBagConstraints.HORIZONTAL, GridBagConstraints.NORTHWEST, 1.0, 0.0, 95 0, 10, 0, 10); 96 97 m_useProps = new Checkbox("Desired properties", false); 98 m_useProps.addItemListener(this); 99 Constrain.constrain(panel, m_useProps, 0, 5, 2, 1, 100 GridBagConstraints.NONE, GridBagConstraints.NORTHWEST, 0.0, 0.0, 101 5, 10, 0, 10); 102 103 m_props = new TextField(40); 104 m_props.setEditable(false); 105 Constrain.constrain(panel, m_props, 0, 6, 2, 1, 106 GridBagConstraints.HORIZONTAL, GridBagConstraints.NORTHWEST, 1.0, 0.0, 107 0, 10, 0, 10); 108 109 Panel policiesPanel = new Panel(); 110 policiesPanel.setLayout(new GridBagLayout()); 111 112 m_exactType = new Checkbox("Exact type", false); 113 Constrain.constrain(policiesPanel, m_exactType, 0, 0, 1, 1, 114 GridBagConstraints.NONE, GridBagConstraints.WEST); 115 116 m_useDynamic = new Checkbox("Dynamic properties", false); 117 Constrain.constrain(policiesPanel, m_useDynamic, 1, 0, 1, 1, 118 GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.0, 0.0, 119 0, 10, 0, 10); 120 121 m_useProxy = new Checkbox("Proxy offers", false); 122 Constrain.constrain(policiesPanel, m_useProxy, 2, 0, 1, 1, 123 GridBagConstraints.NONE, GridBagConstraints.EAST); 124 125 Constrain.constrain(panel, policiesPanel, 0, 7, 2, 1, 126 GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST, 1.0, 0.0, 127 5, 10, 0, 10); 128 129 m_query = new Button("Query"); 130 m_query.setActionCommand("query"); 131 m_query.addActionListener(this); 132 Constrain.constrain(panel, m_query, 0, 8, 2, 1, 133 GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.0, 0.0, 134 5, 10, 0, 10, 30, 3); 135 136 Constrain.constrain(panel, new Label("Results", Label.LEFT), 0, 9, 2, 1, 137 GridBagConstraints.NONE, GridBagConstraints.NORTHWEST, 0.0, 0.0, 138 5, 10, 0, 10); 139 140 m_results = new TextArea(10, 50); 141 m_results.setEditable(false); 142 Constrain.constrain(panel, m_results, 0, 10, 2, 1, 143 GridBagConstraints.BOTH, GridBagConstraints.NORTHWEST, 1.0, 1.0, 144 0, 10, 0, 10); 145 146 m_status = new Label("", Label.LEFT); 147 Constrain.constrain(panel, m_status, 0, 11, 2, 1, 148 GridBagConstraints.HORIZONTAL, GridBagConstraints.NORTHWEST, 1.0, 0.0, 149 0, 10, 3, 10); 150 151 add(panel); 152 } 153 154 155 public void actionPerformed(ActionEvent e) 156 { 157 String cmd = e.getActionCommand(); 158 159 if (cmd.equals("query")) 160 new Thread (this).start(); 161 } 162 163 164 public void itemStateChanged(ItemEvent e) 165 { 166 m_props.setEditable(e.getStateChange() == ItemEvent.SELECTED); 169 } 170 171 172 public void run() 173 { 174 try { 175 showStatus("Performing query..."); 176 m_query.setEnabled(false); 177 m_results.setText(""); 178 179 StringWriter sw = new StringWriter(); 180 PrintWriter pw = new PrintWriter(sw); 181 182 String type = m_types.getSelectedItem(); 183 String constraint = m_constraint.getText(); 184 String preference = m_preference.getText(); 185 186 Policy[] policies = new Policy[3]; 187 policies[0] = new Policy(); 188 policies[0].name = "exact_type_match"; 189 policies[0].value = s_orb.create_any(); 190 policies[0].value.insert_boolean(m_exactType.getState()); 191 policies[1] = new Policy(); 192 policies[1].name = "use_dynamic_properties"; 193 policies[1].value = s_orb.create_any(); 194 policies[1].value.insert_boolean(m_useDynamic.getState()); 195 policies[2] = new Policy(); 196 policies[2].name = "use_proxy_offers"; 197 policies[2].value = s_orb.create_any(); 198 policies[2].value.insert_boolean(m_useProxy.getState()); 199 200 SpecifiedProps desiredProps = new SpecifiedProps(); 201 if (! m_useProps.getState()) 202 ; 205 else 206 { 207 String props = m_props.getText().trim(); 208 if (props.length() == 0) 209 { 210 ; 213 } 214 else 215 { 216 StringTokenizer tok = new StringTokenizer(props, ","); 217 String [] names = new String [tok.countTokens()]; 218 int count = 0; 219 while (tok.hasMoreTokens()) 220 names[count++] = tok.nextToken().trim(); 221 desiredProps.prop_names(names); 222 } 223 } 224 225 OfferSeqHolder offers = new OfferSeqHolder(); 226 OfferIteratorHolder iter = new OfferIteratorHolder(); 227 PolicyNameSeqHolder limits = new PolicyNameSeqHolder(); 228 229 m_lookup.query(type, constraint, preference, policies, desiredProps, 20, 230 offers, iter, limits); 231 232 int count = offers.value.length; 233 showStatus("Received " + count + " offers..."); 234 235 describeOffers(pw, offers.value); 236 237 if (iter.value != null) { 238 OfferSeqHolder seq = new OfferSeqHolder(); 239 boolean more; 240 do { 241 more = iter.value.next_n(20, seq); 242 count += seq.value.length; 243 showStatus("Received " + count + " offers..."); 244 describeOffers(pw, seq.value); 245 } 246 while (more); 247 248 iter.value.destroy(); 249 } 250 251 pw.flush(); 252 m_results.setText(sw.toString()); 253 } 254 catch (IllegalServiceType e) { 255 showStatus("Illegal service type '" + e.type + "'"); 256 } 257 catch (UnknownServiceType e) { 258 showStatus("Unknown service type '" + e.type + "'"); 259 } 260 catch (IllegalConstraint e) { 261 showStatus("Illegal constraint"); 262 } 263 catch (IllegalPreference e) { 264 showStatus("Illegal preference"); 265 } 266 catch (IllegalPolicyName e) { 267 showStatus("Illegal policy '" + e.name + "'"); 268 } 269 catch (PolicyTypeMismatch e) { 270 showStatus("Policy type mismatch for '" + e.the_policy.name + "'"); 271 } 272 catch (InvalidPolicyValue e) { 273 showStatus("Invalid policy value for '" + e.the_policy.name + "'"); 274 } 275 catch (IllegalPropertyName e) { 276 showStatus("Illegal property name '" + e.name + "'"); 277 } 278 catch (DuplicatePropertyName e) { 279 showStatus("Duplicate property name '" + e.name + "'"); 280 } 281 catch (DuplicatePolicyName e) { 282 showStatus("Duplicate policy name '" + e.name + "'"); 283 } 284 catch (org.omg.CORBA.SystemException e) { 285 showStatus("System error occurred"); 286 } 287 finally { 288 m_query.setEnabled(true); 290 } 291 } 292 293 294 protected void describeOffers(PrintWriter pw, Offer[] offers) 295 { 296 for (int i = 0; i < offers.length; i++) { 297 pw.println("Offer:"); 298 pw.println(); 299 300 for (int p = 0; p < offers[i].properties.length; p++) { 301 pw.print(" " + offers[i].properties[p].name + " = "); 302 AnyUtil.print(s_orb, pw, offers[i].properties[p].value); 303 pw.println(); 304 } 305 306 pw.println(); 307 pw.println(" Reference:"); 308 String ref = s_orb.object_to_string(offers[i].reference); 309 pw.println(" " + ref); 310 pw.println(); 311 } 312 } 313 314 315 protected void loadTypes(Choice choice) 316 { 317 choice.removeAll(); 318 String [] types; 319 SpecifiedServiceTypes whichTypes = new SpecifiedServiceTypes(); 320 whichTypes.__default(); 323 324 types = m_repos.list_types(whichTypes); 325 QuickSort.sort(types); 326 for (int i = 0; i < types.length; i++) 327 choice.add(types[i]); 328 } 329 330 331 protected void showStatus(String message) 332 { 333 m_status.setText(message); 334 } 335 336 337 protected void clearStatus() 338 { 339 m_status.setText(""); 340 } 341 342 343 protected static void usage() 344 { 345 System.out.println("Usage: Query iorfile"); 346 System.exit(1); 347 } 348 349 350 public static void main(String [] args) 351 { 352 s_orb = ORB.init(args, null); Lookup lookup = null; 354 355 try 356 { 357 org.omg.CORBA.Object obj = s_orb.resolve_initial_references("TradingService"); 358 359 if (obj == null) 360 { 361 System.out.println("Invalid object"); 362 System.exit(1); 363 } 364 365 lookup = LookupHelper.narrow(obj); 366 } 367 catch (Exception e) { 368 e.printStackTrace(); 369 System.exit(1); 370 } 371 372 Query app = new Query(lookup); 373 374 app.addWindowListener( 375 new WindowAdapter() 376 { 377 public void windowClosing(WindowEvent e) 378 { 379 e.getWindow().dispose(); 380 System.exit(0); 381 } 382 } 383 ); 384 385 app.pack(); 386 app.setVisible(true); 387 } 388 } 389 390 391 392 393 394 395 396 397 398 399 | Popular Tags |