001: /*
002: * Copyright 2007 Hippo
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS"
012: * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package nl.hippo.cms.background;
017:
018: import org.apache.avalon.framework.CascadingException;
019: import org.apache.avalon.framework.activity.Initializable;
020: import org.apache.avalon.framework.parameters.Parameterizable;
021: import org.apache.avalon.framework.service.ServiceException;
022: import org.apache.avalon.framework.service.ServiceManager;
023: import org.apache.avalon.framework.service.Serviceable;
024: import org.apache.avalon.framework.thread.ThreadSafe;
025: import org.apache.cocoon.components.cron.JobScheduler;
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028:
029: /**
030: * @author <a href="mailto:d.dam@hippo.nl">Dennis Dam</a>
031: *
032: * @version $Id: AbstractBackgroundTaskManager.java 8978 2007-11-19 09:57:26Z jstuyts $
033: */
034: public abstract class AbstractBackgroundTaskManager implements
035: Serviceable, Parameterizable, Initializable, ThreadSafe {
036:
037: private final Log logger = LogFactory
038: .getLog(AbstractBackgroundTaskManager.class);
039: private ServiceManager manager;
040: private JobScheduler scheduler;
041: private int interval = -1;
042:
043: // cron expression, e.g. 0 */5 * * * ? * to run every 5 minutes
044: // see http://quartz.sourceforge.net/javadoc/org/quartz/CronTrigger.html for more explanation
045: private String cron = null;
046:
047: public AbstractBackgroundTaskManager() {
048: }
049:
050: public abstract Runnable getTaskRunnable();
051:
052: public abstract boolean isBackgroundTaskActive();
053:
054: public void service(ServiceManager manager) throws ServiceException {
055: this .manager = manager;
056: this .scheduler = (JobScheduler) this .manager
057: .lookup(JobScheduler.ROLE);
058: }
059:
060: public void initialize() {
061: if (getCron() != null) {
062: try {
063: scheduler.addJob(this .getClass().getName(),
064: getTaskRunnable(), getCron(), false);
065: } catch (CascadingException ex) {
066: logger.error("Could not start cron job", ex);
067: }
068: } else {
069: if (getInterval() > 0 && isBackgroundTaskActive()) {
070: try {
071: scheduler.addPeriodicJob(this .getClass().getName(),
072: getTaskRunnable(), getInterval(), false,
073: null, null);
074: } catch (CascadingException ex) {
075: logger.error("Could not start periodic job", ex);
076: }
077: }
078: }
079: }
080:
081: public ServiceManager getManager() {
082: return manager;
083: }
084:
085: public JobScheduler getScheduler() {
086: return scheduler;
087: }
088:
089: public int getInterval() {
090: return interval;
091: }
092:
093: public void setInterval(int interval) {
094: this .interval = interval;
095: }
096:
097: public String getCron() {
098: return cron;
099: }
100:
101: public void setCron(String cron) {
102: this.cron = cron;
103: }
104:
105: }
|