Keyple Service C++ Library - 3.3.5
Component of the Keyple C++ middleware
AbstractPluginAdapter.cpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2025 Calypso Networks Association https://calypsonet.org/ *
3 * *
4 * See the NOTICE file(s) distributed with this work for additional *
5 * information regarding copyright ownership. *
6 * *
7 * This program and the accompanying materials are made available under the *
8 * terms of the Eclipse Public License 2.0 which is available at *
9 * http://www.eclipse.org/legal/epl-2.0 *
10 * *
11 * SPDX-License-Identifier: EPL-2.0 *
12 ******************************************************************************/
13
14#include "keyple/core/service/AbstractPluginAdapter.hpp"
15
16#include <map>
17#include <memory>
18#include <string>
19#include <utility>
20#include <vector>
21
22#include "keyple/core/service/AbstractReaderAdapter.hpp"
23#include "keyple/core/service/LocalConfigurableReaderAdapter.hpp"
24#include "keyple/core/service/ObservableLocalConfigurableReaderAdapter.hpp"
25#include "keyple/core/util/cpp/StringUtils.hpp"
26#include "keyple/core/util/cpp/exception/Exception.hpp"
27#include "keyple/core/util/cpp/exception/IllegalArgumentException.hpp"
28#include "keyple/core/util/cpp/exception/IllegalStateException.hpp"
29
30namespace keyple {
31namespace core {
32namespace service {
33
34using keyple::core::util::cpp::StringUtils;
35using keyple::core::util::cpp::exception::Exception;
36using keyple::core::util::cpp::exception::IllegalArgumentException;
37using keyple::core::util::cpp::exception::IllegalStateException;
38
39AbstractPluginAdapter::AbstractPluginAdapter(
40 const std::string& pluginName,
41 std::shared_ptr<KeyplePluginExtension> pluginExtension)
42: mPluginName(pluginName)
43, mPluginExtension(pluginExtension)
44, mIsRegistered(false)
45{
46}
47
48std::shared_ptr<LocalReaderAdapter>
49AbstractPluginAdapter::buildLocalReaderAdapter(
50 std::shared_ptr<ReaderSpi> readerSpi)
51{
52 std::shared_ptr<LocalReaderAdapter> adapter = nullptr;
53
54 const auto& observable
55 = std::dynamic_pointer_cast<ObservableReaderSpi>(readerSpi);
56 const auto& configurable
57 = std::dynamic_pointer_cast<ConfigurableReaderSpi>(readerSpi);
58
59 if (observable) {
60 if (configurable) {
61 adapter
62 = std::make_shared<ObservableLocalConfigurableReaderAdapter>(
63 configurable, getName());
64 } else {
65 adapter = std::make_shared<ObservableLocalReaderAdapter>(
66 observable, getName());
67 }
68 } else if (configurable) {
69 adapter = std::make_shared<LocalConfigurableReaderAdapter>(
70 configurable, getName());
71 } else {
72 adapter = std::make_shared<LocalReaderAdapter>(readerSpi, getName());
73 }
74
75 return adapter;
76}
77
78void
79AbstractPluginAdapter::checkStatus() const
80{
81 if (!mIsRegistered) {
82 throw IllegalStateException(
83 "Plugin [" + mPluginName + "] is not or no longer registered");
84 }
85}
86
87void
88AbstractPluginAdapter::doRegister()
89{
90 mIsRegistered = true;
91}
92
93void
94AbstractPluginAdapter::doUnregister()
95{
96 mIsRegistered = false;
97
98 for (const auto& pair : mReaders) {
99 try {
100 std::dynamic_pointer_cast<AbstractReaderAdapter>(pair.second)
101 ->doUnregister();
102 } catch (const Exception& e) {
103 mLogger->error(
104 "Error unregistering reader [%] %'\n",
105 pair.second->getName(),
106 e.getMessage());
107 }
108 }
109
110 mReaders.clear();
111 mIsRegistered = false;
112}
113
114const std::string&
115AbstractPluginAdapter::getName() const
116{
117 return mPluginName;
118}
119
120std::shared_ptr<KeyplePluginExtension>
121AbstractPluginAdapter::getExtension(
122 const std::type_info& pluginExtensionClass) const
123{
124 (void)pluginExtensionClass;
125
126 checkStatus();
127
128 return mPluginExtension;
129}
130
131std::shared_ptr<KeypleReaderExtension>
132AbstractPluginAdapter::getReaderExtension(
133 const std::type_info& readerExtensionClass,
134 const std::string& readerName) const
135{
136 checkStatus();
137
138 const auto reader = std::dynamic_pointer_cast<AbstractReaderAdapter>(
139 getReader(readerName));
140 if (reader == nullptr) {
141 throw IllegalArgumentException(
142 "Reader [" + readerName + "] not found!");
143 }
144
145 return reader->getExtension(readerExtensionClass);
146}
147
148std::map<const std::string, std::shared_ptr<CardReader>>&
149AbstractPluginAdapter::getReadersMap()
150{
151 return mReaders;
152}
153
154const std::vector<std::string>
155AbstractPluginAdapter::getReaderNames() const
156{
157 checkStatus();
158
159 std::vector<std::string> readerNames;
160 for (const auto& pair : mReaders) {
161 readerNames.push_back(pair.first);
162 }
163
164 return readerNames;
165}
166
167const std::vector<std::shared_ptr<CardReader>>
168AbstractPluginAdapter::getReaders() const
169{
170 checkStatus();
171
172 std::vector<std::shared_ptr<CardReader>> readers;
173 for (const auto& pair : mReaders) {
174 readers.push_back(pair.second);
175 }
176
177 return readers;
178}
179
180std::shared_ptr<CardReader>
181AbstractPluginAdapter::getReader(const std::string& name) const
182{
183 checkStatus();
184
185 if (mReaders.find(name) != mReaders.end()) {
186 return mReaders.at(name);
187 }
188
189 return nullptr;
190}
191
192std::shared_ptr<CardReader>
193AbstractPluginAdapter::findReader(const std::string& readerNameRegex) const
194{
195 for (const auto& reader : mReaders) {
196 try {
197 if (StringUtils::matches(
198 reader.second->getName(), readerNameRegex)) {
199 return reader.second;
200 }
201
202 } catch (const std::regex_error& e) {
203 throw IllegalArgumentException(
204 "readerNameRegex is invalid: " + std::string(e.what()));
205 }
206 }
207
208 return nullptr;
209}
210
211} /* namespace service */
212} /* namespace core */
213} /* namespace keyple */