Skip to content

Commit

Permalink
Ref #4426: Support CSimple expressions with all DSLs
Browse files Browse the repository at this point in the history
  • Loading branch information
essobedo authored and jamesnetherton committed Jan 23, 2023
1 parent 93e8725 commit 54144df
Show file tree
Hide file tree
Showing 33 changed files with 1,046 additions and 207 deletions.
26 changes: 26 additions & 0 deletions catalog/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3711,6 +3711,19 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-support-dsl</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-support-google-cloud</artifactId>
Expand Down Expand Up @@ -3789,6 +3802,19 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-support-language</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-support-mail</artifactId>
Expand Down
10 changes: 8 additions & 2 deletions docs/modules/ROOT/pages/reference/extensions/core.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,18 @@ If `true`, basic classes are registered for serialization; otherwise basic class
| `boolean`
| `false`

|icon:lock[title=Fixed at build time] [[quarkus.camel.csimple.on-build-time-analysis-failure]]`link:#quarkus.camel.csimple.on-build-time-analysis-failure[quarkus.camel.csimple.on-build-time-analysis-failure]`
|icon:lock[title=Fixed at build time] [[quarkus.camel.expression.on-build-time-analysis-failure]]`link:#quarkus.camel.expression.on-build-time-analysis-failure[quarkus.camel.expression.on-build-time-analysis-failure]`

What to do if it is not possible to extract CSimple expressions from a route definition at build time.
What to do if it is not possible to extract expressions from a route definition at build time.
| `org.apache.camel.quarkus.core.CamelConfig.FailureRemedy`
| `warn`

|icon:lock[title=Fixed at build time] [[quarkus.camel.expression.extraction-enabled]]`link:#quarkus.camel.expression.extraction-enabled[quarkus.camel.expression.extraction-enabled]`

Indicates whether the expression extraction from the route definitions at build time must be done. If disabled, the expressions are compiled at runtime.
| `boolean`
| `true`

|icon:lock[title=Fixed at build time] [[quarkus.camel.event-bridge.enabled]]`link:#quarkus.camel.event-bridge.enabled[quarkus.camel.event-bridge.enabled]`

Whether to enable the bridging of Camel events to CDI events.
Expand Down
25 changes: 19 additions & 6 deletions docs/modules/ROOT/pages/reference/extensions/csimple.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,36 @@ endif::[]
[id="extensions-csimple-camel-quarkus-limitations"]
== Camel Quarkus limitations

CSimple language is supported only in
CSimple language is supported in all existing DSLs, but be aware that to extract the expressions from the existing route definitions, a dry run is done during the startup in JVM mode or during the build in Native mode which adds some log entries of the following type that you can simply ignore.

* XML DSL
* Java DSL when implemented in a class extending `org.apache.camel.builder.RouteBuilder`
[source,text]
----
2023-01-19 16:36:31,779 INFO [org.apa.cam.mai.MainSupport] (build-10) Expression Extractor 3.20.1 is starting
2023-01-19 16:36:32,214 INFO [org.apa.cam.imp.eng.AbstractCamelContext] (build-10) Apache Camel 3.20.1 (Expression Extractor) is starting
2023-01-19 16:36:32,216 INFO [org.apa.cam.imp.eng.AbstractCamelContext] (build-10) Routes startup (started:0)
2023-01-19 16:36:32,217 INFO [org.apa.cam.imp.eng.AbstractCamelContext] (build-10) Apache Camel 3.20.1 (Expression Extractor) started in 128ms (build:22ms init:104ms start:2ms)
2023-01-19 16:36:32,218 INFO [org.apa.cam.mai.MainSupport] (build-10) Waiting until complete: Duration max 1 seconds
2023-01-19 16:36:33,223 INFO [org.apa.cam.mai.MainSupport] (build-10) Duration max seconds triggering shutdown of the JVM
2023-01-19 16:36:33,223 INFO [org.apa.cam.mai.MainSupport] (build-10) Expression Extractor 3.20.1 shutdown
----

The compilation of CSimple scripts happens at build time. To be able to discover the scripts in the route definitions,
The compilation of CSimple scripts happens at build time by default. To be able to discover the scripts in the route definitions,
those definitions need to get assembled at build time. When extending
`org.apache.camel.builder.RouteBuilder`, this basically means instantiating your `RouteBuilder`, setting
a fake `CamelContext` instance on it and calling its `configure()` method. It may fail if your `configure()`
method attempts to access some resource that is only available at runtime.

You can use the `quarkus.camel.csimple.on-build-time-analysis-failure` configuration parameter to decide
You can use the `quarkus.camel.expression.on-build-time-analysis-failure` configuration parameter to decide
what should happen in such cases. Possible values are `warn` (default), `fail` or `ignore`.

You can also simply disable the expression extraction process by setting the configuration parameter `quarkus.camel.expression.extraction-enabled` to `false`.

If the expression extraction process is disabled or fails, the expressions are then compiled at runtime which also means that it
can only work in JVM mode moreover the artifact `org.apache.camel:camel-csimple-joor` has to be added to the classpath.

[WARNING]
====
CSimple language will not work on Camel Quarkus if used in a `org.apache.camel.builder.LambdaRouteBuilder`
CSimple language will not work on Camel Quarkus if used in a `org.apache.camel.builder.LambdaRouteBuilder` or if a `RouteBuilder` depends on dependency injection.
====


Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,16 @@ public enum FailureRemedy {
/**
* Build time configuration options for the Camel CSimple language.
*/
@Deprecated(forRemoval = true)
@ConfigItem
public CSimpleConfig csimple;

/**
* Build time configuration options for the extraction of Camel expressions.
*/
@ConfigItem
public ExpressionConfig expression;

/**
* Build time configuration options for the Camel CDI event bridge.
*/
Expand Down Expand Up @@ -358,6 +365,10 @@ public static class RuntimeCatalogConfig {
public boolean models;
}

/**
* @deprecated use {@link ExpressionConfig} instead
*/
@Deprecated(forRemoval = true)
@ConfigGroup
public static class CSimpleConfig {

Expand All @@ -366,6 +377,21 @@ public static class CSimpleConfig {
public FailureRemedy onBuildTimeAnalysisFailure;
}

@ConfigGroup
public static class ExpressionConfig {

/** What to do if it is not possible to extract expressions from a route definition at build time. */
@ConfigItem(defaultValue = "warn")
public FailureRemedy onBuildTimeAnalysisFailure;

/**
* Indicates whether the expression extraction from the route definitions at build time must be done. If
* disabled, the expressions are compiled at runtime.
*/
@ConfigItem(defaultValue = "true")
public boolean extractionEnabled;
}

@ConfigGroup
public static class EventBridgeConfig {

Expand Down
65 changes: 65 additions & 0 deletions extensions-support/language/deployment/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-support-language-parent</artifactId>
<version>2.17.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>camel-quarkus-support-language-deployment</artifactId>
<name>Camel Quarkus :: Support :: Language :: Deployment</name>

<dependencies>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-core-deployment</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-support-language</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-main</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-extension-processor</artifactId>
<version>${quarkus.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.support.language.deployment;

import io.quarkus.builder.item.MultiBuildItem;

/**
* {@code ExpressionBuildItem} represents an expression in a given language that has been extracted from the route
* definitions.
*/
public final class ExpressionBuildItem extends MultiBuildItem {

final String language;
final String expression;
final boolean predicate;

public ExpressionBuildItem(String language, String expression, boolean predicate) {
this.language = language;
this.expression = expression;
this.predicate = predicate;
}

public String getLanguage() {
return language;
}

public String getExpression() {
return expression;
}

public boolean isPredicate() {
return predicate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.support.language.deployment;

import io.quarkus.builder.item.SimpleBuildItem;

/**
* {@code ExpressionBuildItem} represents the result of the expression extraction process.
*/
public final class ExpressionExtractionResultBuildItem extends SimpleBuildItem {

final boolean success;

public ExpressionExtractionResultBuildItem(boolean success) {
this.success = success;
}

public boolean isSuccess() {
return success;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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.support.language.deployment;

import java.util.ArrayList;
import java.util.List;

import io.quarkus.bootstrap.classloading.QuarkusClassLoader;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.quarkus.core.CamelConfig;
import org.apache.camel.quarkus.core.deployment.spi.CamelRoutesBuilderClassBuildItem;
import org.apache.camel.quarkus.support.language.deployment.dm.DryModeLanguage;
import org.apache.camel.quarkus.support.language.deployment.dm.DryModeMain;
import org.jboss.logging.Logger;

class LanguageSupportProcessor {

private static final Logger LOG = Logger.getLogger(LanguageSupportProcessor.class);

@BuildStep
ExpressionExtractionResultBuildItem extractExpressions(CamelConfig config,
List<CamelRoutesBuilderClassBuildItem> routesBuilderClasses,
BuildProducer<ExpressionBuildItem> producer) throws Exception {
if (config.expression.extractionEnabled) {
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
if (!(loader instanceof QuarkusClassLoader)) {
throw new IllegalStateException(
QuarkusClassLoader.class.getSimpleName() + " expected as the context class loader");
}
final List<Class<?>> routeBuilderClasses = new ArrayList<>(routesBuilderClasses.size());
for (CamelRoutesBuilderClassBuildItem routesBuilderClass : routesBuilderClasses) {
final String className = routesBuilderClass.getDotName().toString();
final Class<?> cl = loader.loadClass(className);

if (RouteBuilder.class.isAssignableFrom(cl)) {
routeBuilderClasses.add(cl);
} else {
LOG.warnf("Language expressions occurring in %s won't be compiled at build time", cl);
}
}
try {
DryModeMain main = new DryModeMain("Expression Extractor", routeBuilderClasses.toArray(new Class<?>[0]));
main.start();
main.run();
for (DryModeLanguage language : main.getLanguages()) {
final String name = language.getName();
for (String exp : language.getPredicates()) {
producer.produce(new ExpressionBuildItem(name, exp, true));
}
for (String exp : language.getExpressions()) {
producer.produce(new ExpressionBuildItem(name, exp, false));
}
}
return new ExpressionExtractionResultBuildItem(true);
} catch (Exception e) {
switch (config.expression.onBuildTimeAnalysisFailure) {
case fail:
throw new RuntimeException(
"Could not extract language expressions."
+ "You may want to set quarkus.camel.expression.on-build-time-analysis-failure to warn or ignore if you do not use languages in your routes",
e);
case warn:
LOG.warn("Could not extract language expressions.", e);
break;
case ignore:
LOG.debug("Could not extract language expressions", e);
break;
default:
throw new IllegalStateException("Unexpected " + CamelConfig.FailureRemedy.class.getSimpleName() + ": "
+ config.expression.onBuildTimeAnalysisFailure);
}
}
}
return new ExpressionExtractionResultBuildItem(false);
}
}
Loading

0 comments on commit 54144df

Please sign in to comment.