1 22 package org.objectweb.petals.processor; 23 24 import java.util.ArrayList ; 25 import java.util.Collections ; 26 import java.util.HashMap ; 27 import java.util.List ; 28 29 import org.objectweb.petals.util.LoggingUtil; 30 31 40 public class TaskProcessor { 41 42 protected List <Task> taskList; 43 44 protected HashMap <String , Object > context; 45 46 protected LoggingUtil log; 47 48 public TaskProcessor(HashMap <String , Object > context, LoggingUtil log) { 49 super(); 50 this.taskList = new ArrayList <Task>(); 51 this.context = context; 52 this.log = log; 53 } 54 55 62 public void addTask(Task task) { 63 taskList.add(task); 64 } 65 66 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 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 e1) { 90 log.error("Error during task unprocessing", e1); 91 } 92 } 93 } 94 } 95 return result; 96 } 97 98 107 public boolean unprocess() { 108 boolean result = true; 109 110 List <Task> reversedTasks = new ArrayList <Task>(taskList); 111 Collections.reverse(reversedTasks); 112 113 for (Task taskToUndo : reversedTasks) { 114 try { 115 taskToUndo.undo(context); 116 } catch (Exception e) { 117 result = false; 118 log.error("Error during task unprocessing", e); 119 } 120 } 121 122 return result; 123 } 124 } 125 | Popular Tags |