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 WeeklyTask extends TimerTask {
034:
035: private long wtExecTime = 0;
036: private int wtExecDay = 0;
037: private int wtMaxExecutions = -1;
038: private Timer wtTimer = null;
039: private Runnable wtWorkHorse = null;
040:
041: private void constructTask(Timer timer, Runnable workhorse,
042: int execDay, long execTime) {
043:
044: if (timer == null)
045: throw new IllegalArgumentException("Timer cannot be null.");
046: if (workhorse == null)
047: throw new IllegalArgumentException(
048: "Task workhorse cannot be null.");
049: wtExecTime = execTime;
050: long maxTime = 24 * 3600000L - 1;
051: if (wtExecTime < 0 || wtExecTime > maxTime) {
052: throw new IllegalArgumentException(
053: "Execution time must be between 0 and " + maxTime
054: + ".");
055: }
056: wtExecDay = execDay;
057: if (wtExecDay < 0 || wtExecDay > 6) {
058: throw new IllegalArgumentException(
059: "Execution day must be between 0 and 6.");
060: }
061: wtTimer = timer;
062: wtWorkHorse = workhorse;
063: scheduleExecution();
064: }
065:
066: public WeeklyTask(Timer timer, Runnable workhorse, int execDay,
067: long execTime) {
068: constructTask(timer, workhorse, execDay, execTime);
069: }
070:
071: public WeeklyTask(Timer timer, Runnable workhorse, int execDay,
072: long execTime, int maxExecs) {
073: if (maxExecs == 0)
074: maxExecs = -1;
075: wtMaxExecutions = maxExecs;
076: constructTask(timer, workhorse, execDay, execTime);
077: }
078:
079: private void scheduleExecution() {
080: Calendar cal = Calendar.getInstance();
081: long currentTime = (cal.get(Calendar.HOUR_OF_DAY) * 3600L
082: + cal.get(Calendar.MINUTE) * 60 + cal
083: .get(Calendar.SECOND))
084: * 1000L + cal.get(Calendar.MILLISECOND);
085: int currentDay = cal.get(Calendar.DAY_OF_WEEK) - 1;
086: long currentMillis = currentDay * 24 * 3600000L + currentTime;
087: long scheduledMillis = (wtExecDay - 1) * 24 * 3600000L
088: + wtExecTime;
089: long delay = scheduledMillis - currentMillis;
090: if (delay <= 0)
091: delay += 7 * 24 * 3600000L;
092: wtTimer.schedule(this , delay);
093:
094: }
095:
096: /* (non-Javadoc)
097: * @see java.lang.Runnable#run()
098: */
099: public void run() {
100: wtWorkHorse.run();
101: if (wtMaxExecutions > 0) {
102: if (wtMaxExecutions == 1)
103: return;
104: else
105: wtMaxExecutions--;
106: }
107: WeeklyTask dt = new WeeklyTask(wtTimer, wtWorkHorse, wtExecDay,
108: wtExecTime, wtMaxExecutions);
109:
110: }
111:
112: }
|