1 64 package com.jcorporate.expresso.services.crontab; 65 66 import org.apache.log4j.Logger; 67 68 import java.util.Calendar ; 69 import java.util.Date ; 70 71 72 84 public class CrontabEntry implements Comparable , java.io.Serializable { 85 private static final Logger log = Logger.getLogger(CrontabEntry.class); 86 87 90 public static final int TIME_UNUSED = -1; 91 92 95 protected static long globalCounter = 0; 96 97 100 protected static Object counterLock = new Object (); 101 102 105 protected transient CrontabListenerI listener; 106 107 110 protected String label = ""; 111 112 115 protected boolean isRelative; 116 117 120 protected boolean isRepetitive; 121 122 125 protected int dayOfMonth = TIME_UNUSED; 126 127 130 protected int dayOfWeek = TIME_UNUSED; 131 132 135 protected int hour = TIME_UNUSED; 136 137 140 protected int minute = TIME_UNUSED; 141 142 145 protected int month = TIME_UNUSED; 146 147 150 protected int year = TIME_UNUSED; 151 152 155 protected long alarmTime; 156 157 160 protected long counterValue; 161 162 165 protected String jobNumber = null; 166 167 174 public CrontabEntry(Date date, CrontabListenerI listener) 175 throws CronException { 176 this.listener = listener; 177 178 setCounterValue(); 179 180 Calendar alarm = Calendar.getInstance(); 181 alarm.setTime(date); 182 minute = alarm.get(Calendar.MINUTE); 183 hour = alarm.get(Calendar.HOUR_OF_DAY); 184 dayOfMonth = alarm.get(Calendar.DAY_OF_MONTH); 185 month = alarm.get(Calendar.MONTH); 186 year = alarm.get(Calendar.YEAR); 187 isRepetitive = false; 188 isRelative = false; 189 alarmTime = date.getTime(); 190 checkEntryTime(); 191 192 if (log.isDebugEnabled()) { 193 log.debug("Constructed new crontab entry: " + this.toString()); 194 } 195 } 196 197 206 public CrontabEntry(int delay, boolean isRepetitive, CrontabListenerI listener) { 207 if (delay < 1) { 208 throw new IllegalArgumentException ("Parameter delay must be >= 1"); 209 } 210 211 setCounterValue(); 212 213 minute = delay; 214 this.listener = listener; 215 this.isRepetitive = isRepetitive; 216 217 isRelative = true; 218 updateEntryTime(); 219 } 220 221 234 public CrontabEntry(int minute, int hour, int dayOfMonth, int month, 235 int dayOfWeek, int year, CrontabListenerI listener) 236 throws CronException { 237 setCounterValue(); 238 239 this.minute = minute; 240 this.hour = hour; 241 this.dayOfMonth = dayOfMonth; 242 this.month = month; 243 this.dayOfWeek = dayOfWeek; 244 this.year = year; 245 this.listener = listener; 246 this.label = "Unlabelled Crontab Entry"; 247 isRepetitive = (year == TIME_UNUSED); 248 isRelative = false; 249 updateEntryTime(); 250 checkEntryTime(); 251 252 if (log.isDebugEnabled()) { 253 log.debug("Constructed new crontab entry: " + this.toString()); 254 } 255 } 256 257 271 public CrontabEntry(int minute, int hour, int dayOfMonth, int month, 272 int dayOfWeek, int year, String jobLabel, CrontabListenerI listener) 273 throws CronException { 274 setCounterValue(); 275 this.minute = minute; 276 this.hour = hour; 277 this.dayOfMonth = dayOfMonth; 278 this.month = month; 279 this.dayOfWeek = dayOfWeek; 280 this.year = year; 281 this.listener = listener; 282 this.label = jobLabel; 283 isRepetitive = (year == TIME_UNUSED); 284 isRelative = false; 285 updateEntryTime(); 286 checkEntryTime(); 287 288 if (log.isDebugEnabled()) { 289 log.debug("Constructed new crontab entry: " + this.toString()); 290 } 291 } 292 293 298 public long getAlarmTime() { 299 return alarmTime; 300 } 301 302 307 public long getCounter() { 308 return this.counterValue; 309 } 310 311 316 public int getDayOfMonth() { 317 return dayOfMonth; 318 } 319 320 325 public int getDayOfWeek() { 326 return dayOfWeek; 327 } 328 329 334 public int getHour() { 335 return hour; 336 } 337 338 343 public boolean isIsRelative() { 344 return isRelative; 345 } 346 347 352 public boolean isIsRepetitive() { 353 return isRepetitive; 354 } 355 356 361 public String getLabel() { 362 return label; 363 } 364 365 370 public CrontabListenerI getListener() { 371 return this.listener; 372 } 373 374 379 public int getMinute() { 380 return minute; 381 } 382 383 388 public int getMonth() { 389 return month; 390 } 391 392 397 public int getYear() { 398 return year; 399 } 400 401 413 public synchronized int compareTo(Object obj) { 414 CrontabEntry entry = (CrontabEntry) obj; 415 416 if (alarmTime < entry.alarmTime) { 417 return -1; 418 } else if (alarmTime > entry.alarmTime) { 419 return 1; 420 } else { 421 return (new Long (counterValue)).compareTo(new Long (entry.counterValue)); 422 } 423 } 424 425 432 public synchronized boolean equals(Object obj) { 433 try { 434 CrontabEntry entry = (CrontabEntry) obj; 435 436 if ((alarmTime == entry.alarmTime) && (listener == entry.listener)) { 437 return true; 438 } 439 } catch (ClassCastException cce) { 440 log.error("Class cast exception comparing objects", cce); 441 } 442 443 return false; 444 } 445 446 447 452 public synchronized String toString() { 453 if (year != TIME_UNUSED) { 454 return "Cron Entry at " + new Date (alarmTime); 455 } 456 457 StringBuffer sb = new StringBuffer ("CronTabEntry params"); 458 sb.append(" minute="); 459 sb.append(minute); 460 sb.append(" hour="); 461 sb.append(hour); 462 sb.append(" dayOfMonth="); 463 sb.append(dayOfMonth); 464 sb.append(" month="); 465 sb.append(month); 466 sb.append(" dayOfWeek="); 467 sb.append(dayOfWeek); 468 sb.append(" (next alarm date=" + new Date (alarmTime) + ")"); 469 470 return sb.toString(); 471 } 472 473 477 public synchronized void updateEntryTime() { 478 if (isRelative) { 479 alarmTime = System.currentTimeMillis() + (minute * 60000); 480 481 return; 482 } 483 484 Calendar now = Calendar.getInstance(); 485 Calendar alarm = (Calendar ) now.clone(); 486 487 if (log.isDebugEnabled()) { 488 log.debug("now: " + now.getTime()); 489 } 490 491 if (year > TIME_UNUSED) { 492 alarm.set(Calendar.YEAR, year); 493 } 494 495 if (month > TIME_UNUSED) { 496 alarm.set(Calendar.MONTH, month); 497 } 498 499 if (hour > TIME_UNUSED) { 500 alarm.set(Calendar.HOUR_OF_DAY, hour); 501 } 502 503 if (minute > TIME_UNUSED) { 504 alarm.set(Calendar.MINUTE, minute); 505 } 506 507 alarm.set(Calendar.SECOND, 0); 508 509 adjustExecutionTime(now, alarm); 511 512 if (log.isDebugEnabled()) { 513 log.debug("alarm: " + alarm.getTime()); 514 } 515 516 alarmTime = alarm.getTime().getTime(); 517 } 518 519 522 protected void setCounterValue() { 523 synchronized (counterLock) { 528 counterValue = globalCounter; 529 globalCounter++; 530 } 531 } 532 533 541 protected synchronized void adjustExecutionTime(Calendar now, Calendar alarm) { 542 if (minute == TIME_UNUSED) { 544 if ((hour > TIME_UNUSED && now.get(Calendar.HOUR_OF_DAY) != hour) || 546 (dayOfMonth > TIME_UNUSED && now.get(Calendar.DAY_OF_MONTH) != dayOfMonth) || 547 (dayOfWeek > TIME_UNUSED && now.get(Calendar.DAY_OF_WEEK) != dayOfWeek)) { 548 alarm.set(Calendar.MINUTE, 0); 551 } else if ((hour <= TIME_UNUSED) && (now.get(Calendar.MINUTE) >= minute)) { 552 alarm.add(Calendar.MINUTE, 1); 554 } else if ((hour > TIME_UNUSED) && (now.get(Calendar.MINUTE) >= minute) && (now.get(Calendar.MINUTE) == 59)) { 555 alarm.roll(Calendar.MINUTE, true); 557 alarm.roll(Calendar.DAY_OF_YEAR, true); 558 } else if (now.get(Calendar.MINUTE) >= minute) { 559 alarm.roll(Calendar.MINUTE, true); 561 } 562 } 563 564 if (minute != TIME_UNUSED && hour == TIME_UNUSED) { 566 if ((dayOfMonth > TIME_UNUSED && now.get(Calendar.DAY_OF_MONTH) != dayOfMonth) || 568 (dayOfWeek > TIME_UNUSED && now.get(Calendar.DAY_OF_WEEK) != dayOfWeek)) { 569 alarm.set(Calendar.HOUR_OF_DAY, 0); 571 } else if ((dayOfMonth <= TIME_UNUSED) && (dayOfWeek <= TIME_UNUSED) && (now.get(Calendar.MINUTE) >= minute)) { 572 alarm.add(Calendar.HOUR_OF_DAY, 1); 574 } else if (now.get(Calendar.MINUTE) >= minute) { 575 alarm.roll(Calendar.HOUR_OF_DAY, true); 577 } 578 } 579 580 588 597 if ((dayOfMonth <= TIME_UNUSED) && (dayOfWeek <= TIME_UNUSED) && 598 (((hour > TIME_UNUSED) && now.get(Calendar.HOUR_OF_DAY) > hour) || 599 ((minute > TIME_UNUSED) && (now.get(Calendar.HOUR_OF_DAY) == hour) && 600 (now.get(Calendar.MINUTE) >= minute)))) { 601 alarm.roll(Calendar.DAY_OF_YEAR, true); 602 } 603 604 if ((month != TIME_UNUSED) && (year == TIME_UNUSED) && 606 ((now.get(Calendar.MONTH) > month) || 607 ((now.get(Calendar.MONTH) == month) && 608 ((now.get(Calendar.DAY_OF_MONTH) > dayOfMonth) || 609 ((now.get(Calendar.DAY_OF_MONTH) == dayOfMonth) && 610 ((now.get(Calendar.HOUR_OF_DAY) > hour) || 611 ((now.get(Calendar.HOUR_OF_DAY) == hour) && 612 (now.get(Calendar.MINUTE) >= minute)))))))) { 613 alarm.add(Calendar.YEAR, 1); 614 } 615 616 if (dayOfWeek != TIME_UNUSED) { 618 int deltaOfDay = (7 + (dayOfWeek - now.get(Calendar.DAY_OF_WEEK))) % 7; 619 620 if (log.isDebugEnabled()) { 621 log.debug("deltaOfDay: " + deltaOfDay); 622 } 623 624 if (deltaOfDay != 0) { 625 alarm.add(Calendar.DAY_OF_YEAR, deltaOfDay); 626 } else if ((now.get(Calendar.HOUR_OF_DAY) > hour) || 627 ((now.get(Calendar.HOUR_OF_DAY) == hour) && 628 (now.get(Calendar.MINUTE) >= minute))) { alarm.add(Calendar.WEEK_OF_YEAR, 1); 630 } 631 } else if (dayOfMonth != TIME_UNUSED) { alarm.set(Calendar.DAY_OF_MONTH, dayOfMonth); 633 634 if ((month == TIME_UNUSED) && 636 ((now.get(Calendar.DAY_OF_MONTH) > dayOfMonth) || 637 ((now.get(Calendar.DAY_OF_MONTH) == dayOfMonth) && 638 ((now.get(Calendar.HOUR_OF_DAY) > hour) || 639 ((now.get(Calendar.HOUR_OF_DAY) == hour) && 640 (now.get(Calendar.MINUTE) >= minute)))))) { 641 alarm.roll(Calendar.MONTH, true); 642 } 643 } 644 } 645 646 651 void checkEntryTime() throws CronException { 652 long delay = alarmTime - System.currentTimeMillis(); 653 654 if (delay <= 1000) { 655 throw new CronException(); 656 } 657 } 658 659 664 public String getJobNumber() { 665 return jobNumber; 666 } 667 668 673 public void setJobNumber(String s) { 674 jobNumber = s; 675 } 676 677 } 678 | Popular Tags |