-
Notifications
You must be signed in to change notification settings - Fork 4
/
ObservableRemotePluginAdapter.java
322 lines (280 loc) · 9.75 KB
/
ObservableRemotePluginAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* **************************************************************************************
* Copyright (c) 2021 Calypso Networks Association https://calypsonet.org/
*
* See the NOTICE file(s) distributed with this work for additional information
* regarding copyright ownership.
*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License 2.0 which is available at http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
************************************************************************************** */
package org.eclipse.keyple.core.service;
import static org.eclipse.keyple.core.service.DistributedUtilAdapter.*;
import com.google.gson.JsonObject;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.eclipse.keyple.core.distributed.remote.ObservableRemotePluginApi;
import org.eclipse.keyple.core.distributed.remote.spi.ObservableRemotePluginSpi;
import org.eclipse.keyple.core.distributed.remote.spi.ObservableRemoteReaderSpi;
import org.eclipse.keyple.core.distributed.remote.spi.RemoteReaderSpi;
import org.eclipse.keyple.core.service.spi.PluginObservationExceptionHandlerSpi;
import org.eclipse.keyple.core.service.spi.PluginObserverSpi;
import org.eclipse.keyple.core.util.Assert;
import org.eclipse.keyple.core.util.json.JsonUtil;
import org.eclipse.keypop.reader.CardReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Implementation of a remote {@link ObservablePlugin}.
*
* @since 2.0.0
*/
final class ObservableRemotePluginAdapter extends RemotePluginAdapter
implements ObservablePlugin, ObservableRemotePluginApi {
private static final Logger logger = LoggerFactory.getLogger(ObservableRemotePluginAdapter.class);
private final ObservableRemotePluginSpi observableRemotePluginSpi;
private final ObservationManagerAdapter<PluginObserverSpi, PluginObservationExceptionHandlerSpi>
observationManager;
private final ExecutorService eventNotificationExecutorService;
/**
* Constructor.
*
* @param observableRemotePluginSpi The associated SPI.
* @since 2.0.0
*/
ObservableRemotePluginAdapter(ObservableRemotePluginSpi observableRemotePluginSpi) {
super(observableRemotePluginSpi);
this.observableRemotePluginSpi = observableRemotePluginSpi;
this.observableRemotePluginSpi.connect((ObservableRemotePluginApi) this);
observationManager = new ObservationManagerAdapter<>(getName(), null);
eventNotificationExecutorService =
observableRemotePluginSpi.getExecutorService() != null
? observableRemotePluginSpi.getExecutorService()
: Executors.newCachedThreadPool();
}
/**
* Notifies asynchronously all registered observers with the provided {@link PluginEvent}.
*
* <p>This method never throws an exception. Any errors at runtime are notified to the application
* using the exception handler.
*
* @param event The plugin event.
* @since 2.0.0
*/
private void notifyObservers(final PluginEvent event) {
if (logger.isDebugEnabled()) {
logger.debug(
"Plugin [{}] notifies event [{}] to {} observer(s)",
getName(),
event.getType().name(),
countObservers());
}
Set<PluginObserverSpi> observers = observationManager.getObservers();
for (final PluginObserverSpi observer : observers) {
eventNotificationExecutorService.execute(
new Runnable() {
@Override
public void run() {
try {
observer.onPluginEvent(event);
} catch (Exception e) {
try {
observationManager
.getObservationExceptionHandler()
.onPluginObservationError(getName(), e);
} catch (Exception e2) {
logger.error("Event notification error: {}", e2.getMessage(), e2);
logger.error("Original cause: {}", e.getMessage(), e);
}
}
}
});
}
}
/**
* {@inheritDoc}
*
* @since 2.0.0
*/
@Override
void unregister() {
Set<String> unregisteredReaderNames = new HashSet<>(getReaderNames());
notifyObservers(
new PluginEventAdapter(getName(), unregisteredReaderNames, PluginEvent.Type.UNAVAILABLE));
clearObservers();
super.unregister();
}
/**
* {@inheritDoc}
*
* @since 2.0.0
*/
@Override
public void addObserver(PluginObserverSpi observer) {
checkStatus();
observationManager.addObserver(observer);
if (observationManager.countObservers() == 1) {
logger.info("Start monitoring of plugin [{}]", getName());
// Start the observation remotely.
JsonObject input = new JsonObject();
input.addProperty(JsonProperty.CORE_API_LEVEL.getKey(), CORE_API_LEVEL);
input.addProperty(JsonProperty.SERVICE.getKey(), PluginService.START_READER_DETECTION.name());
// Execute the remote service.
try {
executePluginServiceRemotely(input, observableRemotePluginSpi, getName(), logger);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throwRuntimeException(e);
}
// Notify the SPI.
observableRemotePluginSpi.onStartObservation();
}
}
/**
* {@inheritDoc}
*
* @since 2.0.0
*/
@Override
public void removeObserver(PluginObserverSpi observer) {
Assert.getInstance().notNull(observer, "observer");
if (observationManager.getObservers().contains(observer)) {
observationManager.removeObserver(observer);
if (observationManager.countObservers() == 0) {
stopPluginMonitoring();
}
}
}
/**
* {@inheritDoc}
*
* @since 2.0.0
*/
@Override
public void clearObservers() {
observationManager.clearObservers();
stopPluginMonitoring();
}
/** Stops the monitoring of the plugin. */
private void stopPluginMonitoring() {
logger.info("Stop plugin monitoring");
// Notify the SPI first.
observableRemotePluginSpi.onStopObservation();
// Stop the observation remotely.
JsonObject input = new JsonObject();
input.addProperty(JsonProperty.CORE_API_LEVEL.getKey(), CORE_API_LEVEL);
input.addProperty(JsonProperty.SERVICE.getKey(), PluginService.STOP_READER_DETECTION.name());
// Execute the remote service.
try {
executePluginServiceRemotely(input, observableRemotePluginSpi, getName(), logger);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throwRuntimeException(e);
}
logger.info("Plugin monitoring stopped");
}
/**
* {@inheritDoc}
*
* @since 2.0.0
*/
@Override
public int countObservers() {
return observationManager.countObservers();
}
/**
* {@inheritDoc}
*
* @since 2.0.0
*/
@Override
public void setPluginObservationExceptionHandler(
PluginObservationExceptionHandlerSpi exceptionHandler) {
checkStatus();
observationManager.setObservationExceptionHandler(exceptionHandler);
}
/**
* {@inheritDoc}
*
* @since 2.0.0
*/
public void addRemoteReader(RemoteReaderSpi remoteReaderSpi, int clientCoreApiLevel) {
checkStatus();
if (logger.isDebugEnabled()) {
logger.debug(
"Plugin [{}] registers reader [{}]",
getName(),
remoteReaderSpi != null ? remoteReaderSpi.getName() : null);
}
Assert.getInstance().notNull(remoteReaderSpi, "remoteReaderSpi");
// Create the reader.
RemoteReaderAdapter remoteReaderAdapter;
if (remoteReaderSpi instanceof ObservableRemoteReaderSpi) {
remoteReaderAdapter =
new ObservableRemoteReaderAdapter(
(ObservableRemoteReaderSpi) remoteReaderSpi, getName(), clientCoreApiLevel);
} else {
remoteReaderAdapter =
new RemoteReaderAdapter(remoteReaderSpi, getName(), null, clientCoreApiLevel);
}
// Register the reader.
getReadersMap().put(remoteReaderAdapter.getName(), remoteReaderAdapter);
remoteReaderAdapter.register();
// Notify observers for a plugin event.
notifyObservers(
new PluginEventAdapter(
getName(), remoteReaderAdapter.getName(), PluginEvent.Type.READER_CONNECTED));
}
/**
* {@inheritDoc}
*
* @since 2.0.0
*/
@Override
public void removeRemoteReader(String remoteReaderName) {
if (logger.isDebugEnabled()) {
logger.debug("Plugin [{}] unregisters reader [{}]", getName(), remoteReaderName);
}
Assert.getInstance().notEmpty(remoteReaderName, "remoteReaderName");
CardReader reader = getReader(remoteReaderName);
if (reader instanceof RemoteReaderAdapter) {
getReadersMap().remove(reader.getName());
((RemoteReaderAdapter) reader).unregister();
} else {
throw new IllegalArgumentException("Reader is not found, not registered or not remote");
}
}
/**
* {@inheritDoc}
*
* @since 2.0.0
*/
@Override
public void onPluginEvent(String jsonData) {
checkStatus();
if (logger.isDebugEnabled()) {
logger.debug("Plugin [{}] receives plugin event: {}", getName(), jsonData);
}
Assert.getInstance().notEmpty(jsonData, "jsonData");
// Extract the event.
PluginEvent pluginEvent;
try {
JsonObject json = JsonUtil.getParser().fromJson(jsonData, JsonObject.class);
pluginEvent =
JsonUtil.getParser()
.fromJson(
json.getAsJsonObject(JsonProperty.PLUGIN_EVENT.getKey()).toString(),
PluginEvent.class);
} catch (RuntimeException e) {
throw new IllegalArgumentException(
String.format("JSON data of the plugin event is malformed: %s", e.getMessage()), e);
}
// Notify the observers for a plugin event.
notifyObservers(pluginEvent);
}
}