001: package org.jacorb.notification.engine;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 1999-2004 Gerald Brose
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: import org.jacorb.notification.util.AbstractPoolable;
025:
026: /**
027: * @author Alphonse Bendt
028: * @version $Id: AbstractTask.java,v 1.17 2005/10/02 15:18:39 alphonse.bendt Exp $
029: */
030:
031: public abstract class AbstractTask extends AbstractPoolable implements
032: Runnable, Schedulable {
033: private TaskExecutor taskExecutor_;
034:
035: protected TaskExecutor getTaskExecutor() {
036: return taskExecutor_;
037: }
038:
039: protected void setTaskExecutor(TaskExecutor taskExecutor) {
040: taskExecutor_ = taskExecutor;
041: }
042:
043: /**
044: * Override this Method in Subclasses to do the "real work".
045: */
046: public abstract void doWork() throws Exception;
047:
048: protected boolean isRunnable() {
049: return true;
050: }
051:
052: /**
053: * run method invoked by TaskExecutor.
054: */
055: public void run() {
056: try {
057: if (isRunnable()) {
058: doWork();
059: }
060: } catch (Exception e) {
061: handleTaskError(this , e);
062: } finally {
063: dispose();
064: }
065: }
066:
067: /**
068: * error handler method that will be invoked if an exception occurs during doWork.
069: *
070: * @param task the task that caused the error.
071: * @param error the exception that was thrown.
072: */
073: abstract void handleTaskError(AbstractTask task, Exception error);
074:
075: protected void checkInterrupt() throws InterruptedException {
076: if (Thread.currentThread().isInterrupted()) {
077: throw new InterruptedException();
078: }
079: }
080:
081: /**
082: * schedule this Task for execution.
083: *
084: * @param directRunAllowed
085: * true, if the task may be run in the calling thread. false, if the TaskExecutor
086: * should be used.
087: */
088: protected void schedule(boolean directRunAllowed) {
089: schedule(taskExecutor_, directRunAllowed);
090: }
091:
092: /**
093: * schedule this Task for execution.
094: *
095: * @param executor
096: * TaskExecutor that should execute this Task
097: *
098: * @param directRunAllowed
099: * true, if the task may be run in the calling thread. false, if the TaskExecutor
100: * should be used.
101: */
102: protected void schedule(TaskExecutor executor,
103: boolean directRunAllowed) {
104: if (directRunAllowed) {
105: run();
106: } else {
107: executor.execute(this);
108: }
109: }
110: }
|