001: /*
002: * (C) Copyright 2005 Nabh Information Systems, Inc.
003: *
004: * All copyright notices regarding Nabh's products MUST remain
005: * intact in the scripts and in the outputted HTML.
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021: package com.nabhinc.util.scheduling;
022:
023: import java.util.Calendar;
024: import java.util.Timer;
025: import java.util.TimerTask;
026:
027: /**
028: *
029: *
030: * @author Padmanabh Dabke
031: * (c) 2005 Nabh Information Systems, Inc. All Rights Reserved.
032: */
033: public class DailyTask extends TimerTask {
034:
035: private long dntExecTime = 0;
036: private int dntMaxExecutions = -1;
037: private Timer dntTimer = null;
038: private Runnable dntWorkHorse = null;
039:
040: private void constructTask(Timer timer, Runnable workhorse,
041: long execTime) {
042: if (timer == null)
043: throw new IllegalArgumentException("Timer cannot be null.");
044: if (workhorse == null)
045: throw new IllegalArgumentException(
046: "Task workhorse cannot be null.");
047: dntExecTime = execTime;
048: long maxTime = 24 * 3600000L - 1;
049: if (dntExecTime < 0 || dntExecTime > maxTime) {
050: throw new IllegalArgumentException(
051: "Execution time must be between 0 and " + maxTime
052: + ".");
053: }
054: dntTimer = timer;
055: dntWorkHorse = workhorse;
056: scheduleExecution();
057: }
058:
059: public DailyTask(Timer timer, Runnable workhorse, long execTime) {
060: constructTask(timer, workhorse, execTime);
061: }
062:
063: public DailyTask(Timer timer, Runnable workhorse, long execTime,
064: int maxExecs) {
065: if (maxExecs == 0)
066: maxExecs = -1;
067: dntMaxExecutions = maxExecs;
068: constructTask(timer, workhorse, execTime);
069:
070: }
071:
072: private void scheduleExecution() {
073: Calendar cal = Calendar.getInstance();
074: long currentTime = (cal.get(Calendar.HOUR_OF_DAY) * 3600L
075: + cal.get(Calendar.MINUTE) * 60 + cal
076: .get(Calendar.SECOND))
077: * 1000L + cal.get(Calendar.MILLISECOND);
078: long delay = dntExecTime - currentTime;
079: if (delay <= 0)
080: delay += 24 * 3600000L;
081: dntTimer.schedule(this , delay);
082:
083: }
084:
085: /* (non-Javadoc)
086: * @see java.lang.Runnable#run()
087: */
088: public void run() {
089: dntWorkHorse.run();
090: if (dntMaxExecutions > 0) {
091: if (dntMaxExecutions == 1)
092: return;
093: else
094: dntMaxExecutions--;
095: }
096: DailyTask dt = new DailyTask(dntTimer, dntWorkHorse,
097: dntExecTime, dntMaxExecutions);
098:
099: }
100:
101: }
|