Skip to content

Commit

Permalink
XChange native support
Browse files Browse the repository at this point in the history
Fixes #769
  • Loading branch information
jamesnetherton committed May 4, 2021
1 parent 096ef6c commit 7b07220
Show file tree
Hide file tree
Showing 20 changed files with 456 additions and 126 deletions.
20 changes: 15 additions & 5 deletions docs/modules/ROOT/pages/reference/extensions/xchange.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
= XChange
:linkattrs:
:cq-artifact-id: camel-quarkus-xchange
:cq-native-supported: false
:cq-status: Preview
:cq-status-deprecation: Preview
:cq-native-supported: true
:cq-status: Stable
:cq-status-deprecation: Stable
:cq-description: Access market data and trade on Bitcoin and Altcoin exchanges.
:cq-deprecated: false
:cq-jvm-since: 1.1.0
:cq-native-since: n/a
:cq-native-since: 1.9.0

[.badges]
[.badge-key]##JVM since##[.badge-supported]##1.1.0## [.badge-key]##Native##[.badge-unsupported]##unsupported##
[.badge-key]##JVM since##[.badge-supported]##1.1.0## [.badge-key]##Native since##[.badge-supported]##1.9.0##

Access market data and trade on Bitcoin and Altcoin exchanges.

Expand All @@ -24,6 +24,10 @@ Please refer to the above link for usage and configuration details.

== Maven coordinates

https://code.quarkus.io/?extension-search=camel-quarkus-xchange[Create a new project with this extension on code.quarkus.io, window="_blank"]

Or add the coordinates to your existing project:

[source,xml]
----
<dependency>
Expand All @@ -33,3 +37,9 @@ Please refer to the above link for usage and configuration details.
----

Check the xref:user-guide/index.adoc[User guide] for more information about writing Camel Quarkus applications.

== SSL in native mode

This extension auto-enables SSL support in native mode. Hence you do not need to add
`quarkus.ssl.native=true` to your `application.properties` yourself. See also
https://quarkus.io/guides/native-and-ssl[Quarkus SSL guide].
6 changes: 3 additions & 3 deletions docs/modules/ROOT/partials/reference/components/xchange.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// This file was generated by camel-quarkus-maven-plugin:update-extension-doc-page
:cq-artifact-id: camel-quarkus-xchange
:cq-artifact-id-base: xchange
:cq-native-supported: false
:cq-status: Preview
:cq-native-supported: true
:cq-status: Stable
:cq-deprecated: false
:cq-jvm-since: 1.1.0
:cq-native-since: n/a
:cq-native-since: 1.9.0
:cq-camel-part-name: xchange
:cq-camel-part-title: XChange
:cq-camel-part-description: Access market data and trade on Bitcoin and Altcoin exchanges.
Expand Down
1 change: 0 additions & 1 deletion extensions-jvm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@
<module>weka</module>
<module>wordpress</module>
<module>workday</module>
<module>xchange</module>
<module>xj</module>
<module>xmpp</module>
<module>xslt-saxon</module>
Expand Down

This file was deleted.

This file was deleted.

1 change: 1 addition & 0 deletions extensions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@
<module>vertx-websocket</module>
<module>vm</module>
<module>weather</module>
<module>xchange</module>
<module>xmlsecurity</module>
<module>xpath</module>
<module>xslt</module>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<name>Camel Quarkus :: XChange :: Deployment</name>

<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jackson-deployment</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-core-deployment</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* 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.quarkus.component.xchange.deployment;

import java.lang.reflect.Modifier;
import java.util.List;

import io.quarkus.bootstrap.model.AppArtifact;
import io.quarkus.bootstrap.model.AppDependency;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
import io.quarkus.deployment.builditem.ExtensionSslNativeSupportBuildItem;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.IndexDependencyBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageProxyDefinitionBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import io.quarkus.deployment.pkg.builditem.CurateOutcomeBuildItem;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.IndexView;
import org.knowm.xchange.BaseExchange;

class XchangeProcessor {

private static final String FEATURE = "camel-xchange";

@BuildStep
FeatureBuildItem feature() {
return new FeatureBuildItem(FEATURE);
}

@BuildStep
ExtensionSslNativeSupportBuildItem activateSslNativeSupport() {
return new ExtensionSslNativeSupportBuildItem(FEATURE);
}

@BuildStep
void indexDependenciesAndResources(
BuildProducer<IndexDependencyBuildItem> indexedDependency,
BuildProducer<NativeImageResourceBuildItem> nativeImageResource,
CurateOutcomeBuildItem curateOutcome) {

List<AppDependency> userDependencies = curateOutcome.getEffectiveModel().getUserDependencies();
for (AppDependency dependency : userDependencies) {
AppArtifact artifact = dependency.getArtifact();

if (artifact.getGroupId().equals("org.knowm.xchange")) {
// Index any org.knowm.xchange dependencies present on the classpath as they contain the APIs for interacting with each crypto exchange
String artifactId = artifact.getArtifactId();
indexedDependency.produce(new IndexDependencyBuildItem(artifact.getGroupId(), artifactId));

// Include crypto exchange metadata resources
String[] split = artifactId.split("-");
if (split.length > 1) {
String cryptoExchange = split[split.length - 1];
nativeImageResource.produce(new NativeImageResourceBuildItem(cryptoExchange + ".json"));
}
}
}

indexedDependency.produce(new IndexDependencyBuildItem("org.jboss.spec.javax.ws.rs", "jboss-jaxrs-api_2.1_spec"));
}

@BuildStep
void registerForReflection(BuildProducer<ReflectiveClassBuildItem> reflectiveClass, CombinedIndexBuildItem combinedIndex) {
// The xchange component dynamically instantiates Exchange classes so they must be registered for reflection
IndexView index = combinedIndex.getIndex();
String[] xchangeClasses = index.getAllKnownSubclasses(DotName.createSimple(BaseExchange.class.getName()))
.stream()
.map(classInfo -> classInfo.name().toString())
.toArray(String[]::new);
reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, xchangeClasses));

// DTO classes need to be serialized / deserialized
String[] dtoClasses = index.getKnownClasses()
.stream()
.map(classInfo -> classInfo.name().toString())
.filter(className -> className.startsWith("org.knowm.xchange.dto"))
.toArray(String[]::new);
reflectiveClass.produce(new ReflectiveClassBuildItem(false, true, dtoClasses));

// rescu REST framework needs reflective access to the value method on some JAX-RS annotations
String[] jaxrsAnnotations = index.getKnownClasses()
.stream()
.filter(ClassInfo::isAnnotation)
.filter(classInfo -> classInfo.firstMethod("value") != null)
.map(classInfo -> classInfo.name().toString())
.filter(className -> className.startsWith("javax.ws.rs"))
.toArray(String[]::new);
reflectiveClass.produce(new ReflectiveClassBuildItem(true, false, jaxrsAnnotations));

}

@BuildStep
void registerProxyClasses(BuildProducer<NativeImageProxyDefinitionBuildItem> nativeImageProxy,
CombinedIndexBuildItem combinedIndexBuildItem) {
// Some xchange libraries use JAX-RS proxies to interact with the exchange APIs so we need to register them
IndexView index = combinedIndexBuildItem.getIndex();
index.getAnnotations(DotName.createSimple("javax.ws.rs.Path"))
.stream()
.map(AnnotationInstance::target)
.filter(target -> target.kind().equals(AnnotationTarget.Kind.CLASS))
.map(AnnotationTarget::asClass)
.filter(classInfo -> Modifier.isInterface(classInfo.flags()))
.map(classInfo -> classInfo.name().toString())
.filter(className -> className.startsWith("org.knowm.xchange"))
.map(NativeImageProxyDefinitionBuildItem::new)
.forEach(nativeImageProxy::produce);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,5 @@
<modules>
<module>deployment</module>
<module>runtime</module>
<module>integration-test</module>
</modules>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

<properties>
<camel.quarkus.jvmSince>1.1.0</camel.quarkus.jvmSince>
<camel.quarkus.nativeSince>1.9.0</camel.quarkus.nativeSince>
</properties>

<dependencyManagement>
Expand All @@ -47,13 +48,32 @@
</dependencyManagement>

<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jackson</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-xchange</artifactId>
<exclusions>
<exclusion>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.ws.rs</groupId>
<artifactId>jboss-jaxrs-api_2.1_spec</artifactId>
</dependency>
<dependency>
<groupId>org.graalvm.nativeimage</groupId>
<artifactId>svm</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

Expand Down

0 comments on commit 7b07220

Please sign in to comment.