KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > examples > panorama > startup > impl > TaskExecutor


1 // Copyright 2004, 2005 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 org.apache.examples.panorama.startup.impl;
16
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
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  * A service that executes a series of {@link org.apache.examples.panorama.startup.impl.Task}s. Tasks have an
27  * ordering based on pre- and post-requisites.
28  *
29  * @author Howard Lewis Ship
30  */

31 public class TaskExecutor implements Runnable JavaDoc
32 {
33     private Log _log;
34
35     private ErrorLog _errorLog;
36
37     private List JavaDoc _tasks;
38
39     private Messages _messages;
40
41     /**
42      * Orders the {@link #setTasks(List) tasks} into an execution order, and executes each in turn.
43      * Logs the elapsed time, number of tasks, and the number of failures (if any).
44      */

45     public void run()
46     {
47         long startTime = System.currentTimeMillis();
48
49         Orderer orderer = new Orderer(_errorLog, task());
50
51         Iterator JavaDoc i = _tasks.iterator();
52         while (i.hasNext())
53         {
54             Task t = (Task) i.next();
55
56             orderer.add(t, t.getId(), t.getAfter(), t.getBefore());
57         }
58
59         List JavaDoc orderedTasks = orderer.getOrderedObjects();
60
61         int failures = 0;
62
63         i = orderedTasks.iterator();
64         while (i.hasNext())
65         {
66             Task t = (Task) i.next();
67
68             if (!execute(t))
69                 failures++;
70         }
71
72         long elapsedTime = System.currentTimeMillis() - startTime;
73
74         if (failures == 0)
75             _log.info(success(orderedTasks.size(), elapsedTime));
76         else
77             _log.info(failure(failures, orderedTasks.size(), elapsedTime));
78     }
79
80     /**
81      * Execute a single task.
82      *
83      * @return true on success, false on failure
84      */

85     private boolean execute(Task t)
86     {
87         _log.info(executingTask(t));
88
89         try
90         {
91             t.execute();
92
93             return true;
94         }
95         catch (Exception JavaDoc ex)
96         {
97             _errorLog.error(exceptionInTask(t, ex), t.getLocation(), ex);
98
99             return false;
100         }
101     }
102
103     private String JavaDoc task()
104     {
105         return _messages.getMessage("task");
106     }
107
108     private String JavaDoc executingTask(Task t)
109     {
110         return _messages.format("executing-task", t.getTitle());
111     }
112
113     private String JavaDoc exceptionInTask(Task t, Throwable JavaDoc cause)
114     {
115         return _messages.format("exception-in-task", t.getTitle(), cause);
116     }
117
118     private String JavaDoc success(int count, long elapsedTimeMillis)
119     {
120         return _messages.format("success", new Integer JavaDoc(count), new Long JavaDoc(elapsedTimeMillis));
121     }
122
123     private String JavaDoc failure(int failureCount, int totalCount, long elapsedTimeMillis)
124     {
125         return _messages.format(
126                 "failure",
127                 new Integer JavaDoc(failureCount),
128                 new Integer JavaDoc(totalCount),
129                 new Long JavaDoc(elapsedTimeMillis));
130     }
131
132     public void setLog(Log log)
133     {
134         _log = log;
135     }
136
137     public void setErrorLog(ErrorLog errorLog)
138     {
139         _errorLog = errorLog;
140     }
141
142     public void setMessages(Messages messages)
143     {
144         _messages = messages;
145     }
146
147     public void setTasks(List JavaDoc list)
148     {
149         _tasks = list;
150     }
151
152 }
Popular Tags