001: /*
002: * The contents of this file are subject to the Sapient Public License
003: * Version 1.0 (the "License"); you may not use this file except in compliance
004: * with the License. You may obtain a copy of the License at
005: * http://carbon.sf.net/License.html.
006: *
007: * Software distributed under the License is distributed on an "AS IS" basis,
008: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
009: * the specific language governing rights and limitations under the License.
010: *
011: * The Original Code is The Carbon Component Framework.
012: *
013: * The Initial Developer of the Original Code is Sapient Corporation
014: *
015: * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
016: */
017:
018: package org.sape.carbon.services.scheduler;
019:
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Iterator;
023: import java.util.Timer;
024:
025: import org.sape.carbon.core.component.ComponentConfiguration;
026: import org.sape.carbon.core.component.lifecycle.Configurable;
027: import org.sape.carbon.core.component.lifecycle.Startable;
028: import org.sape.carbon.core.component.lifecycle.Suspendable;
029:
030: import org.sape.carbon.services.threadpool.ThreadPool;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034:
035: /**
036: * Default implementation for the SchedulerService. This implementation
037: * uses java.util.Timer to schedule tasks. When this service is stopped
038: * or suspended, all tasks are canceled. When this service is started or
039: * resumed, all configured tasks are scheduled.
040: *
041: * @see java.util.Timer
042: *
043: * Copyright 2002 Sapient
044: * @since carbon 1.0
045: * @author Douglas Voet, June 2002
046: * @version $Revision: 1.12 $($Author: dvoet $ / $Date: 2003/11/20 21:46:15 $)
047: */
048: public class DefaultSchedulerServiceImpl implements SchedulerService,
049: Configurable, Startable, Suspendable {
050:
051: /**
052: * Provides a handle to Apache-commons logger
053: */
054: private Log log = LogFactory.getLog(this .getClass());
055:
056: /** Holds the timer object that will execute the tasks. */
057: private Timer internalTimer;
058:
059: /** Holds the collection of scheduled tasks for this service. */
060: private Collection scheduledTasks = new ArrayList();
061:
062: /**
063: * @see Configurable#configure(ComponentConfiguration)
064: */
065: public void configure(ComponentConfiguration configuration) {
066: SchedulerServiceConfiguration schedulerConfiguration = (SchedulerServiceConfiguration) configuration;
067:
068: ThreadPool threadPool = schedulerConfiguration.getThreadPool();
069:
070: scheduledTasks.clear();
071:
072: FixedRateTaskConfiguration[] fixedRateTasks = schedulerConfiguration
073: .getFixedRateTask();
074: for (int i = 0; i < fixedRateTasks.length; i++) {
075: scheduledTasks.add(new FixedRateTask(fixedRateTasks[i],
076: threadPool));
077: }
078:
079: FixedDelayTaskConfiguration[] fixedDelayTasks = schedulerConfiguration
080: .getFixedDelayTask();
081: for (int i = 0; i < fixedDelayTasks.length; i++) {
082: scheduledTasks.add(new FixedDelayTask(fixedDelayTasks[i],
083: threadPool));
084: }
085: }
086:
087: /**
088: * @see Startable#start()
089: */
090: public void start() {
091: scheduleAllTasks();
092: }
093:
094: /**
095: * @see Startable#stop()
096: */
097: public void stop() {
098: if (log.isTraceEnabled()) {
099: log.trace("stopping scheduler");
100: }
101: internalTimer.cancel();
102: }
103:
104: /**
105: * @see Suspendable#resume()
106: */
107: public void resume() {
108: scheduleAllTasks();
109: }
110:
111: /**
112: * @see Suspendable#suspend()
113: */
114: public void suspend() {
115: internalTimer.cancel();
116: }
117:
118: /**
119: * Iterates over tasks and schedules them.
120: */
121: private void scheduleAllTasks() {
122: // create a new timer and make it a daemon so it won't keep the
123: // system from shutting down
124: this .internalTimer = new Timer(true);
125:
126: Iterator taskIterator = this .scheduledTasks.iterator();
127:
128: while (taskIterator.hasNext()) {
129: AbstractTask task = (AbstractTask) taskIterator.next();
130: task.schedule(this.internalTimer);
131: }
132: }
133: }
|