You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2024/08/07 07:47:00 UTC
(camel) branch exp created (now 0c1b6cef309)
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a change to branch exp
in repository https://gitbox.apache.org/repos/asf/camel.git
at 0c1b6cef309 CAMEL-20991: camel-jbang - Make export able to complete for source code that has missing property placeholders
This branch includes the following new commits:
new 0c1b6cef309 CAMEL-20991: camel-jbang - Make export able to complete for source code that has missing property placeholders
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(camel) 01/01: CAMEL-20991: camel-jbang - Make export able to complete for source code that has missing property placeholders
Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch exp
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 0c1b6cef309e68b0d78aaa147f08cd80e46c7131
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Aug 7 09:45:39 2024 +0200
CAMEL-20991: camel-jbang - Make export able to complete for source code that has missing property placeholders
---
.../component/PropertyConfigurerSupport.java | 10 ++++-
.../java/org/apache/camel/main/KameletMain.java | 10 +++++
.../main/download/ExportPropertiesParser.java | 37 +++++++++++++++++
.../camel/main/download/ExportTypeConverter.java | 46 ++++++++++++++++++++++
4 files changed, 102 insertions(+), 1 deletion(-)
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/component/PropertyConfigurerSupport.java b/core/camel-support/src/main/java/org/apache/camel/support/component/PropertyConfigurerSupport.java
index d5db3689c27..984c1d954a6 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/component/PropertyConfigurerSupport.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/component/PropertyConfigurerSupport.java
@@ -31,6 +31,11 @@ import org.apache.camel.util.TimeUtils;
*/
public abstract class PropertyConfigurerSupport {
+ /**
+ * A special magic value that are used by tooling such as camel-jbang export
+ */
+ public static final String MAGIC_VALUE = "@@CamelMagicValue@@";
+
/**
* Converts the property to the expected type
*
@@ -83,7 +88,7 @@ public abstract class PropertyConfigurerSupport {
// special for boolean values with string values as we only want to accept "true" or "false"
if ((type == Boolean.class || type == boolean.class) && value instanceof String) {
String text = (String) value;
- if (!text.equalsIgnoreCase("true") && !text.equalsIgnoreCase("false")) {
+ if (!MAGIC_VALUE.equals(value) && !text.equalsIgnoreCase("true") && !text.equalsIgnoreCase("false")) {
throw new IllegalArgumentException(
"Cannot convert the String value: " + value + " to type: " + type
+ " as the value is not true or false");
@@ -92,6 +97,9 @@ public abstract class PropertyConfigurerSupport {
if (value != null) {
try {
+ if (MAGIC_VALUE.equals(value) && boolean.class == type) {
+ value = "true";
+ }
return camelContext.getTypeConverter().mandatoryConvertTo(type, value);
} catch (NoTypeConversionAvailableException e) {
throw RuntimeCamelException.wrapRuntimeCamelException(e);
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 1a101289814..bb821d240d3 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
@@ -32,6 +32,7 @@ import org.apache.camel.CamelContext;
import org.apache.camel.ExtendedCamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.component.properties.PropertiesComponent;
import org.apache.camel.dsl.support.SourceLoader;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.engine.DefaultCompileStrategy;
@@ -56,6 +57,8 @@ import org.apache.camel.main.download.DependencyDownloaderTransformerResolver;
import org.apache.camel.main.download.DependencyDownloaderUriFactoryResolver;
import org.apache.camel.main.download.DownloadListener;
import org.apache.camel.main.download.DownloadModelineParser;
+import org.apache.camel.main.download.ExportPropertiesParser;
+import org.apache.camel.main.download.ExportTypeConverter;
import org.apache.camel.main.download.KameletAutowiredLifecycleStrategy;
import org.apache.camel.main.download.KameletMainInjector;
import org.apache.camel.main.download.KnownDependenciesResolver;
@@ -401,7 +404,12 @@ public class KameletMain extends MainCommandLineSupport {
boolean export = "true".equals(getInitialProperties().get("camel.jbang.export"));
if (export) {
+ // when exporting we should ignore some errors and keep attempting to export as far as we can
addInitialProperty("camel.component.properties.ignore-missing-property", "true");
+ addInitialProperty("camel.component.properties.ignore-missing-location", "true");
+ PropertiesComponent pc = (PropertiesComponent) answer.getPropertiesComponent();
+ pc.setPropertiesParser(new ExportPropertiesParser());
+ answer.getTypeConverterRegistry().addFallbackTypeConverter(new ExportTypeConverter(), false);
}
boolean prompt = "true".equals(getInitialProperties().get("camel.jbang.prompt"));
@@ -529,6 +537,8 @@ public class KameletMain extends MainCommandLineSupport {
boolean ignoreLoading = "true".equals(getInitialProperties().get("camel.jbang.ignoreLoadingError"));
if (ignoreLoading) {
configure().withRoutesCollectorIgnoreLoadingError(true);
+ 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"));
diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/ExportPropertiesParser.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/ExportPropertiesParser.java
new file mode 100644
index 00000000000..941e013744b
--- /dev/null
+++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/ExportPropertiesParser.java
@@ -0,0 +1,37 @@
+/*
+ * 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 org.apache.camel.component.properties.DefaultPropertiesParser;
+import org.apache.camel.component.properties.PropertiesLookup;
+import org.apache.camel.support.component.PropertyConfigurerSupport;
+
+/**
+ * During export then we can be more flexible and allow missing property placeholders to resolve to a hardcoded value,
+ * so we can keep attempting to export.
+ */
+public class ExportPropertiesParser extends DefaultPropertiesParser {
+
+ @Override
+ public String parseProperty(String key, String value, PropertiesLookup properties) {
+ if (value == null) {
+ return PropertyConfigurerSupport.MAGIC_VALUE;
+ }
+ return value;
+ }
+
+}
diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/ExportTypeConverter.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/ExportTypeConverter.java
new file mode 100644
index 00000000000..86a9e0161a5
--- /dev/null
+++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/ExportTypeConverter.java
@@ -0,0 +1,46 @@
+/*
+ * 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 org.apache.camel.Exchange;
+import org.apache.camel.TypeConversionException;
+import org.apache.camel.support.TypeConverterSupport;
+import org.apache.camel.support.component.PropertyConfigurerSupport;
+
+/**
+ * During export then we can be more flexible and allow missing property placeholders to resolve to a hardcoded value,
+ * so we can keep attempting to export.
+ */
+public class ExportTypeConverter extends TypeConverterSupport {
+
+ @Override
+ public <T> T convertTo(Class<T> type, Exchange exchange, Object value) throws TypeConversionException {
+ if (PropertyConfigurerSupport.MAGIC_VALUE.equals(value)) {
+ // attempt to convert to the given type using different common inputs (boolean, numeric, string)
+ T answer = exchange.getContext().getTypeConverter().tryConvertTo(type, exchange, "true");
+ if (answer == null) {
+ answer = exchange.getContext().getTypeConverter().tryConvertTo(type, exchange, "1");
+ }
+ if (answer == null) {
+ answer = exchange.getContext().getTypeConverter().tryConvertTo(type, exchange,
+ PropertyConfigurerSupport.MAGIC_VALUE);
+ }
+ return answer;
+ }
+ return null;
+ }
+}