This is an automated email from the ASF dual-hosted git repository.
davsclaus 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 1056b793f6a CAMEL-20991: camel-jbang - Make export able to complete
for source co… (#15038)
1056b793f6a is described below
commit 1056b793f6a4e6f29f2ed666ba2bcb15d07e6782
Author: Claus Ibsen <[email protected]>
AuthorDate: Wed Aug 7 13:18:17 2024 +0200
CAMEL-20991: camel-jbang - Make export able to complete for source co…
(#15038)
CAMEL-20991: camel-jbang - Make export able to complete for source code
that has missing property placeholders
---
.../org/apache/camel/util/ObjectHelperTest.java | 14 ++++++
.../component/PropertyConfigurerSupport.java | 10 ++++-
.../java/org/apache/camel/util/ObjectHelper.java | 20 +++++++++
.../java/org/apache/camel/main/KameletMain.java | 10 +++++
.../main/download/ExportPropertiesParser.java | 37 ++++++++++++++++
.../camel/main/download/ExportTypeConverter.java | 50 ++++++++++++++++++++++
6 files changed, 140 insertions(+), 1 deletion(-)
diff --git
a/core/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java
b/core/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java
index e2163f2baa1..e37d01f4e59 100644
--- a/core/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java
@@ -1148,4 +1148,18 @@ public class ObjectHelperTest {
Assertions.assertNull(list.get(98));
Assertions.assertEquals("zzz", list.get(99));
}
+
+ @Test
+ public void testIsNumeric() {
+
Assertions.assertTrue(org.apache.camel.util.ObjectHelper.isNumericType(int.class));
+
Assertions.assertTrue(org.apache.camel.util.ObjectHelper.isNumericType(Integer.class));
+
Assertions.assertTrue(org.apache.camel.util.ObjectHelper.isNumericType(long.class));
+
Assertions.assertTrue(org.apache.camel.util.ObjectHelper.isNumericType(Long.class));
+
Assertions.assertTrue(org.apache.camel.util.ObjectHelper.isNumericType(Double.class));
+
Assertions.assertTrue(org.apache.camel.util.ObjectHelper.isNumericType(float.class));
+
Assertions.assertTrue(org.apache.camel.util.ObjectHelper.isNumericType(byte.class));
+
+
Assertions.assertFalse(org.apache.camel.util.ObjectHelper.isNumericType(String.class));
+
Assertions.assertFalse(org.apache.camel.util.ObjectHelper.isNumericType(Node.class));
+ }
}
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/core/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java
b/core/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java
index 3227ce736e0..7eb4673946a 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java
@@ -990,6 +990,26 @@ public final class ObjectHelper {
return pojo.length;
}
+ /**
+ * Is the give type numeric
+ */
+ public static boolean isNumericType(Class<?> type) {
+ if (type == int.class || type == Integer.class) {
+ return true;
+ } else if (type == long.class || type == Long.class) {
+ return true;
+ } else if (type == double.class || type == Double.class) {
+ return true;
+ } else if (type == float.class || type == Float.class) {
+ return true;
+ } else if (type == short.class || type == Short.class) {
+ return true;
+ } else if (type == byte.class || type == Byte.class) {
+ return true;
+ }
+ return false;
+ }
+
/**
* Converts primitive types such as int to its wrapper type like {@link
Integer}
*/
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..b1f3c23976c
--- /dev/null
+++
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/ExportTypeConverter.java
@@ -0,0 +1,50 @@
+/*
+ * 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;
+import org.apache.camel.util.ObjectHelper;
+
+/**
+ * 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 = null;
+ if (boolean.class == type || Boolean.class == type) {
+ answer =
exchange.getContext().getTypeConverter().tryConvertTo(type, exchange, "true");
+ }
+ if (answer == null && ObjectHelper.isNumericType(type)) {
+ answer =
exchange.getContext().getTypeConverter().tryConvertTo(type, exchange, "1");
+ }
+ if (answer == null && type == String.class) {
+ answer =
exchange.getContext().getTypeConverter().tryConvertTo(type, exchange,
+ PropertyConfigurerSupport.MAGIC_VALUE);
+ }
+ return answer;
+ }
+ return null;
+ }
+}