// --- BEGIN COPYRIGHT BLOCK --- // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; version 2 of the License. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License along // with this program; if not, write to the Free Software Foundation, Inc., // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. // // (C) 2007 Red Hat, Inc. // All rights reserved. // --- END COPYRIGHT BLOCK --- package com.netscape.cmscore.jobs; import java.util.Calendar; import java.util.Enumeration; import java.util.StringTokenizer; import java.util.Vector; import com.netscape.certsrv.apps.CMS; import com.netscape.certsrv.base.EBaseException; import com.netscape.certsrv.jobs.IJobCron; import com.netscape.certsrv.logging.ILogger; /** * class representing one Job cron information *
* here, an "item" refers to one of the 5 fields in a cron string; "element" refers to any comma-deliminated element in * an "item"...which includes both numbers and '-' separated ranges. A cron string in the configuration takes the * following format: minute (0-59), hour (0-23), day of the month (1-31), month of the year (1-12), day of the week * (0-6 with 0=Sunday) *
* e.g. jobsScheduler.job.rnJob1.cron=30 11,23 * * 1-5 In this example, the job "rnJob1" will be executed from Monday * through Friday, at 11:30am and 11:30pm. *
*
* @author cfu
* @version $Revision$, $Date$
*/
public class JobCron implements IJobCron {
/**
* CRON_MINUTE, CRON_HOUR, CRON_DAY_OF_MONTH, CRON_MONTH_OF_YEAR,
* and CRON_DAY_OF_WEEK are to be used in getItem() to
* retrieve the corresponding CronItem
*/
public static final String CRON_MINUTE = "minute";
public static final String CRON_HOUR = "hour";
public static final String CRON_DAY_OF_MONTH = "dom";
public static final String CRON_MONTH_OF_YEAR = "moy";
public static final String CRON_DAY_OF_WEEK = "dow";
private ILogger mLogger = CMS.getLogger();
String mCronString = null;
CronItem cMinute = null;
CronItem cHour = null;
CronItem cDOM = null;
CronItem cMOY = null;
CronItem cDOW = null;
public JobCron(String cronString)
throws EBaseException {
mCronString = cronString;
// create all 5 items in the cron
cMinute = new CronItem(0, 59);
cHour = new CronItem(0, 23);
cDOM = new CronItem(1, 31);
cMOY = new CronItem(1, 12);
cDOW = new CronItem(0, 6); // 0=Sunday
cronToVals(mCronString);
}
private void cronToVals(String cronString)
throws EBaseException {
StringTokenizer st = new StringTokenizer(cronString);
String sMinute = null;
String sHour = null;
String sDayOMonth = null;
String sMonthOYear = null;
String sDayOWeek = null;
try {
if (st.hasMoreTokens()) {
sMinute = st.nextToken();
cMinute.set(sMinute);
}
} catch (EBaseException e) {
log(ILogger.LL_FAILURE,
CMS.getLogMessage("CMSCORE_JOBS_INVALID_MIN", e.toString()));
throw new EBaseException(CMS.getUserMessage("CMS_BASE_INVALID_JOB_CRON"));
}
try {
if (st.hasMoreTokens()) {
sHour = st.nextToken();
cHour.set(sHour);
}
} catch (EBaseException e) {
log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_JOBS_INVALID_HOUR", e.toString()));
throw new EBaseException(CMS.getUserMessage("CMS_BASE_INVALID_JOB_CRON"));
}
if (st.hasMoreTokens()) {
sDayOMonth = st.nextToken();
// cDOM.set(sDayOMonth);
}
try {
if (st.hasMoreTokens()) {
sMonthOYear = st.nextToken();
cMOY.set(sMonthOYear);
}
} catch (EBaseException e) {
log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_JOBS_INVALID_MONTH", e.toString()));
throw new EBaseException(CMS.getUserMessage("CMS_BASE_INVALID_JOB_CRON"));
}
if (st.hasMoreTokens()) {
sDayOWeek = st.nextToken();
// cDOW.set(sDayOWeek);
}
/**
* day-of-month or day-of-week, or both?
* if only one of them is '*', the non '*' one prevails,
* the '*' one will remain empty (no elements)
*/
// day-of-week
if ((sDayOMonth != null)
&& sDayOMonth.equals(CronItem.ALL) && (sDayOWeek != null) && !sDayOWeek.equals(CronItem.ALL)) {
try {
cDOW.set(sDayOWeek);
} catch (EBaseException e) {
log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_JOBS_INVALID_DAY_OF_WEEK", e.toString()));
throw new EBaseException(CMS.getUserMessage("CMS_BASE_INVALID_JOB_CRON"));
}
} else if ((sDayOMonth != null)
&& !sDayOMonth.equals(CronItem.ALL) && (sDayOWeek != null) && sDayOWeek.equals(CronItem.ALL)) {
try {
cDOM.set(sDayOMonth);
} catch (EBaseException e) {
log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_JOBS_INVALID_DAY_OF_MONTH", e.toString()));
throw new EBaseException(CMS.getUserMessage("CMS_BASE_INVALID_JOB_CRON"));
}
} else { // if both '*', every day, if neither is '*', do both
try {
if (sDayOWeek != null) {
cDOW.set(sDayOWeek);
}
} catch (EBaseException e) {
log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_JOBS_INVALID_DAY_OF_WEEK", e.toString()));
throw new EBaseException(CMS.getUserMessage("CMS_BASE_INVALID_JOB_CRON"));
}
try {
if (sDayOMonth != null) {
cDOM.set(sDayOMonth);
}
} catch (EBaseException e) {
log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_JOBS_INVALID_DAY_OF_MONTH", e.toString()));
throw new EBaseException(CMS.getUserMessage("CMS_BASE_INVALID_JOB_CRON"));
}
}
}
/**
* retrieves the cron item
*
* @param item name of the item. must be one of the CRON_*
* strings defined in this class
* @return an instance of the CronItem class which represents the
* requested cron item
*/
public CronItem getItem(String item) {
if (item.equals(CRON_MINUTE)) {
return cMinute;
} else if (item.equals(CRON_HOUR)) {
return cHour;
} else if (item.equals(CRON_DAY_OF_MONTH)) {
return cDOM;
} else if (item.equals(CRON_MONTH_OF_YEAR)) {
return cMOY;
} else if (item.equals(CRON_DAY_OF_WEEK)) {
return cDOW;
} else {
// throw...
}
return null;
}
/**
* Does the element fit any element in the item
*
* @param element the element of "now" in cron format
* @param item the item consists of a vector of elements
* @return boolean (true/false) on whether the element is one of
* the elements in the item
*/
boolean isElement(int element, Vector