Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import io.quarkus.deployment.builditem.ApplicationArchivesBuildItem;
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import io.quarkus.deployment.pkg.steps.NativeOrNativeSourcesBuild;
import io.quarkus.maven.dependency.ArtifactKey;
import io.quarkus.runtime.RuntimeValue;
import io.smallrye.common.annotation.Identifier;
Expand Down Expand Up @@ -108,6 +110,8 @@ class CamelProcessor {
"org.apache.camel.Predicate");
private static final DotName CONVERTER_TYPE = DotName.createSimple(
"org.apache.camel.Converter");
private static final DotName TRANSFORMER_TYPE = DotName.createSimple(
"org.apache.camel.spi.Transformer");

private static final Set<DotName> UNREMOVABLE_BEANS_TYPES = CamelSupport.setOf(
ROUTES_BUILDER_TYPE,
Expand Down Expand Up @@ -431,6 +435,49 @@ CamelComponentNameResolverBuildItem componentNameResolver(
return new CamelComponentNameResolverBuildItem(recorder.createComponentNameResolver(componentNames));
}

/**
* Discovers all Transformer implementations for package scanning and reflection.
* This enables transformer.scan() to work in native mode.
*/
@BuildStep(onlyIf = NativeOrNativeSourcesBuild.class)
void discoverTransformers(
ApplicationArchivesBuildItem applicationArchives,
CombinedIndexBuildItem combinedIndex,
BuildProducer<CamelPackageScanClassBuildItem> packageScanClass,
BuildProducer<ReflectiveClassBuildItem> reflectiveClass) {

IndexView index = combinedIndex.getIndex();

Set<String> internalTransformers = new HashSet<>();
// Ignore all Transformer implementations from org.apache.camel:camel-* dependencies
for (ApplicationArchive archive : applicationArchives.getAllApplicationArchives()) {
ArtifactKey artifactKey = archive.getKey();
if (artifactKey != null && "org.apache.camel".equals(artifactKey.getGroupId())
&& artifactKey.getArtifactId().startsWith("camel-")) {
internalTransformers.addAll(archive.getIndex().getAllKnownSubclasses(TRANSFORMER_TYPE)
.stream()
.map(classInfo -> classInfo.name().toString())
.collect(Collectors.toSet()));
}
}

Set<String> transformerClasses = index.getAllKnownSubclasses(TRANSFORMER_TYPE)
.stream()
.map(classInfo -> classInfo.name().toString())
.filter(className -> !internalTransformers.contains(className))
.collect(Collectors.toSet());

if (!transformerClasses.isEmpty()) {
LOGGER.debug("Found Transformer classes: {}", transformerClasses);
packageScanClass.produce(new CamelPackageScanClassBuildItem(transformerClasses));

// Register transformer classes for reflection so they can be instantiated at runtime
transformerClasses.forEach(className -> reflectiveClass.produce(
ReflectiveClassBuildItem.builder(className)
.build()));
}
}

@Record(ExecutionTime.STATIC_INIT)
@BuildStep
CamelPackageScanClassResolverBuildItem packageScanClassResolver(
Expand Down
17 changes: 17 additions & 0 deletions integration-test-groups/foundation/transformer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-jackson</artifactId>
</dependency>

<!-- test dependencies -->
<dependency>
Expand Down Expand Up @@ -106,6 +110,19 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-jackson-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-log-deployment</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.transformer;

import org.apache.camel.builder.RouteBuilder;

public class CustomTransformerRoutes extends RouteBuilder {
@Override
public void configure() throws Exception {
transformer()
.fromType("plain/text")
.toType("plain/lowercase")
.withJava(org.apache.camel.quarkus.transformer.LowercaseTransformer.class);

from("direct:stringToLowercase")
.inputType("plain/text")
.outputType("plain/lowercase")
.log("transformed message to lowercase");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.transformer;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.dataformat.JsonDataFormat;
import org.apache.camel.model.dataformat.JsonLibrary;

public class DataFormatRoutes extends RouteBuilder {
@Override
public void configure() throws Exception {

JsonDataFormat jsondf = new JsonDataFormat()
.library(JsonLibrary.Jackson)
.allowUnmarshallType(true)
.unmarshalType(TransformerBean.class);

transformer()
.fromType(TransformerBean.class)
.toType("json")
.withDataFormat(jsondf);

from("direct:transformBeanToJson")
.inputType(TransformerBean.class)
.outputType("json")
.log("Transformed TransformerBean to json");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.transformer;

import org.apache.camel.builder.RouteBuilder;

public class DataTypeTransformerRoutes extends RouteBuilder {
@Override
public void configure() throws Exception {

transformer()
.scan("org.apache.camel.quarkus.transformer");

from("direct:stringToUppercase")
.inputType("plain/text")
.outputType("plain/uppercase")
.log("Transformed message to Uppercase");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.transformer;

import org.apache.camel.builder.RouteBuilder;

public class EndpointTransformerRoutes extends RouteBuilder {
@Override
public void configure() throws Exception {
transformer()
.fromType("plain/text")
.toType("plain/reversed")
.withUri("direct:reverseProcessor");

// Endpoint that does the actual transformation
from("direct:reverseProcessor")
.process(exchange -> {
String body = exchange.getMessage().getBody(String.class);
String reversed = new StringBuilder(body).reverse().toString();
exchange.getMessage().setBody("Transformed " + reversed);
});

from("direct:stringToReversed")
.inputType("plain/text")
.outputType("plain/reversed")
.log("Transformed message to reversed");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.transformer;

import org.apache.camel.Message;
import org.apache.camel.spi.DataType;
import org.apache.camel.spi.Transformer;

/**
* Lowercase Transformer
*/
public class LowercaseTransformer extends Transformer {

@Override
public void transform(Message message, DataType fromType, DataType toType) {
message.setBody(("Transformed " + message.getBody(String.class)).toLowerCase());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.transformer;

import org.apache.camel.builder.RouteBuilder;

public class NamedTransformerRoutes extends RouteBuilder {
@Override
public void configure() throws Exception {
from("direct:stringToTrimmed")
.inputType("plain/text")
.outputType("plain/trimmed")
.log("Transformed message to trimmed");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@

import io.quarkus.runtime.annotations.RegisterForReflection;

@RegisterForReflection(fields = false, methods = false)
@RegisterForReflection
public class TransformerBean {
private final String message;

public TransformerBean(String message) {
this.message = message;
}

public String getMessage() {
return message;
}

@Override
public String toString() {
return "Transformed " + message;
Expand Down
Loading