001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: /* $Id: TaskSequence.java 473861 2006-11-12 03:51:14Z gregor $ */
020:
021: package org.apache.lenya.cms.task;
022:
023: import org.apache.avalon.framework.configuration.Configuration;
024: import org.apache.avalon.framework.configuration.ConfigurationException;
025: import org.apache.avalon.framework.parameters.ParameterException;
026: import org.apache.avalon.framework.parameters.Parameters;
027: import org.apache.log4j.Logger;
028:
029: /**
030: * A TaskSequence contains of multiple tasks that are executed successively.
031: * @deprecated Use the usecase framework instead.
032: */
033: public class TaskSequence extends AbstractTask {
034: private static Logger log = Logger.getLogger(TaskSequence.class);
035:
036: // keeps the task order
037: private Task[] tasks;
038: private TaskManager taskManager;
039:
040: /**
041: * Initialize the task
042: * @param configuration The configuration
043: * @throws ConfigurationException if an error occurs
044: */
045: public void init(Configuration configuration)
046: throws ConfigurationException {
047: this .taskManager = new TaskManager(getServiceManager());
048: this .taskManager.configure(configuration);
049:
050: // create task list
051: Configuration[] taskConfigurations = configuration
052: .getChildren(TaskManager.TASK_ELEMENT);
053: this .tasks = new Task[taskConfigurations.length];
054:
055: // set task IDs
056: for (int i = 0; i < this .tasks.length; i++) {
057: String taskId = taskConfigurations[i]
058: .getAttribute(TaskManager.TASK_ID_ATTRIBUTE);
059:
060: try {
061: this .tasks[i] = this .taskManager.getTask(taskId);
062: } catch (ExecutionException e) {
063: throw new ConfigurationException(
064: "Sequence initialization failed: ", e);
065: }
066:
067: log.debug("Adding task '" + taskId + "' to sequence.");
068: }
069: }
070:
071: /**
072: * Returns the tasks in this sequence.
073: * @return The tasks
074: */
075: public Task[] getTasks() {
076: return (Task[]) this .tasks.clone();
077: }
078:
079: /**
080: * Returns the TaskManager that is used to manage the tasks of this TaskSequence.
081: * @return The task manager
082: */
083: protected TaskManager getTaskManager() {
084: return this .taskManager;
085: }
086:
087: /**
088: * Returns the ID of a specific Task.
089: * @param task the specific task for which the task id is requested.
090: * @return the task id of the given task
091: * @throws ExecutionException if the task could not be found.
092: */
093: public String getTaskId(Task task) throws ExecutionException {
094: String[] taskIds = getTaskManager().getTaskIds();
095:
096: for (int j = 0; j < taskIds.length; j++) {
097: if (getTaskManager().getTask(taskIds[j]) == task) {
098: return taskIds[j];
099: }
100: }
101:
102: throw new IllegalStateException("Task-ID for " + task
103: + " not found!");
104: }
105:
106: /**
107: * Executes the tasks.
108: * @param path The path to the tasks
109: * @throws ExecutionException if the execution fails
110: */
111: public void execute(String path) throws ExecutionException {
112:
113: try {
114: Task[] _tasks = getTasks();
115:
116: for (int i = 0; i < _tasks.length; i++) {
117: Task task = _tasks[i];
118: String taskId = getTaskId(task);
119: log.debug("Executing task '" + taskId + "'");
120:
121: // create task parameters
122: Parameters taskParameters = new Parameters();
123: String[] names = getParameters().getNames();
124:
125: for (int parIndex = 0; parIndex < names.length; parIndex++) {
126: String name = names[parIndex];
127: boolean useParameter = true;
128:
129: if (useParameter) {
130: taskParameters.setParameter(name,
131: getParameters().getParameter(name));
132: }
133: }
134:
135: // execute task
136: task.parameterize(taskParameters);
137: task.execute(path);
138: }
139: } catch (final IllegalStateException e) {
140: log.error("Cannot execute TaskSequence: ", e);
141: throw new ExecutionException(e);
142: } catch (final ParameterException e) {
143: log.error("Cannot execute TaskSequence: ", e);
144: throw new ExecutionException(e);
145: } catch (final ExecutionException e) {
146: log.error("Cannot execute TaskSequence: ", e);
147: throw new ExecutionException(e);
148: }
149: }
150: }
|