KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > servlets > KrankListener


1 /******************************************************************************
2  * KrankListener.java
3  * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.servlets;
11
12 import java.net.*;
13 import java.io.*;
14 import org.jdom.*;
15 import org.jdom.input.*;
16 import org.jdom.output.*;
17 import org.openlaszlo.compiler.CompilationError;
18 import org.openlaszlo.server.LPS;
19 import java.util.*;
20
21 import org.openlaszlo.utils.FileUtils;
22
23 import org.apache.log4j.Logger;
24 /**
25    Listen on port 4444 for krank serialization data, write to tmp file
26
27 */

28
29 public class KrankListener extends Thread JavaDoc {
30     private static Logger mLogger = Logger.getLogger(KrankListener.class);
31     public boolean busy = false;
32     public String JavaDoc appname = "";
33     // record how long the run took
34
public long starttime;
35     public long duration;
36     public String JavaDoc appQueryString = "";
37
38     public static final String JavaDoc IDLE = "IDLE";
39     public static final String JavaDoc BUSY = "BUSY";
40     public static final String JavaDoc FINISHED = "FINISHED";
41     public static final String JavaDoc ABORTED = "ABORTED";
42     public String JavaDoc state = IDLE;
43
44     private Socket clientSocket;
45     private ServerSocket serverSocket;
46
47
48     ////////////////////////////////////////////////////////////////
49
// args
50

51     String JavaDoc prefix;
52     File xmlFile;
53     File krankedSWFfilecopy;
54     File krankedSWF;
55     File basepath;
56     File targetSWF;
57     File targetSWFgz;
58     Properties myprops;
59     int krankPortNum = 4444;
60
61
62     public KrankListener() {};
63
64     public KrankListener(
65         String JavaDoc prefix,
66         File xmlFile,
67         File krankedSWF,
68         File krankedSWFfilecopy,
69         File basepath,
70         File targetSWF,
71         File targetSWFgz,
72         Properties myprops) {
73
74         this.prefix = prefix;
75         this.xmlFile = xmlFile;
76         this.krankedSWF = krankedSWF;
77         this.krankedSWFfilecopy = krankedSWFfilecopy;
78         this.basepath = basepath;
79         this.targetSWF = targetSWF;
80         this.targetSWFgz = targetSWFgz;
81         this.myprops = myprops;
82         this.krankPortNum = LPS.getKrankPort();
83     }
84
85
86     ////////////////////////////////////////////////////////////////
87

88
89     // Maybe the only way to interrupt a thread which is waiting on I/O is
90
// to close the socket.
91
public void closeSocket() {
92         try {
93             if (clientSocket != null) {
94                 clientSocket.close();
95             }
96         } catch (IOException e) { }
97             
98         try {
99             if (serverSocket != null) {
100                 serverSocket.close();
101             }
102         } catch (IOException e) { }
103     }
104
105
106     public void setState(String JavaDoc s) {
107         state = s;
108     }
109
110     public String JavaDoc getState() {
111         return state;
112     }
113
114     public boolean isBusy () {
115         return state.equals(BUSY);
116     }
117
118     public boolean isFinished () {
119         return state.equals(FINISHED);
120     }
121
122     public boolean isAborted () {
123         return state.equals(ABORTED);
124     }
125
126     public boolean isIdle () {
127         return state.equals(IDLE);
128     }
129
130     public void setBusy (boolean b) {
131         if (b) {
132             setState(BUSY);
133         } else {
134             setState(IDLE);
135         }
136     }
137
138     public long starttime () {
139         return starttime;
140     }
141     public long getDuration () {
142         return duration;
143     }
144
145     public void setDuration (long d) {
146         duration = d;
147     }
148
149     public void setAppname (String JavaDoc s) {
150         appname = s;
151     }
152     public String JavaDoc getAppname () {
153         return appname;
154     }
155
156     public void setAppQueryString (String JavaDoc s) {
157         appQueryString = s;
158     }
159     public String JavaDoc getAppQueryString () {
160         return appQueryString;
161     }
162
163     public void run() {
164         try {
165             setState(BUSY);
166             mLogger.debug("starting KrankListener on app "+prefix+" now");
167             setAppname(prefix);
168             listen(xmlFile);
169
170             // Ask the cache for the location of the kranked swf
171
// file but make sure it's not gzip'd. The file should
172
// exist because we wouldn't have gotten here if the
173
// server hadn't delivered it to the client, and the
174
// client ran it and completed sending the xml
175
// serialization data back to the server listener. If
176
// the file doesn't exist for some reason, then, hey, no
177
// big deal, we'll throw an exception, and better luck
178
// next time.
179

180             mLogger.info("kranked swf file is at "+krankedSWF.getAbsolutePath());
181
182             FileInputStream in = new FileInputStream(krankedSWF);
183             // Copy to source tree
184
FileOutputStream out = new FileOutputStream(krankedSWFfilecopy);
185             FileUtils.send(in, out);
186             FileUtils.close(out);
187             FileUtils.close(in);
188
189             mLogger.debug("basepath = "+basepath);
190             new org.openlaszlo.sc.Regenerator().compile(myprops, new String JavaDoc[] { basepath.getAbsolutePath()});
191             setState(FINISHED);
192             mLogger.debug("kranking on "+prefix+" finished!");
193
194         } catch (Exception JavaDoc e) {
195             mLogger.error("Exception caught while invoking KrankListener.listen("+xmlFile+"): "+e+":"+e.getMessage());
196             setState(ABORTED);
197             throw new CompilationError("Error invoking KrankListener.listen("+xmlFile+"): "+e+":"+e.getMessage());
198         } finally {
199             // clean up
200
xmlFile.deleteOnExit();
201             krankedSWFfilecopy.deleteOnExit();
202         }
203     }
204
205
206     public void listen (File outputFile) throws IOException, CompilationError {
207         mLogger.info("KrankListener.listen("+outputFile+")");
208         starttime = System.currentTimeMillis();
209         serverSocket = null;
210         try {
211             serverSocket = new ServerSocket(krankPortNum);
212         } catch (IOException e) {
213             mLogger.error("Could not listen on port: "+krankPortNum+".");
214             throw new CompilationError("Krank listener could not listen on port "+krankPortNum+" "+outputFile.getAbsolutePath());
215         }
216
217         mLogger.info("listening for connection on port "+krankPortNum);
218
219         clientSocket = null;
220         try {
221             clientSocket = serverSocket.accept();
222         } catch (IOException e) {
223             mLogger.error("Accept failed.");
224             throw new CompilationError("Krank listener accept() failed for" + outputFile.getAbsolutePath());
225         }
226
227         mLogger.info("accept on port "+krankPortNum+", sending output to '"+outputFile.getAbsolutePath()+"'");
228         BufferedReader in = new BufferedReader(
229                 new InputStreamReader(
230                 clientSocket.getInputStream(), "UTF-8"));
231      
232         PrintWriter sockout = new PrintWriter(clientSocket.getOutputStream());
233         String JavaDoc inputLine;
234         FileOutputStream fs = new FileOutputStream(outputFile);
235         PrintWriter out = new PrintWriter(fs);
236
237         try {
238             int n = 0;
239             int NLINES = 500;
240             while ((inputLine = in.readLine()) != null) {
241                 inputLine = inputLine.replace((char)0, ' ');
242                 //mLogger.debug("SOCKREAD: "+inputLine);
243
n++;
244                 // Perform some XML fixup on the raw input line
245
out.println(inputLine);
246                 if ((n % NLINES) == 0) {
247                     mLogger.info("...read "+n+" lines...");
248                 }
249                 // </_top> indicates that the xml file is complete
250
if (inputLine.indexOf("</_top>") >= 0) {
251                     mLogger.debug("got </_top>, closing socket");
252                     break;
253                 }
254             }
255         } catch (IOException e) {
256             setState(ABORTED);
257             throw new CompilationError(e);
258         } finally {
259             sockout.write("OK"+'\000');
260             sockout.flush();
261             out.flush();
262             out.close();
263             in.close();
264
265             clientSocket.close();
266             serverSocket.close();
267         }
268         mLogger.info("data stream from client closed, output is in '"+outputFile.getAbsolutePath()+"'");
269     }
270 }
271
Popular Tags