1 | // Copyright 2004-2007 Jean-Francois Poilpret |
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 net.sourceforge.hiveboard.event; |
16 | |
17 | import org.apache.commons.logging.Log; |
18 | import org.apache.hivemind.service.ThreadEventNotifier; |
19 | |
20 | import net.sourceforge.hiveboard.Event; |
21 | import net.sourceforge.hiveboard.WhiteBoardUserService; |
22 | import net.sourceforge.hiveevents.Channel; |
23 | |
24 | public class EventDispatcherImpl implements EventDispatcher, Runnable |
25 | { |
26 | public EventDispatcherImpl(Log logger, |
27 | WhiteBoardUserService service, |
28 | ThreadEventNotifier notifier, |
29 | Channel<Event> channel) |
30 | { |
31 | _logger = logger; |
32 | _service = service; |
33 | _notifier = notifier; |
34 | _channel = channel; |
35 | _stop = true; |
36 | _thread = new Thread(this); |
37 | } |
38 | |
39 | public void stop() |
40 | { |
41 | _stop = true; |
42 | } |
43 | |
44 | public void start() |
45 | { |
46 | _stop = false; |
47 | _thread.start(); |
48 | } |
49 | |
50 | public void waitForCompletion() |
51 | { |
52 | try |
53 | { |
54 | _thread.join(); |
55 | } |
56 | catch (InterruptedException e) |
57 | { |
58 | _logger.warn("waitForCompletion", e); |
59 | } |
60 | } |
61 | |
62 | public void run() |
63 | { |
64 | while (!_stop) |
65 | { |
66 | // CSOFF: IllegalCatchCheck |
67 | try |
68 | { |
69 | Event[] events = _service.pull(); |
70 | if (!_stop) |
71 | { |
72 | fireEvents(events); |
73 | } |
74 | } |
75 | catch (Exception e) |
76 | { |
77 | _logger.warn("pull", e); |
78 | delay(); |
79 | } |
80 | // CSON: IllegalCatchCheck |
81 | } |
82 | _notifier.fireThreadCleanup(); |
83 | } |
84 | |
85 | private void delay() |
86 | { |
87 | try |
88 | { |
89 | Thread.sleep(WAIT_AFTER_EXCEPTION); |
90 | } |
91 | catch (InterruptedException e) |
92 | { |
93 | _logger.warn("delay", e); |
94 | } |
95 | } |
96 | |
97 | protected void fireEvents(Event[] events) |
98 | { |
99 | for (int i = 0; i < events.length; i++) |
100 | { |
101 | _channel.push(events[i]); |
102 | } |
103 | } |
104 | |
105 | static final private long WAIT_AFTER_EXCEPTION = 200L; |
106 | |
107 | final private Log _logger; |
108 | final private WhiteBoardUserService _service; |
109 | final private ThreadEventNotifier _notifier; |
110 | final private Channel<Event> _channel; |
111 | final private Thread _thread; |
112 | private boolean _stop; |
113 | } |
114 | |