| 1 16 package org.apache.axis2.util; 17 18 import javax.swing.*; 19 import javax.swing.border.TitledBorder ; 20 import javax.swing.event.ChangeEvent ; 21 import javax.swing.event.ListSelectionEvent ; 22 import javax.swing.event.ListSelectionListener ; 23 import javax.swing.plaf.basic.BasicButtonListener ; 24 import javax.swing.table.DefaultTableModel ; 25 import javax.swing.table.TableColumn ; 26 import javax.swing.table.TableModel ; 27 import javax.swing.text.AttributeSet ; 28 import javax.swing.text.BadLocationException ; 29 import javax.swing.text.Document ; 30 import javax.swing.text.PlainDocument ; 31 import java.awt.*; 32 import java.awt.event.ActionEvent ; 33 import java.awt.event.ActionListener ; 34 import java.io.*; 35 import java.net.ServerSocket ; 36 import java.net.Socket ; 37 import java.net.URL ; 38 import java.text.DateFormat ; 39 import java.text.SimpleDateFormat ; 40 import java.util.Date ; 41 import java.util.Iterator ; 42 import java.util.ResourceBundle ; 43 import java.util.Vector ; 44 45 53 public class tcpmon extends JFrame { 54 57 private JTabbedPane notebook = null; 58 59 62 private static final int STATE_COLUMN = 0; 63 64 67 private static final int OUTHOST_COLUMN = 3; 68 69 72 private static final int REQ_COLUMN = 4; 73 74 77 private static final String DEFAULT_HOST = "127.0.0.1"; 78 79 82 private static final int DEFAULT_PORT = 8080; 83 84 87 class AdminPage extends JPanel { 88 91 public JRadioButton listenerButton, proxyButton; 92 93 96 public JLabel hostLabel, tportLabel; 97 98 101 public NumberField port; 102 103 106 public HostnameField host; 107 108 111 public NumberField tport; 112 113 116 public JTabbedPane noteb; 117 118 121 public JCheckBox HTTPProxyBox; 122 123 126 public HostnameField HTTPProxyHost; 127 128 131 public NumberField HTTPProxyPort; 132 133 136 public JLabel HTTPProxyHostLabel, HTTPProxyPortLabel; 137 138 141 public JLabel delayTimeLabel, delayBytesLabel; 142 143 146 public NumberField delayTime, delayBytes; 147 148 151 public JCheckBox delayBox; 152 153 159 public AdminPage(JTabbedPane notebook, String name) { 160 JPanel mainPane = null; 161 JButton addButton = null; 162 this.setLayout(new BorderLayout()); 163 noteb = notebook; 164 GridBagLayout layout = new GridBagLayout(); 165 GridBagConstraints c = new GridBagConstraints(); 166 mainPane = new JPanel(layout); 167 c.anchor = GridBagConstraints.WEST; 168 c.gridwidth = GridBagConstraints.REMAINDER; 169 mainPane.add( 170 new JLabel( 171 getMessage("newTCP00", "Create a new TCP/IP Monitor...") 172 + " "), c); 173 174 mainPane.add(Box.createRigidArea(new Dimension(1, 5)), c); 176 177 JPanel tmpPanel = new JPanel(new GridBagLayout()); 180 c.anchor = GridBagConstraints.WEST; 181 c.gridwidth = 1; 182 tmpPanel.add(new JLabel(getMessage("listenPort00", "Listen Port #") 183 + " "), c); 184 c.anchor = GridBagConstraints.WEST; 185 c.gridwidth = GridBagConstraints.REMAINDER; 186 tmpPanel.add(port = new NumberField(4), c); 187 mainPane.add(tmpPanel, c); 188 mainPane.add(Box.createRigidArea(new Dimension(1, 5)), c); 189 190 ButtonGroup btns = new ButtonGroup(); 192 c.anchor = GridBagConstraints.WEST; 193 c.gridwidth = GridBagConstraints.REMAINDER; 194 mainPane.add(new JLabel(getMessage("actAs00", "Act as a...")), c); 195 196 c.anchor = GridBagConstraints.WEST; 199 c.gridwidth = GridBagConstraints.REMAINDER; 200 final String listener = getMessage("listener00", "Listener"); 201 mainPane.add(listenerButton = new JRadioButton(listener), c); 202 btns.add(listenerButton); 203 listenerButton.setSelected(true); 204 listenerButton.addActionListener(new ActionListener () { 205 public void actionPerformed(ActionEvent event) { 206 if (listener.equals(event.getActionCommand())) { 207 boolean state = listenerButton.isSelected(); 208 tport.setEnabled(state); 209 host.setEnabled(state); 210 hostLabel.setForeground(state 211 ? Color.black 212 : Color.gray); 213 tportLabel.setForeground(state 214 ? Color.black 215 : Color.gray); 216 } 217 } 218 }); 219 c.anchor = GridBagConstraints.WEST; 220 c.gridwidth = 1; 221 mainPane.add(Box.createRigidArea(new Dimension(25, 0))); 222 mainPane.add(hostLabel = 223 new JLabel(getMessage("targetHostname00", "Target Hostname") 224 + " "), c); 225 c.anchor = GridBagConstraints.WEST; 226 c.gridwidth = GridBagConstraints.REMAINDER; 227 host = new HostnameField(30); 228 mainPane.add(host, c); 229 host.setText(DEFAULT_HOST); 230 c.anchor = GridBagConstraints.WEST; 231 c.gridwidth = 1; 232 mainPane.add(Box.createRigidArea(new Dimension(25, 0))); 233 mainPane.add(tportLabel = 234 new JLabel(getMessage("targetPort00", "Target Port #") 235 + " "), c); 236 c.anchor = GridBagConstraints.WEST; 237 c.gridwidth = GridBagConstraints.REMAINDER; 238 tport = new NumberField(4); 239 mainPane.add(tport, c); 240 tport.setValue(DEFAULT_PORT); 241 242 c.anchor = GridBagConstraints.WEST; 245 c.gridwidth = GridBagConstraints.REMAINDER; 246 final String proxy = getMessage("proxy00", "Proxy"); 247 mainPane.add(proxyButton = new JRadioButton(proxy), c); 248 btns.add(proxyButton); 249 proxyButton.addActionListener(new ActionListener () { 250 public void actionPerformed(ActionEvent event) { 251 if (proxy.equals(event.getActionCommand())) { 252 boolean state = proxyButton.isSelected(); 253 tport.setEnabled(!state); 254 host.setEnabled(!state); 255 hostLabel.setForeground(state 256 ? Color.gray 257 : Color.black); 258 tportLabel.setForeground(state 259 ? Color.gray 260 : Color.black); 261 } 262 } 263 }); 264 265 c.anchor = GridBagConstraints.WEST; 268 c.gridwidth = GridBagConstraints.REMAINDER; 269 mainPane.add(Box.createRigidArea(new Dimension(1, 10)), c); 270 271 JPanel opts = new JPanel(new GridBagLayout()); 274 opts.setBorder(new TitledBorder (getMessage("options00", 275 "Options"))); 276 c.anchor = GridBagConstraints.WEST; 277 c.gridwidth = GridBagConstraints.REMAINDER; 278 mainPane.add(opts, c); 279 280 c.anchor = GridBagConstraints.WEST; 283 c.gridwidth = GridBagConstraints.REMAINDER; 284 final String proxySupport = getMessage("proxySupport00", 285 "HTTP Proxy Support"); 286 opts.add(HTTPProxyBox = new JCheckBox(proxySupport), c); 287 c.anchor = GridBagConstraints.WEST; 288 c.gridwidth = 1; 289 opts.add(HTTPProxyHostLabel = 290 new JLabel(getMessage("hostname00", "Hostname") + " "), c); 291 HTTPProxyHostLabel.setForeground(Color.gray); 292 c.anchor = GridBagConstraints.WEST; 293 c.gridwidth = GridBagConstraints.REMAINDER; 294 opts.add(HTTPProxyHost = new HostnameField(30), c); 295 HTTPProxyHost.setEnabled(false); 296 c.anchor = GridBagConstraints.WEST; 297 c.gridwidth = 1; 298 opts.add(HTTPProxyPortLabel = 299 new JLabel(getMessage("port00", "Port #") + " "), c); 300 HTTPProxyPortLabel.setForeground(Color.gray); 301 c.anchor = GridBagConstraints.WEST; 302 c.gridwidth = GridBagConstraints.REMAINDER; 303 opts.add(HTTPProxyPort = new NumberField(4), c); 304 HTTPProxyPort.setEnabled(false); 305 HTTPProxyBox.addActionListener(new ActionListener () { 306 public void actionPerformed(ActionEvent event) { 307 if (proxySupport.equals(event.getActionCommand())) { 308 boolean b = HTTPProxyBox.isSelected(); 309 Color color = b 310 ? Color.black 311 : Color.gray; 312 HTTPProxyHost.setEnabled(b); 313 HTTPProxyPort.setEnabled(b); 314 HTTPProxyHostLabel.setForeground(color); 315 HTTPProxyPortLabel.setForeground(color); 316 } 317 } 318 }); 319 320 String tmp = System.getProperty("http.proxyHost"); 322 if ((tmp != null) && tmp.equals("")) { 323 tmp = null; 324 } 325 HTTPProxyBox.setSelected(tmp != null); 326 HTTPProxyHost.setEnabled(tmp != null); 327 HTTPProxyPort.setEnabled(tmp != null); 328 HTTPProxyHostLabel.setForeground((tmp != null) 329 ? Color.black 330 : Color.gray); 331 HTTPProxyPortLabel.setForeground((tmp != null) 332 ? Color.black 333 : Color.gray); 334 if (tmp != null) { 335 HTTPProxyBox.setSelected(true); 336 HTTPProxyHost.setText(tmp); 337 tmp = System.getProperty("http.proxyPort"); 338 if ((tmp != null) && tmp.equals("")) { 339 tmp = null; 340 } 341 if (tmp == null) { 342 tmp = "80"; 343 } 344 HTTPProxyPort.setText(tmp); 345 } 346 347 opts.add(Box.createRigidArea(new Dimension(1, 10)), c); 349 c.anchor = GridBagConstraints.WEST; 350 c.gridwidth = GridBagConstraints.REMAINDER; 351 final String delaySupport = getMessage("delay00", 352 "Simulate Slow Connection"); 353 opts.add(delayBox = new JCheckBox(delaySupport), c); 354 355 c.anchor = GridBagConstraints.WEST; 357 c.gridwidth = 1; 358 delayBytesLabel = new JLabel(getMessage("delay01", 359 "Bytes per Pause")); 360 opts.add(delayBytesLabel, c); 361 delayBytesLabel.setForeground(Color.gray); 362 c.anchor = GridBagConstraints.WEST; 363 c.gridwidth = GridBagConstraints.REMAINDER; 364 opts.add(delayBytes = new NumberField(6), c); 365 delayBytes.setEnabled(false); 366 367 c.anchor = GridBagConstraints.WEST; 369 c.gridwidth = 1; 370 delayTimeLabel = new JLabel(getMessage("delay02", 371 "Delay in Milliseconds")); 372 opts.add(delayTimeLabel, c); 373 delayTimeLabel.setForeground(Color.gray); 374 c.anchor = GridBagConstraints.WEST; 375 c.gridwidth = GridBagConstraints.REMAINDER; 376 opts.add(delayTime = new NumberField(6), c); 377 delayTime.setEnabled(false); 378 379 delayBox.addActionListener(new ActionListener () { 381 public void actionPerformed(ActionEvent event) { 382 if (delaySupport.equals(event.getActionCommand())) { 383 boolean b = delayBox.isSelected(); 384 Color color = b 385 ? Color.black 386 : Color.gray; 387 delayBytes.setEnabled(b); 388 delayTime.setEnabled(b); 389 delayBytesLabel.setForeground(color); 390 delayTimeLabel.setForeground(color); 391 } 392 } 393 }); 394 395 mainPane.add(Box.createRigidArea(new Dimension(1, 10)), c); 398 399 c.anchor = GridBagConstraints.WEST; 402 c.gridwidth = GridBagConstraints.REMAINDER; 403 final String add = getMessage("add00", "Add"); 404 mainPane.add(addButton = new JButton(add), c); 405 this.add(new JScrollPane(mainPane), BorderLayout.CENTER); 406 407 addButton.addActionListener(new ActionListener () { 409 public void actionPerformed(ActionEvent event) { 410 if (add.equals(event.getActionCommand())) { 411 String text; 412 Listener l = null; 413 int lPort; 414 lPort = port.getValue(0); 415 if (lPort == 0) { 416 417 return; 419 } 420 String tHost = host.getText(); 421 int tPort = 0; 422 tPort = tport.getValue(0); 423 SlowLinkSimulator slowLink = null; 424 if (delayBox.isSelected()) { 425 int bytes = delayBytes.getValue(0); 426 int time = delayTime.getValue(0); 427 slowLink = new SlowLinkSimulator(bytes, time); 428 } 429 try { 430 l = new Listener (noteb, null, lPort, tHost, tPort, 431 proxyButton.isSelected(), 432 slowLink); 433 } catch (Exception e) { 434 e.printStackTrace(); 435 } 436 437 text = HTTPProxyHost.getText(); 440 if ("".equals(text)) { 441 text = null; 442 } 443 l.HTTPProxyHost = text; 444 text = HTTPProxyPort.getText(); 445 int proxyPort = HTTPProxyPort.getValue(-1); 446 if (proxyPort != -1) { 447 l.HTTPProxyPort = Integer.parseInt(text); 448 } 449 450 port.setText(null); 452 } 453 } 454 }); 455 notebook.addTab(name, this); 456 notebook.repaint(); 457 notebook.setSelectedIndex(notebook.getTabCount() - 1); 458 } 459 } 460 461 465 class SocketWaiter extends Thread { 466 469 ServerSocket sSocket = null; 470 471 474 Listener listener; 475 476 479 int port; 480 481 484 boolean pleaseStop = false; 485 486 492 public SocketWaiter(Listener l, int p) { 493 listener = l; 494 port = p; 495 start(); 496 } 497 498 501 public void run() { 502 try { 503 listener.setLeft( 504 new JLabel( 505 getMessage("wait00", " Waiting for Connection..."))); 506 listener.repaint(); 507 sSocket = new ServerSocket (port); 508 for (; ;) { 509 Socket inSocket = sSocket.accept(); 510 if (pleaseStop) { 511 break; 512 } 513 new Connection(listener, inSocket); 514 inSocket = null; 515 } 516 } catch (Exception exp) { 517 if (!"socket closed".equals(exp.getMessage())) { 518 JLabel tmp = new JLabel(exp.toString()); 519 tmp.setForeground(Color.red); 520 listener.setLeft(tmp); 521 listener.setRight(new JLabel("")); 522 listener.stop(); 523 } 524 } 525 } 526 527 530 public void halt() { 531 try { 532 pleaseStop = true; 533 new Socket ("127.0.0.1", port); 534 if (sSocket != null) { 535 sSocket.close(); 536 } 537 } catch (Exception e) { 538 e.printStackTrace(); 539 } 540 } 541 } 542 543 546 static class SlowLinkSimulator { 547 550 private int delayBytes; 551 552 555 private int delayTime; 556 557 560 private int currentBytes; 561 562 565 private int totalBytes; 566 567 573 public SlowLinkSimulator(int delayBytes, int delayTime) { 574 this.delayBytes = delayBytes; 575 this.delayTime = delayTime; 576 } 577 578 584 public SlowLinkSimulator(SlowLinkSimulator that) { 585 this.delayBytes = that.delayBytes; 586 this.delayTime = that.delayTime; 587 } 588 589 594 public int getTotalBytes() { 595 return totalBytes; 596 } 597 598 604 public void pump(int bytes) { 605 totalBytes += bytes; 606 if (delayBytes == 0) { 607 608 return; 610 } 611 currentBytes += bytes; 612 if (currentBytes > delayBytes) { 613 614 int delaysize = currentBytes / delayBytes; 616 long delay = delaysize * (long) delayTime; 617 618 currentBytes = currentBytes % delayBytes; 620 621 try { 623 Thread.sleep(delay); 624 } catch (InterruptedException e) { 625 ; } 627 } 628 } 629 630 635 public int getCurrentBytes() { 636 return currentBytes; 637 } 638 639 644 public void setCurrentBytes(int currentBytes) { 645 this.currentBytes = currentBytes; 646 } 647 } 648 649 653 class SocketRR extends Thread { 654 657 Socket inSocket = null; 658 659 662 Socket outSocket = null; 663 664 667 JTextArea textArea; 668 669 672 InputStream in = null; 673 674 677 OutputStream out = null; 678 679 682 boolean xmlFormat; 683 684 687 volatile boolean done = false; 688 689 692 TableModel tmodel = null; 693 694 697 int tableIndex = 0; 698 699 702 String type = null; 703 704 707 Connection myConnection = null; 708 709 712 SlowLinkSimulator slowLink; 713 714 729 public SocketRR(Connection c, Socket inputSocket, 730 InputStream inputStream, Socket outputSocket, 731 OutputStream outputStream, JTextArea _textArea, 732 boolean format, TableModel tModel, int index, 733 final String type, SlowLinkSimulator slowLink) { 734 inSocket = inputSocket; 735 in = inputStream; 736 outSocket = outputSocket; 737 out = outputStream; 738 textArea = _textArea; 739 xmlFormat = format; 740 tmodel = tModel; 741 tableIndex = index; 742 this.type = type; 743 myConnection = c; 744 this.slowLink = slowLink; 745 start(); 746 } 747 748 753 public boolean isDone() { 754 return (done); 755 } 756 757 760 public void run() { 761 try { 762 byte[] buffer = new byte[4096]; 763 byte[] tmpbuffer = new byte[8192]; 764 int saved = 0; 765 int len; 766 int i1, i2; 767 int i; 768 int reqSaved = 0; 769 int tabWidth = 3; 770 boolean atMargin = true; 771 int thisIndent = -1, nextIndent = -1, previousIndent = -1; 772 if (tmodel != null) { 773 String tmpStr = (String ) tmodel.getValueAt(tableIndex, 774 REQ_COLUMN); 775 if (!"".equals(tmpStr)) { 776 reqSaved = tmpStr.length(); 777 } 778 } 779 a: 780 for (; ;) { 781
|