1 8 package org.jboss.cache.aop; 9 10 11 import org.apache.log4j.Logger; 12 import org.jboss.cache.*; 13 import org.jgroups.View; 14 15 import javax.swing.*; 16 import javax.swing.event.TableModelEvent ; 17 import javax.swing.event.TableModelListener ; 18 import javax.swing.event.TreeSelectionEvent ; 19 import javax.swing.event.TreeSelectionListener ; 20 import javax.swing.table.DefaultTableModel ; 21 import javax.swing.table.TableColumn ; 22 import javax.swing.tree.*; 23 import java.awt.*; 24 import java.awt.event.*; 25 import java.io.File ; 26 import java.util.*; 27 28 30 41 public class TreeCacheAopView 42 { 43 TreeCacheAopGui gui_ = null; 44 TreeCacheAop cache_ = null; 45 static Logger logger_ = Logger.getLogger(TreeCacheAopView.class.getName()); 46 47 public TreeCacheAopView(TreeCacheAop cache) throws Exception 48 { 49 this.cache_ = cache; 50 } 51 52 public void start() throws Exception 53 { 54 if (gui_ == null) { 55 logger_.info("start(): creating the GUI"); 56 gui_ = new TreeCacheAopGui(cache_); 57 } 58 } 59 60 public void stop() 61 { 62 if (gui_ != null) { 63 logger_.info("stop(): disposing the GUI"); 64 gui_.dispose(); 65 gui_ = null; 66 } 67 } 68 69 void populateTree(String dir) throws Exception 70 { 71 File file = new File (dir); 72 73 if (!file.exists()) return; 74 75 put(dir, null); 76 77 if (file.isDirectory()) { 78 String [] children = file.list(); 79 if (children != null && children.length > 0) { 80 for (int i = 0; i < children.length; i++) 81 populateTree(dir + "/" + children[i]); 82 } 83 } 84 } 85 86 void put(String fqn, Map m) 87 { 88 try { 89 cache_.put(fqn, m); 90 } catch (Throwable t) { 91 System.err.println("TreeCacheAopView.put(): " + t); 92 } 93 } 94 95 public static void main(String args[]) 96 { 97 TreeCacheAop tree = null; 98 TreeCacheAopView demo; 99 String start_directory = null; 100 String resource="META-INF/replSync-service.xml"; 101 102 for(int i=0; i < args.length; i++) { 103 if(args[i].equals("-config")) { 104 resource=args[++i]; 105 continue; 106 } 107 help(); 108 return; 109 } 110 111 112 try { 113 tree = new TreeCacheAop(); 114 PropertyConfigurator config = new PropertyConfigurator(); 115 config.configure(tree, resource); 116 117 tree.addTreeCacheListener(new TreeCacheView.MyListener()); 118 tree.createService(); 119 tree.startService(); 120 121 Runtime.getRuntime().addShutdownHook(new ShutdownThread(tree)); 122 123 demo = new TreeCacheAopView(tree); 124 demo.start(); 125 if (start_directory != null && start_directory.length() > 0) { 126 demo.populateTree(start_directory); 127 } 128 } catch (Exception ex) { 129 ex.printStackTrace(); 130 } 131 } 132 133 134 static class ShutdownThread extends Thread 135 { 136 TreeCacheAop tree = null; 137 138 ShutdownThread(TreeCacheAop tree) 139 { 140 this.tree = tree; 141 } 142 143 public void run() 144 { 145 tree.stop(); 146 } 147 } 148 149 static void help() 150 { 151 System.out.println("TreeCacheAopView [-help] [-config <configuration file (XML)]" + 152 "[-mbean_name <name of TreeCache MBean>] " + 153 "[-start_directory <dirname>] [-props <props>] " + 154 "[-use_queue <true/false>] [-queue_interval <ms>] " + 155 "[-queue_max_elements <num>]"); 156 } 157 158 } 159 160 161 class TreeCacheAopGui extends JFrame implements WindowListener, TreeCacheListener, 162 TreeSelectionListener , TableModelListener 163 { 164 TreeCacheAop cache_; 165 DefaultTreeModel tree_model = null; 166 JTree jtree = null; 167 DefaultTableModel table_model = new DefaultTableModel (); 168 JTable table = new JTable(table_model); 169 MyNode root = new MyNode(SEP, Fqn.fromString(SEP)); 170 String props = null; 171 String selected_node = null; 172 JPanel tablePanel = null; 173 JMenu operationsMenu = null; 174 JPopupMenu operationsPopup = null; 175 JMenuBar menubar = null; 176 boolean use_system_exit = false; 177 static String SEP = TreeCache.SEPARATOR; 178 private static final int KEY_COL_WIDTH = 20; 179 private static final int VAL_COL_WIDTH = 300; 180 final String STRING = String .class.getName(); 181 final String MAP = Map.class.getName(); 182 final String OBJECT = Object .class.getName(); 183 String currentNodeSelected = null; 184 185 public TreeCacheAopGui(TreeCacheAop cache) throws Exception 186 { 187 this.cache_ = cache; 188 189 cache_.addTreeCacheListener(this); 190 addNotify(); 191 setTitle("TreeCacheAopGui: mbr=" + getLocalAddress()); 192 193 tree_model = new DefaultTreeModel(root); 194 jtree = new JTree(tree_model); 195 jtree.setDoubleBuffered(true); 196 jtree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); 197 198 JPanel panel = new JPanel(); 199 panel.setLayout(new BorderLayout()); 200 JScrollPane scroll_pane = new JScrollPane(jtree); 201 panel.add(scroll_pane, BorderLayout.CENTER); 202 203 populateTree(); 204 205 getContentPane().add(panel, BorderLayout.CENTER); 206 addWindowListener(this); 207 208 table_model.setColumnIdentifiers(new String []{"Name", "Value"}); 209 table_model.addTableModelListener(this); 210 211 setTableColumnWidths(); 212 213 tablePanel = new JPanel(); 214 tablePanel.setLayout(new BorderLayout()); 215 tablePanel.add(table.getTableHeader(), BorderLayout.NORTH); 216 tablePanel.add(table, BorderLayout.CENTER); 217 218 getContentPane().add(tablePanel, BorderLayout.SOUTH); 219 220 jtree.addTreeSelectionListener(this); 222 MouseListener ml = new MouseAdapter() 223 { 224 public void mouseClicked(MouseEvent e) 225 { 226 int selRow = jtree.getRowForLocation(e.getX(), e.getY()); 227 TreePath selPath = jtree.getPathForLocation(e.getX(), e.getY()); 228 if (selRow != -1) { 229 selected_node = makeFQN(selPath.getPath()); 230 jtree.setSelectionPath(selPath); 231 232 if (e.getModifiers() == java.awt.event.InputEvent.BUTTON3_MASK) { 233 operationsPopup.show(e.getComponent(), 234 e.getX(), e.getY()); 235 } 236 } 237 } 238 }; 239 240 jtree.addMouseListener(ml); 241 242 createMenus(); 243 setLocation(50, 50); 244 setSize(getInsets().left + getInsets().right + 485, 245 getInsets().top + getInsets().bottom + 367); 246 247 init(); 248 setVisible(true); 249 } 250 251 void setSystemExit(boolean flag) 252 { 253 use_system_exit = flag; 254 } 255 256 public void windowClosed(WindowEvent event) 257 { 258 } 259 260 public void windowDeiconified(WindowEvent event) 261 { 262 } 263 264 public void windowIconified(WindowEvent event) 265 { 266 } 267 268 public void windowActivated(WindowEvent event) 269 { 270 } 271 272 public void windowDeactivated(WindowEvent event) 273 { 274 } 275 276 public void windowOpened(WindowEvent event) 277 { 278 } 279 280 public void windowClosing(WindowEvent event) 281 { 282 dispose(); 283 } 284 285 286 public void tableChanged(TableModelEvent evt) 287 { 288 int row, col; 289 String key, val; 290 291 if (evt.getType() == TableModelEvent.UPDATE) { 292 row = evt.getFirstRow(); 293 col = evt.getColumn(); 294 if (col == 0) { key = (String ) table_model.getValueAt(row, col); 296 val = (String ) table_model.getValueAt(row, col + 1); 297 if (key != null && val != null) { 298 300 try { 301 cache_.put(selected_node, key, val); 302 } catch (Exception e) { 303 e.printStackTrace(); 304 } 305 306 } 307 } else { key = (String ) table_model.getValueAt(row, col - 1); 309 val = (String ) table.getValueAt(row, col); 310 if (key != null && val != null) { 311 put(selected_node, key, val); 312 } 313 } 314 } 315 } 316 317 318 public void valueChanged(TreeSelectionEvent evt) 319 { 320 TreePath path = evt.getPath(); 321 String fqn = SEP; 322 String component_name; 323 Map data = null; 324 325 for (int i = 0; i < path.getPathCount(); i++) { 326 component_name = ((MyNode) path.getPathComponent(i)).name; 327 if (component_name.equals(SEP)) 328 continue; 329 if (fqn.equals(SEP)) 330 fqn += component_name; 331 else 332 fqn = fqn + SEP + component_name; 333 } 334 data = getData(fqn); 335 System.out.println("valueChanged(): fqn: " + fqn + " data: " + data); 336 if (data != null) { 337 getContentPane().add(tablePanel, BorderLayout.SOUTH); 338 populateTable(data); 339 validate(); 340 } else { 341 clearTable(); 342 getContentPane().remove(tablePanel); 343 validate(); 344 } 345 } 346 347 348 349 350 351 public void nodeCreated(Fqn fqn) 352 { 353 MyNode n, p; 354 355 n = root.add(fqn); 356 if (n != null) { 357 p = (MyNode) n.getParent(); 358 tree_model.reload(p); 359 jtree.scrollPathToVisible(new TreePath(n.getPath())); 360 } 361 } 362 363 public void nodeRemoved(Fqn fqn) 364 { 365 MyNode n; 366 TreeNode par; 367 368 n = root.findNode(fqn.toString()); 369 if (n != null) { 370 n.removeAllChildren(); 371 par = n.getParent(); 372 n.removeFromParent(); 373 tree_model.reload(par); 374 } 375 } 376 377 public void nodeLoaded(Fqn fqn) { 378 nodeCreated(fqn); 379 } 380 381 public void nodeEvicted(Fqn fqn) { 382 nodeRemoved(fqn); 383 } 384 385 public void nodeModified(final Fqn fqn) 386 { 387 Map data; 392 if (currentNodeSelected != null && !currentNodeSelected.equals(fqn.toString())) return; data = getData(fqn.toString()); 394 populateTable(data); 398 401 } 402 403 public void nodeVisited(Fqn fqn) 404 { 405 } 407 408 public void cacheStarted(TreeCache cache) 409 { 410 } 412 413 public void cacheStopped(TreeCache cache) 414 { 415 } 417 418 public void viewChange(final View new_view) 419 { 420 new Thread () 421 { 422 public void run() 423 { 424 Vector mbrship; 425 if (new_view != null && (mbrship = new_view.getMembers()) != null) { 426 _put(SEP, "members", mbrship); 427 _put(SEP, "coordinator", mbrship.firstElement()); 428 } 429 } 430 }.start(); 431 } 432 433 434 435 436 437 438 439 440 public void run() 441 { 442 443 } 444 445 446 447 448 449 452 void init() 453 { 454 Vector mbrship = null; 455 456 addGuiNode(SEP); 457 458 mbrship = getMembers() != null ? (Vector) getMembers().clone() : null; 459 if (mbrship != null && mbrship.size() > 0) { 460 _put(SEP, "members", mbrship); 461 _put(SEP, "coordinator", mbrship.firstElement()); 462 } 463 } 464 465 466 469 private void populateTree() 470 { 471 addGuiNode(SEP); 472 } 473 474 475 478 void addGuiNode(String fqn) 479 { 480 Set children; 481 String child_name; 482 483 if (fqn == null) return; 484 485 root.add(Fqn.fromString(fqn)); 487 488 children = getChildrenNames(fqn); 490 if (children != null) { 491 for (Iterator it = children.iterator(); it.hasNext();) { 492 child_name = (String ) it.next(); 493 addGuiNode(fqn + SEP + child_name); 494 } 495 } 496 } 497 498 499 String makeFQN(Object [] path) 500 { 501 StringBuffer sb = new StringBuffer (""); 502 String tmp_name; 503 504 if (path == null) return null; 505 for (int i = 0; i < path.length; i++) { 506 tmp_name = ((MyNode) path[i]).name; 507 if (tmp_name.equals(SEP)) 508 continue; 509 else 510 sb.append(SEP + tmp_name); 511 } 512 tmp_name = sb.toString(); 513 if (tmp_name.length() == 0) 514 return SEP; 515 else 516 return tmp_name; 517 } 518 519 void clearTable() 520 { 521 int num_rows = table.getRowCount(); 522 523 if (num_rows > 0) { 524 for (int i = 0; i < num_rows; i++) 525 table_model.removeRow(0); 526 table_model.fireTableRowsDeleted(0, num_rows - 1); 527 repaint(); 528 } 529 } 530 531 532 void populateTable(Map data) 533 { 534 String key, strval = "<null>"; 535 Object val; 536 int num_rows = 0; 537 Map.Entry entry; 538 539 if (data == null) return; 540 num_rows = data.size(); 541 clearTable(); 542 543 if (num_rows > 0) { 544 for (Iterator it = data.entrySet().iterator(); it.hasNext();) { 545 entry = (Map.Entry) it.next(); 546 key = (String ) entry.getKey(); 547 val = entry.getValue(); 548 if (val != null) strval = val.toString(); 549 table_model.addRow(new Object []{key, strval}); 550 } 551 table_model.fireTableRowsInserted(0, num_rows - 1); 552 validate(); 553 } 554 } 555 556 private void setTableColumnWidths() 557 { 558 table.sizeColumnsToFit(JTable.AUTO_RESIZE_NEXT_COLUMN); 559 TableColumn column = null; 560 column = table.getColumnModel().getColumn(0); 561 column.setMinWidth(KEY_COL_WIDTH); 562 column.setPreferredWidth(KEY_COL_WIDTH); 563 column = table.getColumnModel().getColumn(1); 564 column.setPreferredWidth(VAL_COL_WIDTH); 565 } 566 567 private void createMenus() 568 { 569 menubar = new JMenuBar(); 570 operationsMenu = new JMenu("Operations"); 571 AddNodeAction addNode = new AddNodeAction(); 572 addNode.putValue(AbstractAction.NAME, "Add to this node"); 573 RemoveNodeAction removeNode = new RemoveNodeAction(); 574 removeNode.putValue(AbstractAction.NAME, "Remove this node"); 575 AddModifyDataForNodeAction addModAction = new AddModifyDataForNodeAction(); 576 addModAction.putValue(AbstractAction.NAME, "Add/Modify data"); 577 PrintLockInfoAction print_locks = new PrintLockInfoAction(); 578 print_locks.putValue(AbstractAction.NAME, "Print lock information (stdout)"); 579 ReleaseAllLocksAction release_locks = new ReleaseAllLocksAction(); 580 release_locks.putValue(AbstractAction.NAME, "Release all locks"); 581 ExitAction exitAction = new ExitAction(); 582 exitAction.putValue(AbstractAction.NAME, "Exit"); 583 operationsMenu.add(addNode); 584 operationsMenu.add(removeNode); 585 operationsMenu.add(addModAction); 586 operationsMenu.add(print_locks); 587 operationsMenu.add(release_locks); 588 operationsMenu.add(exitAction); 589 menubar.add(operationsMenu); 590 setJMenuBar(menubar); 591 592 operationsPopup = new JPopupMenu(); 593 operationsPopup.add(addNode); 594 operationsPopup.add(removeNode); 595 operationsPopup.add(addModAction); 596 } 597 598 Object getLocalAddress() 599 { 600 try { 601 return cache_.getLocalAddress(); 602 } catch (Throwable t) { 603 System.err.println("TreeCacheAopGui.getLocalAddress(): " + t); 604 return null; 605 } 606 } 607 608 Map getData(String fqn) 609 { 610 Map data; 611 Set keys; 612 String key; 613 Object value; 614 615 if (fqn == null) return null; 616 currentNodeSelected = fqn; 618 619 MyNode node = root.findNode(fqn); 621 if (node == null) return null; 622 keys = getKeys(node.getFqn()); 624 if (keys == null) return null; 625 data = new HashMap(); 626 for (Iterator it = keys.iterator(); it.hasNext();) { 627 key = it.next().toString(); 628 value = get(node.getFqn(), key); 629 if (value != null) 630 data.put(key, value); 631 } 632 return data; 633 } 634 635 636 void put(String fqn, Map m) 637 { 638 try { 639 cache_.put(fqn, m); 640 } catch (Throwable t) { 641 System.err.println("TreeCacheAopGui.put(): " + t); 642 } 643 } 644 645 646 private void put(String fqn, String key, Object value) 647 { 648 try { 649 cache_.put(fqn, key, value); 650 } catch (Throwable t) { 651 System.err.println("TreeCacheAopGui.put(): " + t); 652 } 653 } 654 655 void _put(String fqn, String key, Object value) 656 { 657 try { 658 cache_._put(null, fqn, key, value, false); 659 } catch (Throwable t) { 660 System.err.println("TreeCacheAopGui._put(): " + t); 661 } 662 } 663 664 Set getKeys(Fqn fqn) 665 { 666 try { 667 return cache_.getKeys(fqn); 668 } catch (Throwable t) { 669 t.printStackTrace(); 670 System.err.println("TreeCacheAopGui.getKeys(): " + t); 671 return null; 672 } 673 } 674 675 Object get(Fqn fqn, String key) 676 { 677 try { 678 return cache_.get(fqn, key); 679 } catch (Throwable t) { 680 System.err.println("TreeCacheAopGui.get(): " + t); 681 return null; 682 } 683 } 684 685 Set getChildrenNames(String fqn) 686 { 687 try { 688 return cache_.getChildrenNames(fqn); 689 } catch (Throwable t) { 690 System.err.println("TreeCacheAopGui.getChildrenNames(): " + t); 691 return null; 692 } 693 } 694 695 Vector getMembers() 696 { 697 try { 698 return cache_.getMembers(); 699 } catch (Throwable t) { 700 System.err.println("TreeCacheAopGui.getMembers(): " + t); 701 return null; 702 } 703 } 704 705 706 707 708 709 class ExitAction extends AbstractAction 710 { 711 public void actionPerformed(ActionEvent e) 712 { 713 dispose(); 714 System.exit(0); 715 } 716 } 717 718 class AddNodeAction extends AbstractAction 719 { 720 public void actionPerformed(ActionEvent e) 721 { 722 JTextField fqnTextField = new JTextField(); 723 if (selected_node != null) 724 fqnTextField.setText(selected_node); 725 Object [] information = {"Enter fully qualified name", 726 fqnTextField}; 727 final String btnString1 = "OK"; 728 final String btnString2 = "Cancel"; 729 Object [] options = {btnString1, btnString2}; 730 int userChoice = JOptionPane.showOptionDialog(null, 731 information, 732 "Add Node", 733 JOptionPane.YES_NO_OPTION, 734 JOptionPane.PLAIN_MESSAGE, 735 null, 736 options, 737 options[0]); 738 if (userChoice == 0) { 739 String userInput = fqnTextField.getText(); 740 put(userInput, null); 741 } 742 } 743 } 744 745 746 class PrintLockInfoAction extends AbstractAction 747 { 748 public void actionPerformed(ActionEvent e) 749 { 750 System.out.println("\n*** lock information ****\n" + cache_.printLockInfo()); 751 } 752 } 753 754 class ReleaseAllLocksAction extends AbstractAction 755 { 756 public void actionPerformed(ActionEvent e) 757 { 758 cache_.releaseAllLocks("/"); 759 } 760 } 761 762 class RemoveNodeAction extends AbstractAction 763 { 764 public void actionPerformed(ActionEvent e) 765 { 766 try { 767 cache_.remove(selected_node); 768 } catch (Throwable t) { 769 System.err.println("RemoveNodeAction.actionPerformed(): " + t); 770 } 771 } 772 } 773 774 class AddModifyDataForNodeAction extends AbstractAction 775 { 776 public void actionPerformed(ActionEvent e) 777 { 778 Map data = getData(selected_node); 779 if (data != null) { 780 } else { 781 clearTable(); 782 data = new HashMap(); 783 data.put("Add Key", "Add Value"); 784 785 } 786 populateTable(data); 787 getContentPane().add(tablePanel, BorderLayout.SOUTH); 788 validate(); 789 790 } 791 } 792 793 794 class MyNode extends DefaultMutableTreeNode 795 { 796 String name = "<unnamed>"; Fqn fqn; 799 MyNode(String name, Fqn fqn) 800 { 801 this.fqn = fqn; 802 this.name = name; 803 } 804 805 public Fqn getFqn() 806 { 807 return fqn; 808 } 809 810 814 public MyNode add(Fqn fqn) 815 { 816 MyNode curr, n, ret = null; 817 StringTokenizer tok; 818 String child_name; 819 820 if (fqn == null) return null; 821 curr = this; 822 String fqnStr = fqn.toString(); 823 tok = new StringTokenizer(fqnStr, TreeCacheAopGui.SEP); 824 825 int i = 0; 826 while (tok.hasMoreTokens()) { 827 child_name = tok.nextToken(); 828 n = curr.findChild(child_name); 829 if (n == null) { 830 n = new MyNode(child_name, fqn.getFqnChild(i + 1)); 831 if (ret == null) ret = n; 832 curr.add(n); 833 } 834 curr = n; 835 i++; 836 } 837 return ret; 838 } 839 840 841 844 public void remove(String fqn) 845 { 846 removeFromParent(); 847 } 848 849 850 MyNode findNode(String fqn) 851 { 852 MyNode curr, n; 853 StringTokenizer tok; 854 String child_name; 855 856 if (fqn == null) return null; 857 curr = this; 858 tok = new StringTokenizer(fqn, TreeCacheAopGui.SEP); 859 860 while (tok.hasMoreTokens()) { 861 child_name = tok.nextToken(); 862 n = curr.findChild(child_name); 863 if (n == null) 864 return null; 865 curr = n; 866 } 867 return curr; 868 } 869 870 871 MyNode findChild(String relative_name) 872 { 873 MyNode child; 874 875 if (relative_name == null || getChildCount() == 0) 876 return null; 877 for (int i = 0; i < getChildCount(); i++) { 878 child = (MyNode) getChildAt(i); 879 if (child.name == null) { 880 continue; 881 } 882 883 if (child.name.equals(relative_name)) 884 return child; 885 } 886 return null; 887 } 888 889 890 String print(int indent) 891 { 892 StringBuffer sb = new StringBuffer (); 893 894 for (int i = 0; i < indent; i++) 895 sb.append(" "); 896 if (!isRoot()) { 897 if (name == null) 898 sb.append("/<unnamed>"); 899 else { 900 sb.append(TreeCacheAopGui.SEP + name); 901 } 902 } 903 sb.append("\n"); 904 if (getChildCount() > 0) { 905 if (isRoot()) 906 indent = 0; 907 else 908 indent += 4; 909 for (int i = 0; i < getChildCount(); i++) 910 sb.append(((MyNode) getChildAt(i)).print(indent)); 911 } 912 return sb.toString(); 913 } 914 915 916 public String toString() 917 { 918 return name; 919 } 920 921 } 922 923 924 } 925 926 927 928 929 930 931 932 933 934 935 936 937 | Popular Tags |