1 19 20 23 24 package org.netbeans.modules.web.monitor.client; 25 26 import java.util.ArrayList ; 27 import java.util.Comparator ; 28 import java.util.Date ; 29 import java.util.Enumeration ; 30 import java.util.Hashtable ; 31 import java.util.List ; 32 import java.util.Vector ; 33 import java.util.Iterator ; 34 import java.io.File ; 35 import java.io.FileNotFoundException ; 36 import java.io.InputStreamReader ; 37 import java.io.IOException ; 38 import java.io.OutputStream ; 39 import java.io.PrintWriter ; 40 import java.net.InetAddress ; 41 import java.net.MalformedURLException ; 42 import java.net.Socket ; 43 import java.net.UnknownHostException ; 44 import java.net.URL ; 45 46 import javax.swing.SwingUtilities ; 47 48 import org.openide.DialogDisplayer; 49 import org.openide.ErrorManager; 50 import org.openide.NotifyDescriptor; 51 import org.openide.awt.HtmlBrowser; 52 import org.openide.filesystems.FileAlreadyLockedException; 53 import org.openide.filesystems.FileObject; 54 import org.openide.filesystems.FileLock; 55 import org.openide.filesystems.FileUtil; 56 import org.openide.filesystems.FileSystem; 57 import org.openide.filesystems.Repository; 58 import org.openide.filesystems.URLMapper; 59 import org.openide.nodes.Node; 60 import org.openide.nodes.AbstractNode; 61 import org.openide.nodes.Children; 62 import org.openide.util.NbBundle; 63 import org.openide.util.RequestProcessor; 64 65 import org.netbeans.modules.web.monitor.data.*; 66 67 class Controller { 68 69 final static String REPLAY="netbeans.replay"; final static String PORT="netbeans.replay.port"; final static String REPLAYSTATUS="netbeans.replay.status"; final static String REPLAYSESSION="netbeans.replay.session"; static final boolean debug = false; 75 77 private transient static String server = "localhost"; private transient static int port = 8080; 81 82 private static FileObject monDir = null; 84 private static FileObject currDir = null; 85 private static FileObject saveDir = null; 86 private static FileObject replayDir = null; 87 88 final static String monDirStr = "HTTPMonitor"; final static String currDirStr = "current"; final static String saveDirStr = "save"; final static String replayDirStr = "replay"; 93 private transient NavigateNode root = null; 95 private Children.SortedArray currTrans = null; 96 private Children.SortedArray savedTrans = null; 97 98 private Hashtable currBeans = null; 100 private Hashtable saveBeans = null; 101 102 private transient Comparator comp = null; 103 104 private boolean useBrowserCookie = true; 105 106 private static Controller instance = null; 107 private Date startDate; 108 109 private Controller() { 110 startDate = new Date (System.currentTimeMillis() - 30000); 113 currBeans = new Hashtable (); 114 saveBeans = new Hashtable (); 115 createNodeStructure(); 116 119 } 120 121 static Controller getInstance() { 122 if(instance == null) 123 instance = new Controller(); 124 return instance; 125 } 126 127 131 private void createNodeStructure() { 132 133 comp = new CompTime(true); 134 currTrans = new Children.SortedArray(); 135 currTrans.setComparator(comp); 136 savedTrans = new Children.SortedArray(); 137 savedTrans.setComparator(comp); 138 139 CurrNode currNode = new CurrNode(currTrans); 140 SavedNode savedNode = new SavedNode(savedTrans); 141 142 Node[] kids = new Node[2]; 143 kids[0] = currNode; 144 kids[1] = savedNode; 145 146 Children children = new Children.Array(); 147 children.add(kids); 148 root = new NavigateNode(children); 149 150 151 } 152 153 156 void addTransaction(String id) { 157 158 if(debug) log("Creating node for " + id); 159 TransactionNode[] nodes = new TransactionNode[1]; 160 MonitorData md = retrieveMonitorData(id, currDirStr); 161 try { 162 nodes[0] = createTransactionNode(md, true); 163 currTrans.add(nodes); 164 } 165 catch(Exception ex) { 166 } 168 } 169 170 173 protected NavigateNode getRoot() { 174 return root; 175 } 176 177 178 protected static FileObject getMonDir() throws FileNotFoundException { 179 180 if(monDir == null || !monDir.isFolder()) { 181 try { 182 createDirectories(); 183 } 184 catch(FileNotFoundException ex) { 185 throw ex; 186 } 187 } 188 return monDir; 189 } 190 191 192 protected static FileObject getCurrDir() throws FileNotFoundException { 193 194 if(currDir == null || !currDir.isFolder()) { 195 try{ 196 createDirectories(); 197 } 198 catch(FileNotFoundException ex) { 199 throw ex; 200 } 201 } 202 return currDir; 203 } 204 205 protected static FileObject getReplayDir() throws FileNotFoundException { 206 207 if(replayDir == null || !replayDir.isFolder()) { 208 try{ 209 createDirectories(); 210 } 211 catch(FileNotFoundException ex) { 212 throw ex; 213 } 214 } 215 return replayDir; 216 } 217 218 219 protected static FileObject getSaveDir() throws FileNotFoundException { 220 221 if(saveDir == null || !saveDir.isFolder()) { 222 try{ 223 createDirectories(); 224 } 225 catch(FileNotFoundException ex) { 226 throw ex; 227 } 228 } 229 return saveDir; 230 } 231 232 boolean haveDirectories() { 233 if(currDir == null) { 234 try { 235 currDir = getCurrDir(); 236 } 237 catch(Exception ex) { 238 return false; 239 } 240 } 241 242 if(saveDir == null) { 243 try { 244 saveDir = getSaveDir(); 245 } 246 catch(Exception ex) { 247 return false; 248 } 249 } 250 return true; 251 } 252 253 254 private static void createDirectories() throws FileNotFoundException { 255 256 if(debug) log("Now in createDirectories()"); 258 FileObject rootdir = 259 Repository.getDefault().getDefaultFileSystem().getRoot(); 260 if(debug) { 261 log("Root directory is " + rootdir.getName()); File rootF = FileUtil.toFile(rootdir); 263 log("Root directory abs path " + rootF.getAbsolutePath()); 265 } 266 267 FileLock lock = null; 268 269 if(monDir == null || !monDir.isFolder()) { 270 try { 271 monDir = rootdir.getFileObject(monDirStr); 272 } 273 catch(Exception ex) { 274 } 275 276 if(monDir == null || !monDir.isFolder()) { 277 if(monDir != null) { 278 try { 279 lock = monDir.lock(); 280 monDir.delete(lock); 281 } 282 catch(FileAlreadyLockedException falex) { 283 throw new FileNotFoundException (); 284 } 285 catch(IOException ex) { 286 throw new FileNotFoundException (); 287 } 288 finally { 289 if(lock != null) lock.releaseLock(); 290 } 291 } 292 try { 293 monDir = rootdir.createFolder(monDirStr); 294 } 295 catch(IOException ioex) { 296 if(debug) ioex.printStackTrace(); 297 } 298 } 299 if(monDir == null || !monDir.isFolder()) 300 throw new FileNotFoundException (); 301 } 302 303 if(debug) 304 log("monitor directory is " + monDir.getName()); 306 308 if(currDir == null || !currDir.isFolder()) { 309 310 try { 311 currDir = monDir.getFileObject(currDirStr); 312 } 313 catch(Exception ex) { } 314 315 if(currDir == null || !currDir.isFolder()) { 316 lock = null; 317 if(currDir != null) { 318 try { 319 lock = currDir.lock(); 320 currDir.delete(lock); 321 } 322 catch(FileAlreadyLockedException falex) { 323 throw new FileNotFoundException (); 324 } 325 catch(IOException ex) { 326 throw new FileNotFoundException (); 327 } 328 finally { 329 if(lock != null) lock.releaseLock(); 330 } 331 } 332 try { 333 currDir = monDir.createFolder(currDirStr); 334 } 335 catch(IOException ex) { 336 if(debug) ex.printStackTrace(); 337 } 338 } 339 if(currDir == null || !currDir.isFolder()) 340 throw new FileNotFoundException (); 341 } 342 343 if(debug) log("curr directory is " + currDir.getName()); 345 if(saveDir == null || !saveDir.isFolder()) { 347 try { 348 saveDir = monDir.getFileObject(saveDirStr); 349 } 350 catch(Exception ex) { } 351 352 if(saveDir == null || !saveDir.isFolder()) { 353 if(saveDir != null) { 354 lock = null; 355 try { 356 lock = saveDir.lock(); 357 saveDir.delete(lock); 358 } 359 catch(FileAlreadyLockedException falex) { 360 throw new FileNotFoundException (); 361 } 362 catch(IOException ex) { 363 throw new FileNotFoundException (); 364 } 365 finally { 366 if(lock != null) lock.releaseLock(); 367 } 368 } 369 try { 370 saveDir = monDir.createFolder(saveDirStr); 371 } 372 catch(IOException ex) { 373 if(debug) ex.printStackTrace(); 374 } 375 } 376 if(saveDir == null || !saveDir.isFolder()) 377 throw new FileNotFoundException (); 378 379 if(debug) 380 log("save directory is " + saveDir.getName()); } 382 383 385 if(replayDir == null || !replayDir.isFolder()) { 386 387 try { 388 replayDir = monDir.getFileObject(replayDirStr); 389 } 390 catch(Exception ex) { } 391 392 if(replayDir == null || !replayDir.isFolder()) { 393 if(replayDir != null) { 394 lock = null; 395 try { 396 lock = replayDir.lock(); 397 replayDir.delete(lock); 398 } 399 catch(FileAlreadyLockedException falex) { 400 throw new FileNotFoundException (); 401 } 402 catch(IOException ex) { 403 throw new FileNotFoundException (); 404 } 405 finally { 406 if(lock != null) lock.releaseLock(); 407 } 408 } 409 try { 410 replayDir = monDir.createFolder(replayDirStr); 411 } 412 catch(Exception ex) { 413 if(debug) ex.printStackTrace(); 414 } 415 } 416 if(replayDir == null || !replayDir.isFolder()) 417 throw new FileNotFoundException (); 418 419 if(debug) 420 log("replay directory is " + replayDir.getName()); } 422 } 423 424 425 435 void replayTransaction(Node node) { 436 437 if(debug) 438 log("replayTransaction(Node node) from node " + 439 node.getName()); 441 if(!checkServer(true)) return; 442 443 TransactionNode tn = null; 444 try { 445 tn = (TransactionNode)node; 446 } 447 catch(ClassCastException cce) { 448 if(debug) 449 log("Selected node was not a transaction node"); return; 451 } 452 453 MonitorData md = getMonitorData(tn, false, false); 454 if (md == null) { 455 String msg = NbBundle.getMessage(Controller.class, "MSG_NoMonitorData"); 456 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, msg); 457 return; 458 } 459 if(!useBrowserCookie) 460 md.getRequestData().setReplaceSessionCookie(true); 461 462 if(debug) { 463 log("Replace is " + String.valueOf(md.getRequestData().getReplaceSessionCookie())); 465 String fname = md.createTempFile("control-replay.xml"); log("Wrote replay data to " + fname); } 468 469 String status; 470 if(tn.isCurrent()) status = currDirStr; 471 else status = saveDirStr; 472 try { 473 replayTransaction(md, status); 474 } 475 catch(UnknownHostException uhe) { 476 478 Object [] options = { 479 NbBundle.getBundle(Controller.class).getString("MON_OK"), 480 }; 481 482 NotifyDescriptor noServerDialog = 483 new NotifyDescriptor(NbBundle.getMessage(Controller.class, "MON_Exec_server_no_host", md.getServerName()), 484 NbBundle.getBundle(Controller.class).getString("MON_Exec_server"), 485 NotifyDescriptor.DEFAULT_OPTION, 486 NotifyDescriptor.INFORMATION_MESSAGE, 487 options, 488 options[0]); 489 DialogDisplayer.getDefault().notify(noServerDialog); 490 491 } 492 catch(IOException ioe) { 493 494 if(debug) { 495 log(ioe.getMessage()); 496 ioe.printStackTrace(); 497 } 498 499 Object [] options = { 501 NbBundle.getBundle(Controller.class).getString("MON_OK"), 502 }; 503 504 NotifyDescriptor noServerDialog = 505 new NotifyDescriptor(NbBundle.getMessage(Controller.class, "MON_Exec_server_start", md.getServerAndPort()), 506 NbBundle.getBundle(Controller.class).getString("MON_Exec_server"), 507 NotifyDescriptor.DEFAULT_OPTION, 508 NotifyDescriptor.INFORMATION_MESSAGE, 509 options, 510 options[0]); 511 DialogDisplayer.getDefault().notify(noServerDialog); 512 } 513 } 514 515 519 void replayTransaction(MonitorData md) 520 throws UnknownHostException , IOException { 521 522 529 if(debug) log("replayTransaction(MonitorData md)"); 531 FileObject fo; 532 FileLock lock = null; 533 OutputStream out = null; 534 PrintWriter pw = null; 535 536 if(debug) log("Creating record for replay"); 538 String id = md.getAttributeValue("id"); 540 try { 541 fo = getReplayDir().createData(id, "xml"); if(debug) log(" Created file for replay data"); 547 } 548 catch(IOException ioex) { 549 550 try { 551 fo = getReplayDir().getFileObject(id, "xml"); 552 } 553 catch(IllegalArgumentException iaex) { 554 throw new IOException ("No replay dir"); 557 } 558 559 if(!fo.isData()) { 560 throw new IOException ("Can't create file, giving up"); 561 } 562 563 try { 564 lock = fo.lock(); 565 } 566 catch(FileAlreadyLockedException falex) { 567 throw new IOException ("Old file exist, islocked"); 568 } 569 570 try { 571 fo.delete(lock); 572 } 573 catch(IOException ioex2) { 574 throw new IOException ("Couldn't delete old file"); 575 } 576 finally { 577 if(lock != null) lock.releaseLock(); 578 } 579 580 try { 581 fo = getReplayDir().createData(id, "xml"); } 583 catch(IOException ioex2) { 584 if(debug) log(" Couldn't create file for replay data"); 585 throw ioex2; 586 } 587 } 588 589 try { 590 lock = fo.lock(); 591 } 592 catch(FileAlreadyLockedException fale) { 593 if(debug) log("Can't get a file lock for the replay file"); 594 throw new IOException (); 595 } 596 597 try { 598 out = fo.getOutputStream(lock); 599 pw = new PrintWriter (out); 600 md.write(pw); 601 if(debug) log("...record complete"); 603 if(debug) { 604 String fname = 605 md.createTempFile("control-record.xml"); log("Wrote replay data to " + fname); } 608 } 609 catch(IOException ioex) { 610 throw ioex; 611 } 612 finally { 613 if(lock != null) lock.releaseLock(); 614 try { 615 pw.close(); 616 } 617 catch(Throwable t) { 618 } 619 try { 620 out.close(); 621 } 622 catch(Throwable t) { 623 } 624 } 625 626 try { 627 replayTransaction(md, replayDirStr); 628 } 629 catch(UnknownHostException uhe) { 630 throw uhe; 631 } 632 catch(IOException ioe) { 633 throw ioe; 634 } 635 } 636 637 640 void replayTransaction(MonitorData md, String status) 641 throws UnknownHostException , IOException { 642 643 if(debug) log("replayTransaction(MonitorData md, String status )"); 645 URL url = null; 646 try { 647 String name = md.getServerName(); 648 int port = md.getServerPort(); 649 650 StringBuffer uriBuf = new StringBuffer (128); 651 uriBuf.append(md.getRequestData().getAttributeValue("uri")); uriBuf.append("?"); uriBuf.append(REPLAY); 654 uriBuf.append("="); uriBuf.append(md.getAttributeValue("id")); uriBuf.append("&"); uriBuf.append(REPLAYSTATUS); 658 uriBuf.append("="); uriBuf.append(status); 660 661 String portS = null; 662 try { 663 FileObject fo = Repository.getDefault().getDefaultFileSystem().getRoot(); 664 URL u = getSampleHTTPServerURL(); 665 portS = 666 String.valueOf(u.getPort()); 667 } 668 catch(Exception ex) { 669 } 671 if(portS != null) { 672 uriBuf.append("&"); uriBuf.append(PORT); 674 uriBuf.append("="); uriBuf.append(portS); 676 } 677 678 679 if(md.getRequestData().getReplaceSessionCookie()) { 680 uriBuf.append("&"); uriBuf.append(REPLAYSESSION); 682 uriBuf.append("="); uriBuf.append(md.getRequestData().getSessionID()); 684 } 685 url = new URL ("http", name, port, uriBuf.toString()); } 687 catch(MalformedURLException me) { 688 if(debug) log(me.getMessage()); 689 } 690 catch(NumberFormatException ne) { 691 if(debug) log(ne.getMessage()); 692 } 693 694 try { 696 showReplay(url); 697 } 698 catch(UnknownHostException uhe) { 699 throw uhe; 700 } 701 catch(IOException ioe) { 702 throw ioe; 703 } 704 } 705 706 void saveTransaction(Node[] nodes) { 707 708 if(!haveDirectories()) { 709 log("Couldn't get the directory"); return; 713 } 714 715 Node[] newNodes = new Node[nodes.length]; 716 TransactionNode mvNode; 717 String id; 718 719 boolean error = false; 720 721 for(int i=0; i < nodes.length; ++i) { 722 723 mvNode = (TransactionNode)nodes[i]; 724 id = mvNode.getID(); 725 726 if(debug) log(" Saving " + id); 728 if(currBeans.containsKey(id)) 729 saveBeans.put(id, currBeans.remove(id)); 730 731 734 FileLock lock = null; 735 try { 736 FileObject fold = 737 currDir.getFileObject(id, "xml"); lock = fold.lock(); 739 fold.copy(saveDir, id, "xml"); if(debug) log(fold.getName()); 741 fold.delete(lock); 742 mvNode.setCurrent(false); 743 newNodes[i] = mvNode; 744 } 745 catch(FileAlreadyLockedException falex) { 746 error = true; 747 } 749 catch(IOException ioex) { 750 error = true; 751 } 753 catch(Exception ex) { 754 error = true; 755 } 757 finally { 758 if(lock != null) lock.releaseLock(); 759 } 760 761 } 762 if(!error) currTrans.remove(nodes); 763 savedTrans.add(newNodes); 764 } 765 766 769 770 void deleteTransaction(final Node[] nodes) { 771 772 if(!haveDirectories()) { 773 log("Couldn't get the directory"); return; 777 } 778 779 if((nodes == null) || (nodes.length == 0)) return; 781 782 final ProgressMonitor progressMonitor = new ProgressMonitor(); 783 784 RequestProcessor.getDefault().post(new Runnable () { 785 public void run() { 786 Thread.yield(); 788 currTrans.remove(nodes); 790 savedTrans.remove(nodes); 791 792 int oldValue = 0; 793 for(int i=0; i < nodes.length; i++) { 794 TransactionNode node = (TransactionNode)nodes[i]; 795 FileObject fileObject = null; 796 if (node.isCurrent()) { 797 String id = node.getID(); 798 fileObject = currDir.getFileObject(id, "xml"); 799 currBeans.remove(id); 800 } else { 801 String id = node.getID(); 802 fileObject = saveDir.getFileObject(id, "xml"); 803 saveBeans.remove(id); 804 } 805 if (fileObject != null) { 806 FileLock lock = null; 808 try { 809 lock = fileObject.lock(); 810 fileObject.delete(lock); 811 } catch(FileAlreadyLockedException falex) { 812 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, falex); 813 } catch(IOException IOex) { 814 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, IOex); 815 } finally { 816 if(lock != null) { 817 lock.releaseLock(); 818 } 819 } 820 } 821 final int newValue = 100*(i+1)/nodes.length; 823 if (newValue > oldValue) { 824 oldValue = newValue; 825 SwingUtilities.invokeLater(new Runnable () { 826 public void run (){ 827 progressMonitor.setValue(newValue); 828 } 829 }); 830 } 831 } 832 SwingUtilities.invokeLater(new Runnable () { 833 public void run (){ 834 progressMonitor.close(); 835 } 836 }); 837 } 838 }); 839 progressMonitor.setVisible(true); 840 } 841 842 void deleteDirectory(String dir) { 843 844 if(!haveDirectories()) { 845 log("Couldn't get the directory"); return; 849 } 850 851 final FileObject directory; 852 if(dir.equals(saveDirStr)) { 853 directory = saveDir; 854 savedTrans.remove(savedTrans.getNodes()); 855 saveBeans.clear(); 856 } 857 858 else { 859 directory = currDir; 860 currTrans.remove(currTrans.getNodes()); 861 currBeans.clear(); 862 } 863 if (directory.getChildren().length == 0) { 864 return; 865 } 866 final ProgressMonitor progressMonitor = new ProgressMonitor(); 867 868 RequestProcessor.getDefault().post(new Runnable () { 869 public void run() { 870 Thread.yield(); 871 872 int number = directory.getChildren().length; 873 int oldValue = -1; 874 int i = 0; 875 876 for(Enumeration e = directory.getData(false); e.hasMoreElements(); ++i) { 877 FileObject fo = (FileObject) e.nextElement(); 878 FileLock lock = null; 879 try { 880 lock = fo.lock(); 881 fo.delete(lock); 882 } catch(FileAlreadyLockedException falex) { 883 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, falex); 884 } catch(IOException IOex) { 885 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, IOex); 886 } finally { 887 if(lock != null) { 888 lock.releaseLock(); 889 } 890 } 891 final int newValue = 100 * i/number; 893 if (newValue > oldValue) { 894 oldValue = newValue; 895 SwingUtilities.invokeLater(new Runnable () { 896 public void run() { 897 progressMonitor.setValue(newValue); 898 } 899 }); 900 } 901 } 902 SwingUtilities.invokeLater(new Runnable () { 903 public void run() { 904 progressMonitor.close(); 905 } 906 }); 907 } 908 }); 909 progressMonitor.setVisible(true); 910 } 911 912 void deleteTransactions() { 913 deleteDirectory(Constants.Files.save); 914 deleteDirectory(Constants.Files.current); 915 savedTrans.remove(savedTrans.getNodes()); 916 currTrans.remove(currTrans.getNodes()); 917 } 918 919 920 void getTransactions() { 921 922 if(debug) log("getTransactions"); 924 if(!haveDirectories()) { 925 log("Couldn't get the directory"); return; 929 } 930 931 Enumeration e = null; 932 Vector nodes = new Vector (); 933 int numtns = 0; 934 TransactionNode[] tns = null; 935 FileObject fo = null; 936 String id = null; 937 MonitorData md = null; 938 939 currTrans.remove(currTrans.getNodes()); 940 if(debug) log("getTransactions removed old nodes"); 942 e = currDir.getData(false); 943 final List fileObjectsToDelete = new ArrayList (); 944 while(e.hasMoreElements()) { 945 946 fo = (FileObject)e.nextElement(); 947 if (fo.lastModified().after(startDate)) { 949 id = fo.getName(); 950 if(debug) 951 log("getting current transaction: " + id); 953 md = retrieveMonitorData(id, currDir); 955 if (md != null) { 956 nodes.add(createTransactionNode(md, true)); 957 } 958 } else { 959 fileObjectsToDelete.add(fo); 960 } 961 } 962 RequestProcessor.getDefault().post(new Runnable () { 963 public void run() { 964 for (Iterator it = fileObjectsToDelete.iterator(); it.hasNext(); ) { 965 try { 966 ((FileObject) it.next()).delete(); 967 } catch (IOException e) { 968 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 969 } 970 } 971 } 972 }); 973 974 numtns = nodes.size(); 975 tns = new TransactionNode[numtns]; 976 for(int i=0;i<numtns;++i) 977 tns[i] = (TransactionNode)nodes.elementAt(i); 978 currTrans.add(tns); 979 980 981 savedTrans.remove(savedTrans.getNodes()); 982 nodes = new Vector (); 983 e = saveDir.getData(false); 984 while(e.hasMoreElements()) { 985 986 fo = (FileObject)e.nextElement(); 987 id = fo.getName(); 988 if(debug) 989 log("getting current transaction: " + id); md = retrieveMonitorData(id, saveDir); 992 if (md != null) { 993 nodes.add(createTransactionNode(md, false)); 994 } 995 } 996 997 numtns = nodes.size(); 998 tns = new TransactionNode[numtns]; 999 for(int i=0;i<numtns;++i) { 1000 tns[i] = (TransactionNode)nodes.elementAt(i); 1001 if(debug) 1002 log("Adding saved node" + tns[i].toString()); 1004 } 1005 savedTrans.add(tns); 1006 } 1007 1008 private int parseStatusCode(String statusCode) { 1009 if (statusCode == null) { 1010 return -1; 1011 } 1012 int statusCodeNum = 200; 1015 try { 1016 int idx = statusCode.indexOf(':'); 1017 if (idx != -1) { 1018 statusCode = statusCode.substring(0, idx); 1019 statusCodeNum = Integer.valueOf(statusCode).intValue(); 1020 } 1021 } catch(NumberFormatException nfe) { 1022 } 1024 return statusCodeNum; 1025 } 1026 1027 private TransactionNode createTransactionNode(MonitorData md, boolean current) { 1028 1029 if(debug) log("createTransactionNode(MonitorData)"); Dispatches dis = null; 1031 try { 1032 dis = md.getDispatches(); 1033 } 1034 catch(Exception ex) { 1035 } 1038 1039 TransactionNode node = null; 1040 1041 if(dis == null || dis.sizeDispatchData() == 0 ) { 1043 1044 if(debug) log("No dispatched requests"); node = new TransactionNode(md.getAttributeValue("id"), md.getAttributeValue("method"), md.getAttributeValue("resource"), current, 1049 parseStatusCode(md.getRequestData().getAttributeValue("status"))); } 1051 else { 1052 1053 int numChildren = dis.sizeDispatchData(); 1054 if(debug) log("We had some dispatched requests: " + String.valueOf(numChildren)); 1056 if(debug) log("\t for id " + md.getAttributeValue("resource")); Children.Array nested = new Children.Array(); 1060 1061 NestedNode[] nds = new NestedNode[numChildren]; 1064 for(int i=0; i<numChildren; ++i) { 1065 if(debug) { 1066 log("Getting a new monitor data object"); log(dis.getDispatchData(i).getAttributeValue("resource")); } 1069 nds[i] = createNestedNode(dis.getDispatchData(i), 1070 md.getAttributeValue("method"), null, i); 1072 } 1073 1074 nested.add(nds); 1075 node = new TransactionNode(md.getAttributeValue("id"), md.getAttributeValue("method"), md.getAttributeValue("resource"), nested, 1079 current, 1080 parseStatusCode(md.getRequestData().getAttributeValue("status"))); 1082 } 1083 return node; 1084 } 1085 1086 1087 private NestedNode createNestedNode(DispatchData dd, 1088 String method, 1089 int[] locator, 1090 int index) { 1091 1092 1093 Dispatches dis = dd.getDispatches(); 1094 NestedNode node = null; 1095 1096 int[] newloc = null; 1097 if(locator != null) { 1098 newloc = new int[locator.length + 1]; 1099 int j=0; 1100 while(j<locator.length) { 1101 newloc[j] = locator[j]; 1102 ++j; 1103 } 1104 newloc[j]=index; 1105 } 1106 else { 1107 newloc = new int[1]; 1108 newloc[0] = index; 1109 } 1110 1111 if(dis == null || dis.sizeDispatchData() == 0 ) { 1113 node = new NestedNode(dd.getAttributeValue("resource"), method, 1115 newloc, 1116 parseStatusCode(dd.getRequestData().getAttributeValue("status"))); } 1118 else { 1119 int numChildren = dis.sizeDispatchData(); 1120 Children.Array nested = new Children.Array(); 1121 NestedNode[] nds = new NestedNode[numChildren]; 1122 for(int i=0; i<numChildren; ++i) { 1123 nds[i] = createNestedNode(dis.getDispatchData(i), 1124 method, newloc, i); 1125 } 1126 1127 nested.add(nds); 1128 node = new NestedNode(dd.getAttributeValue("resource"), method, 1130 nested, 1131 newloc, 1132 parseStatusCode(dd.getRequestData().getAttributeValue("status"))); } 1134 return node; 1135 } 1136 1137 1138 1142 static void setServer(String loc, int p) { 1143 port = p; 1144 server = loc; 1145 return; 1146 } 1147 1148 void setComparator(Comparator comp) { 1149 currTrans.setComparator(comp); 1150 savedTrans.setComparator(comp); 1151 } 1152 1153 1164 void setUseBrowserCookie(boolean value) { 1165 useBrowserCookie = value; 1166 if(debug) 1167 log("Setting useBrowserCookie to " + String.valueOf(useBrowserCookie)); 1169 } 1170 1171 boolean getUseBrowserCookie() { 1172 return useBrowserCookie; 1173 } 1174 1175 1182 DataRecord getDataRecord(AbstractNode node) { 1183 return getDataRecord(node, true); 1184 } 1185 1186 1192 DataRecord getDataRecord(AbstractNode anode, boolean fromCache) { 1193 1194 if(debug) log("Entered getDataRecord()"); 1196 if(anode instanceof TransactionNode) { 1197 1198 if(debug) log("TransactionNode"); 1200 MonitorData md = getMonitorData((TransactionNode)anode, 1204 fromCache, true); 1205 if (md == null) { 1206 String msg = NbBundle.getMessage(Controller.class, "MSG_NoMonitorData"); 1207 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, msg); 1208 return null; 1209 } 1210 return (DataRecord)md; 1211 } 1212 else if(anode instanceof NestedNode) { 1213 1214 NestedNode node = (NestedNode)anode; 1215 1216 if(debug) log(node.toString()); 1217 1218 int index[] = node.getIndex(); 1219 1220 AbstractNode parent = (AbstractNode)node.getParentNode(); 1221 if(parent == null) { 1222 if(debug) log("null parent, something went wrong!"); return null; 1224 } 1225 1226 while(parent != null && !(parent instanceof TransactionNode)) { 1227 if(debug) log("Parent is not transaction node"); if(debug) log(parent.toString()); 1229 parent = (AbstractNode)(parent.getParentNode()); 1230 } 1231 1232 if(debug) log("We got the transaction node"); 1234 MonitorData md = getMonitorData((TransactionNode)parent, 1237 true, true); 1238 if (md == null) { 1239 String msg = NbBundle.getMessage(Controller.class, "MSG_NoMonitorData"); 1240 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, msg); 1241 return null; 1242 } 1243 DataRecord dr = (DataRecord)md; 1244 int[] nodeindex = node.getIndex(); 1245 1246 int c = 0; 1247 while(c<nodeindex.length) { 1248 if(debug) log("Doing the data record cycle"); if(debug) log(String.valueOf(c) + ":" + String.valueOf(nodeindex[c])); 1251 Dispatches dis = dr.getDispatches(); 1252 dr = (DataRecord)dis.getDispatchData(nodeindex[c]); 1253 ++c; 1254 } 1255 return dr; 1256 } 1257 return null; 1258 } 1259 1260 1268 MonitorData getMonitorData(TransactionNode node, 1269 boolean fromCache, 1270 boolean cacheIt) { 1271 1272 String id = node.getID(); 1273 Hashtable ht = null; 1274 FileObject dir = null; 1275 1276 if(node.isCurrent()) { 1277 ht = currBeans; 1278 dir = currDir; 1279 if(debug) log("node is current"); } 1281 else { 1282 ht = saveBeans; 1283 dir = saveDir; 1284 } 1285 1286 if(debug) { 1287 log("node id is " + node.getID()); log("using directory " + dir.getName()); } 1290 1291 if(fromCache && ht.containsKey(id)) 1292 return (MonitorData)(ht.get(id)); 1293 1294 MonitorData md = retrieveMonitorData(id, dir); 1295 if(cacheIt && md != null) ht.put(id, md); 1296 return md; 1297 } 1298 1299 1304 MonitorData retrieveMonitorData(String id, String dirS) { 1305 1306 if(debug) 1307 log("retrieveMonitorData(String, String)"); if(!haveDirectories()) { 1309 log("Couldn't get the directory"); return null; 1312 } 1313 1314 FileObject dir = null; 1315 1316 if (dirS.equalsIgnoreCase(currDirStr)) dir = currDir; 1317 else if (dirS.equalsIgnoreCase(saveDirStr)) dir = saveDir; 1318 else if (dirS.equalsIgnoreCase(replayDirStr)) dir = replayDir; 1319 1320 if(debug) log("Directory = " + dir.getName()); return retrieveMonitorData(id, dir); 1322 } 1323 1324 1329 MonitorData retrieveMonitorData(String id, FileObject dir) { 1330 1331 if(debug) 1335 log("retrieveMonitorData(String, FileObject)"); if(!haveDirectories()) { 1337 log("Couldn't get the directory"); return null; 1340 } 1341 1342 MonitorData md = null; 1343 FileObject fo = null; 1344 FileLock lock = null; 1345 InputStreamReader in = null; 1346 1347 try { 1348 fo = dir.getFileObject(id, "xml"); if(debug) log("From file: " + FileUtil.toFile(fo).getAbsolutePath()); 1351 if(debug) log("Locking it..."); lock = fo.lock(); 1353 if(debug) log("Getting InputStreamReader"); in = new InputStreamReader (fo.getInputStream()); 1355 if(debug) log("Creating monitor data"); md = MonitorData.createGraph(in); 1357 try { 1358 if(dir == replayDir) fo.delete(lock); 1359 } 1360 catch(IOException ioex2) {} 1361 } 1362 catch(FileAlreadyLockedException falex) { 1363 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, falex); 1364 if(debug) { 1365 log("File is locked: " + fo.getNameExt()); falex.printStackTrace(); 1367 } 1368 } 1369 catch(IOException ioex) { 1370 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioex); 1371 if(debug) { 1372 log("Couldn't read data file: " + fo.getNameExt()); ioex.printStackTrace(); 1374 } 1375 } 1376 catch(Exception ex) { 1377 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 1378 if(debug) { 1379 log("Something went wrong when retrieving record"); ex.printStackTrace(); 1381 } 1382 } 1383 finally { 1384 try { in.close(); } 1385 catch(Throwable t) {} 1386 if(lock != null) lock.releaseLock(); 1387 } 1388 if(debug) log("We're done!"); return md; 1390 } 1391 1392 private void showReplay(final URL url) throws UnknownHostException , 1393 IOException { 1394 1395 if(debug) 1396 log("showReplay(URL url) url is " + url.toString()); ServerCheck sc = new ServerCheck(url.getHost()); 1400 if(debug) log("host is " + url.getHost()); Thread t = new Thread (sc); 1402 t.start(); 1403 try { 1404 t.join(2000); 1405 } 1406 catch(InterruptedException ie) { 1407 } 1408 t = null; 1409 if(!sc.isServerGood()) { 1410 if(debug) 1411 log("showReplay(): No host"); throw new UnknownHostException (); 1413 } 1414 1415 if(debug) log("performed server check"); 1417 try { 1419 Socket server = new Socket (url.getHost(), url.getPort()); 1420 server.close(); 1421 server = null; 1422 } 1423 catch(UnknownHostException uhe) { 1424 if(debug) log("showReplay(): uhe2"); throw uhe; 1426 } 1427 catch(IOException ioe) { 1428 if(debug) 1429 log("showReplay(): No service"); throw ioe; 1431 } 1432 1433 if(debug) log("showReplay(): reaching the end..."); 1435 SwingUtilities.invokeLater(new Runnable () { 1437 public void run () { 1438 HtmlBrowser.URLDisplayer.getDefault().showURL(url); 1439 }}); 1440 } 1441 1442 private static void log(final String s) { 1444 System.out.println("Controller::" + s); } 1446 1447 1448 private static URL getSampleHTTPServerURL() { 1449 FileSystem fs = Repository.getDefault().getDefaultFileSystem(); 1450 FileObject fo = fs.findResource("HTTPServer_DUMMY"); 1451 if (fo == null) { 1452 return null; 1453 } 1454 URL u = URLMapper.findURL(fo, URLMapper.NETWORK); 1455 return u; 1456 } 1457 1458 boolean checkServer(boolean replay) { 1459 1460 try { 1461 URL u = getSampleHTTPServerURL(); 1462 if(debug) log("Getting HTTP server - url " + u); 1463 if (u.getProtocol().equals("http")) { 1464 if(debug) log("Got the HTTP server"); 1466 return true; 1467 } 1468 } 1469 catch(Throwable t) { 1470 1471 if(debug) { 1472 log("Exception: " + t.getMessage()); 1473 t.printStackTrace(); 1474 } 1475 1476 Object [] options = { 1477 NbBundle.getBundle(Controller.class).getString("MON_OK"), 1478 }; 1479 String msg = null; 1480 if(replay) 1481 msg = NbBundle.getBundle(Controller.class).getString("MON_CantReplay"); 1482 else 1483 msg = NbBundle.getBundle(Controller.class).getString("MON_NoServer"); 1484 1485 1486 NotifyDescriptor noServerDialog = 1487 new NotifyDescriptor(msg, 1488 NbBundle.getBundle(Controller.class).getString("MON_NoServerTitle"), 1489 NotifyDescriptor.DEFAULT_OPTION, 1490 NotifyDescriptor.INFORMATION_MESSAGE, 1491 options, 1492 options[0]); 1493 DialogDisplayer.getDefault().notify(noServerDialog); 1494 } 1495 return false; 1496 } 1497 1498 1501 class ServerCheck implements Runnable { 1502 1503 boolean serverGood = false; 1504 String serverName = null; 1505 1506 ServerCheck(String name) { 1507 serverName = name; 1508 } 1509 1510 public void run() { 1511 try { 1512 InetAddress.getByName(serverName); 1513 serverGood = true; 1514 1515 } 1516 catch (UnknownHostException e) { 1517 serverGood = false; 1518 } 1519 } 1520 1521 boolean isServerGood() { 1522 return serverGood; 1523 } 1524 1525 } 1526 1527 1530 class CompTime implements Comparator { 1531 1532 boolean descend = true; 1533 1534 CompTime(boolean descend) { 1535 this.descend = descend; 1536 } 1537 1538 public int compare(Object o1, Object o2) { 1539 1540 if(debug) { 1541 log("In compareTime"); log("Comparing " + String.valueOf(o1) + " and " + String.valueOf(o2)); log("Cast the nodes"); } 1546 1547 TransactionNode n1 = (TransactionNode)o1; 1548 TransactionNode n2 = (TransactionNode)o2; 1549 1550 if(debug) { 1551 try { 1552 log(n1.getID()); 1553 log(n2.getID()); 1554 } 1555 catch(Exception ex) {}; 1556 } 1557 1558 int result; 1559 if(descend) 1560 result = n1.getID().compareTo(n2.getID()); 1561 else result = n2.getID().compareTo(n1.getID()); 1562 if(debug) log("End of compareTime"); return result; 1564 } 1565 } 1566 1567 void updateNodeNames() { 1571 1572 TransactionNode tn; 1573 1574 Node[] nodes = currTrans.getNodes(); 1575 int size = nodes.length; 1576 for(int i=0; i<size; ++i) { 1577 tn = (TransactionNode)nodes[i]; 1578 tn.setNameString(); 1579 } 1580 1581 nodes = savedTrans.getNodes(); 1582 size = nodes.length; 1583 for(int i=0; i<size; ++i) { 1584 tn = (TransactionNode)nodes[i]; 1585 tn.setNameString(); 1586 } 1587 } 1588 1589 1592 class CompAlpha implements Comparator { 1593 1594 public int compare(Object o1, Object o2) { 1595 if(debug) log("In compareAlpha"); TransactionNode n1 = (TransactionNode)o1; 1597 TransactionNode n2 = (TransactionNode)o2; 1598 if(debug) log("cast the nodes"); if(debug) { 1600 log("Comparing " + String.valueOf(o1) + " and " + String.valueOf(o2)); try { 1603 log("names"); log(n1.getName()); 1605 log(n2.getName()); 1606 log("IDs"); log(n1.getID()); 1608 log(n2.getID()); 1609 } 1610 catch(Exception ex) {}; 1611 } 1612 int diff = n1.getName().compareTo(n2.getName()); 1613 if(diff == 0) 1614 return n1.getID().compareTo(n2.getID()); 1615 else 1616 return diff; 1617 } 1618 } 1619} | Popular Tags |