1 6 7 8 9 package com.sun.j2ee.blueprints.swingclient; 10 11 import javax.xml.parsers.*; 12 13 import javax.xml.transform.*; 14 15 import javax.xml.transform.dom.*; 16 17 import javax.xml.transform.stream.*; 18 19 import org.w3c.dom.*; 20 21 import java.io.*; 22 23 import java.util.*; 24 25 26 27 28 29 36 37 public class MainWindow extends javax.swing.JFrame { 38 39 40 41 42 43 public MainWindow() { 44 45 initComponents(); 46 47 initServiceURL(); 48 49 loadFileInTextPane(fileTextField.getText()); 50 51 } 52 53 54 55 private void initServiceURL() { 56 57 59 61 String serviceurl = System.getProperty("stringwebservice.url"); 62 63 if (serviceurl != null) { 64 65 serviceUrlTextField.setText(serviceurl); 66 67 } 68 69 } 70 71 80 81 83 private void initComponents() { 84 85 paramTypeButtonGroup = new javax.swing.ButtonGroup (); 86 87 topPanel = new javax.swing.JPanel (); 88 89 title1Label = new javax.swing.JLabel (); 90 91 title2Label = new javax.swing.JLabel (); 92 93 centerPanel = new javax.swing.JPanel (); 94 95 jPanel3 = new javax.swing.JPanel (); 96 97 fileLabel = new javax.swing.JLabel (); 98 99 jPanel2 = new javax.swing.JPanel (); 100 101 fileTextField = new javax.swing.JTextField (); 102 103 loadFileButton = new javax.swing.JButton (); 104 105 clearButton = new javax.swing.JButton (); 106 107 descriptionLabel = new javax.swing.JLabel (); 108 109 scrollPane = new javax.swing.JScrollPane (); 110 111 textPane = new javax.swing.JTextPane (); 112 113 bottomPanel = new javax.swing.JPanel (); 114 115 paramTypePanel = new javax.swing.JPanel (); 116 117 paramTypeLabel = new javax.swing.JLabel (); 118 119 radioButtonPanel = new javax.swing.JPanel (); 120 121 stubsRadioButton = new javax.swing.JRadioButton (); 122 123 dynamicProxiesRadioButton = new javax.swing.JRadioButton (); 124 125 diiRadioButton = new javax.swing.JRadioButton (); 126 127 servicePanel = new javax.swing.JPanel (); 128 129 sendPOButton = new javax.swing.JButton (); 130 131 serviceUrlTextField = new javax.swing.JTextField (); 132 133 serviceUrlLabel = new javax.swing.JLabel (); 134 135 136 137 getContentPane().setLayout(new java.awt.BorderLayout (10, 10)); 138 139 140 141 setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 142 143 setTitle("Swing Client for a J2EE Web Service"); 144 145 setName("Swing Client for J2EE Web Service"); 146 147 topPanel.setLayout(new java.awt.BorderLayout (10, 10)); 148 149 150 151 topPanel.setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (5, 5, 5, 5))); 152 153 topPanel.setPreferredSize(new java.awt.Dimension (33, 50)); 154 155 title1Label.setFont(new java.awt.Font ("MS Sans Serif", 1, 16)); 156 157 title1Label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); 158 159 title1Label.setText("Swing Client for the Purchase Order Web"); 160 161 title1Label.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); 162 163 topPanel.add(title1Label, java.awt.BorderLayout.NORTH); 164 165 166 167 title2Label.setFont(new java.awt.Font ("MS Sans Serif", 1, 16)); 168 169 title2Label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); 170 171 title2Label.setText("Service in Java BluePrints Solutions Catalog"); 172 173 title2Label.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); 174 175 title2Label.setMinimumSize(new java.awt.Dimension (33, 21)); 176 177 topPanel.add(title2Label, java.awt.BorderLayout.SOUTH); 178 179 180 181 getContentPane().add(topPanel, java.awt.BorderLayout.NORTH); 182 183 184 185 centerPanel.setLayout(new java.awt.BorderLayout (10, 10)); 186 187 188 189 centerPanel.setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (5, 5, 5, 5))); 190 191 jPanel3.setLayout(new java.awt.BorderLayout (10, 0)); 192 193 194 195 fileLabel.setLabelFor(fileTextField); 196 197 fileLabel.setText("Load Purchase Order XML File From: "); 198 199 jPanel3.add(fileLabel, java.awt.BorderLayout.CENTER); 200 201 202 203 jPanel2.setLayout(new java.awt.FlowLayout (java.awt.FlowLayout.CENTER, 10, 5)); 204 205 206 207 fileTextField.setText("/resources/purchaseorder.xml"); 208 209 fileTextField.setMinimumSize(new java.awt.Dimension (50, 21)); 210 211 fileTextField.setPreferredSize(new java.awt.Dimension (200, 21)); 212 213 jPanel2.add(fileTextField); 214 215 216 217 loadFileButton.setText("Load"); 218 219 loadFileButton.addActionListener(new java.awt.event.ActionListener () { 220 221 public void actionPerformed(java.awt.event.ActionEvent evt) { 222 223 loadFileButtonActionPerformed(evt); 224 225 } 226 227 }); 228 229 230 231 jPanel2.add(loadFileButton); 232 233 234 235 clearButton.setText("Clear"); 236 237 clearButton.addActionListener(new java.awt.event.ActionListener () { 238 239 public void actionPerformed(java.awt.event.ActionEvent evt) { 240 241 clearButtonActionPerformed(evt); 242 243 } 244 245 }); 246 247 248 249 jPanel2.add(clearButton); 250 251 252 253 jPanel3.add(jPanel2, java.awt.BorderLayout.SOUTH); 254 255 256 257 descriptionLabel.setText("<html>This Swing application illustrates three communication modes to access a Web service <br>from a stand-alone client: Stubs, Dynamic Proxy and Dynaic Invocation Interface (DII). "); 258 259 descriptionLabel.setAutoscrolls(true); 260 261 descriptionLabel.setMaximumSize(new java.awt.Dimension (200, 42)); 262 263 descriptionLabel.setMinimumSize(new java.awt.Dimension (200, 42)); 264 265 descriptionLabel.setPreferredSize(new java.awt.Dimension (200, 42)); 266 267 jPanel3.add(descriptionLabel, java.awt.BorderLayout.NORTH); 268 269 descriptionLabel.getAccessibleContext().setAccessibleName("<html>This Swing application illustrates three communication modes to access a Web service <br>from a stand-alone client: Stubs, Dynamic Proxy and Dynaic Invocation Interface (DII)."); 270 271 272 273 centerPanel.add(jPanel3, java.awt.BorderLayout.NORTH); 274 275 276 277 scrollPane.setAutoscrolls(true); 278 279 textPane.setMinimumSize(new java.awt.Dimension (200, 175)); 280 281 textPane.setPreferredSize(new java.awt.Dimension (200, 175)); 282 283 scrollPane.setViewportView(textPane); 284 285 286 287 centerPanel.add(scrollPane, java.awt.BorderLayout.CENTER); 288 289 290 291 getContentPane().add(centerPanel, java.awt.BorderLayout.CENTER); 292 293 294 295 bottomPanel.setLayout(new java.awt.BorderLayout (10, 5)); 296 297 298 299 bottomPanel.setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (5, 5, 5, 5))); 300 301 paramTypePanel.setLayout(new java.awt.BorderLayout ()); 302 303 304 305 paramTypeLabel.setText("Invoke StringPurchaseOrder Web Service Using the Communication Mode:"); 306 307 paramTypePanel.add(paramTypeLabel, java.awt.BorderLayout.NORTH); 308 309 310 311 radioButtonPanel.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION); 312 313 paramTypeButtonGroup.add(stubsRadioButton); 314 315 stubsRadioButton.setSelected(true); 316 317 stubsRadioButton.setText("Stubs"); 318 319 radioButtonPanel.add(stubsRadioButton); 320 321 322 323 paramTypeButtonGroup.add(dynamicProxiesRadioButton); 324 325 dynamicProxiesRadioButton.setText("Dynamic Proxy"); 326 327 radioButtonPanel.add(dynamicProxiesRadioButton); 328 329 330 331 paramTypeButtonGroup.add(diiRadioButton); 332 333 diiRadioButton.setText("Dynamic Invocation Interface (DII)"); 334 335 radioButtonPanel.add(diiRadioButton); 336 337 338 339 paramTypePanel.add(radioButtonPanel, java.awt.BorderLayout.SOUTH); 340 341 342 343 bottomPanel.add(paramTypePanel, java.awt.BorderLayout.NORTH); 344 345 346 347 servicePanel.setLayout(new java.awt.BorderLayout (10, 5)); 348 349 350 351 sendPOButton.setText("Send PO"); 352 353 sendPOButton.addActionListener(new java.awt.event.ActionListener () { 354 355 public void actionPerformed(java.awt.event.ActionEvent evt) { 356 357 sendPOButtonActionPerformed(evt); 358 359 } 360 361 }); 362 363 364 365 servicePanel.add(sendPOButton, java.awt.BorderLayout.EAST); 366 367 368 369 serviceUrlTextField.setText("http://localhost:8080/webservice/StringPurchaseOrderService"); 370 371 servicePanel.add(serviceUrlTextField, java.awt.BorderLayout.CENTER); 372 373 374 375 serviceUrlLabel.setText("Web Service URL:"); 376 377 servicePanel.add(serviceUrlLabel, java.awt.BorderLayout.WEST); 378 379 380 381 bottomPanel.add(servicePanel, java.awt.BorderLayout.SOUTH); 382 383 384 385 getContentPane().add(bottomPanel, java.awt.BorderLayout.SOUTH); 386 387 388 389 pack(); 390 391 } 392 393 395 396 397 private void loadFileButtonActionPerformed(java.awt.event.ActionEvent evt) { 399 loadFileInTextPane(fileTextField.getText()); 400 401 } 403 404 405 private void loadFileInTextPane(String fileName) { 406 407 try { 408 409 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 410 411 DocumentBuilder builder = factory.newDocumentBuilder(); 412 413 InputStream is = getClass().getResourceAsStream(fileName); 414 415 Document doc = builder.parse(is); 416 417 Source source = new DOMSource(doc); 418 419 Transformer transformer = TransformerFactory.newInstance().newTransformer(); 420 421 StringWriter sw = new StringWriter(); 422 423 Result result = new StreamResult(sw); 424 425 transformer.transform(source, result); 426 427 textPane.setText(sw.toString()); 428 429 } catch (Exception e) { 430 431 e.printStackTrace(); 432 433 } 434 435 } 436 437 438 439 private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) { 441 fileTextField.setText(""); 442 443 textPane.setText(""); 444 445 } 447 448 449 private void sendPOButtonActionPerformed(java.awt.event.ActionEvent evt) { 451 String result = ""; 452 453 POServiceBD bd = new POServiceBD(serviceUrlTextField.getText()); 454 455 String xmlDocStr = textPane.getText(); 456 457 String responseMsg = ""; 458 459 try { 460 461 if (stubsRadioButton.isSelected()) { 462 463 result = bd.submitPOUsingStubs(xmlDocStr); 464 465 } else if (dynamicProxiesRadioButton.isSelected()) { 466 467 result = bd.submitPOUsingDynamicProxy(xmlDocStr); 468 469 } else if (diiRadioButton.isSelected()) { 470 471 result = bd.submitPOUsingDII(xmlDocStr); 472 473 } 474 475 responseMsg = "The Web service returned: " + result; 476 477 } catch (InvalidPOException e) { 478 479 responseMsg = "The submitted purchase order is invalid.\n\n The error reported by the server is\n" + convert(e); 480 481 } catch (RuntimeException e) { 482 483 responseMsg = "The StringPurchaseOrder Web service is not available.\n\n The error reported by the server is\n" + convert(e); 484 485 } 486 487 textPane.setText(responseMsg); 488 489 } 491 492 493 private String convert(Exception e) { 494 495 ByteArrayOutputStream stackTrace = new ByteArrayOutputStream(); 496 497 PrintStream ps = new PrintStream(stackTrace); 498 499 ps.println("Received Exception: " + e.getMessage()); 500 501 e.printStackTrace(new PrintStream(stackTrace)); 502 503 return stackTrace.toString(); 504 505 } 506 507 512 513 public static void main(String args[]) { 514 515 java.awt.EventQueue.invokeLater(new Runnable () { 516 517 public void run() { 518 519 MainWindow window = new MainWindow(); 520 521 window.setVisible(true); 522 523 } 524 525 }); 526 527 } 528 529 530 531 533 private javax.swing.JPanel bottomPanel; 534 535 private javax.swing.JPanel centerPanel; 536 537 private javax.swing.JButton clearButton; 538 539 private javax.swing.JLabel descriptionLabel; 540 541 private javax.swing.JRadioButton diiRadioButton; 542 543 private javax.swing.JRadioButton dynamicProxiesRadioButton; 544 545 private javax.swing.JLabel fileLabel; 546 547 private javax.swing.JTextField fileTextField; 548 549 private javax.swing.JPanel jPanel2; 550 551 private javax.swing.JPanel jPanel3; 552 553 private javax.swing.JButton loadFileButton; 554 555 private javax.swing.ButtonGroup paramTypeButtonGroup; 556 557 private javax.swing.JLabel paramTypeLabel; 558 559 private javax.swing.JPanel paramTypePanel; 560 561 private javax.swing.JPanel radioButtonPanel; 562 563 private javax.swing.JScrollPane scrollPane; 564 565 private javax.swing.JButton sendPOButton; 566 567 private javax.swing.JPanel servicePanel; 568 569 private javax.swing.JLabel serviceUrlLabel; 570 571 private javax.swing.JTextField serviceUrlTextField; 572 573 private javax.swing.JRadioButton stubsRadioButton; 574 575 private javax.swing.JTextPane textPane; 576 577 private javax.swing.JLabel title1Label; 578 579 private javax.swing.JLabel title2Label; 580 581 private javax.swing.JPanel topPanel; 582 583 585 586 587 } 588 589 | Popular Tags |