This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-4.8.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-4.8.x by this push: new 56dd835190f CAMEL-21516: camel-jbang - Transform route from xml to yaml with uri-as-parameters for OSGi blueprint 56dd835190f is described below commit 56dd835190f9c99b541b94b97bf90134f18e2510 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Fri Dec 6 11:33:48 2024 +0100 CAMEL-21516: camel-jbang - Transform route from xml to yaml with uri-as-parameters for OSGi blueprint --- .../dsl/jbang/core/commands/TransformTest.java | 19 +++++++++- .../src/test/resources/blueprint-out.yaml | 39 ++++++++++++++++++++ .../src/test/resources/blueprint.xml | 41 ++++++++++++++++++++++ .../java/org/apache/camel/main/KameletMain.java | 23 +++++++----- .../DependencyDownloaderComponentResolver.java | 13 +++++-- 5 files changed, 124 insertions(+), 11 deletions(-) diff --git a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/TransformTest.java b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/TransformTest.java index 281832614be..e13048f3862 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/TransformTest.java +++ b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/TransformTest.java @@ -65,10 +65,27 @@ class TransformTest { Assertions.assertEquals(expected, data); } + @Test + public void shouldTransformBlueprintToYaml() throws Exception { + String name = workingDir + "/blueprint.yaml"; + File out = new File(name); + + String[] args = new String[] { "--output=" + out.getPath() }; + TransformRoute command = createCommand(new String[] { "src/test/resources/blueprint.xml" }, args); + int exit = command.doCall(); + Assertions.assertEquals(0, exit); + + Assertions.assertTrue(out.exists()); + String data = IOHelper.loadText(new FileInputStream(out)); + String expected + = IOHelper.stripLineComments(Paths.get("src/test/resources/blueprint-out.yaml"), "#", true); + Assertions.assertEquals(expected, data); + } + private TransformRoute createCommand(String[] files, String... args) { TransformRoute command = new TransformRoute(new CamelJBangMain()); - CommandLine.populateCommand(command, "--format=yaml", "--uri-as-parameters"); + CommandLine.populateCommand(command, "--format=yaml"); if (args != null) { CommandLine.populateCommand(command, args); } diff --git a/dsl/camel-jbang/camel-jbang-core/src/test/resources/blueprint-out.yaml b/dsl/camel-jbang/camel-jbang-core/src/test/resources/blueprint-out.yaml new file mode 100644 index 00000000000..dd0c4cc0107 --- /dev/null +++ b/dsl/camel-jbang/camel-jbang-core/src/test/resources/blueprint-out.yaml @@ -0,0 +1,39 @@ +# +# 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. +# + +- beans: + - name: userService + type: "org.apache.camel.example.rest.UserService" +- route: + from: + uri: direct + parameters: + name: getUser + steps: + - to: + uri: bean + parameters: + beanName: userService + method: "getUser(${header.id})" + - filter: + simple: + expression: "${body} == null" + steps: + - setHeader: + name: Exchange.HTTP_RESPONSE_CODE + constant: + expression: 404 \ No newline at end of file diff --git a/dsl/camel-jbang/camel-jbang-core/src/test/resources/blueprint.xml b/dsl/camel-jbang/camel-jbang-core/src/test/resources/blueprint.xml new file mode 100644 index 00000000000..adcdee6dfe3 --- /dev/null +++ b/dsl/camel-jbang/camel-jbang-core/src/test/resources/blueprint.xml @@ -0,0 +1,41 @@ +<?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. + +--> +<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.osgi.org/xmlns/blueprint/v1.0.0 https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"> + + <!-- a bean for user services --> + <bean id="userService" class="org.apache.camel.example.rest.UserService"/> + + <camelContext id="myCamel" xmlns="http://camel.apache.org/schema/blueprint"> + <route> + <from uri="direct:getUser"/> + <to uri="bean:userService?method=getUser(${header.id})"/> + <filter> + <simple>${body} == null</simple> + <setHeader name="Exchange.HTTP_RESPONSE_CODE"> + <constant>404</constant> + </setHeader> + </filter> + </route> + </camelContext> + +</blueprint> 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 9d33203742d..36961db5834 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 @@ -484,6 +484,20 @@ public class KameletMain extends MainCommandLineSupport { // in case we use saga SagaDownloader.registerDownloadReifiers(this); + // if transforming DSL then disable processors as we just want to work on the model (not runtime processors) + boolean transform = "true".equals(getInitialProperties().get("camel.jbang.transform")); + if (transform) { + // we just want to transform, so disable custom bean or processors as they may use code that does not work + answer.getGlobalOptions().put(ProcessorReifier.DISABLE_BEAN_OR_PROCESS_PROCESSORS, "true"); + // stub everything + this.stubPattern = "*"; + blueprintXmlBeansHandler.setTransform(true); + } + if (silent) { + // silent should not include http server + configure().httpServer().withEnabled(false); + } + if (silent || "*".equals(stubPattern)) { // turn off auto-wiring when running in silent mode (or stub = *) mainConfigurationProperties.setAutowiredEnabled(false); @@ -571,13 +585,6 @@ public class KameletMain extends MainCommandLineSupport { answer.getPropertiesComponent().setIgnoreMissingProperty(true); answer.getPropertiesComponent().setIgnoreMissingLocation(true); } - // if transforming DSL then disable processors as we just want to work on the model (not runtime processors) - boolean transform = "true".equals(getInitialProperties().get("camel.jbang.transform")); - if (transform) { - // we just want to transform, so disable custom bean or processors as they may use code that does not work - answer.getGlobalOptions().put(ProcessorReifier.DISABLE_BEAN_OR_PROCESS_PROCESSORS, "true"); - blueprintXmlBeansHandler.setTransform(true); - } if (silent) { // silent should not include http server configure().httpServer().withEnabled(false); @@ -640,7 +647,7 @@ public class KameletMain extends MainCommandLineSupport { answer.getCamelContextExtension().setDefaultFactoryFinder(ff); answer.getCamelContextExtension().addContextPlugin(ComponentResolver.class, - new DependencyDownloaderComponentResolver(answer, stubPattern, silent)); + new DependencyDownloaderComponentResolver(answer, stubPattern, silent, transform)); answer.getCamelContextExtension().addContextPlugin(DataFormatResolver.class, new DependencyDownloaderDataFormatResolver(answer, stubPattern, silent)); answer.getCamelContextExtension().addContextPlugin(LanguageResolver.class, diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderComponentResolver.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderComponentResolver.java index e54bb6cf114..9ab484698bc 100644 --- a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderComponentResolver.java +++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderComponentResolver.java @@ -37,17 +37,23 @@ public final class DependencyDownloaderComponentResolver extends DefaultComponen private static final String ACCEPTED_STUB_NAMES = "stub,bean,class,direct,kamelet,log,platform-http,rest,seda,vertx-http"; + private static final String ACCEPTED_TRANSFORM_NAMES + = "stub,direct,kamelet,log,seda"; + private final CamelCatalog catalog = new DefaultCamelCatalog(); private final CamelContext camelContext; private final DependencyDownloader downloader; private final String stubPattern; private final boolean silent; + private final boolean transform; - public DependencyDownloaderComponentResolver(CamelContext camelContext, String stubPattern, boolean silent) { + public DependencyDownloaderComponentResolver(CamelContext camelContext, String stubPattern, boolean silent, + boolean transform) { this.camelContext = camelContext; this.downloader = camelContext.hasService(DependencyDownloader.class); this.stubPattern = stubPattern; this.silent = silent; + this.transform = transform; } @Override @@ -64,7 +70,7 @@ public final class DependencyDownloaderComponentResolver extends DefaultComponen } else { answer = super.resolveComponent("stub", context); } - if ((silent || stubPattern != null) && answer instanceof StubComponent) { + if ((silent || transform || stubPattern != null) && answer instanceof StubComponent) { StubComponent sc = (StubComponent) answer; // enable shadow mode on stub component sc.setShadow(true); @@ -112,6 +118,9 @@ public final class DependencyDownloaderComponentResolver extends DefaultComponen } private boolean accept(String name) { + if (transform) { + return ACCEPTED_TRANSFORM_NAMES.contains(name); + } if (stubPattern == null) { return true; }