1 4 5 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 28 29 public class KrankListener extends Thread { 30 private static Logger mLogger = Logger.getLogger(KrankListener.class); 31 public boolean busy = false; 32 public String appname = ""; 33 public long starttime; 35 public long duration; 36 public String appQueryString = ""; 37 38 public static final String IDLE = "IDLE"; 39 public static final String BUSY = "BUSY"; 40 public static final String FINISHED = "FINISHED"; 41 public static final String ABORTED = "ABORTED"; 42 public String state = IDLE; 43 44 private Socket clientSocket; 45 private ServerSocket serverSocket; 46 47 48 51 String 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 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 88 89 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 s) { 107 state = s; 108 } 109 110 public String 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 s) { 150 appname = s; 151 } 152 public String getAppname () { 153 return appname; 154 } 155 156 public void setAppQueryString (String s) { 157 appQueryString = s; 158 } 159 public String 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 180 mLogger.info("kranked swf file is at "+krankedSWF.getAbsolutePath()); 181 182 FileInputStream in = new FileInputStream(krankedSWF); 183 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 [] { basepath.getAbsolutePath()}); 191 setState(FINISHED); 192 mLogger.debug("kranking on "+prefix+" finished!"); 193 194 } catch (Exception 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 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 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 n++; 244 out.println(inputLine); 246 if ((n % NLINES) == 0) { 247 mLogger.info("...read "+n+" lines..."); 248 } 249 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 |