KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > appserver > debugger > applet > Updater


1 /*-----------------------------------------------------------------------------
2  * Enhydra Java Application Server
3  * Copyright 1997-2000 Lutris Technologies, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  * must display the following acknowledgement:
16  * This product includes Enhydra software developed by Lutris
17  * Technologies, Inc. and its contributors.
18  * 4. Neither the name of Lutris Technologies nor the names of its contributors
19  * may be used to endorse or promote products derived from this software
20  * without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY LUTRIS TECHNOLOGIES AND CONTRIBUTORS ``AS IS''
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL LUTRIS TECHNOLOGIES OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *-----------------------------------------------------------------------------
34  * $Id: Updater.java,v 1.1 2004/04/06 15:24:32 rale Exp $
35  *-----------------------------------------------------------------------------
36  */

37
38
39
40
41
42 package com.lutris.appserver.debugger.applet;
43
44 import java.util.Vector JavaDoc;
45
46
47 import java.io.*;
48 import java.net.*;
49 import java.awt.*;
50 import java.awt.event.*;
51 import java.applet.AppletContext JavaDoc;
52 import javax.swing.*;
53 import javax.swing.event.*;
54 import javax.swing.border.*;
55 import javax.swing.table.*;
56 import com.lutris.appserver.debugger.applet.io.*;
57
58
59 public class Updater implements Runnable JavaDoc {
60
61     private static long currentStateId = -1;
62
63     private static Thread JavaDoc autoUpdateThread;
64
65     private long timeToDie;
66
67     private Updater(int seconds) {
68         timeToDie = System.currentTimeMillis() + (seconds * 1000);;
69     }
70
71
72
73     public static boolean updateState(int wait) {
74 System.out.println("In updateState()");
75         synchronized (Globals.networkLock) {
76             long startTime = System.currentTimeMillis();
77             Snapshot s = null;
78             try {
79                 s = fetchSnapshot(wait);
80             } catch (DebuggerException e) {
81                 JOptionPane.showMessageDialog(Globals.japplet,
82                         e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
83                 return false;
84             }
85             if (s == null)
86                 return true;
87           
88            
89             int sel = Globals.transactionList.getSelectedIndex();
90             int oldId = -1;
91             if (sel >= 0) {
92                 TransactionDescription t = (TransactionDescription) Globals.transactionList.getModel().getElementAt(sel);
93                 oldId = t.id;
94             }
95
96             currentStateId = s.stateId;
97             Globals.servletModel.updateToSnapshot(s);
98             Globals.transactionModel.updateToSnapshot(s);
99
100             if (sel >= 0) {
101                 int row = Globals.transactionModel.idToRow(oldId);
102                 if (row >= 0)
103                     Globals.transactionList.setSelectedIndex(row);
104                 else
105                     Globals.transactionList.clearSelection();
106             }
107         }
108 System.out.println("updateState() done");
109         return true;
110     }
111
112  
113
114
115
116
117     private static Snapshot fetchSnapshot(int wait) throws DebuggerException {
118         try {
119             Globals.japplet.showStatus("Getting status from server...");
120           
121             URL target = new URL(Globals.japplet.getDocumentBase(), "DebugServer.po?action=getSnapshot&id=" + currentStateId + "&wait=" + wait);
122            
123             URLConnection conn = target.openConnection();
124             
125             conn.setDoInput(true);
126             conn.setDoOutput(false);
127             conn.setUseCaches(false);
128             conn.setRequestProperty("Accept", "application/java-serialized");
129           
130             conn.connect();
131           
132            
133            
134            
135            
136             InputStream in = conn.getInputStream();
137            
138         
139             ObjectInputStream oin = new ObjectInputStream(in);
140         
141            
142             Object JavaDoc o = oin.readObject();
143            
144             Globals.japplet.showStatus("");
145           
146          
147          
148          
149             return (Snapshot) o;
150         } catch (MalformedURLException e) {
151             throw new DebuggerException("Unable to find server: " + e.getMessage());
152         } catch (StreamCorruptedException e) {
153             throw new DebuggerException("Bad data returned from server.");
154         } catch (OptionalDataException e) {
155             throw new DebuggerException("Bad data returned from server.");
156         } catch (ClassNotFoundException JavaDoc e) {
157             throw new DebuggerException("Bad data returned from server.");
158         } catch (IOException e) {
159             throw new DebuggerException("Error connecting to server: " + e.getMessage());
160         }
161     }
162
163
164     //------------------------------------------------------------
165

166     public static void startThread() {
167          if (autoUpdateThread != null)
168              killThread();
169          autoUpdateThread = new Thread JavaDoc(new Updater(300));
170          autoUpdateThread.start();
171      }
172
173       
174      
175     public static void killThread() {
176         if (autoUpdateThread != null) {
177             autoUpdateThread.stop();
178             autoUpdateThread = null;
179         }
180     }
181
182
183     public static boolean threadRunning() {
184         return (autoUpdateThread != null);
185     }
186
187
188     public void run() {
189         while (true) {
190             boolean ok = updateState(30);
191             if (!ok || (System.currentTimeMillis() >= timeToDie)) {
192 System.out.println("THREAD ERROR/TIMEOUT");
193                 autoUpdateThread = null;
194 System.out.println("st");
195                 Globals.serverButton.setText(Globals.inactiveTitle);
196 System.out.println("en");
197                 Globals.updateButton.setEnabled(true);
198                 break;
199             }
200         }
201     }
202
203 }
204
Popular Tags