KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > monitor > client > Controller


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /**
21  * @author Ana von Klopp
22  */

23
24 package org.netbeans.modules.web.monitor.client;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Comparator JavaDoc;
28 import java.util.Date JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.Hashtable JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Vector JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.io.File JavaDoc;
35 import java.io.FileNotFoundException JavaDoc;
36 import java.io.InputStreamReader JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.io.OutputStream JavaDoc;
39 import java.io.PrintWriter JavaDoc;
40 import java.net.InetAddress JavaDoc;
41 import java.net.MalformedURLException JavaDoc;
42 import java.net.Socket JavaDoc;
43 import java.net.UnknownHostException JavaDoc;
44 import java.net.URL JavaDoc;
45
46 import javax.swing.SwingUtilities JavaDoc;
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     // REPLAY strings - must be coordinated with server.MonitorFilter
70
final static String JavaDoc REPLAY="netbeans.replay"; //NOI18N
71
final static String JavaDoc PORT="netbeans.replay.port"; //NOI18N
72
final static String JavaDoc REPLAYSTATUS="netbeans.replay.status"; //NOI18N
73
final static String JavaDoc REPLAYSESSION="netbeans.replay.session"; //NOI18N
74
static final boolean debug = false;
75     //private transient static boolean starting = true;
76

77     // Test server location and port
78
// Should use InetAddress.getLocalhost() instead
79
private transient static String JavaDoc server = "localhost"; //NOI18N
80
private transient static int port = 8080;
81
82     // Location of the files
83
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 JavaDoc monDirStr = "HTTPMonitor"; // NOI18N
89
final static String JavaDoc currDirStr = "current"; // NOI18N
90
final static String JavaDoc saveDirStr = "save"; // NOI18N
91
final static String JavaDoc replayDirStr = "replay"; // NOI18N
92

93     // Constant nodes etc we need to know about
94
private transient NavigateNode root = null;
95     private Children.SortedArray currTrans = null;
96     private Children.SortedArray savedTrans = null;
97
98     // These are the ones that should go.
99
private Hashtable JavaDoc currBeans = null;
100     private Hashtable JavaDoc saveBeans = null;
101     
102     private transient Comparator JavaDoc comp = null;
103
104     private boolean useBrowserCookie = true;
105
106     private static Controller instance = null;
107     private Date JavaDoc startDate;
108     
109     private Controller() {
110         // TODO: setting the startup date to 30s earlier from now to prevent deletion of the
111
// first request (#56880), the ideal fix should be to use the IDE startup time
112
startDate = new Date JavaDoc(System.currentTimeMillis() - 30000);
113     currBeans = new Hashtable JavaDoc();
114     saveBeans = new Hashtable JavaDoc();
115     createNodeStructure();
116         /*
117     registerBrowserListener();
118         */

119     }
120
121     static Controller getInstance() {
122     if(instance == null)
123         instance = new Controller();
124     return instance;
125     }
126  
127     /**
128      * Invoked at startup, creates the root folder and the folder for
129      * current and saved transactions (and their children arrays).
130      */

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     /**
154      * Adds a transaction to the list of current transactions.
155      */

156     void addTransaction(String JavaDoc 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 JavaDoc ex) {
166         // If there is some kind of parsing exception, do nothing
167
}
168     }
169
170     /**
171      * Adds a transaction to the list of current transactions.
172      */

173     protected NavigateNode getRoot() {
174     return root;
175     }
176
177
178     protected static FileObject getMonDir() throws FileNotFoundException JavaDoc {
179     
180     if(monDir == null || !monDir.isFolder()) {
181         try {
182         createDirectories();
183         }
184         catch(FileNotFoundException JavaDoc ex) {
185         throw ex;
186         }
187     }
188     return monDir;
189     }
190     
191
192     protected static FileObject getCurrDir() throws FileNotFoundException JavaDoc {
193      
194     if(currDir == null || !currDir.isFolder()) {
195         try{
196         createDirectories();
197         }
198         catch(FileNotFoundException JavaDoc ex) {
199         throw ex;
200         }
201     }
202     return currDir;
203     }
204
205     protected static FileObject getReplayDir() throws FileNotFoundException JavaDoc {
206      
207     if(replayDir == null || !replayDir.isFolder()) {
208         try{
209         createDirectories();
210         }
211         catch(FileNotFoundException JavaDoc ex) {
212         throw ex;
213         }
214     }
215     return replayDir;
216     }
217     
218
219     protected static FileObject getSaveDir() throws FileNotFoundException JavaDoc {
220      
221     if(saveDir == null || !saveDir.isFolder()) {
222         try{
223         createDirectories();
224         }
225         catch(FileNotFoundException JavaDoc 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 JavaDoc ex) {
238         return false;
239         }
240     }
241     
242     if(saveDir == null) {
243         try {
244         saveDir = getSaveDir();
245         }
246         catch(Exception JavaDoc ex) {
247         return false;
248         }
249     }
250     return true;
251     }
252     
253
254     private static void createDirectories() throws FileNotFoundException JavaDoc {
255
256     if(debug) log("Now in createDirectories()"); // NOI18N
257

258     FileObject rootdir =
259         Repository.getDefault().getDefaultFileSystem().getRoot();
260     if(debug) {
261         log("Root directory is " + rootdir.getName()); // NOI18N
262
File JavaDoc rootF = FileUtil.toFile(rootdir);
263         log("Root directory abs path " + // NOI18N
264
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 JavaDoc 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 JavaDoc();
284             }
285             catch(IOException JavaDoc ex) {
286             throw new FileNotFoundException JavaDoc();
287             }
288             finally {
289             if(lock != null) lock.releaseLock();
290             }
291         }
292         try {
293             monDir = rootdir.createFolder(monDirStr);
294         }
295         catch(IOException JavaDoc ioex) {
296             if(debug) ioex.printStackTrace();
297         }
298         }
299         if(monDir == null || !monDir.isFolder())
300         throw new FileNotFoundException JavaDoc();
301     }
302
303     if(debug)
304         log("monitor directory is " + monDir.getName());// NOI18N
305

306     // Current directory
307

308     if(currDir == null || !currDir.isFolder()) {
309
310         try {
311         currDir = monDir.getFileObject(currDirStr);
312         }
313         catch(Exception JavaDoc 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 JavaDoc();
324             }
325             catch(IOException JavaDoc ex) {
326             throw new FileNotFoundException JavaDoc();
327             }
328             finally {
329             if(lock != null) lock.releaseLock();
330             }
331         }
332         try {
333             currDir = monDir.createFolder(currDirStr);
334         }
335         catch(IOException JavaDoc ex) {
336             if(debug) ex.printStackTrace();
337         }
338         }
339         if(currDir == null || !currDir.isFolder())
340         throw new FileNotFoundException JavaDoc();
341     }
342     
343     if(debug) log("curr directory is " + currDir.getName()); // NOI18N
344

345     // Save Directory
346
if(saveDir == null || !saveDir.isFolder()) {
347         try {
348         saveDir = monDir.getFileObject(saveDirStr);
349         }
350         catch(Exception JavaDoc 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 JavaDoc();
361             }
362             catch(IOException JavaDoc ex) {
363             throw new FileNotFoundException JavaDoc();
364             }
365             finally {
366             if(lock != null) lock.releaseLock();
367             }
368         }
369         try {
370             saveDir = monDir.createFolder(saveDirStr);
371         }
372         catch(IOException JavaDoc ex) {
373             if(debug) ex.printStackTrace();
374         }
375         }
376         if(saveDir == null || !saveDir.isFolder())
377         throw new FileNotFoundException JavaDoc();
378
379         if(debug)
380         log("save directory is " + saveDir.getName()); // NOI18N
381
}
382
383     // Replay Directory
384

385     if(replayDir == null || !replayDir.isFolder()) {
386
387         try {
388         replayDir = monDir.getFileObject(replayDirStr);
389         }
390         catch(Exception JavaDoc 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 JavaDoc();
401             }
402             catch(IOException JavaDoc ex) {
403             throw new FileNotFoundException JavaDoc();
404             }
405             finally {
406             if(lock != null) lock.releaseLock();
407             }
408         }
409         try {
410             replayDir = monDir.createFolder(replayDirStr);
411         }
412         catch(Exception JavaDoc ex) {
413             if(debug) ex.printStackTrace();
414         }
415         }
416         if(replayDir == null || !replayDir.isFolder())
417         throw new FileNotFoundException JavaDoc();
418
419         if(debug)
420         log("replay directory is " + replayDir.getName());// NOI18N
421
}
422     }
423
424
425     /**
426      * Invoked by ReplayAction. Replays the transaction corresponding to
427      * the selected node.
428      *
429      * PENDING - it would be better if the nodes know which server
430      * they were processed on. This would be the case if we listed the
431      * nodes separately depending on the server that collected the
432      * data.
433      *
434      */

435     void replayTransaction(Node node) {
436
437     if(debug)
438         log("replayTransaction(Node node) from node " +
439         node.getName()); // NOI18N
440

441     if(!checkServer(true)) return;
442
443     TransactionNode tn = null;
444     try {
445         tn = (TransactionNode)node;
446     }
447     catch(ClassCastException JavaDoc cce) {
448         if(debug)
449         log("Selected node was not a transaction node");//NOI18N
450
return;
451     }
452     
453     MonitorData md = getMonitorData(tn, false, false);
454         if (md == null) {
455             String JavaDoc 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 " + // NOI18N
464
String.valueOf(md.getRequestData().getReplaceSessionCookie()));
465         String JavaDoc fname = md.createTempFile("control-replay.xml"); // NOI18N
466
log("Wrote replay data to " + fname);// NOI18N
467
}
468     
469     String JavaDoc status;
470     if(tn.isCurrent()) status = currDirStr;
471     else status = saveDirStr;
472     try {
473         replayTransaction(md, status);
474     }
475     catch(UnknownHostException JavaDoc uhe) {
476         // Notify the user that there is no host
477

478         Object JavaDoc[] 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 JavaDoc ioe) {
493
494         if(debug) {
495         log(ioe.getMessage());
496         ioe.printStackTrace();
497         }
498         
499         // Notify the user that the server is not running
500
Object JavaDoc[] 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     /**
516      * Invoked by EditPanel. Replays the transaction corresponding to
517      * the selected node.
518      */

519     void replayTransaction(MonitorData md)
520     throws UnknownHostException JavaDoc, IOException JavaDoc {
521
522     // PENDING - can't make UI changes right now for Sierra
523
// any exception thrown in this method indicates that we
524
// couldn't even get to the monitor data, and we should add an
525
// additional panel to that effect. Also, unreadable monitor
526
// data should cause the transaction to be removed from the
527
// pane.
528

529     if(debug) log("replayTransaction(MonitorData md)"); //NOI18N
530

531     FileObject fo;
532     FileLock lock = null;
533     OutputStream JavaDoc out = null;
534     PrintWriter JavaDoc pw = null;
535
536     if(debug) log("Creating record for replay"); //NOI18N
537

538     String JavaDoc id = md.getAttributeValue("id"); // NOI18N
539

540     try {
541         // This will fail if the file already exists. This can
542
// happen if the replay previously failed because the
543
// server was not running. This is dealt with in the
544
// catch clause below.
545
fo = getReplayDir().createData(id, "xml"); //NOI18N
546
if(debug) log(" Created file for replay data");
547     }
548     catch(IOException JavaDoc ioex) {
549
550         try {
551         fo = getReplayDir().getFileObject(id, "xml");
552         }
553         catch(IllegalArgumentException JavaDoc iaex) {
554         // This is only thrown if getReplayDir() is not a
555
// folder. This should not happen.
556
throw new IOException JavaDoc("No replay dir");
557         }
558
559         if(!fo.isData()) {
560         throw new IOException JavaDoc("Can't create file, giving up");
561         }
562
563         try {
564          lock = fo.lock();
565         }
566         catch(FileAlreadyLockedException falex) {
567         throw new IOException JavaDoc("Old file exist, islocked");
568         }
569
570         try {
571         fo.delete(lock);
572         }
573         catch(IOException JavaDoc ioex2) {
574         throw new IOException JavaDoc("Couldn't delete old file");
575         }
576         finally {
577         if(lock != null) lock.releaseLock();
578         }
579     
580         try {
581         fo = getReplayDir().createData(id, "xml"); //NOI18N
582
}
583         catch(IOException JavaDoc 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 JavaDoc();
595     }
596
597     try {
598         out = fo.getOutputStream(lock);
599         pw = new PrintWriter JavaDoc(out);
600         md.write(pw);
601         if(debug) log("...record complete"); //NOI18N
602

603         if(debug) {
604         String JavaDoc fname =
605             md.createTempFile("control-record.xml"); // NOI18N
606
log("Wrote replay data to " + fname); // NOI18N
607
}
608     }
609     catch(IOException JavaDoc ioex) {
610         throw ioex;
611     }
612     finally {
613         if(lock != null) lock.releaseLock();
614         try {
615         pw.close();
616         }
617         catch(Throwable JavaDoc t) {
618         }
619         try {
620         out.close();
621         }
622         catch(Throwable JavaDoc t) {
623         }
624     }
625     
626     try {
627         replayTransaction(md, replayDirStr);
628     }
629     catch(UnknownHostException JavaDoc uhe) {
630         throw uhe;
631     }
632     catch(IOException JavaDoc ioe) {
633         throw ioe;
634     }
635     }
636     
637     /**
638      *
639      */

640     void replayTransaction(MonitorData md, String JavaDoc status)
641     throws UnknownHostException JavaDoc, IOException JavaDoc {
642     
643     if(debug) log("replayTransaction(MonitorData md, String status )"); //NOI18N
644

645     URL JavaDoc url = null;
646     try {
647         String JavaDoc name = md.getServerName();
648         int port = md.getServerPort();
649         
650         StringBuffer JavaDoc uriBuf = new StringBuffer JavaDoc(128);
651         uriBuf.append(md.getRequestData().getAttributeValue("uri")); //NOI18N
652
uriBuf.append("?"); //NOI18N
653
uriBuf.append(REPLAY);
654         uriBuf.append("="); //NOI18N
655
uriBuf.append(md.getAttributeValue("id")); //NOI18N
656
uriBuf.append("&"); //NOI18N
657
uriBuf.append(REPLAYSTATUS);
658         uriBuf.append("="); //NOI18N
659
uriBuf.append(status);
660
661         String JavaDoc portS = null;
662         try {
663         FileObject fo = Repository.getDefault().getDefaultFileSystem().getRoot();
664         URL JavaDoc u = getSampleHTTPServerURL();
665         portS =
666             String.valueOf(u.getPort()/*HttpServer.getRepositoryRoot().getPort()*/);
667         }
668         catch(Exception JavaDoc ex) {
669         // No internal HTTP server, do nothing
670
}
671         if(portS != null) {
672         uriBuf.append("&"); //NOI18N
673
uriBuf.append(PORT);
674         uriBuf.append("="); //NOI18N
675
uriBuf.append(portS);
676         }
677
678
679         if(md.getRequestData().getReplaceSessionCookie()) {
680         uriBuf.append("&"); //NOI18N
681
uriBuf.append(REPLAYSESSION);
682         uriBuf.append("="); //NOI18N
683
uriBuf.append(md.getRequestData().getSessionID());
684         }
685         url = new URL JavaDoc("http", name, port, uriBuf.toString()); //NOI18N
686
}
687     catch(MalformedURLException JavaDoc me) {
688         if(debug) log(me.getMessage());
689     }
690     catch(NumberFormatException JavaDoc ne) {
691         if(debug) log(ne.getMessage());
692     }
693
694     // Send the url to the browser.
695
try {
696         showReplay(url);
697     }
698     catch(UnknownHostException JavaDoc uhe) {
699         throw uhe;
700     }
701     catch(IOException JavaDoc ioe) {
702         throw ioe;
703     }
704     }
705
706     void saveTransaction(Node[] nodes) {
707
708     if(!haveDirectories()) {
709         // PENDING - report the error properly
710
// This should not happen
711
log("Couldn't get the directory"); //NOI18N
712
return;
713     }
714
715     Node[] newNodes = new Node[nodes.length];
716     TransactionNode mvNode;
717     String JavaDoc 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); //NOI18N
727

728         if(currBeans.containsKey(id))
729         saveBeans.put(id, currBeans.remove(id));
730         
731         // Note I didn't load the bean here yet. Will only do that
732
// if the data is displayed.
733

734         FileLock lock = null;
735         try {
736         FileObject fold =
737             currDir.getFileObject(id, "xml"); //NOI18N
738
lock = fold.lock();
739         fold.copy(saveDir, id, "xml"); //NOI18N
740
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         // PENDING report properly
748
}
749         catch(IOException JavaDoc ioex) {
750         error = true;
751         // PENDING report properly
752
}
753         catch(Exception JavaDoc ex) {
754         error = true;
755         // PENDING report properly
756
}
757         finally {
758         if(lock != null) lock.releaseLock();
759         }
760         
761     }
762     if(!error) currTrans.remove(nodes);
763     savedTrans.add(newNodes);
764     }
765   
766     /**
767      * Invoked by DeleteAction. Deletes a saved transaction
768      */

769
770     void deleteTransaction(final Node[] nodes) {
771
772     if(!haveDirectories()) {
773         // PENDING - report the error property
774
// This should not happen
775
log("Couldn't get the directory"); //NOI18N
776
return;
777     }
778
779     // PENDING
780
if((nodes == null) || (nodes.length == 0)) return;
781         
782         final ProgressMonitor progressMonitor = new ProgressMonitor();
783         
784         RequestProcessor.getDefault().post(new Runnable JavaDoc () {
785             public void run() {
786                 // give awt thread chance to draw the progress monitor
787
Thread.yield();
788                 // remove nodes
789
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 JavaDoc id = node.getID();
798                         fileObject = currDir.getFileObject(id, "xml");
799                         currBeans.remove(id);
800                     } else {
801                         String JavaDoc id = node.getID();
802                         fileObject = saveDir.getFileObject(id, "xml");
803                         saveBeans.remove(id);
804                     }
805                     if (fileObject != null) {
806                         // delete the file
807
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 JavaDoc IOex) {
814                             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, IOex);
815                         } finally {
816                             if(lock != null) {
817                                 lock.releaseLock();
818                             }
819                         }
820                     }
821                     // update the progress monitor if needed
822
final int newValue = 100*(i+1)/nodes.length;
823                     if (newValue > oldValue) {
824                         oldValue = newValue;
825                         SwingUtilities.invokeLater(new Runnable JavaDoc () {
826                             public void run (){
827                                 progressMonitor.setValue(newValue);
828                             }
829                         });
830                     }
831                 }
832                 SwingUtilities.invokeLater(new Runnable JavaDoc () {
833                     public void run (){
834                         progressMonitor.close();
835                     }
836                 });
837             }
838         });
839         progressMonitor.setVisible(true);
840     }
841
842     void deleteDirectory(String JavaDoc dir) {
843
844     if(!haveDirectories()) {
845         // PENDING - report the error property
846
// This should not happen
847
log("Couldn't get the directory"); //NOI18N
848
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 JavaDoc () {
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 JavaDoc 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 JavaDoc IOex) {
885                         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, IOex);
886                     } finally {
887                         if(lock != null) {
888                             lock.releaseLock();
889                         }
890                     }
891                     // update the progress monitor if needed
892
final int newValue = 100 * i/number;
893                     if (newValue > oldValue) {
894                         oldValue = newValue;
895                         SwingUtilities.invokeLater(new Runnable JavaDoc() {
896                             public void run() {
897                                 progressMonitor.setValue(newValue);
898                             }
899                         });
900                     }
901                 }
902                 SwingUtilities.invokeLater(new Runnable JavaDoc() {
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"); //NOI18N
923

924     if(!haveDirectories()) {
925         // PENDING - report the error property
926
// This should not happen
927
log("Couldn't get the directory"); //NOI18N
928
return;
929     }
930
931     Enumeration JavaDoc e = null;
932     Vector JavaDoc nodes = new Vector JavaDoc();
933     int numtns = 0;
934     TransactionNode[] tns = null;
935     FileObject fo = null;
936     String JavaDoc id = null;
937     MonitorData md = null;
938     
939     currTrans.remove(currTrans.getNodes());
940     if(debug) log("getTransactions removed old nodes"); //NOI18N
941

942     e = currDir.getData(false);
943         final List JavaDoc fileObjectsToDelete = new ArrayList JavaDoc();
944     while(e.hasMoreElements()) {
945
946         fo = (FileObject)e.nextElement();
947             // #43213 - avoiding ModuleInstall class, delaying deletion of old records until now
948
if (fo.lastModified().after(startDate)) {
949                 id = fo.getName();
950                 if(debug)
951                     log("getting current transaction: " + id); //NOI18N
952

953                 // Retrieve the monitordata
954
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 JavaDoc () {
963             public void run() {
964                 for (Iterator JavaDoc it = fileObjectsToDelete.iterator(); it.hasNext(); ) {
965                     try {
966                         ((FileObject) it.next()).delete();
967                     } catch (IOException JavaDoc 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 JavaDoc();
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); //NOI18N
990
// Retrieve the monitordata
991
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()); //NOI18N
1003

1004    }
1005    savedTrans.add(tns);
1006    }
1007    
1008    private int parseStatusCode(String JavaDoc statusCode) {
1009        if (statusCode == null) {
1010            return -1;
1011        }
1012        // The statusCode is expected to look like e.g. "404: Not Found", if not
1013
// the status code was not resolved, which mostly means there was no error => 200
1014
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 JavaDoc nfe) {
1022            // ignore
1023
}
1024        return statusCodeNum;
1025    }
1026        
1027    private TransactionNode createTransactionNode(MonitorData md, boolean current) {
1028
1029    if(debug) log("createTransactionNode(MonitorData)"); //NOI18N
1030
Dispatches dis = null;
1031    try {
1032        dis = md.getDispatches();
1033    }
1034    catch(Exception JavaDoc ex) {
1035        // Any parsing exception at this point, just ignore this
1036
// part of the request
1037
}
1038
1039    TransactionNode node = null;
1040    
1041    // No dispatched requests, we add a regular transaction node
1042
if(dis == null || dis.sizeDispatchData() == 0 ) {
1043        
1044        if(debug) log("No dispatched requests"); //NOI18N
1045
node = new TransactionNode(md.getAttributeValue("id"), // NOI18N
1046
md.getAttributeValue("method"), // NOI18N
1047
md.getAttributeValue("resource"), //NOI18N
1048
current,
1049                                       parseStatusCode(md.getRequestData().getAttributeValue("status"))); // NOI18N
1050
}
1051    else {
1052
1053        int numChildren = dis.sizeDispatchData();
1054        if(debug) log("We had some dispatched requests: " + //NOI18N
1055
String.valueOf(numChildren));
1056        if(debug) log("\t for id " + //NOI18N
1057
md.getAttributeValue("resource")); //NOI18N
1058
// Create all the children. 1
1059
Children.Array nested = new Children.Array();
1060        
1061        // First we create an array of children that has the same
1062
// size as the set of nodes.
1063
NestedNode[] nds = new NestedNode[numChildren];
1064        for(int i=0; i<numChildren; ++i) {
1065        if(debug) {
1066            log("Getting a new monitor data object"); //NOI18N
1067
log(dis.getDispatchData(i).getAttributeValue("resource")); //NOI18N
1068
}
1069        nds[i] = createNestedNode(dis.getDispatchData(i),
1070                      md.getAttributeValue("method"), // NOI18N
1071
null, i);
1072        }
1073        
1074        nested.add(nds);
1075        node = new TransactionNode(md.getAttributeValue("id"), // NOI18N
1076
md.getAttributeValue("method"), // NOI18N
1077
md.getAttributeValue("resource"), //NOI18N
1078
nested,
1079                                       current,
1080                                       parseStatusCode(md.getRequestData().getAttributeValue("status"))); // NOI18N
1081

1082    }
1083    return node;
1084    }
1085    
1086
1087    private NestedNode createNestedNode(DispatchData dd,
1088                    String JavaDoc 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    // No dispatched requests, we add a regular transaction node
1112
if(dis == null || dis.sizeDispatchData() == 0 ) {
1113        node = new NestedNode(dd.getAttributeValue("resource"),// NOI18N
1114
method,
1115                            newloc,
1116                            parseStatusCode(dd.getRequestData().getAttributeValue("status"))); // NOI18N
1117
}
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"), // NOI18N
1129
method,
1130                              nested,
1131                              newloc,
1132                              parseStatusCode(dd.getRequestData().getAttributeValue("status"))); // NOI18N
1133
}
1134    return node;
1135    }
1136
1137
1138    /**
1139     * Sets the machine name and port of the web server. Not used in
1140     * this version, we do not support remote debugging.
1141     */

1142    static void setServer(String JavaDoc loc, int p) {
1143    port = p;
1144    server = loc;
1145    return;
1146    }
1147
1148    void setComparator(Comparator JavaDoc comp) {
1149    currTrans.setComparator(comp);
1150    savedTrans.setComparator(comp);
1151    }
1152
1153    /** This method toggles whether the request uses the browser's
1154     * cookie or a cookie specified by the user. In 3.6, it is not
1155     * possible to configure the monitor to use user-specified
1156     * cookies, but I leave the method, in case it becomes possible in
1157     * the future. Basically, we can no longer set the cookie on the
1158     * server side (the Servlet APIs does not provide any method for
1159     * doing this) but we could technically tell the browser that
1160     * issues the replay request to send another cookie (the APIs for
1161     * that are not there now). If so, the feature can be
1162     * reintroduced.
1163     */

1164    void setUseBrowserCookie(boolean value) {
1165    useBrowserCookie = value;
1166    if(debug)
1167        log("Setting useBrowserCookie to " + //NOI18N
1168
String.valueOf(useBrowserCookie));
1169    }
1170
1171    boolean getUseBrowserCookie() {
1172    return useBrowserCookie;
1173    }
1174    
1175    /**
1176     * @param node A node on the Monitor GUI
1177     * @return a data record
1178     * Convenience method - this gets the DataRecord corresponding to
1179     * a node on the TransactionView panel from the cache if it is
1180     * present. This is used to display the data from the node.
1181     */

1182    DataRecord getDataRecord(AbstractNode node) {
1183    return getDataRecord(node, true);
1184    }
1185        
1186    /**
1187     * @param node A node on the Monitor GUI
1188     * @param fromCache true if it is OK to get the data record from
1189     * the cache
1190     * @return a data record
1191     */

1192    DataRecord getDataRecord(AbstractNode anode, boolean fromCache) {
1193
1194    if(debug) log("Entered getDataRecord()"); //NOI18N
1195

1196    if(anode instanceof TransactionNode) {
1197
1198        if(debug) log("TransactionNode"); //NOI18N
1199

1200        // Since this method is used to retrieve data records for
1201
// the purposes of displaying the transaction, we cache
1202
// the result
1203
MonitorData md = getMonitorData((TransactionNode)anode,
1204                        fromCache, true);
1205            if (md == null) {
1206                String JavaDoc 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!"); //NOI18N
1223
return null;
1224        }
1225        
1226        while(parent != null && !(parent instanceof TransactionNode)) {
1227        if(debug) log("Parent is not transaction node"); //NOI18N
1228
if(debug) log(parent.toString());
1229        parent = (AbstractNode)(parent.getParentNode());
1230        }
1231        
1232        if(debug) log("We got the transaction node"); //NOI18N
1233

1234        // We get the data record, from cache if it is present,
1235
// and cache the node also
1236
MonitorData md = getMonitorData((TransactionNode)parent,
1237                        true, true);
1238            if (md == null) {
1239                String JavaDoc 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"); //NOI18N
1249
if(debug) log(String.valueOf(c) + ":" + //NOI18N
1250
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    /**
1261     * @param node A node on the Monitor GUI
1262     * @param fromCache true if it is OK to get the data record from
1263     * the cache
1264     * @param cacheIt true if it is OK to cache the data that we
1265     * retrieve
1266     * @return a data record, <code>null</code> if monitor date could not be got
1267     */

1268    MonitorData getMonitorData(TransactionNode node,
1269                      boolean fromCache,
1270                      boolean cacheIt) {
1271
1272    String JavaDoc id = node.getID();
1273    Hashtable JavaDoc ht = null;
1274    FileObject dir = null;
1275     
1276    if(node.isCurrent()) {
1277        ht = currBeans;
1278        dir = currDir;
1279        if(debug) log("node is current"); //NOI18N
1280
}
1281    else {
1282        ht = saveBeans;
1283        dir = saveDir;
1284    }
1285    
1286    if(debug) {
1287        log("node id is " + node.getID()); //NOI18N
1288
log("using directory " + dir.getName()); //NOI18N
1289
}
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    /**
1300     * @param id The ID of the record
1301     * @param dirS The name of the directory in which the transaction
1302     * resides
1303     **/

1304    MonitorData retrieveMonitorData(String JavaDoc id, String JavaDoc dirS) {
1305
1306    if(debug)
1307        log("retrieveMonitorData(String, String)"); //NOI18N
1308
if(!haveDirectories()) {
1309        // PENDING - report the error property
1310
log("Couldn't get the directory"); //NOI18N
1311
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()); //NOI18N
1321
return retrieveMonitorData(id, dir);
1322    }
1323    
1324    /**
1325     * @param id The ID of the record.
1326     * @param dir The directory in which the transaction resides.
1327     * @return monitor date, <code>null</code> if monitor date could not be retrieved.
1328     */

1329    MonitorData retrieveMonitorData(String JavaDoc id, FileObject dir) {
1330
1331    // PENDING - this method needs an error reporting mechanism in
1332
// case the monitor data cannot be retrieved. Now it will just
1333
// return null.
1334
if(debug)
1335        log("retrieveMonitorData(String, FileObject)"); //NOI18N
1336
if(!haveDirectories()) {
1337        // PENDING - report the error property
1338
log("Couldn't get the directory"); //NOI18N
1339
return null;
1340    }
1341    
1342    MonitorData md = null;
1343    FileObject fo = null;
1344    FileLock lock = null;
1345    InputStreamReader JavaDoc in = null;
1346    
1347    try {
1348        fo = dir.getFileObject(id, "xml"); // NOI18N
1349
if(debug) log("From file: " + //NOI18N
1350
FileUtil.toFile(fo).getAbsolutePath());
1351        if(debug) log("Locking it..."); //NOI18N
1352
lock = fo.lock();
1353        if(debug) log("Getting InputStreamReader"); //NOI18N
1354
in = new InputStreamReader JavaDoc(fo.getInputStream());
1355        if(debug) log("Creating monitor data"); //NOI18N
1356
md = MonitorData.createGraph(in);
1357        try {
1358        if(dir == replayDir) fo.delete(lock);
1359        }
1360        catch(IOException JavaDoc ioex2) {}
1361    }
1362    catch(FileAlreadyLockedException falex) {
1363            ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, falex);
1364        if(debug) {
1365        log("File is locked: " + fo.getNameExt()); //NOI18N
1366
falex.printStackTrace();
1367        }
1368    }
1369    catch(IOException JavaDoc ioex) {
1370            ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioex);
1371        if(debug) {
1372        log("Couldn't read data file: " + fo.getNameExt()); //NOI18N
1373
ioex.printStackTrace();
1374        }
1375    }
1376    catch(Exception JavaDoc ex) {
1377            ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
1378        if(debug) {
1379        log("Something went wrong when retrieving record"); //NOI18N
1380
ex.printStackTrace();
1381        }
1382    }
1383    finally {
1384        try { in.close(); }
1385        catch(Throwable JavaDoc t) {}
1386        if(lock != null) lock.releaseLock();
1387    }
1388    if(debug) log("We're done!"); //NOI18N
1389
return md;
1390    }
1391
1392    private void showReplay(final URL JavaDoc url) throws UnknownHostException JavaDoc,
1393                                        IOException JavaDoc {
1394    
1395    if(debug)
1396        log("showReplay(URL url) url is " + url.toString()); // NOI18N
1397
// First we check that we can find a host of the name that's
1398
// specified
1399
ServerCheck sc = new ServerCheck(url.getHost());
1400    if(debug) log("host is " + url.getHost()); //NOI18N
1401
Thread JavaDoc t = new Thread JavaDoc(sc);
1402    t.start();
1403    try {
1404        t.join(2000);
1405    }
1406    catch(InterruptedException JavaDoc ie) {
1407    }
1408    t = null;
1409    if(!sc.isServerGood()) {
1410        if(debug)
1411        log("showReplay(): No host"); // NOI18N
1412
throw new UnknownHostException JavaDoc();
1413    }
1414    
1415    if(debug) log("performed server check"); // NOI18N
1416

1417    // Next we see if we can connect to the server
1418
try {
1419        Socket JavaDoc server = new Socket JavaDoc(url.getHost(), url.getPort());
1420        server.close();
1421        server = null;
1422    }
1423    catch(UnknownHostException JavaDoc uhe) {
1424        if(debug) log("showReplay(): uhe2"); // NOI18N
1425
throw uhe;
1426    }
1427    catch(IOException JavaDoc ioe) {
1428        if(debug)
1429        log("showReplay(): No service"); // NOI18N
1430
throw ioe;
1431    }
1432    
1433    if(debug) log("showReplay(): reaching the end..."); // NOI18N
1434

1435        // window system code must be run in AWT thread
1436
SwingUtilities.invokeLater(new Runnable JavaDoc() {
1437            public void run () {
1438                HtmlBrowser.URLDisplayer.getDefault().showURL(url);
1439        }});
1440    }
1441
1442    // PENDING - use the logger instead
1443
private static void log(final String JavaDoc s) {
1444    System.out.println("Controller::" + s); //NOI18N
1445
}
1446
1447
1448    private static URL JavaDoc getSampleHTTPServerURL() {
1449        FileSystem fs = Repository.getDefault().getDefaultFileSystem();
1450        FileObject fo = fs.findResource("HTTPServer_DUMMY");
1451        if (fo == null) {
1452            return null;
1453        }
1454        URL JavaDoc u = URLMapper.findURL(fo, URLMapper.NETWORK);
1455        return u;
1456    }
1457
1458    boolean checkServer(boolean replay) {
1459
1460    try {
1461        URL JavaDoc u = getSampleHTTPServerURL();
1462        if(debug) log("Getting HTTP server - url " + u);
1463        if (u.getProtocol().equals("http")) {
1464            //HttpServer.getRepositoryRoot();
1465
if(debug) log("Got the HTTP server");
1466            return true;
1467        }
1468    }
1469    catch(Throwable JavaDoc t) {
1470
1471        if(debug) {
1472        log("Exception: " + t.getMessage());
1473        t.printStackTrace();
1474        }
1475
1476        Object JavaDoc[] options = {
1477        NbBundle.getBundle(Controller.class).getString("MON_OK"),
1478        };
1479        String JavaDoc 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    /**
1499     * Does the server we try to replay on exist?
1500     */

1501    class ServerCheck implements Runnable JavaDoc {
1502
1503    boolean serverGood = false;
1504    String JavaDoc serverName = null;
1505    
1506    ServerCheck(String JavaDoc name) {
1507        serverName = name;
1508    }
1509    
1510    public void run() {
1511        try {
1512        InetAddress.getByName(serverName);
1513        serverGood = true;
1514        
1515        }
1516        catch (UnknownHostException JavaDoc e) {
1517        serverGood = false;
1518        }
1519    }
1520    
1521    boolean isServerGood() {
1522        return serverGood;
1523    }
1524    
1525    }
1526
1527    /**
1528     * Sort by time
1529     */

1530    class CompTime implements Comparator JavaDoc {
1531
1532    boolean descend = true;
1533
1534    CompTime(boolean descend) {
1535        this.descend = descend;
1536    }
1537
1538    public int compare(Object JavaDoc o1, Object JavaDoc o2) {
1539
1540        if(debug) {
1541        log("In compareTime"); //NOI18N
1542
log("Comparing " + String.valueOf(o1) + //NOI18N
1543
" and " + String.valueOf(o2)); //NOI18N
1544
log("Cast the nodes"); //NOI18N
1545
}
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 JavaDoc 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"); //NOI18N
1563
return result;
1564    }
1565    }
1566
1567    // PENDING
1568
// Really dumb way of forcing this, but I couldn't get the tree to
1569
// repaint... Will remove this method when that works.
1570
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    /**
1590     * Sort alphabetically
1591     */

1592    class CompAlpha implements Comparator JavaDoc {
1593
1594    public int compare(Object JavaDoc o1, Object JavaDoc o2) {
1595        if(debug) log("In compareAlpha"); //NOI18N
1596
TransactionNode n1 = (TransactionNode)o1;
1597        TransactionNode n2 = (TransactionNode)o2;
1598        if(debug) log("cast the nodes"); //NOI18N
1599
if(debug) {
1600        log("Comparing " + String.valueOf(o1) + //NOI18N
1601
" and " + String.valueOf(o2)); //NOI18N
1602
try {
1603            log("names"); //NOI18N
1604
log(n1.getName());
1605            log(n2.getName());
1606            log("IDs"); //NOI18N
1607
log(n1.getID());
1608            log(n2.getID());
1609        }
1610        catch(Exception JavaDoc 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} // Controller
1620
Popular Tags