KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > task > TaskSequence


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: TaskSequence.java 42598 2004-03-01 16:18:28Z gregor $ */
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 /**
29  * A TaskSequence contains of multiple tasks that are executed successively.
30  */

31 public class TaskSequence extends AbstractTask {
32     private static Category log = Category.getInstance(TaskSequence.class);
33
34     // keeps the task order
35
private Task[] tasks;
36     private TaskManager taskManager;
37
38     /**
39      * DOCUMENT ME!
40      *
41      * @param configuration DOCUMENT ME!
42      *
43      * @throws ConfigurationException DOCUMENT ME!
44      */

45     public void init(Configuration configuration) throws ConfigurationException {
46         taskManager = new TaskManager();
47         taskManager.configure(configuration);
48
49         // create task list
50
Configuration[] taskConfigurations = configuration.getChildren(TaskManager.TASK_ELEMENT);
51         tasks = new Task[taskConfigurations.length];
52
53         // set task IDs
54
for (int i = 0; i < tasks.length; i++) {
55             String JavaDoc 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     /**
68      * Returns the tasks in this sequence.
69      *
70      * @return DOCUMENT ME!
71      */

72     public Task[] getTasks() {
73         return (Task[]) tasks.clone();
74     }
75
76     /**
77      * Returns the TaskManager that is used to manage the tasks of this TaskSequence.
78      *
79      * @return DOCUMENT ME!
80      */

81     protected TaskManager getTaskManager() {
82         return taskManager;
83     }
84
85     /**
86      * Returns the ID of a specific Task.
87      *
88      * @param task the specific task for which the task id is requested.
89      *
90      * @return the task id of the given task
91      *
92      * @throws ExecutionException if the task could not be found.
93      */

94     public String JavaDoc getTaskId(Task task) throws ExecutionException {
95         String JavaDoc[] 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 JavaDoc("Task-ID for " + task + " not found!");
104     }
105
106     /**
107      * Executes the tasks.
108      *
109      * @param path DOCUMENT ME!
110      *
111      * @throws ExecutionException if the execution fails
112      */

113     public void execute(String JavaDoc 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 JavaDoc taskId = getTaskId(task);
120                 log.debug("Executing task '" + taskId + "'");
121
122                 // create task parameters
123
Parameters taskParameters = new Parameters();
124                 String JavaDoc[] names = getParameters().getNames();
125
126                 for (int parIndex = 0; parIndex < names.length; parIndex++) {
127                     String JavaDoc name = names[parIndex];
128                     boolean useParameter = true;
129
130                     if (useParameter) {
131                         taskParameters.setParameter(name, getParameters().getParameter(name));
132                     }
133                 }
134
135                 // execute task
136
task.parameterize(taskParameters);
137                 task.execute(path);
138             }
139         } catch (Exception JavaDoc e) {
140             log.error("Cannot execute TaskSequence: ", e);
141         }
142     }
143 }
144
Popular Tags