|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
TaskExecutor.java | 100% | 100% | 100% | 100% |
|
1 |
// Copyright 2004 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
14 |
|
|
15 |
package com.panorama.startup.impl;
|
|
16 |
|
|
17 |
import java.util.Iterator;
|
|
18 |
import java.util.List;
|
|
19 |
|
|
20 |
import org.apache.commons.logging.Log;
|
|
21 |
import org.apache.hivemind.ErrorHandler;
|
|
22 |
import org.apache.hivemind.Messages;
|
|
23 |
import org.apache.hivemind.order.Orderer;
|
|
24 |
|
|
25 |
/**
|
|
26 |
* A service that executes a series of {@link com.panorama.startup.impl.Task}s. Tasks have
|
|
27 |
* an ordering based on pre- and post-requisites.
|
|
28 |
*
|
|
29 |
* @author Howard Lewis Ship
|
|
30 |
*/
|
|
31 |
public class TaskExecutor implements Runnable |
|
32 |
{ |
|
33 |
private ErrorHandler _errorHandler;
|
|
34 |
private Log _log;
|
|
35 |
private List _tasks;
|
|
36 |
private Messages _messages;
|
|
37 |
|
|
38 |
/**
|
|
39 |
* Orders the {@link #setTasks(List) tasks} into an execution order, and executes
|
|
40 |
* each in turn. Logs the elapsed time, number of tasks, and the number of failures (if any).
|
|
41 |
*/
|
|
42 | 2 |
public void run() |
43 |
{ |
|
44 | 2 |
long startTime = System.currentTimeMillis();
|
45 |
|
|
46 | 2 |
Orderer orderer = new Orderer(_log, _errorHandler, task());
|
47 |
|
|
48 | 2 |
Iterator i = _tasks.iterator(); |
49 | 2 |
while (i.hasNext())
|
50 |
{ |
|
51 | 3 |
Task t = (Task) i.next(); |
52 |
|
|
53 | 3 |
orderer.add(t, t.getId(), t.getAfter(), t.getBefore()); |
54 |
} |
|
55 |
|
|
56 | 2 |
List orderedTasks = orderer.getOrderedObjects(); |
57 |
|
|
58 | 2 |
int failures = 0;
|
59 |
|
|
60 | 2 |
i = orderedTasks.iterator(); |
61 | 2 |
while (i.hasNext())
|
62 |
{ |
|
63 | 3 |
Task t = (Task) i.next(); |
64 |
|
|
65 | 3 |
if (!execute(t))
|
66 | 1 |
failures++; |
67 |
} |
|
68 |
|
|
69 | 2 |
long elapsedTime = System.currentTimeMillis() - startTime;
|
70 |
|
|
71 | 2 |
if (failures == 0)
|
72 | 1 |
_log.info(success(orderedTasks.size(), elapsedTime)); |
73 |
else
|
|
74 | 1 |
_log.info(failure(failures, orderedTasks.size(), elapsedTime)); |
75 |
} |
|
76 |
|
|
77 |
/**
|
|
78 |
* Execute a single task.
|
|
79 |
*
|
|
80 |
* @return true on success, false on failure
|
|
81 |
*/
|
|
82 | 3 |
private boolean execute(Task t) |
83 |
{ |
|
84 | 3 |
_log.info(executingTask(t)); |
85 |
|
|
86 | 3 |
try
|
87 |
{ |
|
88 | 3 |
t.execute(); |
89 |
|
|
90 | 2 |
return true; |
91 |
} |
|
92 |
catch (Exception ex)
|
|
93 |
{ |
|
94 | 1 |
_errorHandler.error(_log, exceptionInTask(t, ex), t.getLocation(), ex); |
95 |
|
|
96 | 1 |
return false; |
97 |
} |
|
98 |
} |
|
99 |
|
|
100 | 2 |
private String task()
|
101 |
{ |
|
102 | 2 |
return _messages.getMessage("task"); |
103 |
} |
|
104 |
|
|
105 | 3 |
private String executingTask(Task t)
|
106 |
{ |
|
107 | 3 |
return _messages.format("executing-task", t.getTitle()); |
108 |
} |
|
109 |
|
|
110 | 1 |
private String exceptionInTask(Task t, Throwable cause)
|
111 |
{ |
|
112 | 1 |
return _messages.format("exception-in-task", t.getTitle(), cause); |
113 |
} |
|
114 |
|
|
115 | 1 |
private String success(int count, long elapsedTimeMillis) |
116 |
{ |
|
117 | 1 |
return _messages.format("success", new Integer(count), new Long(elapsedTimeMillis)); |
118 |
} |
|
119 |
|
|
120 | 1 |
private String failure(int failureCount, int totalCount, long elapsedTimeMillis) |
121 |
{ |
|
122 | 1 |
return _messages.format(
|
123 |
"failure",
|
|
124 |
new Integer(failureCount),
|
|
125 |
new Integer(totalCount),
|
|
126 |
new Long(elapsedTimeMillis));
|
|
127 |
} |
|
128 |
|
|
129 | 2 |
public void setErrorHandler(ErrorHandler handler) |
130 |
{ |
|
131 | 2 |
_errorHandler = handler; |
132 |
} |
|
133 |
|
|
134 | 2 |
public void setLog(Log log) |
135 |
{ |
|
136 | 2 |
_log = log; |
137 |
} |
|
138 |
|
|
139 | 2 |
public void setMessages(Messages messages) |
140 |
{ |
|
141 | 2 |
_messages = messages; |
142 |
} |
|
143 |
|
|
144 | 2 |
public void setTasks(List list) |
145 |
{ |
|
146 | 2 |
_tasks = list; |
147 |
} |
|
148 |
|
|
149 |
} |
|
150 |
|
|