KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.jacorb.notification.interfaces.FilterStage;
29
30 /**
31  * @author Alphonse Bendt
32  * @version $Id: AbstractFilterTask.java,v 1.14 2005/04/27 10:48:40 alphonse.bendt Exp $
33  */

34 public abstract class AbstractFilterTask extends AbstractMessageTask
35 {
36     private final TaskFactory taskFactory_;
37
38     /**
39      * for debugging purpose.
40      */

41     private static final boolean STRICT_CHECKING = true;
42
43     /**
44      * empty default value for field arrayCurrentFilterStage_. its used instead of null
45      */

46     protected static final FilterStage[] EMPTY_FILTERSTAGE = new FilterStage[0];
47
48     /**
49      * FilterStages to process.
50      */

51     protected FilterStage[] arrayCurrentFilterStage_;
52
53     /**
54      * child FilterStages for which evaluation was successful. these Stages are to be eval'd by the
55      * next Task. As each Task is processed by one Thread at a time unsynchronized ArrayList can be
56      * used here.
57      */

58     private final List JavaDoc listOfFilterStageToBeProcessed_ = new ArrayList JavaDoc();
59
60     ////////////////////
61

62     protected AbstractFilterTask(TaskFactory taskFactory, TaskExecutor taskExecutor)
63     {
64         setTaskExecutor(taskExecutor);
65
66         taskFactory_ = taskFactory;
67
68         arrayCurrentFilterStage_ = EMPTY_FILTERSTAGE;
69     }
70
71     ////////////////////
72

73     public final void doWork() throws InterruptedException JavaDoc
74     {
75         if (arrayCurrentFilterStage_.length > 0)
76         {
77             doFilter();
78         }
79     }
80
81     protected abstract void doFilter() throws InterruptedException JavaDoc;
82
83     protected TaskFactory getTaskFactory()
84     {
85         return taskFactory_;
86     }
87
88     protected boolean isFilterStageListEmpty()
89     {
90         return listOfFilterStageToBeProcessed_.isEmpty();
91     }
92
93     protected void addFilterStage(FilterStage s)
94     {
95         listOfFilterStageToBeProcessed_.add(s);
96     }
97
98     protected void addFilterStage(List JavaDoc s)
99     {
100         if (STRICT_CHECKING)
101         {
102             Iterator JavaDoc i = s.iterator();
103
104             while (i.hasNext())
105             {
106                 if (!(i.next() instanceof FilterStage))
107                 {
108                     throw new IllegalArgumentException JavaDoc();
109                 }
110             }
111         }
112         listOfFilterStageToBeProcessed_.addAll(s);
113     }
114
115     /**
116      * set the FilterStages for the next run.
117      */

118     public void setCurrentFilterStage(FilterStage[] currentFilterStage)
119     {
120         arrayCurrentFilterStage_ = currentFilterStage;
121     }
122
123     /**
124      * get the matching FilterStages of the previous run.
125      */

126     public FilterStage[] getFilterStageToBeProcessed()
127     {
128         return (FilterStage[]) listOfFilterStageToBeProcessed_.toArray(EMPTY_FILTERSTAGE);
129     }
130
131     /**
132      * clear the result of the previous run.
133      */

134     public void clearFilterStageToBeProcessed()
135     {
136         listOfFilterStageToBeProcessed_.clear();
137     }
138
139     public synchronized void reset()
140     {
141         super.reset();
142
143         clearFilterStageToBeProcessed();
144         arrayCurrentFilterStage_ = EMPTY_FILTERSTAGE;
145     }
146
147     public void handleTaskError(AbstractTask task, Throwable JavaDoc error)
148     {
149         logger_.fatalError("Error while Filtering in Task:" + task, error);
150     }
151
152     /**
153      * Schedule this Task on its default Executor for execution.
154      */

155     public void schedule() throws InterruptedException JavaDoc
156     {
157         // as all FilterTasks share their Executor, queuing of this
158
// Task can be avoided if there are no other Tasks to run.
159
// in this case this Task will be run immediately.
160
schedule(!getTaskExecutor().isTaskQueued());
161     }
162 }
Popular Tags