1 19 package org.openbravo.erpCommon.ad_background; 20 21 import java.sql.*; 22 import java.text.*; 23 import java.util.Date ; 24 import javax.servlet.*; 25 import java.io.*; 26 import org.openbravo.base.secureApp.VariablesSecureApp; 27 import org.openbravo.erpCommon.utility.SequenceIdData; 28 import org.openbravo.base.HttpBaseServlet; 29 import org.openbravo.database.ConnectionProvider; 30 31 public class PeriodicBackground implements Runnable { 32 public ConnectionProvider conn; 33 public HttpBaseServlet baseServlet; 34 private Thread runner; 35 private long seconds; 36 private boolean available=true; 37 private boolean isactive=true; 38 private boolean isprocessing=false; 39 private boolean cancelRequested=false; 40 private boolean directLaunch=false; 41 private boolean isFullTime=false; 42 private boolean writingLog=false; 43 public VariablesSecureApp vars=null; 44 public String adProcessId=""; 45 public String adPinstanceId = ""; 46 public String adClientId = ""; 47 private StringBuffer lastLog=new StringBuffer (); 48 private StringBuffer message=new StringBuffer (); 49 private PrintWriter log; 50 private String pid; 51 private String logFileString; 52 private BackgroundProcess object; 53 private boolean debugMode = false; 54 55 56 public PeriodicBackground(HttpBaseServlet _base, long _seconds, String _logFileString, String _adProcessId, String _objectName) throws IOException { 57 runner = new Thread (this); 58 this.seconds = _seconds; 59 this.logFileString = _logFileString; 60 this.conn = _base; 61 this.baseServlet = _base; 62 this.adProcessId = _adProcessId; 63 if (!startClass(_objectName)) { 64 runner.interrupt(); 65 throw new IOException("Couldn't load class " + _objectName); 66 } else if (!writePID()) { 67 runner.interrupt(); 68 throw new IOException("Couldn't write file " + this.logFileString); 69 } 70 } 71 72 private boolean startClass(String _objectName) { 73 Class objectClass = null; 74 try { 75 objectClass = Class.forName(_objectName); 76 } catch (ClassNotFoundException clex) { 77 clex.printStackTrace(); 78 } 79 80 if (objectClass!=null) { 81 try { 82 object = (BackgroundProcess) objectClass.newInstance(); 83 if (object==null) return false; 84 return true; 85 } catch (Exception e) { 86 e.printStackTrace(); 87 } 88 } 89 return false; 90 } 91 92 public void setSeconds(long _seconds) { 93 this.seconds = _seconds; 94 } 95 96 public void setLogFilePath(String _logFileString) { 97 this.logFileString = _logFileString; 98 } 99 100 public void setConnection(ConnectionProvider _conn) { 101 this.conn = _conn; 102 } 103 104 public void setADProcessID(String _adProcessId) { 105 this.adProcessId = _adProcessId; 106 } 107 108 public String getADProcessID() { 109 return this.adProcessId; 110 } 111 112 public void start() { 113 runner.start(); 114 } 115 116 public boolean writePID() { 117 try { 118 SimpleDateFormat formatter = new SimpleDateFormat ("yyyy.MM.dd G 'at' hh:mm:ss:SSS a zzz"); 119 Date nowc = new Date (); 120 pid = formatter.format(nowc); 121 122 BufferedWriter pidout = new BufferedWriter(new 123 FileWriter(logFileString + "pid" + adProcessId)); 124 pidout.write(pid); 125 pidout.close(); 126 } catch(IOException ex) { 127 ex.printStackTrace(); 128 return false; 129 } 130 return true; 131 } 132 133 public boolean isSamePID() { 134 try { 135 BufferedReader in = new BufferedReader(new 136 FileReader(logFileString + "pid" + adProcessId)); 137 String curr_pid = in.readLine(); 138 in.close(); 139 return (curr_pid.equals(pid)); 140 } catch(IOException ex) { 141 System.out.println("Can't read the file for pid info: " + 142 logFileString + "pid" + adProcessId); 143 } 144 return false; 145 } 146 147 public void initializeParams() { 148 cancelRequested=false; 149 isprocessing=false; 150 directLaunch=false; 151 isFullTime=false; 152 writingLog=false; 153 } 154 155 public void run() { 156 boolean forever = true; 157 158 while(forever) { 159 try { 160 if (!isSamePID()) return; 161 Thread.sleep((seconds*1000)); 162 if (isActive()) { 163 initializeParams(); 164 try { 165 process(); 166 } catch(Exception ex) { 167 ex.printStackTrace(); 168 } 169 addLog("Finalizacion del proceso background."); 170 initializeParams(); 171 } else if (isDirectProcess()) { 172 if (debugMode) debug("run()"); 173 if (cancelRequested) { 174 initializeParams(); 175 } else { 176 try { 177 object.processPL(this, true); 178 } catch(Exception ex) { 179 ex.printStackTrace(); 180 } 181 addLog("Finalizacion del proceso manual."); 182 this.vars = null; 183 initializeParams(); 184 } 185 } 186 setProcessing(false); 187 } catch(InterruptedException e) { 188 return; 189 } 190 } 191 } 192 193 private void process() throws Exception { 194 PeriodicBackgroundData[] data = null; 195 try { 196 data = PeriodicBackgroundData.select(conn, ""); 197 } catch (ServletException ex) { 198 ex.printStackTrace(); 199 return; 200 } 201 if (data==null) return; 202 for (int j=0;j<data.length;j++) { 203 if (!isSamePID()) return; 204 else if (!isProcesable(false, data[j].adClientId)) { 205 continue; 206 } 207 setProcessing(true); 208 this.adClientId = data[j].adClientId; 209 this.message.setLength(0); 210 this.lastLog.setLength(0); 211 object.processPL(this, false); 212 } 213 } 214 215 public boolean isDirectProcess() { 216 return(directLaunch); 217 } 218 219 public String saveLog(String adNoteId, String adClientId) { 220 String strMessage="", strNewMessage=""; 221 try { 222 if (adNoteId==null || adNoteId.equals("")) { 223 adNoteId = SequenceIdData.getSequence(conn, "AD_Note", adClientId); 224 PeriodicBackgroundData.insert(conn, adNoteId, adClientId, ""); 225 } 226 if (this.message.length()>2000) { 227 strMessage = this.message.toString().substring(0, 1990) + "..."; 228 strNewMessage = this.message.toString().substring(1990); 229 this.message.setLength(0); 230 this.message.append("...").append(strNewMessage); 231 } else strMessage = this.message.toString(); 232 PeriodicBackgroundData.update(conn, strMessage, adNoteId); 233 if (!strNewMessage.equals("")) adNoteId = saveLog("", adClientId); 234 } catch (ServletException ex) { 235 ex.printStackTrace(); 236 } 237 return adNoteId; 238 } 239 240 public void addLog(String texto) { 241 addLog(texto, true); 242 } 243 244 public void addLog(String texto, boolean generalLog) { 245 writingLog=true; 246 Timestamp tmp = new Timestamp(System.currentTimeMillis()); 247 if (generalLog) this.message.append(tmp.toString()).append(" - ").append(texto).append("<br>"); 248 lastLog.append("<span>").append(tmp.toString()).append(" - ").append(texto).append("</span><br>"); 249 writingLog=false; 250 } 251 252 public void clearLastLog() { 253 lastLog.setLength(0); 254 } 255 256 public boolean isProcesable(boolean directProcess, String adClientId) { 257 if (directLaunch!=directProcess) return false; 258 if (!directProcess) { 259 if (isActive() && isPlannedRange(adClientId)) { 260 try { 261 isFullTime = PeriodicBackgroundData.getFullTime(conn, adClientId); 262 } catch (ServletException ex) { 263 ex.printStackTrace(); 264 isFullTime=false; 265 } 266 return true; 267 } else return false; 268 } else if (isActive() || this.cancelRequested) return false; 269 return true; 270 } 271 272 public boolean isPlannedRange(String AD_Client_ID) { 273 try { 274 return (PeriodicBackgroundData.checkPlannedRange(conn, AD_Client_ID, adProcessId)); 275 } catch (ServletException e) { 276 e.printStackTrace(); 277 } 278 return false; 279 } 280 281 public boolean isWritingLog() { 282 return writingLog; 283 } 284 285 public String getLog() { 286 return(message.toString()); 287 } 288 289 public String getOut() { 290 return(lastLog.toString()); 291 } 292 293 public boolean isActive() { 294 return (isactive); 295 } 296 297 public void setActive(boolean status) { 298 if (!status || !isDirectProcess()) this.isactive = status; 299 } 300 301 public void cancelDirectProcess() { 302 this.cancelRequested = true; 303 } 304 305 public boolean isCanceledDirectProcess() { 306 return this.cancelRequested; 307 } 308 309 public boolean isProcessing() { 310 return(this.isprocessing); 311 } 312 313 public void setProcessing(boolean status) { 314 this.isprocessing = status; 315 } 316 317 private void debug(String processName) { 318 System.out.println(processName + "***************************** adPinstanceId: " + this.adPinstanceId + " - directLaunch: " + this.directLaunch + " - cancelRequest: " + this.cancelRequested + " - adClientId: " + this.adClientId); 319 } 320 321 public boolean directLaunch(VariablesSecureApp vars, String stradPinstanceId) throws ServletException { 322 this.directLaunch = true; 323 String strClient = PeriodicBackgroundData.selectClientId(conn, stradPinstanceId); 324 if (isProcesable(true, strClient)) { 325 this.vars = vars; 326 this.adPinstanceId = stradPinstanceId; 327 this.directLaunch = true; 328 this.cancelRequested=false; 329 this.adClientId = strClient; 330 if (debugMode) debug("directLaunch(vars, " + stradPinstanceId + "): " + strClient); 331 return true; 332 } else { 333 addLog("El proceso no se puede lanzar de forma manual si está activo el proceso background"); 334 this.directLaunch = false; 335 this.cancelRequested=false; 336 setProcessing(false); 337 return false; 338 } 339 } 340 341 public void doPause() throws Exception { 342 if (!isSamePID()) throw new Exception ("End of thread"); 343 if (!isDirectProcess() || !isFullTime) Thread.sleep((seconds*1000)); 344 } 345 346 public boolean canContinue(boolean directProcess, String client) { 347 if (!isProcesable(directProcess, client)) { 348 cancelRequested=false; 349 setProcessing(false); 350 return false; 351 } 352 return true; 353 } 354 355 public void destroy(int millis) throws SQLException { 356 available=false; 357 runner.interrupt(); 358 359 try { 360 runner.join(millis); 361 } catch(InterruptedException e){} } 363 364 public void destroy() { 365 try { 366 destroy(10000); 367 } catch(SQLException e) {} 368 } 369 } 370 | Popular Tags |