Keyple Service Resource C++ Library - 3.1.1
Component of the Keyple C++ middleware
PluginsConfigurator.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/resource/PluginsConfigurator.hpp"
15
16#include "keyple/core/service/PoolPlugin.hpp"
17#include "keyple/core/service/spi/PluginObservationExceptionHandlerSpi.hpp"
18#include "keyple/core/util/KeypleAssert.hpp"
19#include "keyple/core/util/cpp/Arrays.hpp"
20#include "keyple/core/util/cpp/exception/IllegalArgumentException.hpp"
21#include "keyple/core/util/cpp/exception/IllegalStateException.hpp"
22
23namespace keyple {
24namespace core {
25namespace service {
26namespace resource {
27
28using keyple::core::service::PoolPlugin;
29using keyple::core::service::spi::PluginObservationExceptionHandlerSpi;
30using keyple::core::util::Assert;
31using keyple::core::util::cpp::Arrays;
32using keyple::core::util::cpp::exception::IllegalArgumentException;
33using keyple::core::util::cpp::exception::IllegalStateException;
34
37
38/* BUILDER ------------------------------------------------------------------ */
39
41: mAllocationStrategy(AllocationStrategy::FIRST)
42, mAllocationStrategyConfigured(false)
43, mUsageTimeoutMillis(0)
44, mUsageTimeoutMillisConfigured(false)
45{
46}
47
49PluginsConfigurator::Builder::withAllocationStrategy(
50 const AllocationStrategy allocationStrategy)
51{
52 if (mAllocationStrategyConfigured == true) {
53 throw IllegalStateException("Allocation strategy already configured");
54 }
55
56 mAllocationStrategy = allocationStrategy;
57 mAllocationStrategyConfigured = true;
58
59 return *this;
60}
61
63PluginsConfigurator::Builder::withUsageTimeout(const int usageTimeoutMillis)
64{
65 Assert::getInstance().greaterOrEqual(
66 usageTimeoutMillis, 1, "usageTimeoutMillis");
67
68 if (mUsageTimeoutMillisConfigured == true) {
69 throw IllegalStateException("Usage timeout already configured");
70 }
71
72 mUsageTimeoutMillis = usageTimeoutMillis;
73 mUsageTimeoutMillisConfigured = true;
74
75 return *this;
76}
77
79PluginsConfigurator::Builder::addPlugin(
80 std::shared_ptr<Plugin> plugin,
81 std::shared_ptr<ReaderConfiguratorSpi> readerConfiguratorSpi)
82{
83 return addPluginWithMonitoring(
84 plugin, readerConfiguratorSpi, nullptr, nullptr);
85}
86
88PluginsConfigurator::Builder::addPluginWithMonitoring(
89 std::shared_ptr<Plugin> plugin,
90 std::shared_ptr<ReaderConfiguratorSpi> readerConfiguratorSpi,
91 std::shared_ptr<PluginObservationExceptionHandlerSpi>
92 pluginObservationExceptionHandlerSpi,
93 std::shared_ptr<CardReaderObservationExceptionHandlerSpi>
94 readerObservationExceptionHandlerSpi)
95{
96 Assert::getInstance()
97 .notNull(plugin, "plugin")
98 .notNull(readerConfiguratorSpi, "readerConfiguratorSpi");
99
100 const auto pool = std::dynamic_pointer_cast<PoolPlugin>(plugin);
101 if (pool != nullptr) {
102 throw IllegalArgumentException(
103 "Plugin must be an instance of Plugin or ObservablePlugin");
104 }
105
106 if (Arrays::contains(mPlugins, plugin)) {
107 throw IllegalStateException("Plugin already configured");
108 }
109
110 mPlugins.push_back(plugin);
111 mConfiguredPlugins.push_back(std::make_shared<ConfiguredPlugin>(
112 plugin,
113 readerConfiguratorSpi,
114 pluginObservationExceptionHandlerSpi,
115 readerObservationExceptionHandlerSpi));
116
117 return *this;
118}
119
120std::shared_ptr<PluginsConfigurator>
121PluginsConfigurator::Builder::build()
122{
123 if (mPlugins.empty()) {
124 throw IllegalStateException("No plugin was configured");
125 }
126
127 if (mAllocationStrategyConfigured == false) {
128 mAllocationStrategy = AllocationStrategy::FIRST;
129 mAllocationStrategyConfigured = true;
130 }
131
132 if (mUsageTimeoutMillisConfigured == false) {
133 /* Infinite */
134 mUsageTimeoutMillis = 0;
135 mUsageTimeoutMillisConfigured = true;
136 }
137
138 return std::make_shared<PluginsConfigurator>(this);
139}
140
141/* CONFIGURED PLUGIN -------------------------------------------------------- */
142
144 std::shared_ptr<Plugin> plugin,
145 std::shared_ptr<ReaderConfiguratorSpi> readerConfiguratorSpi,
146 std::shared_ptr<PluginObservationExceptionHandlerSpi>
147 pluginObservationExceptionHandlerSpi,
148 std::shared_ptr<CardReaderObservationExceptionHandlerSpi>
149 readerObservationExceptionHandlerSpi)
150: mPlugin(plugin)
151, mReaderConfiguratorSpi(readerConfiguratorSpi)
152, mWithPluginMonitoring(false)
153, mPluginObservationExceptionHandlerSpi(nullptr)
154, mWithReaderMonitoring(false)
155, mReaderObservationExceptionHandlerSpi(nullptr)
156{
157 if (pluginObservationExceptionHandlerSpi != nullptr) {
158 mWithPluginMonitoring = true;
159 mPluginObservationExceptionHandlerSpi
160 = pluginObservationExceptionHandlerSpi;
161 }
162
163 if (readerObservationExceptionHandlerSpi != nullptr) {
164 mWithReaderMonitoring = true;
165 mReaderObservationExceptionHandlerSpi
166 = readerObservationExceptionHandlerSpi;
167 }
168}
169
170std::shared_ptr<Plugin>
171PluginsConfigurator::ConfiguredPlugin::getPlugin() const
172{
173 return mPlugin;
174}
175
176std::shared_ptr<ReaderConfiguratorSpi>
177PluginsConfigurator::ConfiguredPlugin::getReaderConfiguratorSpi() const
178{
179 return mReaderConfiguratorSpi;
180}
181
182bool
183PluginsConfigurator::ConfiguredPlugin::isWithPluginMonitoring() const
184{
185 return mWithPluginMonitoring;
186}
187
188std::shared_ptr<PluginObservationExceptionHandlerSpi>
189PluginsConfigurator::ConfiguredPlugin::getPluginObservationExceptionHandlerSpi()
190 const
191{
192 return mPluginObservationExceptionHandlerSpi;
193}
194
195bool
196PluginsConfigurator::ConfiguredPlugin::isWithReaderMonitoring() const
197{
198 return mWithReaderMonitoring;
199}
200
201std::shared_ptr<CardReaderObservationExceptionHandlerSpi>
202PluginsConfigurator::ConfiguredPlugin::getReaderObservationExceptionHandlerSpi()
203 const
204{
205 return mReaderObservationExceptionHandlerSpi;
206}
207
208/* PLUGINS CONFIGURATOR ----------------------------------------------------- */
209
211PluginsConfigurator::getAllocationStrategy() const
212{
213 return mAllocationStrategy;
214}
215
216int
217PluginsConfigurator::getUsageTimeoutMillis() const
218{
219 return mUsageTimeoutMillis;
220}
221
222const std::vector<std::shared_ptr<Plugin>>&
223PluginsConfigurator::getPlugins() const
224{
225 return mPlugins;
226}
227
228const std::vector<std::shared_ptr<ConfiguredPlugin>>&
229PluginsConfigurator::getConfiguredPlugins() const
230{
231 return mConfiguredPlugins;
232}
233
235PluginsConfigurator::builder()
236{
237 return new Builder();
238}
239
240PluginsConfigurator::PluginsConfigurator(PluginsConfigurator::Builder* builder)
241: mAllocationStrategy(builder->mAllocationStrategy)
242, mUsageTimeoutMillis(builder->mUsageTimeoutMillis)
243, mPlugins(builder->mPlugins)
244, mConfiguredPlugins(builder->mConfiguredPlugins)
245{
246 /* Deleted builder here. It's been allocated with new */
247 delete builder;
248}
249
250} /* namespace resource */
251} /* namespace service */
252} /* namespace core */
253} /* namespace keyple */
PluginsConfigurator::AllocationStrategy AllocationStrategy
PluginsConfigurator::ConfiguredPlugin ConfiguredPlugin
CardResourceProfileConfigurator::Builder Builder