001: // Copyright 2004, 2005 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.examples.panorama.startup.impl;
016:
017: import java.util.Iterator;
018: import java.util.List;
019:
020: import org.apache.commons.logging.Log;
021: import org.apache.hivemind.ErrorLog;
022: import org.apache.hivemind.Messages;
023: import org.apache.hivemind.order.Orderer;
024:
025: /**
026: * A service that executes a series of {@link org.apache.examples.panorama.startup.impl.Task}s. Tasks have an
027: * ordering based on pre- and post-requisites.
028: *
029: * @author Howard Lewis Ship
030: */
031: public class TaskExecutor implements Runnable {
032: private Log _log;
033:
034: private ErrorLog _errorLog;
035:
036: private List _tasks;
037:
038: private Messages _messages;
039:
040: /**
041: * Orders the {@link #setTasks(List) tasks} into an execution order, and executes each in turn.
042: * Logs the elapsed time, number of tasks, and the number of failures (if any).
043: */
044: public void run() {
045: long startTime = System.currentTimeMillis();
046:
047: Orderer orderer = new Orderer(_errorLog, task());
048:
049: Iterator i = _tasks.iterator();
050: while (i.hasNext()) {
051: Task t = (Task) i.next();
052:
053: orderer.add(t, t.getId(), t.getAfter(), t.getBefore());
054: }
055:
056: List orderedTasks = orderer.getOrderedObjects();
057:
058: int failures = 0;
059:
060: i = orderedTasks.iterator();
061: while (i.hasNext()) {
062: Task t = (Task) i.next();
063:
064: if (!execute(t))
065: failures++;
066: }
067:
068: long elapsedTime = System.currentTimeMillis() - startTime;
069:
070: if (failures == 0)
071: _log.info(success(orderedTasks.size(), elapsedTime));
072: else
073: _log.info(failure(failures, orderedTasks.size(),
074: elapsedTime));
075: }
076:
077: /**
078: * Execute a single task.
079: *
080: * @return true on success, false on failure
081: */
082: private boolean execute(Task t) {
083: _log.info(executingTask(t));
084:
085: try {
086: t.execute();
087:
088: return true;
089: } catch (Exception ex) {
090: _errorLog
091: .error(exceptionInTask(t, ex), t.getLocation(), ex);
092:
093: return false;
094: }
095: }
096:
097: private String task() {
098: return _messages.getMessage("task");
099: }
100:
101: private String executingTask(Task t) {
102: return _messages.format("executing-task", t.getTitle());
103: }
104:
105: private String exceptionInTask(Task t, Throwable cause) {
106: return _messages.format("exception-in-task", t.getTitle(),
107: cause);
108: }
109:
110: private String success(int count, long elapsedTimeMillis) {
111: return _messages.format("success", new Integer(count),
112: new Long(elapsedTimeMillis));
113: }
114:
115: private String failure(int failureCount, int totalCount,
116: long elapsedTimeMillis) {
117: return _messages.format("failure", new Integer(failureCount),
118: new Integer(totalCount), new Long(elapsedTimeMillis));
119: }
120:
121: public void setLog(Log log) {
122: _log = log;
123: }
124:
125: public void setErrorLog(ErrorLog errorLog) {
126: _errorLog = errorLog;
127: }
128:
129: public void setMessages(Messages messages) {
130: _messages = messages;
131: }
132:
133: public void setTasks(List list) {
134: _tasks = list;
135: }
136:
137: }
|