01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19:
20: package org.apache.axis2.deployment.scheduler;
21:
22: import java.util.Date;
23: import java.util.Timer;
24: import java.util.TimerTask;
25:
26: public class Scheduler {
27: private final Timer timer = new Timer(true);
28:
29: private void reschedule(SchedulerTask schedulerTask,
30: DeploymentIterator iterator) {
31: Date time = iterator.next();
32:
33: if (time == null) {
34: schedulerTask.cancel();
35: } else {
36: synchronized (schedulerTask.lock) {
37: if (schedulerTask.state != SchedulerTask.CANCELLED) {
38: schedulerTask.timerTask = new SchedulerTimerTask(
39: schedulerTask, iterator);
40: timer.schedule(schedulerTask.timerTask, time);
41: }
42: }
43: }
44: }
45:
46: /**
47: * Schedules the specified task for execution according to the specified schedule.
48: * If times specified by the <code>ScheduleIterator</code> are in the past they are
49: * scheduled for immediate execution.
50: *
51: * @param schedulerTask task to be scheduled
52: * @param iterator iterator that describes the schedule
53: * @throws IllegalStateException if task was already scheduled or cancelled,
54: * scheduler was cancelled, or scheduler thread terminated.
55: */
56: public void schedule(SchedulerTask schedulerTask,
57: DeploymentIterator iterator) {
58: Date time = iterator.next();
59:
60: if (time == null) {
61: schedulerTask.cancel();
62: } else {
63: synchronized (schedulerTask.lock) {
64: schedulerTask.state = SchedulerTask.SCHEDULED;
65: schedulerTask.timerTask = new SchedulerTimerTask(
66: schedulerTask, iterator);
67: timer.schedule(schedulerTask.timerTask, time);
68: }
69: }
70: }
71:
72: public void cleanup() {
73: timer.cancel();
74: }
75:
76: public class SchedulerTimerTask extends TimerTask {
77: private DeploymentIterator iterator;
78: private SchedulerTask schedulerTask;
79:
80: public SchedulerTimerTask(SchedulerTask schedulerTask,
81: DeploymentIterator iterator) {
82: this .schedulerTask = schedulerTask;
83: this .iterator = iterator;
84: }
85:
86: public void run() {
87: schedulerTask.run();
88: reschedule(schedulerTask, iterator);
89: }
90: }
91: }
|