01: /*
02:
03: * <copyright>
04: *
05: * Copyright 2002-2007 BBNT Solutions, LLC
06: * under sponsorship of the Defense Advanced Research Projects
07: * Agency (DARPA).
08: *
09: * You can redistribute this software and/or modify it under the
10: * terms of the Cougaar Open Source License as published on the
11: * Cougaar Open Source Website (www.cougaar.org).
12: *
13: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
15: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
16: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
17: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24: *
25: * </copyright>
26:
27: */
28:
29: package org.cougaar.qos.qrs;
30:
31: import java.util.Timer;
32: import java.util.TimerTask;
33:
34: import org.cougaar.util.log.Logger;
35:
36: class TaskSchedulerImpl extends Timer implements TaskScheduler {
37: private static class Task extends TimerTask {
38: Runnable body;
39:
40: Task(Runnable body) {
41: this .body = body;
42: }
43:
44: public void run() {
45: try {
46: body.run();
47: } catch (Throwable t) {
48: Logger logger = Logging
49: .getLogger(TaskSchedulerImpl.class);
50: logger.error("Task threw an Exception", t);
51: }
52: }
53: }
54:
55: private TimerTask makeTask(Runnable body) {
56: return new Task(body);
57: }
58:
59: public Object schedule(Runnable body, long delay, long period) {
60: TimerTask task = makeTask(body);
61: try {
62: super .schedule(task, delay, period);
63: } catch (Exception ex) {
64: Logger logger = Logging.getLogger(TaskSchedulerImpl.class);
65: logger.error("Unable to schedule task", ex);
66: return null;
67: }
68: return task;
69: }
70:
71: public Object schedule(Runnable body, long delay) {
72: TimerTask task = makeTask(body);
73: try {
74: super .schedule(task, delay);
75: } catch (Exception ex) {
76: Logger logger = Logging.getLogger(TaskSchedulerImpl.class);
77: logger.error("Unable to schedule task", ex);
78: return null;
79:
80: }
81: return task;
82: }
83:
84: public void unschedule(Object task) {
85: if (task != null) {
86: ((TimerTask) task).cancel();
87: }
88: }
89:
90: public void cancel() {
91: try {
92: throw new RuntimeException();
93: } catch (Exception ex) {
94: Logger logger = Logging.getLogger(TaskSchedulerImpl.class);
95: logger.error("TaskScheduler cancelled", ex);
96: }
97: }
98: }
|