Skip to content

Commit

Permalink
Add BeanIO native support
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesnetherton committed Oct 22, 2024
1 parent 4782c6f commit 44da257
Show file tree
Hide file tree
Showing 38 changed files with 290 additions and 51 deletions.

This file was deleted.

1 change: 0 additions & 1 deletion extensions-jvm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
<module>azure-servicebus</module>
<module>azure-storage-datalake</module>
<module>barcode</module>
<module>beanio</module>
<module>bonita</module>
<module>chatscript</module>
<module>chunk</module>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<name>Camel Quarkus :: BeanIO :: Deployment</name>

<dependencies>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-xml-jaxp-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,137 @@
/*
* 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.beanio.deployment;

import java.io.IOException;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;

import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.IndexDependencyBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBundleBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import org.beanio.BeanReaderErrorHandler;
import org.beanio.annotation.Record;
import org.beanio.stream.RecordParserFactory;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.AnnotationTarget.Kind;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.IndexView;

class BeanioProcessor {
private static final String FEATURE = "camel-beanio";

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

@BuildStep
IndexDependencyBuildItem indexDependencies() {
return new IndexDependencyBuildItem("com.github.beanio", "beanio");
}

@BuildStep
BeanioPropertiesBuildItem beanioProperties() {
try {
Properties properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("org/beanio/internal/config/beanio.properties"));
return new BeanioPropertiesBuildItem(properties);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@BuildStep
void nativeImageResources(BuildProducer<NativeImageResourceBuildItem> nativeImageResource) {
nativeImageResource.produce(new NativeImageResourceBuildItem("org/beanio/internal/config/beanio.properties"));
nativeImageResource.produce(new NativeImageResourceBuildItem("beanio.properties"));
}

@BuildStep
void registerForReflection(
BeanioPropertiesBuildItem beanioProperties,
CombinedIndexBuildItem combinedIndex,
BuildProducer<ReflectiveClassBuildItem> reflectiveClass) {
Properties properties = beanioProperties.getProperties();

Set<String> handlersAndFactories = properties.keySet()
.stream()
.filter(key -> key.toString().contains("Factory") || key.toString().contains("Handler"))
.map(properties::get)
.map(Object::toString)
.collect(Collectors.toUnmodifiableSet());
reflectiveClass.produce(ReflectiveClassBuildItem.builder(handlersAndFactories.toArray(new String[0])).build());

IndexView index = combinedIndex.getIndex();
Set<String> recordParsers = index.getAllKnownImplementors(RecordParserFactory.class)
.stream()
.map(ClassInfo::name)
.map(DotName::toString)
.collect(Collectors.toUnmodifiableSet());
reflectiveClass.produce(ReflectiveClassBuildItem.builder(recordParsers.toArray(new String[0])).methods(true).build());

Set<String> parserConfiguration = index.getKnownClasses()
.stream()
.map(ClassInfo::name)
.map(DotName::toString)
.filter(name -> name.startsWith("org.beanio") && name.endsWith("ParserConfiguration"))
.collect(Collectors.toUnmodifiableSet());
reflectiveClass
.produce(ReflectiveClassBuildItem.builder(parserConfiguration.toArray(new String[0])).methods(true).build());

Set<String> errorHandlers = index.getAllKnownImplementors(BeanReaderErrorHandler.class)
.stream()
.map(ClassInfo::name)
.map(DotName::toString)
.collect(Collectors.toUnmodifiableSet());
reflectiveClass.produce(ReflectiveClassBuildItem.builder(errorHandlers.toArray(new String[0])).build());

Set<String> recordClasses = index.getAnnotations(Record.class)
.stream()
.map(AnnotationInstance::target)
.filter(target -> target.kind().equals(Kind.CLASS))
.map(AnnotationTarget::asClass)
.map(ClassInfo::name)
.map(DotName::toString)
.collect(Collectors.toUnmodifiableSet());
reflectiveClass
.produce(ReflectiveClassBuildItem.builder(recordClasses.toArray(new String[0])).fields(true).methods(true)
.build());
}

@BuildStep
void registerResourceBundles(BeanioPropertiesBuildItem beanioProperties,
BuildProducer<NativeImageResourceBundleBuildItem> nativeImageResourceBundle) {
Properties properties = beanioProperties.getProperties();
properties.keySet()
.stream()
.filter(key -> key.toString().endsWith(".messages"))
.map(properties::get)
.map(Object::toString)
.map(NativeImageResourceBundleBuildItem::new)
.forEach(nativeImageResourceBundle::produce);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.apache.camel.quarkus.component.beanio.deployment;

import java.util.Properties;

import io.quarkus.builder.item.SimpleBuildItem;

public final class BeanioPropertiesBuildItem extends SimpleBuildItem {
private final Properties properties;

public BeanioPropertiesBuildItem(Properties properties) {
this.properties = properties;
}

public Properties getProperties() {
return properties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-extensions-jvm</artifactId>
<artifactId>camel-quarkus-extensions</artifactId>
<version>3.16.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@

<properties>
<camel.quarkus.jvmSince>3.8.0</camel.quarkus.jvmSince>
<camel.quarkus.nativeSince>3.16.0</camel.quarkus.nativeSince>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-xml-jaxp</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-core</artifactId>
Expand Down
31 changes: 31 additions & 0 deletions extensions/beanio/runtime/src/main/doc/usage.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
=== BeanIO in native mode

==== XML mapping files

When BeanIO configuration is defined in XML files that are read from the classpath.
You must ensure each file is added to the native application image.
To do this, add the `quarkus.native.resources.includes` configuration property to `application.properties`. For example.

[source,properties]
----
quarkus.native.resources.includes=mapping.xml,model/other-mapping.xml
----

More information about selecting resources for inclusion in the native executable can be found at xref:user-guide/native-mode.adoc#embedding-resource-in-native-executable[Embedding resources in native executable].

==== BeanIO Record classes

All classes that participate in BeanIO marshal / unmarshal operations must be registered for reflection.

This can be achieved with the `@RegisterForReflection`
annotation or with configuration property `quarkus.camel.native.reflection.include-patterns`. For example:

[source,java]
----
@RegisterForReflection
public class Employee {
...
}
----

Refer to the xref:user-guide/native-mode.adoc#reflection[Native mode] user guide for more information.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ description: "Marshal and unmarshal Java beans to and from flat files (such as C
metadata:
icon-url: "https://raw.githubusercontent.com/apache/camel-website/main/antora-ui-camel/src/img/logo-d.svg"
sponsor: "Apache Software Foundation"
unlisted: true
guide: "https://camel.apache.org/camel-quarkus/latest/reference/extensions/beanio.html"
categories:
- "integration"
status:
- "preview"
- "stable"
1 change: 1 addition & 0 deletions extensions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<module>base64</module>
<module>bean</module>
<module>bean-validator</module>
<module>beanio</module>
<module>bindy</module>
<module>box</module>
<module>braintree</module>
Expand Down
1 change: 0 additions & 1 deletion integration-tests-jvm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
<module>azure-servicebus</module>
<module>azure-storage-datalake</module>
<module>barcode</module>
<module>beanio</module>
<module>bonita</module>
<module>chatscript</module>
<module>chunk</module>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,33 @@
</dependency>
</dependencies>
</profile>
<profile>
<id>native</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<properties>
<quarkus.native.enabled>true</quarkus.native.enabled>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
package org.apache.camel.quarkus.component.beanio.it.model;

import io.quarkus.runtime.annotations.RegisterForReflection;

@RegisterForReflection
public class A1Record extends AbstractRecord {
Double currentPrice;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
package org.apache.camel.quarkus.component.beanio.it.model;

import io.quarkus.runtime.annotations.RegisterForReflection;

@RegisterForReflection
public abstract class AbstractRecord {
String sedol;
String source;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
package org.apache.camel.quarkus.component.beanio.it.model;

import io.quarkus.runtime.annotations.RegisterForReflection;

@RegisterForReflection
public class B1Record extends AbstractRecord {
String securityName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

import java.util.Date;

import io.quarkus.runtime.annotations.RegisterForReflection;

@RegisterForReflection
public class Employee {
private String firstName;
private String lastName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
package org.apache.camel.quarkus.component.beanio.it.model;

import io.quarkus.runtime.annotations.RegisterForReflection;

@RegisterForReflection
public class Error {
private final String record;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import java.util.HashMap;
import java.util.Map;

import io.quarkus.runtime.annotations.RegisterForReflection;
import org.apache.camel.dataformat.beanio.BeanIOHeader;

@RegisterForReflection
public class Header implements BeanIOHeader {
String identifier;
String recordType;
Expand Down
Loading

0 comments on commit 44da257

Please sign in to comment.