1 32 33 package com.knowgate.scheduler; 34 35 import java.lang.ref.SoftReference ; 36 37 import java.util.Date ; 38 import java.util.Properties ; 39 40 import java.sql.Connection ; 41 import java.sql.CallableStatement ; 42 import java.sql.PreparedStatement ; 43 import java.sql.Statement ; 44 import java.sql.ResultSet ; 45 import java.sql.SQLException ; 46 import java.sql.Types ; 47 import java.sql.Timestamp ; 48 49 import java.io.IOException ; 50 import java.io.FileNotFoundException ; 51 import java.io.File ; 52 import java.io.FileWriter ; 53 54 import java.util.Properties ; 55 56 import javax.mail.MessagingException ; 57 58 import com.knowgate.debug.DebugFile; 59 import com.knowgate.jdc.JDCConnection; 60 import com.knowgate.misc.Environment; 61 import com.knowgate.misc.Gadgets; 62 63 import com.knowgate.dataobjs.DB; 64 import com.knowgate.dataobjs.DBBind; 65 import com.knowgate.dataobjs.DBPersist; 66 67 72 public abstract class Job extends DBPersist { 73 private Properties oParams; 74 private Properties oEnvProps; 75 private File oLogFile; 76 private DBBind oDataBind; 77 78 protected int iPendingAtoms; 79 80 public Job() { 81 super(DB.k_jobs, "Job"); 82 oParams = null; 83 oEnvProps = null; 84 iPendingAtoms = 0; 85 oLogFile = null; 86 oDataBind = null; 87 } 88 89 91 102 public abstract Object process (Atom oAtm) 103 throws SQLException ,FileNotFoundException ,IOException ,MessagingException , 104 NullPointerException ; 105 106 108 111 public abstract void free (); 112 113 115 119 public int pending() { 120 return iPendingAtoms; 121 } 122 123 125 public void abort(JDCConnection oConn) throws SQLException ,IllegalStateException { 126 if (DebugFile.trace) { 127 DebugFile.writeln("Begin Job.abort()"); 128 DebugFile.incIdent(); 129 DebugFile.writeln("gu_job="+getStringNull(DB.gu_job,"null")); 130 } 131 short iStatus; 132 String sSQL; 133 PreparedStatement oUpdt; 134 PreparedStatement oStmt = oConn.prepareStatement("SELECT "+DB.id_status+" FROM "+DB.k_jobs+" WHERE "+DB.gu_job+"=?", 135 ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 136 oStmt.setString(1, getStringNull(DB.gu_job,null)); 137 ResultSet oRSet = oStmt.executeQuery(); 138 if (oRSet.next()) 139 iStatus = oRSet.getShort(1); 140 else 141 iStatus = 100; 142 oRSet.close(); 143 oStmt.close(); 144 if (100==iStatus) 145 throw new SQLException ("Job "+getStringNull(DB.gu_job,"null")+" not found"); 146 if (Atom.STATUS_ABORTED==iStatus) 147 throw new IllegalStateException ("Job "+getStringNull(DB.gu_job,"null")+" was already aborted"); 148 else if (Atom.STATUS_FINISHED==iStatus) 149 throw new IllegalStateException ("Job "+getStringNull(DB.gu_job,"null")+" was already finished"); 150 sSQL = "UPDATE "+DB.k_job_atoms+" SET "+DB.id_status+"="+String.valueOf(Atom.STATUS_ABORTED)+","+DB.dt_execution+"=NULL WHERE "+DB.gu_job+"=? AND "+DB.id_status+"<>"+String.valueOf(Atom.STATUS_FINISHED); 151 if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement("+sSQL+")"); 152 oUpdt = oConn.prepareStatement(sSQL); 153 oUpdt.setString(1, getStringNull(DB.gu_job,null)); 154 oUpdt.executeUpdate(); 155 oUpdt.close(); 156 sSQL = "UPDATE "+DB.k_jobs+" SET "+DB.id_status+"="+String.valueOf(Atom.STATUS_ABORTED)+","+DB.dt_finished+"="+DBBind.Functions.GETDATE+" WHERE "+DB.gu_job+"=?"; 157 oUpdt = oConn.prepareStatement(sSQL); 158 oUpdt.setString(1, getStringNull(DB.gu_job,null)); 159 oUpdt.executeUpdate(); 160 oUpdt.close(); 161 if (DebugFile.trace) { 162 DebugFile.decIdent(); 163 DebugFile.writeln("End Job.abort()"); 164 } 165 } 167 169 175 public boolean load(JDCConnection oConn, Object [] PKVals) throws SQLException { 176 boolean bRetVal; 177 String sList; 178 String sPageSet; 179 String sAttachImages; 180 Statement oStmt; 181 ResultSet oRSet; 182 183 if (DebugFile.trace) { 184 DebugFile.writeln("Begin Job.load([Connection], Object[])"); 185 DebugFile.incIdent(); 186 } 187 188 oParams = null; 189 190 bRetVal = super.load(oConn, PKVals); 191 192 if (bRetVal) { 193 194 oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 195 196 try { oStmt.setQueryTimeout(60); } catch (SQLException sqle) { } 197 198 if (DebugFile.trace) DebugFile.writeln("Statement.executeQuery(SELECT COUNT(*) FROM " + DB.k_job_atoms + " WHERE " + DB.gu_job + "='" + getStringNull(DB.gu_job,"null") + "' AND (" + DB.id_status + "=" + String.valueOf(Atom.STATUS_PENDING) + " OR " + DB.id_status + "=" + String.valueOf(Atom.STATUS_SUSPENDED) + "))"); 199 200 oRSet = oStmt.executeQuery("SELECT COUNT(*) FROM " + DB.k_job_atoms + " WHERE " + DB.gu_job + "='" + getString(DB.gu_job) + "' AND (" + DB.id_status + "=" + String.valueOf(Atom.STATUS_PENDING) + " OR " + DB.id_status + "=" + String.valueOf(Atom.STATUS_SUSPENDED) + " OR " + DB.id_status + "=" + String.valueOf(Atom.STATUS_RUNNING) + ")"); 201 oRSet.next(); 202 iPendingAtoms = oRSet.getInt(1); 203 oRSet.close(); 204 205 oStmt.close(); 206 207 if (DebugFile.trace) DebugFile.writeln("pending atoms = " + String.valueOf(iPendingAtoms)); 208 209 sPageSet = getParameter("gu_pageset"); 210 211 if (null!=sPageSet) { 212 213 oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 214 215 oRSet = oStmt.executeQuery("SELECT " + DB.gu_workarea + "," + DB.nm_pageset + " FROM " + DB.k_pagesets + " WHERE " + DB.gu_pageset + "='" + sPageSet + "'"); 216 217 if (oRSet.next()) { 218 oParams.put("gu_workarea", oRSet.getString(1)); 219 oParams.put("nm_pageset", oRSet.getString(2)); 220 } 221 else { 222 bRetVal = false; 223 if (DebugFile.trace) 224 DebugFile.writeln("ERROR: PageSet " + sPageSet + 225 " referenced by job " + getString(DB.gu_job) + 226 " was not found"); 227 } 228 oRSet.close(); 229 oStmt.close(); 230 231 } 233 sList = getParameter("gu_list"); 234 235 if (null!=sList) { 236 237 oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 238 239 oRSet = oStmt.executeQuery("SELECT " + DB.tx_sender + "," + DB.tx_from + "," + DB.tx_reply + "," + DB.tx_subject + " FROM " + DB.k_lists + " WHERE " + DB.gu_list + "='" + sList + "'"); 240 241 if (oRSet.next()) { 242 oParams.put("tx_sender", oRSet.getString(1)); 243 oParams.put("tx_from", oRSet.getString(2)); 244 oParams.put("tx_reply", oRSet.getString(2)); 245 oParams.put("tx_subject", oRSet.getString(2)); 246 } 247 else { 248 bRetVal = false; 249 if (DebugFile.trace) 250 DebugFile.writeln("ERROR: List " + sList + 251 " referenced by job " + getString(DB.gu_job) + 252 " was not found"); 253 } 254 oRSet.close(); 255 oStmt.close(); 256 257 } 259 sAttachImages = getParameter("bo_attachimages"); 260 261 if (null==sAttachImages) 262 oParams.put("bo_attachimages", "1"); 263 else 264 oParams.put("bo_attachimages", sAttachImages); 265 266 } 268 if (DebugFile.trace) { 269 DebugFile.decIdent(); 270 DebugFile.writeln("End Job.load() : " + String.valueOf(bRetVal)); 271 } 272 273 return bRetVal; 274 } 276 278 283 public boolean delete(JDCConnection oConn) throws SQLException { 284 return Job.delete(oConn, getString(DB.gu_job)); 285 } 286 287 289 295 public boolean store(JDCConnection oConn) throws SQLException { 296 297 if (!AllVals.containsKey(DB.gu_job)) 298 put(DB.gu_job, Gadgets.generateUUID()); 299 else 300 put(DB.dt_modified, new Timestamp (new Date ().getTime())); 301 302 if (!AllVals.containsKey(DB.id_status)) 303 put(DB.id_status, STATUS_PENDING); 304 305 return super.store(oConn); 306 } 307 308 310 314 315 public Properties getParameters() { 316 String aParams[]; 317 int iParams; 318 int iDot; 319 320 if (null==oParams) { 321 oParams = new Properties (); 322 if (!isNull(DB.tx_parameters)) { 323 aParams = Gadgets.split(getString(DB.tx_parameters), ","); 324 iParams = aParams.length; 325 for (int p = 0; p < iParams; p++) { 326 iDot = aParams[p].indexOf(':'); 327 if (iDot <= 0) 328 oParams.put(aParams[p], ""); 329 else 330 oParams.put(aParams[p].substring(0, iDot), 331 aParams[p].substring(iDot + 1)); 332 } } } 336 return oParams; 337 } 339 341 346 public String getParameter(String sParamName) { 347 return getParameters().getProperty(sParamName); 348 } 349 350 351 357 public String getProperty(String sPropertyName) { 358 return oEnvProps.getProperty(sPropertyName); 359 } 360 361 363 366 public Properties getProperties() { 367 return oEnvProps; 368 } 369 370 372 377 public File logFile() { 378 return oLogFile; 379 } 380 381 383 387 public void log (String sStr) { 388 FileWriter oWriter; 389 390 if (oLogFile!=null) { 391 oWriter = null; 392 try { 393 oWriter = new FileWriter (oLogFile, true); 394 oWriter.write(sStr); 395 oWriter.close(); 396 oWriter = null; 397 } 398 catch (IOException ioe) { 399 if (null!=oWriter) { try {oWriter.close();} catch (IOException e) {} } 400 } 401 } } 404 406 410 public DBBind getDataBaseBind() { 411 return oDataBind; 412 } 413 414 416 420 public void setDataBaseBind(DBBind oDbb) { 421 oDataBind = oDbb; 422 } 423 424 426 442 public void setStatus(JDCConnection oConn, int iStatus) throws SQLException { 443 444 PreparedStatement oStmt; 445 446 if (DebugFile.trace) { 447 DebugFile.writeln("Begin Job.setStatus([Connection], " + String.valueOf(iStatus) + ")"); 448 DebugFile.incIdent(); 449 } 450 451 if (Job.STATUS_FINISHED==iStatus) { 452 453 oStmt = oConn.prepareStatement("UPDATE " + DB.k_jobs + " SET " + DB.id_status + "=" + String.valueOf(iStatus) + "," + DB.dt_finished + "=? WHERE " + DB.gu_job + "='" + getString(DB.gu_job) + "'"); 454 455 try {oStmt.setQueryTimeout(10);} catch (SQLException sqle) {} 456 457 oStmt.setTimestamp(1, new Timestamp (new java.util.Date ().getTime())); 458 oStmt.executeUpdate(); 459 oStmt.close(); 460 461 } 462 463 else { 464 465 oStmt = oConn.prepareStatement("UPDATE " + DB.k_jobs + " SET " + DB.id_status + "=" + String.valueOf(iStatus) + " WHERE " + DB.gu_job + "='" + getString(DB.gu_job) + "'"); 466 467 try {oStmt.setQueryTimeout(10);} catch (SQLException sqle) {} 468 469 oStmt.executeUpdate(); 470 oStmt.close(); 471 472 } 473 474 if (DebugFile.trace) { 475 DebugFile.decIdent(); 476 DebugFile.writeln("End Job.setStatus()"); 477 } 478 } 480 488 public void resolveAtomsEMails(JDCConnection oConn) 489 throws SQLException { 490 Statement oStmt; 491 CallableStatement oCall; 492 if (DebugFile.trace) { 493 DebugFile.writeln("Begin Job.resolveAtomsEMails()"); 494 DebugFile.incIdent(); 495 } 496 switch (oConn.getDataBaseProduct()) { 497 case JDCConnection.DBMS_POSTGRESQL: 498 oStmt = oConn.createStatement(); 499 oStmt.executeQuery("SELECT k_sp_resolve_atoms('"+getStringNull(DB.gu_job,null)+"')"); 500 oStmt.close(); 501 break; 502 default: 503 oCall = oConn.prepareCall("{ call k_sp_resolve_atoms('"+getStringNull(DB.gu_job,null)+"') }"); 504 oCall.execute(); 505 oCall.close(); 506 } 507 if (DebugFile.trace) { 508 DebugFile.decIdent(); 509 DebugFile.writeln("End Job.resolveAtomsEMails()"); 510 } 511 } 512 513 516 523 public static boolean delete(JDCConnection oConn, String sJobId) throws SQLException { 524 Statement oStmt; 525 CallableStatement oCall; 526 boolean bRetVal; 527 if (DebugFile.trace) { 528 DebugFile.writeln("Begin Job.delete([Connection]," + sJobId + ")"); 529 DebugFile.incIdent(); 530 } 531 532 switch (oConn.getDataBaseProduct()) { 533 case JDCConnection.DBMS_POSTGRESQL: 534 oStmt = oConn.createStatement(); 535 if (DebugFile.trace) DebugFile.writeln("Connection.executeQuery(SELECT k_sp_del_job ('" + sJobId + "')"); 536 oStmt.executeQuery("SELECT k_sp_del_job ('" + sJobId + "')"); 537 oStmt.close(); 538 bRetVal = true; 539 break; 540 default: 541 if (DebugFile.trace) DebugFile.writeln("Connection.prepareCall({ call k_sp_del_job ('" + sJobId + "') }"); 542 oCall = oConn.prepareCall("{ call k_sp_del_job ('" + sJobId + "') }"); 543 bRetVal = oCall.execute(); 544 oCall.close(); 545 } 546 if (DebugFile.trace) { 547 DebugFile.decIdent(); 548 DebugFile.writeln("End Job.delete() : " + String.valueOf(bRetVal)); 549 } 550 return bRetVal; 551 } 553 555 568 public static synchronized Job instantiate(JDCConnection oConn, String sJobId, Properties oEnvironmentProps) 569 570 throws ClassNotFoundException , IllegalAccessException , SQLException , InstantiationException , FileNotFoundException { 571 572 Class oJobImplementation; 573 PreparedStatement oStmt; 574 ResultSet oRSet; 575 String sStorage; 576 String sCmmdId, sClassNm; 577 Job oRetObj; 578 579 if (DebugFile.trace) { 580 DebugFile.writeln("Begin Job.instantiate([Connection]," + sJobId + ")"); 581 DebugFile.incIdent(); 582 DebugFile.writeln("Connection.prepareStatement(SELECT id_command,tx_command,nm_class FROM k_lu_job_commands WHERE id_command=(SELECT id_command FROM k_jobs WHERE gu_job='" + sJobId + "'))"); 583 } 584 585 oStmt = oConn.prepareStatement("SELECT id_command,tx_command,nm_class FROM k_lu_job_commands WHERE id_command=(SELECT id_command FROM k_jobs WHERE gu_job=?)", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 586 oStmt.setString(1, sJobId); 587 588 if (DebugFile.trace) DebugFile.writeln("PreparedStatementStatement.executeQuery()"); 589 590 oRSet = oStmt.executeQuery(); 591 592 if (oRSet.next()) { 593 594 sCmmdId = oRSet.getString(1); 595 596 if (DebugFile.trace) DebugFile.writeln("Class Id =" + sCmmdId); 597 598 sClassNm = oRSet.getString(3); 599 600 if (DebugFile.trace) DebugFile.writeln("Class Name =" + sClassNm); 601 } 603 else { 604 sCmmdId = null; 605 sClassNm = "null"; 606 607 if (DebugFile.trace) DebugFile.writeln("ERROR: job command id. not found"); 608 } 609 610 oRSet.close(); 611 oStmt.close(); 612 613 if (null==sCmmdId) 614 oRetObj = null; 615 else { 616 if (DebugFile.trace) DebugFile.writeln("Class.forName(" + sClassNm + ");"); 617 618 oJobImplementation = Class.forName(sClassNm); 619 oRetObj = (Job) oJobImplementation.newInstance(); 620 621 if (oRetObj.load(oConn, new Object [] {sJobId})) { 622 if (null!=oConn.getPool()) { 623 if (DebugFile.trace) DebugFile.writeln("setting Job database binding"); 624 oRetObj.setDataBaseBind( (DBBind) oConn.getPool().getDatabaseBinding()); 625 } else { 626 if (DebugFile.trace) DebugFile.writeln("Job has no database binding"); 627 } 628 oRetObj.oEnvProps = oEnvironmentProps; 629 } else { 630 oRetObj = null; 631 } 632 } 634 if (null!=oRetObj) { 635 sStorage = oEnvironmentProps.getProperty("storage"); 636 637 if (null != sStorage) { 638 if (!sStorage.endsWith(System.getProperty("file.separator"))) sStorage += System.getProperty("file.separator"); 639 640 sStorage += "jobs"; 641 642 644 oRetObj.oLogFile = new File (sStorage); 645 if (!oRetObj.oLogFile.exists()) oRetObj.oLogFile.mkdir(); 646 if (!oRetObj.oLogFile.exists()) throw new FileNotFoundException (sStorage); 647 648 650 sStorage += System.getProperty("file.separator") + oRetObj.getString(DB.gu_workarea); 651 oRetObj.oLogFile = new File (sStorage); 652 if (!oRetObj.oLogFile.exists()) oRetObj.oLogFile.mkdir(); 653 if (!oRetObj.oLogFile.exists()) throw new FileNotFoundException (sStorage); 654 655 oRetObj.oLogFile = new File (sStorage + System.getProperty("file.separator") + sJobId); 657 if (!oRetObj.oLogFile.exists()) oRetObj.oLogFile.mkdir(); 658 if (!oRetObj.oLogFile.exists()) throw new FileNotFoundException (sStorage); 659 660 oRetObj.oLogFile = new File (sStorage + System.getProperty("file.separator") + sJobId + ".txt"); 662 663 } } 666 if (DebugFile.trace) { 667 DebugFile.decIdent(); 668 DebugFile.writeln("End Job.instantiate()"); 669 } 670 671 return oRetObj; 672 } 674 687 public static synchronized Job instantiate(JDCConnection oConn, String sJobId, String sProfileName) 688 throws ClassNotFoundException , IllegalAccessException , SQLException , InstantiationException , FileNotFoundException { 689 return instantiate(oConn,sJobId,Environment.getProfile(sProfileName)); 690 } 691 692 695 private static void printUsage() { 696 System.out.println(""); 697 System.out.println("Usage:"); 698 System.out.println("Job create job_type cnf_file_path xml_file_path [gu_job]"); 699 System.out.println("job_type is one of {MAIL | MIME | FAX | SAVE | FTP}"); 700 } 701 702 public static void main(String [] argv) 703 throws SQLException , org.xml.sax.SAXException , java.io.IOException , 704 ClassNotFoundException , IllegalAccessException , InstantiationException { 705 706 DBPersist oJob; 707 DBBind oDBB; 708 JDCConnection oCon; 709 710 if (argv.length!=4 && argv.length!=5) 711 printUsage(); 712 else 713 if (!argv[0].equals("create")) 714 printUsage(); 715 716 else if (!argv[1].equalsIgnoreCase("MAIL") && !argv[1].equalsIgnoreCase("FAX") && 717 !argv[1].equalsIgnoreCase("SAVE") && !argv[1].equalsIgnoreCase("FTP") && 718 !argv[1].equalsIgnoreCase("MIME") ) 719 printUsage(); 720 721 else { 722 oDBB = new DBBind(argv[2]); 723 724 oCon = oDBB.getConnection("job_main"); 725 726 oJob = new DBPersist(DB.k_jobs, argv[1]); 727 728 oJob.parseXML(argv[3]); 729 730 if (argv.length==5) 731 oJob.replace (DB.gu_job, argv[4]); 732 733 else if (!oJob.getItemMap().containsKey(DB.gu_job)) 734 oJob.put (DB.gu_job, com.knowgate.misc.Gadgets.generateUUID()); 735 736 oJob.store(oCon); 737 738 oCon.close(); 739 740 oDBB.close(); 741 742 System.out.println("gu_job:"+oJob.getString(DB.gu_job)); 743 } 745 } 747 750 public static final short STATUS_ABORTED = -1; 751 public static final short STATUS_FINISHED = 0; 752 public static final short STATUS_PENDING = 1; 753 public static final short STATUS_SUSPENDED = 2; 754 public static final short STATUS_RUNNING = 3; 755 public static final short STATUS_INTERRUPTED = 4; 756 757 } 758 | Popular Tags |