1 17 18 19 20 package org.apache.lenya.cms.task; 21 22 import org.apache.avalon.framework.configuration.Configuration; 23 import org.apache.avalon.framework.configuration.ConfigurationException; 24 import org.apache.avalon.framework.parameters.Parameters; 25 import org.apache.log4j.Category; 26 27 28 31 public class TaskSequence extends AbstractTask { 32 private static Category log = Category.getInstance(TaskSequence.class); 33 34 private Task[] tasks; 36 private TaskManager taskManager; 37 38 45 public void init(Configuration configuration) throws ConfigurationException { 46 taskManager = new TaskManager(); 47 taskManager.configure(configuration); 48 49 Configuration[] taskConfigurations = configuration.getChildren(TaskManager.TASK_ELEMENT); 51 tasks = new Task[taskConfigurations.length]; 52 53 for (int i = 0; i < tasks.length; i++) { 55 String taskId = taskConfigurations[i].getAttribute(TaskManager.TASK_ID_ATTRIBUTE); 56 57 try { 58 tasks[i] = taskManager.getTask(taskId); 59 } catch (ExecutionException e) { 60 throw new ConfigurationException("Sequence initialization failed: ", e); 61 } 62 63 log.debug("Adding task '" + taskId + "' to sequence."); 64 } 65 } 66 67 72 public Task[] getTasks() { 73 return (Task[]) tasks.clone(); 74 } 75 76 81 protected TaskManager getTaskManager() { 82 return taskManager; 83 } 84 85 94 public String getTaskId(Task task) throws ExecutionException { 95 String [] taskIds = getTaskManager().getTaskIds(); 96 97 for (int j = 0; j < taskIds.length; j++) { 98 if (getTaskManager().getTask(taskIds[j]) == task) { 99 return taskIds[j]; 100 } 101 } 102 103 throw new IllegalStateException ("Task-ID for " + task + " not found!"); 104 } 105 106 113 public void execute(String path) throws ExecutionException { 114 try { 115 Task[] tasks = getTasks(); 116 117 for (int i = 0; i < tasks.length; i++) { 118 Task task = tasks[i]; 119 String taskId = getTaskId(task); 120 log.debug("Executing task '" + taskId + "'"); 121 122 Parameters taskParameters = new Parameters(); 124 String [] names = getParameters().getNames(); 125 126 for (int parIndex = 0; parIndex < names.length; parIndex++) { 127 String name = names[parIndex]; 128 boolean useParameter = true; 129 130 if (useParameter) { 131 taskParameters.setParameter(name, getParameters().getParameter(name)); 132 } 133 } 134 135 task.parameterize(taskParameters); 137 task.execute(path); 138 } 139 } catch (Exception e) { 140 log.error("Cannot execute TaskSequence: ", e); 141 } 142 } 143 } 144 | Popular Tags |