// --- 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.Hashtable; import java.util.Vector; import com.netscape.certsrv.apps.CMS; import com.netscape.certsrv.base.EBaseException; import com.netscape.certsrv.base.IConfigStore; import com.netscape.certsrv.base.ISubsystem; import com.netscape.certsrv.jobs.EJobsException; import com.netscape.certsrv.jobs.IJob; import com.netscape.certsrv.jobs.IJobCron; import com.netscape.certsrv.jobs.IJobsScheduler; import com.netscape.certsrv.jobs.JobPlugin; import com.netscape.certsrv.logging.ILogger; import com.netscape.cmscore.util.Debug; /** * This is a daemon thread that handles scheduled jobs like cron would * do with different jobs. This daemon wakes up at a pre-configured * interval to see * if there is any job to be done, if so, a thread is created to execute * the job(s). *
* The interval jobsScheduler.interval in the configuration is specified as number of minutes. If not set, the
* default is 1 minute. Note that the cron specification for each job CAN NOT be finer than the granularity of the
* Scheduler daemon interval. For example, if the daemon interval is set to 5 minute, a job cron for every minute at 7am
* on each Tuesday (e.g. * 7 * * 2) will result in the execution of the job thread only once every 5 minutes during that
* hour. The inteval value is recommended at 1 minute, setting it otherwise has the potential of forever missing the
* beat. Use with caution.
*
* @author cfu
* @see JobCron
* @version $Revision$, $Date$
*/
public class JobsScheduler implements Runnable, IJobsScheduler {
protected static final long MINUTE_MILLI = 60000;
protected static final String DELIM = ",";
/**
* Scheduler thread doing job scheduling
*/
protected String mId = ID;
protected Thread mScheduleThread = null;
public Hashtable
* the config params have the following formats: jobScheduler.impl.[implementation name].class=[package name]
* jobScheduler.job.[job name].pluginName=[implementation name] jobScheduler.job.[job name].cron=[crontab format]
* jobScheduler.job.[job name].[any job specific params]=[values]
*
* @param config jobsScheduler configStore
*/
public void init(ISubsystem owner, IConfigStore config)
throws EBaseException, EJobsException {
mLogger = CMS.getLogger();
// read in config parameters and set variables
mConfig = config;
// getting/setting interval
int i;
try {
i = mConfig.getInteger(PROP_INTERVAL);
} catch (Exception e) {
i = 1; // default 1 minute
}
setInterval(i);
IConfigStore c = mConfig.getSubStore(PROP_IMPL);
Enumeration
* Use with caution. Should not do it when sharing with others
*
* @param id name to be applied to an Jobs Scheduler subsystem
*/
public void setId(String id) throws EBaseException {
mId = id;
}
/**
* creates and starts the daemon thread
*/
public void startDaemon() {
mScheduleThread = new Thread(this, "JobScheduler");
log(ILogger.LL_INFO, "started Jobs Scheduler daemon thread");
mScheduleThread.setDaemon(true);
mScheduleThread.start();
}
/**
* registers the administration servlet with the administration subsystem.
*/
public void startup() throws EBaseException {
//remove, already logged from S_ADMIN
//String infoMsg = "Jobs Scheduler subsystem administration Servlet registered";
//log(ILogger.LL_INFO, infoMsg);
}
/**
* shuts down Jobs one by one.
*
*/
public void shutdown() {
for (IJob job : mJobs.values()) {
job.stop();
}
}
/**
* Returns the root configuration storage of this system.
*
*
* @return configuration store of this subsystem
*/
public IConfigStore getConfigStore() {
return mConfig;
}
/**
* Gets configuration parameters for the given
* job plugin.
*
* @param implName Name of the job plugin.
* @return Hashtable of required parameters.
*/
public String[] getConfigParams(String implName)
throws EJobsException {
if (Debug.ON)
Debug.trace("in getCofigParams()");
// is this a registered implname?
JobPlugin plugin = mJobPlugins.get(implName);
if (plugin == null) {
log(ILogger.LL_FAILURE,
CMS.getLogMessage("CMSCORE_JOBS_CLASS_NOT_FOUND", implName));
if (Debug.ON)
Debug.trace("Job plugin " + implName + " not found.");
throw new EJobsException(CMS.getUserMessage("CMS_JOB_PLUGIN_NOT_FOUND",
implName));
}
// XXX can find an instance of this plugin in existing
// auth manager instantces to avoid instantiation just for this.
// a temporary instance
String className = plugin.getClassPath();
if (Debug.ON)
Debug.trace("className = " + className);
try {
IJob jobInst = (IJob)
Class.forName(className).newInstance();
if (Debug.ON)
Debug.trace("class instantiated");
return (jobInst.getConfigParams());
} catch (InstantiationException e) {
log(ILogger.LL_FAILURE,
CMS.getLogMessage("CMSCORE_JOBS_CREATE_NEW", e.toString()));
if (Debug.ON)
Debug.trace("class NOT instantiated: " + e);
throw new EJobsException(CMS.getUserMessage("CMS_JOB_LOAD_CLASS_FAILED", className));
} catch (ClassNotFoundException e) {
log(ILogger.LL_FAILURE,
CMS.getLogMessage("CMSCORE_JOBS_CREATE_NEW", e.toString()));
if (Debug.ON)
Debug.trace("class NOT instantiated: " + e);
throw new EJobsException(CMS.getUserMessage("CMS_JOB_LOAD_CLASS_FAILED", className));
} catch (IllegalAccessException e) {
log(ILogger.LL_FAILURE,
CMS.getLogMessage("CMSCORE_JOBS_CREATE_NEW", e.toString()));
if (Debug.ON)
Debug.trace("class NOT instantiated: " + e);
throw new EJobsException(CMS.getUserMessage("CMS_JOB_LOAD_CLASS_FAILED", className));
}
}
public void setInterval(int minutes) {
mInterval = minutes * MINUTE_MILLI;
}
/**
* logs an entry in the log file.
*/
public void log(int level, String msg) {
if (mLogger == null)
return;
mLogger.log(ILogger.EV_SYSTEM, ILogger.S_OTHER,
level, msg);
}
public Hashtable