This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 4d9757a CAMEL-16978: camel-xchange - Support more than one crypto
exchanges.
4d9757a is described below
commit 4d9757a8d7adbbdeb30010a01d1754eb233284ce
Author: Claus Ibsen <[email protected]>
AuthorDate: Sun Nov 14 10:53:54 2021 +0100
CAMEL-16978: camel-xchange - Support more than one crypto exchanges.
---
.../camel/component/xchange/XChangeComponent.java | 33 +++++++++----------
.../component/xchange/XChangeConfiguration.java | 31 ------------------
.../camel/component/xchange/XChangeHelper.java | 37 ++++++++++++++++++++++
.../xchange/account/AccountProducerTest.java | 15 +++------
4 files changed, 58 insertions(+), 58 deletions(-)
diff --git
a/components/camel-xchange/src/main/java/org/apache/camel/component/xchange/XChangeComponent.java
b/components/camel-xchange/src/main/java/org/apache/camel/component/xchange/XChangeComponent.java
index c0c05fc..3d866e9 100644
---
a/components/camel-xchange/src/main/java/org/apache/camel/component/xchange/XChangeComponent.java
+++
b/components/camel-xchange/src/main/java/org/apache/camel/component/xchange/XChangeComponent.java
@@ -16,6 +16,7 @@
*/
package org.apache.camel.component.xchange;
+import java.util.HashMap;
import java.util.Map;
import org.apache.camel.Endpoint;
@@ -28,40 +29,40 @@ import org.knowm.xchange.utils.Assert;
@Component("xchange")
public class XChangeComponent extends DefaultComponent {
- private XChange xchange;
+ private final Map<String, XChange> xchanges = new HashMap<>();
@Override
protected Endpoint createEndpoint(String uri, String remaining,
Map<String, Object> parameters) throws Exception {
- // Init the configuration
- XChangeConfiguration configuration = new XChangeConfiguration(this);
-
- // Set the required name of the exchange
+ XChangeConfiguration configuration = new XChangeConfiguration();
configuration.setName(remaining);
XChangeEndpoint endpoint = new XChangeEndpoint(uri, this,
configuration);
setProperties(endpoint, parameters);
- // after configuring endpoint then create xchange
- XChange xchange = createXChange(configuration);
+ // after configuring endpoint then get or create xchange instance
+ XChange xchange = getOrCreateXChange(remaining);
endpoint.setXchange(xchange);
return endpoint;
}
- public XChange getXChange() {
- return xchange;
+ public XChange getXChange(String name) {
+ return xchanges.get(name);
}
- private synchronized XChange createXChange(XChangeConfiguration
configuration) {
+ @Override
+ protected void doShutdown() throws Exception {
+ super.doShutdown();
+ xchanges.clear();
+ }
+ private synchronized XChange getOrCreateXChange(String name) {
+ XChange xchange = xchanges.get(name);
if (xchange == null) {
-
- // Get the XChange implementation
- Class<? extends Exchange> exchangeClass =
configuration.getXChangeClass();
- Assert.notNull(exchangeClass, "XChange not supported: " +
configuration.getName());
-
- // Create the XChange and associated Endpoint
+ Class<? extends Exchange> exchangeClass =
XChangeHelper.loadXChangeClass(getCamelContext(), name);
+ Assert.notNull(exchangeClass, "XChange not supported: " + name);
xchange = new
XChange(ExchangeFactory.INSTANCE.createExchange(exchangeClass));
+ xchanges.put(name, xchange);
}
return xchange;
diff --git
a/components/camel-xchange/src/main/java/org/apache/camel/component/xchange/XChangeConfiguration.java
b/components/camel-xchange/src/main/java/org/apache/camel/component/xchange/XChangeConfiguration.java
index d5fe013..b783d29 100644
---
a/components/camel-xchange/src/main/java/org/apache/camel/component/xchange/XChangeConfiguration.java
+++
b/components/camel-xchange/src/main/java/org/apache/camel/component/xchange/XChangeConfiguration.java
@@ -16,16 +16,10 @@
*/
package org.apache.camel.component.xchange;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Set;
-
import org.apache.camel.spi.Metadata;
import org.apache.camel.spi.UriParam;
import org.apache.camel.spi.UriParams;
import org.apache.camel.spi.UriPath;
-import org.apache.camel.util.ObjectHelper;
-import org.knowm.xchange.Exchange;
import org.knowm.xchange.currency.Currency;
import org.knowm.xchange.currency.CurrencyPair;
@@ -57,8 +51,6 @@ public class XChangeConfiguration {
public static final String HEADER_CURRENCY = "Currency";
public static final String HEADER_CURRENCY_PAIR = "CurrencyPair";
- static Map<String, Class<? extends Exchange>> xchangeMapping = new
HashMap<>();
-
@UriPath(description = "The exchange to connect to")
@Metadata(required = true)
private String name;
@@ -73,10 +65,6 @@ public class XChangeConfiguration {
@UriParam(description = "The currency pair")
private String currencyPair;
- public XChangeConfiguration(XChangeComponent component) {
- ObjectHelper.notNull(component, "component");
- }
-
public String getName() {
return name;
}
@@ -128,23 +116,4 @@ public class XChangeConfiguration {
this.currencyPair = currencyPair;
}
- @SuppressWarnings("unchecked")
- public Class<? extends Exchange> getXChangeClass() {
- Class<? extends Exchange> xchangeClass = xchangeMapping.get(name);
- if (xchangeClass == null) {
- String firstUpper = name.substring(0, 1).toUpperCase() +
name.substring(1);
- String className = "org.knowm.xchange." + name + "." + firstUpper
+ "Exchange";
- ClassLoader classLoader = getClass().getClassLoader();
- try {
- xchangeClass = (Class<? extends Exchange>)
classLoader.loadClass(className);
- } catch (ClassNotFoundException e) {
- // ignore
- }
- }
- return xchangeClass;
- }
-
- public Set<String> getSupportedXChangeNames() {
- return xchangeMapping.keySet();
- }
}
diff --git
a/components/camel-xchange/src/main/java/org/apache/camel/component/xchange/XChangeHelper.java
b/components/camel-xchange/src/main/java/org/apache/camel/component/xchange/XChangeHelper.java
new file mode 100644
index 0000000..f785800
--- /dev/null
+++
b/components/camel-xchange/src/main/java/org/apache/camel/component/xchange/XChangeHelper.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.xchange;
+
+import org.apache.camel.CamelContext;
+import org.knowm.xchange.Exchange;
+
+public final class XChangeHelper {
+
+ private XChangeHelper() {
+ }
+
+ public static Class<? extends Exchange> loadXChangeClass(CamelContext
camelContext, String name) {
+ String firstUpper = name.substring(0, 1).toUpperCase() +
name.substring(1);
+ String className = "org.knowm.xchange." + name + "." + firstUpper +
"Exchange";
+ try {
+ return camelContext.getClassResolver().resolveClass(className,
Exchange.class);
+ } catch (Exception e) {
+ // ignore
+ }
+ return null;
+ }
+}
diff --git
a/components/camel-xchange/src/test/java/org/apache/camel/component/xchange/account/AccountProducerTest.java
b/components/camel-xchange/src/test/java/org/apache/camel/component/xchange/account/AccountProducerTest.java
index ea95037..2b8463d 100644
---
a/components/camel-xchange/src/test/java/org/apache/camel/component/xchange/account/AccountProducerTest.java
+++
b/components/camel-xchange/src/test/java/org/apache/camel/component/xchange/account/AccountProducerTest.java
@@ -36,7 +36,6 @@ public class AccountProducerTest extends CamelTestSupport {
return new RouteBuilder() {
@Override
public void configure() {
-
from("direct:balances")
.to("xchange:binance?service=account&method=balances");
@@ -50,9 +49,7 @@ public class AccountProducerTest extends CamelTestSupport {
}
@Test
- @SuppressWarnings("unchecked")
- void testBalances() {
-
+ public void testBalances() {
assumeTrue(hasAPICredentials());
List<Balance> balances = template.requestBody("direct:balances", null,
List.class);
@@ -60,9 +57,7 @@ public class AccountProducerTest extends CamelTestSupport {
}
@Test
- @SuppressWarnings("unchecked")
- void testWallets() {
-
+ public void testWallets() {
assumeTrue(hasAPICredentials());
List<Wallet> wallets = template.requestBody("direct:wallets", null,
List.class);
@@ -70,9 +65,7 @@ public class AccountProducerTest extends CamelTestSupport {
}
@Test
- @SuppressWarnings("unchecked")
- void testFundingHistory() {
-
+ public void testFundingHistory() {
assumeTrue(hasAPICredentials());
List<FundingRecord> records =
template.requestBody("direct:fundingHistory", null, List.class);
@@ -81,6 +74,6 @@ public class AccountProducerTest extends CamelTestSupport {
private boolean hasAPICredentials() {
XChangeComponent component = context().getComponent("xchange",
XChangeComponent.class);
- return component.getXChange().getExchangeSpecification().getApiKey()
!= null;
+ return
component.getXChange("binance").getExchangeSpecification().getApiKey() != null;
}
}