KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > processor > TaskProcessor


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2006 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: TaskProcessor.java 154 6 oct. 06 ofabre $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.processor;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.objectweb.petals.util.LoggingUtil;
30
31 /**
32  * The TaskProcessor executes an ordered List of Tasks : it calls the <i>execute</i>
33  * method on each Task. It ensures the system state recovery in case of failure
34  * during the Tasks processing : it will call the <i>undo</i> method on each
35  * allready executed Task before the whole process crash.
36  *
37  * @author ofabre - EBM Websourcing
38  *
39  */

40 public class TaskProcessor {
41
42     protected List JavaDoc<Task> taskList;
43
44     protected HashMap JavaDoc<String JavaDoc, Object JavaDoc> context;
45
46     protected LoggingUtil log;
47
48     public TaskProcessor(HashMap JavaDoc<String JavaDoc, Object JavaDoc> context, LoggingUtil log) {
49         super();
50         this.taskList = new ArrayList JavaDoc<Task>();
51         this.context = context;
52         this.log = log;
53     }
54
55     /**
56      * Add a new Task in the List of Tasks to execute. Task will be processed in
57      * the same order as they have been added to the List.
58      *
59      * @param task
60      * a new Task to process
61      */

62     public void addTask(Task task) {
63         taskList.add(task);
64     }
65
66     /**
67      * Process each Task previously added to the Processor, in the same order as
68      * they have been added to it. If an exception occurs during the processing
69      * of a Task, the processor will undo this Task and all previously executed
70      * Tasks, in the reverse order as they have been executed.
71      *
72      * @return true if the processor managed to process all tasks, false
73      * otherwise
74      *
75      */

76     public boolean process() {
77         boolean result = true;
78         for (int i = 0; i < taskList.size() && result == true; i++) {
79             Task task = taskList.get(i);
80             try {
81                 task.execute(context);
82             } catch (Throwable JavaDoc e) {
83                 result = false;
84                 log.error("Task processing failed.", e);
85                 for (int j = i - 1; j >= 0; j--) {
86                     Task taskToUndo = taskList.get(j);
87                     try {
88                         taskToUndo.undo(context);
89                     } catch (Exception JavaDoc e1) {
90                         log.error("Error during task unprocessing", e1);
91                     }
92                 }
93             }
94         }
95         return result;
96     }
97
98     /**
99      * Unprocess each Task previously added to the Processor, in the reversed
100      * order they have been added to it. If an error occurs during a Task
101      * unprocessing, the processor skips it and continues to unprocess other
102      * Tasks.
103      *
104      * @return true if the processor managed to unprocess all Tasks, false
105      * otherwise
106      */

107     public boolean unprocess() {
108         boolean result = true;
109
110         List JavaDoc<Task> reversedTasks = new ArrayList JavaDoc<Task>(taskList);
111         Collections.reverse(reversedTasks);
112
113         for (Task taskToUndo : reversedTasks) {
114             try {
115                 taskToUndo.undo(context);
116             } catch (Exception JavaDoc e) {
117                 result = false;
118                 log.error("Error during task unprocessing", e);
119             }
120         }
121
122         return result;
123     }
124 }
125
Popular Tags