This is an automated email from the ASF dual-hosted git repository. zhfeng 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 42bfeb85612 CAMEL-21117: camel-jbang - Detect transacted dsl and add camel-jta automatically (#15318) 42bfeb85612 is described below commit 42bfeb85612a8e8f67dfc41082179b4677e200e4 Author: Zheng Feng <zh.f...@gmail.com> AuthorDate: Tue Aug 27 10:32:39 2024 +0800 CAMEL-21117: camel-jbang - Detect transacted dsl and add camel-jta automatically (#15318) --- .../java/org/apache/camel/main/KameletMain.java | 4 ++ .../camel/main/download/TransactedDownloader.java | 75 ++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java index 22290636038..f2e203a5e24 100644 --- a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java +++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java @@ -67,6 +67,7 @@ import org.apache.camel.main.download.MavenDependencyDownloader; import org.apache.camel.main.download.PackageNameSourceLoader; import org.apache.camel.main.download.PromptPropertyPlaceholderSource; import org.apache.camel.main.download.StubBeanRepository; +import org.apache.camel.main.download.TransactedDownloader; import org.apache.camel.main.download.TypeConverterLoaderDownloadListener; import org.apache.camel.main.injection.AnnotationDependencyInjection; import org.apache.camel.main.reload.OpenApiGeneratorReloadStrategy; @@ -453,6 +454,9 @@ public class KameletMain extends MainCommandLineSupport { // in case we use circuit breakers CircuitBreakerDownloader.registerDownloadReifiers(); + // in case we use transacted + TransactedDownloader.registerDownloadReifiers(this); + if (silent || "*".equals(stubPattern)) { // turn off auto-wiring when running in silent mode (or stub = *) mainConfigurationProperties.setAutowiredEnabled(false); diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/TransactedDownloader.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/TransactedDownloader.java new file mode 100644 index 00000000000..d0fa95d2fc0 --- /dev/null +++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/TransactedDownloader.java @@ -0,0 +1,75 @@ +/* + * 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.main.download; + +import java.util.Arrays; + +import org.apache.camel.NamedNode; +import org.apache.camel.Processor; +import org.apache.camel.Route; +import org.apache.camel.main.KameletMain; +import org.apache.camel.model.TransactedDefinition; +import org.apache.camel.reifier.ProcessReifier; +import org.apache.camel.reifier.ProcessorReifier; +import org.apache.camel.spi.TransactedPolicy; + +/** + * When using transacted then we need to download the runtime implementation + */ +public class TransactedDownloader { + private static final String[] TRANSACTED_POLICIES = new String[] { + "PROPAGATION_REQUIRED", + "PROPAGATION_REQUIRES_NEW", + "PROPAGATION_SUPPORTS", + "PROPAGATION_NOT_SUPPORTED", + "PROPAGATION_NEVER", + "PROPAGATION_MANDATORY", + "PROPAGATION_NESTED" + }; + + private TransactedDownloader() { + } + + public static void registerDownloadReifiers(KameletMain main) { + + ProcessorReifier.registerReifier(TransactedDefinition.class, + (route, processorDefinition) -> { + if (processorDefinition instanceof TransactedDefinition) { + DependencyDownloader downloader = route.getCamelContext().hasService(DependencyDownloader.class); + if (downloader != null) { + downloader.downloadDependency("org.apache.camel", "camel-jta", + route.getCamelContext().getVersion()); + TransactedPolicy policy = new DummyTransactedPolicy(); + Arrays.stream(TRANSACTED_POLICIES).forEach(p -> main.bind(p, policy)); + } + } + return ProcessReifier.coreReifier(route, processorDefinition); + }); + } + + private static class DummyTransactedPolicy implements TransactedPolicy { + @Override + public void beforeWrap(final Route route, final NamedNode definition) { + + } + + @Override + public Processor wrap(final Route route, final Processor processor) { + return null; + } + } +}