1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.observation.remote.internal; |
21 |
|
|
22 |
|
import javax.inject.Inject; |
23 |
|
import javax.inject.Singleton; |
24 |
|
|
25 |
|
import org.slf4j.Logger; |
26 |
|
import org.xwiki.component.annotation.Component; |
27 |
|
import org.xwiki.component.manager.ComponentLookupException; |
28 |
|
import org.xwiki.component.manager.ComponentManager; |
29 |
|
import org.xwiki.component.phase.Initializable; |
30 |
|
import org.xwiki.component.phase.InitializationException; |
31 |
|
import org.xwiki.context.Execution; |
32 |
|
import org.xwiki.context.ExecutionContext; |
33 |
|
import org.xwiki.context.ExecutionContextManager; |
34 |
|
import org.xwiki.observation.ObservationManager; |
35 |
|
import org.xwiki.observation.event.ApplicationStoppedEvent; |
36 |
|
import org.xwiki.observation.remote.LocalEventData; |
37 |
|
import org.xwiki.observation.remote.NetworkAdapter; |
38 |
|
import org.xwiki.observation.remote.RemoteEventData; |
39 |
|
import org.xwiki.observation.remote.RemoteEventException; |
40 |
|
import org.xwiki.observation.remote.RemoteObservationManager; |
41 |
|
import org.xwiki.observation.remote.RemoteObservationManagerConfiguration; |
42 |
|
import org.xwiki.observation.remote.RemoteObservationManagerContext; |
43 |
|
import org.xwiki.observation.remote.converter.EventConverterManager; |
44 |
|
|
45 |
|
|
46 |
|
@link |
47 |
|
|
48 |
|
@version |
49 |
|
@since |
50 |
|
|
51 |
|
@Component |
52 |
|
@Singleton |
|
|
| 85.1% |
Uncovered Elements: 7 (47) |
Complexity: 15 |
Complexity Density: 0.48 |
|
53 |
|
public class DefaultRemoteObservationManager implements RemoteObservationManager, Initializable |
54 |
|
{ |
55 |
|
|
56 |
|
@link |
57 |
|
|
58 |
|
@Inject |
59 |
|
private RemoteObservationManagerConfiguration configuration; |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
@Inject |
65 |
|
private EventConverterManager eventConverterManager; |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
@Inject |
71 |
|
private ObservationManager observationManager; |
72 |
|
|
73 |
|
|
74 |
|
@link |
75 |
|
|
76 |
|
@Inject |
77 |
|
private RemoteObservationManagerContext remoteEventManagerContext; |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
@Inject |
83 |
|
private Execution execution; |
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
@Inject |
89 |
|
private ExecutionContextManager executionContextManager; |
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
@Inject |
95 |
|
private ComponentManager componentManager; |
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
@Inject |
101 |
|
private Logger logger; |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
private NetworkAdapter networkAdapter; |
107 |
|
|
|
|
| 75% |
Uncovered Elements: 2 (8) |
Complexity: 3 |
Complexity Density: 0.38 |
|
108 |
5 |
@Override... |
109 |
|
public void initialize() throws InitializationException |
110 |
|
{ |
111 |
5 |
try { |
112 |
5 |
String networkAdapterHint = this.configuration.getNetworkAdapter(); |
113 |
5 |
this.networkAdapter = this.componentManager.getInstance(NetworkAdapter.class, networkAdapterHint); |
114 |
|
} catch (ComponentLookupException e) { |
115 |
0 |
throw new InitializationException("Failed to initialize network adapter [" |
116 |
|
+ this.configuration.getNetworkAdapter() + "]", e); |
117 |
|
} |
118 |
|
|
119 |
|
|
120 |
5 |
for (String channelId : this.configuration.getChannels()) { |
121 |
4 |
try { |
122 |
4 |
startChannel(channelId); |
123 |
|
} catch (RemoteEventException e) { |
124 |
0 |
this.logger.error("Failed to start channel [" + channelId + "]", e); |
125 |
|
} |
126 |
|
} |
127 |
|
} |
128 |
|
|
|
|
| 93.3% |
Uncovered Elements: 1 (15) |
Complexity: 5 |
Complexity Density: 0.56 |
|
129 |
1083 |
@Override... |
130 |
|
public void notify(LocalEventData localEvent) |
131 |
|
{ |
132 |
1085 |
if (this.remoteEventManagerContext.isRemoteState()) { |
133 |
|
|
134 |
24 |
return; |
135 |
|
} |
136 |
|
|
137 |
|
|
138 |
1061 |
RemoteEventData remoteEvent = this.eventConverterManager.createRemoteEventData(localEvent); |
139 |
|
|
140 |
|
|
141 |
1060 |
if (remoteEvent != null) { |
142 |
34 |
this.networkAdapter.send(remoteEvent); |
143 |
|
} |
144 |
|
|
145 |
1062 |
if (localEvent.getEvent() instanceof ApplicationStoppedEvent) { |
146 |
3 |
try { |
147 |
3 |
this.networkAdapter.stopAllChannels(); |
148 |
|
} catch (RemoteEventException e) { |
149 |
0 |
this.logger.error("Failed to stop channels", e); |
150 |
|
} |
151 |
|
} |
152 |
|
} |
153 |
|
|
|
|
| 88.9% |
Uncovered Elements: 1 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
154 |
9 |
@Override... |
155 |
|
public void notify(RemoteEventData remoteEvent) |
156 |
|
{ |
157 |
|
|
158 |
9 |
initializeContext(); |
159 |
|
|
160 |
9 |
LocalEventData localEvent = this.eventConverterManager.createLocalEventData(remoteEvent); |
161 |
|
|
162 |
|
|
163 |
9 |
if (localEvent != null) { |
164 |
|
|
165 |
9 |
this.remoteEventManagerContext.pushRemoteState(); |
166 |
|
|
167 |
9 |
try { |
168 |
9 |
this.observationManager.notify(localEvent.getEvent(), localEvent.getSource(), localEvent.getData()); |
169 |
|
} finally { |
170 |
|
|
171 |
9 |
this.remoteEventManagerContext.popRemoteState(); |
172 |
|
} |
173 |
|
} |
174 |
|
} |
175 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
176 |
5 |
@Override... |
177 |
|
public void startChannel(String channelId) throws RemoteEventException |
178 |
|
{ |
179 |
5 |
this.networkAdapter.startChannel(channelId); |
180 |
|
} |
181 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
182 |
0 |
@Override... |
183 |
|
public void stopChannel(String channelId) throws RemoteEventException |
184 |
|
{ |
185 |
0 |
this.networkAdapter.stopChannel(channelId); |
186 |
|
} |
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
|
|
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
191 |
9 |
private void initializeContext()... |
192 |
|
{ |
193 |
9 |
if (this.execution.getContext() == null) { |
194 |
4 |
ExecutionContext context = new ExecutionContext(); |
195 |
|
|
196 |
4 |
try { |
197 |
4 |
this.executionContextManager.initialize(context); |
198 |
|
} catch (Exception e) { |
199 |
0 |
this.logger.error("failed to initialize execution context", e); |
200 |
|
} |
201 |
|
} |
202 |
|
} |
203 |
|
} |