1 | package com.hammurapi.eventbus; |
2 | |
3 | import java.io.Serializable; |
4 | import java.util.Iterator; |
5 | import java.util.logging.Level; |
6 | import java.util.logging.Logger; |
7 | |
8 | import com.hammurapi.eventbus.AbstractEventBus.Handle; |
9 | import com.hammurapi.eventbus.EventHandlerBase.Mode; |
10 | import com.hammurapi.eventbus.local.LocalEventBusBase; |
11 | import com.hammurapi.extract.Predicate; |
12 | |
13 | class RetractTask<E,P extends Comparable<P>,C,K, H extends AbstractEventBus.Handle<E,P,C,K>, S extends EventStore<E,P,C,H,S>> implements Runnable, Serializable { |
14 | private static final Logger logger = Logger.getLogger(HandlerTask.class.getName()); |
15 | |
16 | private RetractCommand<E, P, C, K, H, S> retractCommand; |
17 | |
18 | private InferenceContext<E, P, C, K, H, S> inferenceContext; |
19 | |
20 | RetractTask( |
21 | RetractCommand<E, P, C, K, H, S> retractCommand, |
22 | InferenceContext<E, P, C, K, H, S> inferenceContext) { |
23 | this.retractCommand = retractCommand; |
24 | this.inferenceContext = inferenceContext; |
25 | } |
26 | |
27 | @Override |
28 | public void run() { |
29 | try { |
30 | processCommand(retractCommand, inferenceContext); |
31 | } catch (Exception e) { |
32 | logger.log(Level.SEVERE, "Exception during event handling: "+e, e); |
33 | if (inferenceContext.getBus().getExceptionHandler()!=null) { |
34 | inferenceContext.getBus().getExceptionHandler().handleException(e); |
35 | } |
36 | if (inferenceContext.getRootHandle()!=null) { |
37 | inferenceContext.getRootHandle().handleException(e); |
38 | } |
39 | } |
40 | } |
41 | |
42 | static <E,P extends Comparable<P>,C,K, H extends AbstractEventBus.Handle<E,P,C,K>, S extends EventStore<E,P,C,H,S>> void processCommand( |
43 | RetractCommand<E, P, C, K, H, S> retractCommand, |
44 | InferenceContext<E,P,C,K,H,S> iContext) { |
45 | |
46 | final InferenceContext<E,P,C,K,H,S> inferenceContext = InferencePolicy.AFTER_HANDLER.equals(iContext.getBus().getInferencePolicy()) ? iContext.wrap() : iContext; |
47 | |
48 | EventDispatchContext<E, P, C, H, S> context = new EventDispatchContext<E, P, C, H, S>() { |
49 | |
50 | @Override |
51 | public void post(E event, Predicate<E, S>... validators) { |
52 | // Post from remove handler is considered a direct post. |
53 | InferenceCommand<E, P, C, K, H, S> postCommand = new PostCommand<E, P, C, K, H, S>(event, true, null, null, null, inferenceContext.createNext(), validators); |
54 | inferenceContext.postInferenceCommand(postCommand); |
55 | } |
56 | |
57 | @Override |
58 | public void consume(int index) { |
59 | throw new UnsupportedOperationException("This method is not supported in REMOVE mode"); |
60 | } |
61 | |
62 | @Override |
63 | public void consume(E event) { |
64 | throw new UnsupportedOperationException("This method is not supported in REMOVE mode"); |
65 | } |
66 | |
67 | @Override |
68 | public void update(E event) { |
69 | throw new UnsupportedOperationException("This method is not supported in REMOVE mode"); |
70 | } |
71 | |
72 | @Override |
73 | public void removeHandler() { |
74 | throw new UnsupportedOperationException("This method is not supported in REMOVE mode"); |
75 | // bus.removeHandlers(registrationKey); |
76 | } |
77 | |
78 | @Override |
79 | public S getEventStore() { |
80 | return inferenceContext.getBus().getStore(); |
81 | } |
82 | |
83 | @Override |
84 | public Mode getMode() { |
85 | return Mode.REMOVE; |
86 | } |
87 | |
88 | }; |
89 | |
90 | retractCommand.getHandler().post(context, retractCommand.getEvents()); |
91 | |
92 | // If no exception - post events |
93 | if (InferencePolicy.AFTER_HANDLER.equals(inferenceContext.getBus().getInferencePolicy())) { |
94 | inferenceContext.processInferenceCommands(); |
95 | } |
96 | } |
97 | |
98 | } |
99 | |