01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.servicemix.components.varscheduler;
18:
19: import java.util.TimerTask;
20:
21: /**
22: * A task run by a {@link Scheduler}.
23: *
24: * @author George Gastaldi (gastaldi)
25: */
26: public abstract class SchedulerTask implements Runnable {
27:
28: static final int VIRGIN = 0;
29:
30: static final int SCHEDULED = 1;
31:
32: static final int CANCELLED = 2;
33:
34: final Object lock = new Object();
35:
36: int state = VIRGIN;
37:
38: TimerTask timerTask;
39:
40: protected SchedulerTask() {
41: }
42:
43: public abstract void run();
44:
45: /**
46: * Cancels task.
47: *
48: * @return true if task already scheduled
49: */
50: public boolean cancel() {
51: synchronized (lock) {
52: if (timerTask != null) {
53: timerTask.cancel();
54: }
55: boolean result = state == SCHEDULED;
56: state = CANCELLED;
57: return result;
58: }
59: }
60:
61: public long scheduledExecutionTime() {
62: synchronized (lock) {
63: return timerTask == null ? 0 : timerTask
64: .scheduledExecutionTime();
65: }
66: }
67:
68: }
|