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 MonthlyTask extends TimerTask {
034:
035: private long mtExecTime = 0;
036: private int mtExecDay = 0;
037: private int mtMaxExecutions = -1;
038: private Timer mtTimer = null;
039: private Runnable mtWorkHorse = 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: mtExecTime = execTime;
050: long maxTime = 24 * 3600000L - 1;
051: if (mtExecTime < 0 || mtExecTime > maxTime) {
052: throw new IllegalArgumentException(
053: "Execution time must be between 0 and " + maxTime
054: + ".");
055: }
056: mtExecDay = execDay;
057: if (mtExecDay < 1 || mtExecDay > 31) {
058: throw new IllegalArgumentException(
059: "Execution day of the month must be between 1 and 31.");
060: }
061: mtTimer = timer;
062: mtWorkHorse = workhorse;
063: scheduleExecution();
064: }
065:
066: public MonthlyTask(Timer timer, Runnable workhorse, int execDay,
067: long execTime) {
068: constructTask(timer, workhorse, execDay, execTime);
069: }
070:
071: public MonthlyTask(Timer timer, Runnable workhorse, int execDay,
072: long execTime, int maxExecs) {
073: if (maxExecs == 0)
074: maxExecs = -1;
075: mtMaxExecutions = 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_MONTH) - 1;
086: long currentMillis = currentDay * 24 * 3600000L + currentTime;
087: int maxDayInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
088: int execDay = mtExecDay > maxDayInMonth ? maxDayInMonth
089: : mtExecDay;
090: long scheduledMillis = (execDay - 1) * 24 * 3600000L
091: + mtExecTime;
092: long delay = scheduledMillis - currentMillis;
093: if (delay <= 0)
094: delay += (maxDayInMonth * 24 * 3600000L);
095: mtTimer.schedule(this , delay);
096:
097: }
098:
099: /* (non-Javadoc)
100: * @see java.lang.Runnable#run()
101: */
102: public void run() {
103: mtWorkHorse.run();
104: if (mtMaxExecutions > 0) {
105: if (mtMaxExecutions == 1)
106: return;
107: else
108: mtMaxExecutions--;
109: }
110: MonthlyTask dt = new MonthlyTask(mtTimer, mtWorkHorse,
111: mtExecDay, mtExecTime, mtMaxExecutions);
112:
113: }
114:
115: }
|