001: /***
002: * Retrotranslator: a Java bytecode transformer that translates Java classes
003: * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
004: *
005: * Copyright (c) 2005 - 2008 Taras Puchko
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * 3. Neither the name of the copyright holders nor the names of its
017: * contributors may be used to endorse or promote products derived from
018: * this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
024: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
025: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
026: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
027: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
028: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
029: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
030: * THE POSSIBILITY OF SUCH DAMAGE.
031: */package net.sf.retrotranslator.runtime.java.util;
032:
033: import java.util.concurrent.*;
034: import net.sf.retrotranslator.registry.Advanced;
035:
036: /**
037: * @author Taras Puchko
038: */
039: @Advanced("Timer.All")
040: public abstract class TimerTask_ implements Runnable {
041:
042: final TimerCommand command = new TimerCommand();
043:
044: protected TimerTask_() {
045: }
046:
047: public abstract void run();
048:
049: public boolean cancel() {
050: return command.cancel();
051: }
052:
053: public long scheduledExecutionTime() {
054: return command.getExecutionTime();
055: }
056:
057: class TimerCommand implements Runnable {
058:
059: private boolean periodic;
060: private boolean cancelled;
061: private long executionTime;
062: private ScheduledFuture future;
063: private ScheduledThreadPoolExecutor executor;
064:
065: public void run() {
066: try {
067: saveExecutionTime();
068: TimerTask_.this .run();
069: } catch (ThreadDeath e) {
070: shutdownExecutor();
071: throw e;
072: }
073: }
074:
075: private synchronized void saveExecutionTime() {
076: executionTime = System.currentTimeMillis()
077: + future.getDelay(TimeUnit.MILLISECONDS);
078: }
079:
080: private synchronized void shutdownExecutor() {
081: executor
082: .setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
083: executor
084: .setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
085: executor.shutdown();
086: }
087:
088: synchronized long getExecutionTime() {
089: return executionTime;
090: }
091:
092: synchronized boolean cancel() {
093: cancelled = true;
094: if (future == null) {
095: return false;
096: }
097: if (!periodic
098: && future.getDelay(TimeUnit.MILLISECONDS) <= 0) {
099: future.cancel(false);
100: return false;
101: }
102: return future.cancel(false);
103: }
104:
105: synchronized void runOnce(ScheduledThreadPoolExecutor executor,
106: long delay) {
107: check();
108: init(false, executor, executor.schedule(this , delay,
109: TimeUnit.MILLISECONDS));
110: }
111:
112: synchronized void runWithFixedDelay(
113: ScheduledThreadPoolExecutor executor, long delay,
114: long period) {
115: check();
116: init(true, executor, executor.scheduleWithFixedDelay(this ,
117: delay, period, TimeUnit.MILLISECONDS));
118: }
119:
120: synchronized void runAtFixedRate(
121: ScheduledThreadPoolExecutor executor, long delay,
122: long period) {
123: check();
124: init(true, executor, executor.scheduleAtFixedRate(this ,
125: delay, period, TimeUnit.MILLISECONDS));
126: }
127:
128: private void check() {
129: if (cancelled || executor != null) {
130: throw new IllegalStateException();
131: }
132: }
133:
134: private void init(boolean periodic,
135: ScheduledThreadPoolExecutor executor,
136: ScheduledFuture future) {
137: this.periodic = periodic;
138: this.executor = executor;
139: this.future = future;
140: }
141: }
142:
143: }
|