-
Notifications
You must be signed in to change notification settings - Fork 4
/
AbstractPluginAdapter.java
217 lines (201 loc) · 5.83 KB
/
AbstractPluginAdapter.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
/* **************************************************************************************
* 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 java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.PatternSyntaxException;
import org.eclipse.keyple.core.common.KeyplePluginExtension;
import org.eclipse.keyple.core.common.KeypleReaderExtension;
import org.eclipse.keyple.core.plugin.PluginIOException;
import org.eclipse.keyple.core.plugin.spi.reader.ConfigurableReaderSpi;
import org.eclipse.keyple.core.plugin.spi.reader.ReaderSpi;
import org.eclipse.keyple.core.plugin.spi.reader.observable.ObservableReaderSpi;
import org.eclipse.keypop.reader.CardReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Abstract class for all plugins.
*
* @since 2.0.0
*/
abstract class AbstractPluginAdapter implements Plugin {
private static final Logger logger = LoggerFactory.getLogger(AbstractPluginAdapter.class);
static final String REMOTE_READER_NAME_SUFFIX = " (Remote)";
private final String pluginName;
private final KeyplePluginExtension pluginExtension;
private boolean isRegistered;
private final Map<String, CardReader> readers;
/**
* Constructor.
*
* @param pluginName The name of the plugin.
* @param pluginExtension The associated plugin extension SPI.
* @since 2.0.0
*/
AbstractPluginAdapter(String pluginName, KeyplePluginExtension pluginExtension) {
this.pluginName = pluginName;
this.pluginExtension = pluginExtension;
this.readers = new ConcurrentHashMap<>();
}
/**
* Builds a local reader adapter using the provided SPI.
*
* @param readerSpi The associated reader SPI.
* @return A new instance.
*/
final LocalReaderAdapter buildLocalReaderAdapter(ReaderSpi readerSpi) {
LocalReaderAdapter adapter;
if (readerSpi instanceof ObservableReaderSpi) {
if (readerSpi instanceof ConfigurableReaderSpi) {
adapter =
new ObservableLocalConfigurableReaderAdapter(
(ConfigurableReaderSpi) readerSpi, getName());
} else {
adapter = new ObservableLocalReaderAdapter((ObservableReaderSpi) readerSpi, getName());
}
} else if (readerSpi instanceof ConfigurableReaderSpi) {
adapter = new LocalConfigurableReaderAdapter((ConfigurableReaderSpi) readerSpi, getName());
} else {
adapter = new LocalReaderAdapter(readerSpi, getName());
}
return adapter;
}
/**
* Check if the plugin is registered.
*
* @throws IllegalStateException is thrown when plugin is not or no longer registered.
* @since 2.0.0
*/
final void checkStatus() {
if (!isRegistered) {
throw new IllegalStateException(
String.format("Plugin [%s] is not or no longer registered", pluginName));
}
}
/**
* Changes the plugin status to registered.
*
* @throws PluginIOException If registration failed.
* @since 2.0.0
*/
void register() throws PluginIOException {
isRegistered = true;
}
/**
* Unregisters the plugin and the readers present in its list.
*
* @since 2.0.0
*/
void unregister() {
for (CardReader reader : readers.values()) {
try {
((AbstractReaderAdapter) reader).unregister();
} catch (Exception e) {
logger.error("Error unregistering reader [{}]", reader.getName(), e);
}
}
readers.clear();
isRegistered = false;
}
/**
* {@inheritDoc}
*
* @since 2.0.0
*/
@Override
public final String getName() {
return pluginName;
}
/**
* {@inheritDoc}
*
* @since 2.0.0
*/
@Override
public final <T extends KeyplePluginExtension> T getExtension(Class<T> pluginExtensionClass) {
checkStatus();
return pluginExtensionClass.cast(pluginExtension);
}
/**
* {@inheritDoc}
*
* @since 2.1.0
*/
@Override
public final <T extends KeypleReaderExtension> T getReaderExtension(
Class<T> readerExtensionClass, String readerName) {
checkStatus();
AbstractReaderAdapter reader = (AbstractReaderAdapter) getReader(readerName);
if (reader == null) {
throw new IllegalArgumentException("Reader [" + readerName + "] not found");
}
return reader.getExtension(readerExtensionClass);
}
/**
* Gets the Map of all connected readers.
*
* @since 2.0.0
*/
final Map<String, CardReader> getReadersMap() {
return readers;
}
/**
* {@inheritDoc}
*
* @since 2.0.0
*/
@Override
public final Set<String> getReaderNames() {
checkStatus();
return new HashSet<>(readers.keySet());
}
/**
* {@inheritDoc}
*
* @since 2.0.0
*/
@Override
public final Set<CardReader> getReaders() {
checkStatus();
return new HashSet<>(readers.values());
}
/**
* {@inheritDoc}
*
* @since 2.0.0
*/
@Override
public final CardReader getReader(String name) {
checkStatus();
return readers.get(name);
}
/**
* {@inheritDoc}
*
* @since 3.1.0
*/
@Override
public final CardReader findReader(String readerNameRegex) {
for (CardReader reader : readers.values()) {
try {
if (reader.getName().matches(readerNameRegex)) {
return reader;
}
} catch (PatternSyntaxException e) {
throw new IllegalArgumentException("readerNameRegex is invalid: " + e.getMessage(), e);
}
}
return null;
}
}