KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > engine > AbstractTask


1 package org.jacorb.notification.engine;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1999-2004 Gerald Brose
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */

23
24 import org.jacorb.notification.util.AbstractPoolable;
25
26 /**
27  * @author Alphonse Bendt
28  * @version $Id: AbstractTask.java,v 1.15 2005/04/27 10:48:40 alphonse.bendt Exp $
29  */

30
31 public abstract class AbstractTask extends AbstractPoolable implements Runnable JavaDoc, Schedulable
32 {
33     private TaskExecutor taskExecutor_;
34
35     ////////////////////
36

37     protected TaskExecutor getTaskExecutor()
38     {
39         return taskExecutor_;
40     }
41
42     protected void setTaskExecutor(TaskExecutor taskExecutor)
43     {
44         taskExecutor_ = taskExecutor;
45     }
46
47     /**
48      * Override this Method in Subclasses to do the "real work".
49      */

50     public abstract void doWork() throws Exception JavaDoc;
51
52     protected boolean isRunnable()
53     {
54         return true;
55     }
56     
57     /**
58      * template method.
59      * <ol>
60      * <li>Call doWork()
61      * </ol>
62      */

63     public void run()
64     {
65         try
66         {
67             if (isRunnable())
68             {
69                 doWork();
70             }
71         } catch (Throwable JavaDoc t)
72         {
73             handleTaskError(this, t);
74         } finally
75         {
76             dispose();
77         }
78     }
79
80     abstract void handleTaskError(AbstractTask t, Throwable JavaDoc error);
81
82     
83     protected void checkInterrupt() throws InterruptedException JavaDoc
84     {
85         if (Thread.currentThread().isInterrupted())
86         {
87             throw new InterruptedException JavaDoc();
88         }
89     }
90
91     /**
92      * Run this Task on its configured Executor.
93      *
94      * @param directRunAllowed
95      * this param specified if its allowed to run this Task on the calling Thread.
96      * @exception InterruptedException
97      * if an error occurs
98      */

99     protected void schedule(boolean directRunAllowed) throws InterruptedException JavaDoc
100     {
101         schedule(taskExecutor_, directRunAllowed);
102     }
103
104     /**
105      * Run this Task on the provided Executor.
106      *
107      * @param executor
108      * a <code>TaskExecutor</code> value
109      * @param directRunAllowed
110      * a <code>boolean</code> value
111      * @exception InterruptedException
112      * if an error occurs
113      */

114     protected void schedule(TaskExecutor executor, boolean directRunAllowed)
115             throws InterruptedException JavaDoc
116     {
117         if (directRunAllowed)
118         {
119             run();
120         }
121         else
122         {
123             executor.execute(this);
124         }
125     }
126 }
Popular Tags