1 64 65 package com.jcorporate.expresso.core.job; 66 67 import com.jcorporate.expresso.core.ExpressoSchema; 68 import com.jcorporate.expresso.core.db.DBException; 69 import com.jcorporate.expresso.core.dbobj.SecuredDBObject; 70 import com.jcorporate.expresso.core.i18n.Messages; 71 import com.jcorporate.expresso.core.misc.EMailSender; 72 import com.jcorporate.expresso.core.misc.EventHandler; 73 import com.jcorporate.expresso.core.misc.StringUtil; 74 import com.jcorporate.expresso.core.registry.ExpressoThread; 75 import com.jcorporate.expresso.core.security.User; 76 import com.jcorporate.expresso.kernel.util.ClassLocator; 77 import com.jcorporate.expresso.kernel.util.FastStringBuffer; 78 import com.jcorporate.expresso.services.crontab.CrontabEntry; 79 import com.jcorporate.expresso.services.dbobj.Event; 80 import com.jcorporate.expresso.services.dbobj.JobQueue; 81 import com.jcorporate.expresso.services.dbobj.JobQueueParam; 82 import com.jcorporate.expresso.services.dbobj.Setup; 83 import org.apache.log4j.Logger; 84 85 import java.util.Enumeration ; 86 import java.util.HashMap ; 87 import java.util.Hashtable ; 88 import java.util.Iterator ; 89 import java.util.List ; 90 import java.util.Map ; 91 import java.util.StringTokenizer ; 92 import java.util.Vector ; 93 94 108 public abstract class Job 109 extends ExpressoThread { 110 111 114 private JobQueue myJobQueue = null; 115 116 119 private String myUser = null; 120 121 124 private String myJobNumber = null; 125 126 129 private Hashtable myFunctions = new Hashtable (); 130 131 134 private Hashtable myParameterNames = new Hashtable (); 135 136 139 private Hashtable myParamValidValues = new Hashtable (); 140 141 144 private int m_jobParamsNum = -1; 145 146 149 private List m_jobParamsEntries = null; 150 151 157 private Map jobParameterValueMap = null; 158 159 162 protected static Logger log = Logger.getLogger(Job.class); 163 164 167 private String mySchema = null; 168 169 172 private boolean m_useCron = false; 173 174 177 private CrontabEntry m_cronAlarmEntry = null; 178 179 180 183 public static final String IS_NOTIFY_ON_JOB_SUCCESS = "isNotifyOnJobSuccess"; 184 185 188 public Job() { 189 this.setDaemon(true); 190 } 191 192 193 201 protected void addFunction(String name, String descrip) { 202 myFunctions.put(name, descrip); 203 } 204 205 210 public void setUseCron(boolean useCron) { 211 m_useCron = useCron; 212 } 213 214 219 public void setCronAlarmEntry(CrontabEntry obj) { 220 m_cronAlarmEntry = obj; 221 } 222 223 228 public CrontabEntry getCronAlarmEntry() { 229 return m_cronAlarmEntry; 230 } 231 232 237 public boolean useCron() { 238 return m_useCron; 239 } 240 241 248 protected void addParameter(String paramCode, String paramDescrip) { 249 myParameterNames.put(paramCode, paramDescrip); 250 } 251 252 260 public void addParamValidValues(String paramCode, Vector paramValidValues) { 261 myParamValidValues.put(paramCode, paramValidValues); 262 } 263 264 270 protected void finish(String msg) { 271 finish(msg, null); 272 } 273 274 281 protected void finish(String msg, Throwable t) { 282 log.info("Finishing job"); 283 284 FastStringBuffer mailmsg = new FastStringBuffer(64); 285 mailmsg.append(msg + "\n"); 286 287 boolean success = true; 288 289 try { 290 String dbName = "default"; 291 JobQueue jq = getJobQueueEntry(); 292 293 if (jq == null) { 294 mailmsg.append("Job '" + getClass().getName() + 295 "' has no job queue entry available\n"); 296 success = false; 297 } else { 298 299 if (StringUtil.notNull(jq.getField(JobQueue.FLD_JOBCRON_PARAMS)).length() == 0) { 303 jq.setField("StatusCode", JobQueue.JOB_STATUS_COMPLETED); 304 jq.update(); 305 dbName = jq.getDataContext(); 306 } else if (this.getCronAlarmEntry() != null 307 && this.getCronAlarmEntry().isIsRepetitive() == false) { 308 jq.setField("StatusCode", JobQueue.JOB_STATUS_COMPLETED); 309 jq.update(); 310 dbName = jq.getDataContext(); 311 } 312 } 313 314 User u = new User(); 315 u.setDataContext(dbName); 316 317 u.setUid(jq.getField("ExpUid")); 318 319 if (!u.find()) { 320 mailmsg.append("Unable to locate user '" + 321 jq.getField("ExpUid") + "' to notify them " + 322 " of job completion in database '" + 323 jq.getDataContext() + "'"); 324 success = false; 325 } 326 327 if (t != null) { 328 log.error("Job " + jq.getField("JobNumber") + 329 " failed in db '" + jq.getDataContext() + "'", t); 330 mailmsg.append("Job '" + getClass().getName() + "' Failed. " + 331 " Job Number :" + getJobNumber() + 332 " from User '" + getUser() + "'"); 333 success = false; 334 } 335 336 if (success) { 337 String isNotify = Setup.getValueUnrequired(getDataContext(), IS_NOTIFY_ON_JOB_SUCCESS); 339 if (StringUtil.toBoolean(isNotify)) { 340 log.debug("Notifying user " + u.getUid() + 341 " that job completed successfully"); 342 mailmsg.append("Job " + jq.getField("JobNumber") + 343 " Completed"); 344 u.notify("Job " + jq.getField("JobNumber") + " Completed", 345 mailmsg.toString()); 346 } 347 } else { 348 new Event(dbName, "SYSERROR", mailmsg.toString(), success); 349 } 350 EventHandler.flush(); 351 } catch (Exception de) { 352 de.printStackTrace(System.err); 353 log.error("Error finishing job:", de); 354 } 355 } 356 357 362 public Hashtable getFunctions() { 363 return (Hashtable ) myFunctions.clone(); 364 } 365 366 371 public String getJobNumber() { 372 return myJobNumber; 373 } 374 375 381 public JobQueue getJobQueueEntry() 382 throws DBException { 383 if (myJobQueue == null) { 384 throw new DBException("Job queue entry not initialized"); 385 } 386 387 return myJobQueue; 388 } 389 390 395 public Hashtable getParameterNamesAndDescriptions() { 396 return (Hashtable ) myParameterNames.clone(); 397 } 398 399 405 public String getParameterDescription(String paramName) { 406 return (String ) myParameterNames.get(paramName); 407 } 408 409 415 public Vector getParamValidValues(String paramCode) { 416 return (Vector ) myParamValidValues.get(paramCode); 417 } 418 419 425 public String getTitle() { 426 return ("No Title"); 427 } 428 429 434 public String getUser() { 435 return myUser; 436 } 437 438 444 public boolean multiThreaded() { 445 return false; 446 } 447 448 451 public void run() { 452 super.run(); 453 } 454 455 462 protected synchronized void sendMail(String subject, String myRecipients, 463 Vector mailMessage) { 464 log.info("Sending e-mail '" + subject + "' to user(s):" + 465 myRecipients); 466 467 Enumeration e = mailMessage.elements(); 468 String bigString = (""); 469 470 while (e.hasMoreElements()) { 471 bigString = bigString + (String ) e.nextElement() + "\n"; 472 } 473 try { 474 String oneRecipient = (""); 475 StringTokenizer stk = new StringTokenizer (myRecipients, ";"); 476 477 while (stk.hasMoreTokens()) { 478 oneRecipient = stk.nextToken(); 479 480 EMailSender ems = new EMailSender(); 481 ems.setDBName(getDataContext()); 482 ems.send(oneRecipient, subject, bigString); 483 } 484 } catch (Exception ie) { 485 log.error("Error sending mail", ie); 486 } 487 } 488 489 495 public synchronized void setQueue(JobQueue newJobQueue) { 496 myJobQueue = newJobQueue; 497 498 try { 499 myUser = myJobQueue.getField("ExpUid"); 500 myJobNumber = myJobQueue.getField("JobNumber"); 501 } catch (DBException de) { 502 myUser = ("Unknown:" + de.getMessage()); 503 myJobNumber = ("Unknown:" + de.getMessage()); 504 } 505 } 506 507 512 protected String getDataContext() throws DBException { 513 return getJobQueueEntry().getDataContext(); 514 } 515 516 525 protected void setSchema(String schemaClass) { 526 StringUtil.assertNotBlank(schemaClass, "Cannot set blank schema"); 527 mySchema = schemaClass; 528 } 529 530 537 protected String getString(String stringCode) { 538 Object [] args = {}; 539 540 return getString(stringCode, args); 541 } 542 543 553 protected String getString(String stringCode, String arg1) { 554 Object [] args = {arg1}; 555 556 return getString(stringCode, args); 557 } 558 559 570 protected String getString(String stringCode, String arg1, String arg2) { 571 Object [] args = {arg1, arg2}; 572 573 return getString(stringCode, args); 574 } 575 576 588 protected String getString(String stringCode, String arg1, String arg2, 589 String arg3) { 590 Object [] args = {arg1, arg2, arg3}; 591 592 return getString(stringCode, args); 593 } 594 595 608 protected String getString(String stringCode, String arg1, String arg2, 609 String arg3, String arg4) { 610 Object [] args = {arg1, arg2, arg3, arg4}; 611 612 return getString(stringCode, args); 613 } 614 615 627 protected String getString(String stringCode, Object [] args) { 628 if (mySchema == null) { 629 setSchema(ExpressoSchema.class.getName()); 630 } 631 try { 632 JobQueue jq = myJobQueue; 633 634 if (jq != null) { 635 return Messages.getStringForUser(getJobQueueEntry().getFieldInt("ExpUid"), 636 getJobQueueEntry().getDataContext(), mySchema, 637 stringCode, args); 638 } else { 639 return Messages.getString(mySchema, stringCode, args); 640 } 641 } catch (DBException de) { 642 log.error(de); 643 644 return "Unable to retrieve string:" + de.getMessage(); 645 } 646 } 647 648 654 protected boolean hasParameters() 655 throws DBException { 656 if (m_jobParamsNum == -1) { 657 JobQueueParam jqp = new JobQueueParam(SecuredDBObject.SYSTEM_ACCOUNT); 658 jqp.setDataContext(getDataContext()); 659 jqp.setField(JobQueueParam.FLD_JOB_NUMBER, getJobNumber()); 660 m_jobParamsEntries = jqp.searchAndRetrieveList(); 661 m_jobParamsNum = m_jobParamsEntries.size(); 662 } 663 664 return (m_jobParamsNum > 0); 665 } 666 667 674 protected List getJobParameterList() 675 throws DBException { 676 List ret = null; 677 678 if (hasParameters()) { 679 ret = m_jobParamsEntries; 680 } 681 682 return ret; 683 } 684 685 693 protected String getJobParameter(String paramCode) 694 throws DBException { 695 if (paramCode == null) { 696 return null; 697 } 698 699 if (this.jobParameterValueMap == null) { 700 if (hasParameters()) { 701 List v = this.getJobParameterList(); 702 jobParameterValueMap = new HashMap (v.size()); 703 704 for (Iterator i = v.iterator(); i.hasNext();) { 706 JobQueueParam param = (JobQueueParam) i.next(); 707 jobParameterValueMap.put(param.getField(JobQueueParam.FLD_PARAM_CODE), 708 param.getField(JobQueueParam.FLD_PARAM_VALUE)); 709 } 710 } else { 711 return null; } 713 } 714 715 return (String ) jobParameterValueMap.get(paramCode); 716 } 717 718 726 protected String getSchema(String schemaClass) 727 throws ServerException { 728 return mySchema; 729 } 730 731 732 739 public synchronized static Job instantiate(String className) 740 throws ServerException { 741 StringUtil.assertNotBlank(className, 742 "Job class name " + 743 " may not be blank or null here"); 744 745 try { 746 Class c = ClassLocator.loadClass(className); 747 748 return (Job) c.newInstance(); 749 } catch (ClassNotFoundException cn) { 750 throw new ServerException("Job object '" + className + 751 "' not found", cn); 752 } catch (InstantiationException ie) { 753 throw new ServerException("Job object '" + className + 754 "' cannot be instantiated", ie); 755 } catch (IllegalAccessException iae) { 756 throw new ServerException("Illegal access loading " + 757 "Job object '" + className + "'", iae); 758 } 759 } 760 761 762 } 763 | Popular Tags |