1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.examples.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.ErrorLog; |
22 |
| import org.apache.hivemind.Messages; |
23 |
| import org.apache.hivemind.order.Orderer; |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| public class TaskExecutor implements Runnable |
32 |
| { |
33 |
| private Log _log; |
34 |
| |
35 |
| private ErrorLog _errorLog; |
36 |
| |
37 |
| private List _tasks; |
38 |
| |
39 |
| private Messages _messages; |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
2
| public void run()
|
46 |
| { |
47 |
2
| long startTime = System.currentTimeMillis();
|
48 |
| |
49 |
2
| Orderer orderer = new Orderer(_errorLog, task());
|
50 |
| |
51 |
2
| Iterator i = _tasks.iterator();
|
52 |
2
| while (i.hasNext())
|
53 |
| { |
54 |
3
| Task t = (Task) i.next();
|
55 |
| |
56 |
3
| orderer.add(t, t.getId(), t.getAfter(), t.getBefore());
|
57 |
| } |
58 |
| |
59 |
2
| List orderedTasks = orderer.getOrderedObjects();
|
60 |
| |
61 |
2
| int failures = 0;
|
62 |
| |
63 |
2
| i = orderedTasks.iterator();
|
64 |
2
| while (i.hasNext())
|
65 |
| { |
66 |
3
| Task t = (Task) i.next();
|
67 |
| |
68 |
3
| if (!execute(t))
|
69 |
1
| failures++;
|
70 |
| } |
71 |
| |
72 |
2
| long elapsedTime = System.currentTimeMillis() - startTime;
|
73 |
| |
74 |
2
| if (failures == 0)
|
75 |
1
| _log.info(success(orderedTasks.size(), elapsedTime));
|
76 |
| else |
77 |
1
| _log.info(failure(failures, orderedTasks.size(), elapsedTime));
|
78 |
| } |
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
| |
84 |
| |
85 |
3
| private boolean execute(Task t)
|
86 |
| { |
87 |
3
| _log.info(executingTask(t));
|
88 |
| |
89 |
3
| try
|
90 |
| { |
91 |
3
| t.execute();
|
92 |
| |
93 |
2
| return true;
|
94 |
| } |
95 |
| catch (Exception ex) |
96 |
| { |
97 |
1
| _errorLog.error(exceptionInTask(t, ex), t.getLocation(), ex);
|
98 |
| |
99 |
1
| return false;
|
100 |
| } |
101 |
| } |
102 |
| |
103 |
2
| private String task()
|
104 |
| { |
105 |
2
| return _messages.getMessage("task");
|
106 |
| } |
107 |
| |
108 |
3
| private String executingTask(Task t)
|
109 |
| { |
110 |
3
| return _messages.format("executing-task", t.getTitle());
|
111 |
| } |
112 |
| |
113 |
1
| private String exceptionInTask(Task t, Throwable cause)
|
114 |
| { |
115 |
1
| return _messages.format("exception-in-task", t.getTitle(), cause);
|
116 |
| } |
117 |
| |
118 |
1
| private String success(int count, long elapsedTimeMillis)
|
119 |
| { |
120 |
1
| return _messages.format("success", new Integer(count), new Long(elapsedTimeMillis));
|
121 |
| } |
122 |
| |
123 |
1
| private String failure(int failureCount, int totalCount, long elapsedTimeMillis)
|
124 |
| { |
125 |
1
| return _messages.format(
|
126 |
| "failure", |
127 |
| new Integer(failureCount), |
128 |
| new Integer(totalCount), |
129 |
| new Long(elapsedTimeMillis)); |
130 |
| } |
131 |
| |
132 |
2
| public void setLog(Log log)
|
133 |
| { |
134 |
2
| _log = log;
|
135 |
| } |
136 |
| |
137 |
2
| public void setErrorLog(ErrorLog errorLog)
|
138 |
| { |
139 |
2
| _errorLog = errorLog;
|
140 |
| } |
141 |
| |
142 |
2
| public void setMessages(Messages messages)
|
143 |
| { |
144 |
2
| _messages = messages;
|
145 |
| } |
146 |
| |
147 |
2
| public void setTasks(List list)
|
148 |
| { |
149 |
2
| _tasks = list;
|
150 |
| } |
151 |
| |
152 |
| } |