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/02/08 13:52:44 UTC
(camel) 08/09: CAMEL-20378: Languages should be thread-safe and be configured only via properties array, all in the same way.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch lang11
in repository https://gitbox.apache.org/repos/asf/camel.git
commit ab8fa1a9af500518a268ad0a1ef9937f395dd7da
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Feb 8 14:32:25 2024 +0100
CAMEL-20378: Languages should be thread-safe and be configured only via properties array, all in the same way.
---
.../camel/language/datasonnet/Datasonnet.java | 28 +++++++-
.../DatasonnetAnnotationExpressionFactory.java | 77 ++++++++++++++++++++++
.../datasonnet/DatasonnetAnnotationTest.java | 54 +++++++++++++++
3 files changed, 158 insertions(+), 1 deletion(-)
diff --git a/components/camel-datasonnet/src/main/java/org/apache/camel/language/datasonnet/Datasonnet.java b/components/camel-datasonnet/src/main/java/org/apache/camel/language/datasonnet/Datasonnet.java
index 6e5de5fa328..69f58d2eabc 100644
--- a/components/camel-datasonnet/src/main/java/org/apache/camel/language/datasonnet/Datasonnet.java
+++ b/components/camel-datasonnet/src/main/java/org/apache/camel/language/datasonnet/Datasonnet.java
@@ -31,7 +31,33 @@ import org.apache.camel.support.language.LanguageAnnotation;
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
-@LanguageAnnotation(language = "datasonnet")
+@LanguageAnnotation(language = "datasonnet", factory = DatasonnetAnnotationExpressionFactory.class)
public @interface Datasonnet {
+
+ /**
+ * The datasonnet expression which will be applied
+ */
String value();
+
+ /**
+ * The desired return type from the evaluated datasonnet.
+ */
+ Class<?> resultType() default Object.class;
+
+ /**
+ * Source to use, instead of message body. You can prefix with variable:, header:, or property: to specify kind of
+ * source. Otherwise, the source is assumed to be a variable. Use empty or null to use default source, which is the
+ * message body.
+ */
+ String source() default "";
+
+ /**
+ * The message body media type.
+ */
+ String bodyMediaType() default "";
+
+ /**
+ * The media type to output.
+ */
+ String outputMediaType() default "";
}
diff --git a/components/camel-datasonnet/src/main/java/org/apache/camel/language/datasonnet/DatasonnetAnnotationExpressionFactory.java b/components/camel-datasonnet/src/main/java/org/apache/camel/language/datasonnet/DatasonnetAnnotationExpressionFactory.java
new file mode 100644
index 00000000000..08fa3e129dd
--- /dev/null
+++ b/components/camel-datasonnet/src/main/java/org/apache/camel/language/datasonnet/DatasonnetAnnotationExpressionFactory.java
@@ -0,0 +1,77 @@
+/*
+ * 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.language.datasonnet;
+
+import java.lang.annotation.Annotation;
+
+import com.datasonnet.document.MediaType;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Expression;
+import org.apache.camel.support.builder.ExpressionBuilder;
+import org.apache.camel.support.language.DefaultAnnotationExpressionFactory;
+import org.apache.camel.support.language.LanguageAnnotation;
+
+/**
+ * Factory for the Datasonnet expression annotations.
+ */
+public class DatasonnetAnnotationExpressionFactory extends DefaultAnnotationExpressionFactory {
+
+ @Override
+ public Expression createExpression(
+ CamelContext camelContext, Annotation annotation, LanguageAnnotation languageAnnotation,
+ Class<?> expressionReturnType) {
+
+ String ds = getExpressionFromAnnotation(annotation);
+ DatasonnetExpression answer = new DatasonnetExpression(ds);
+
+ Class<?> resultType = getResultType(annotation);
+ if (resultType.equals(Object.class)) {
+ resultType = expressionReturnType;
+ }
+ String source = getSource(annotation);
+ answer.setSource(ExpressionBuilder.singleInputExpression(source));
+
+ if (annotation instanceof Datasonnet) {
+ Datasonnet ann = (Datasonnet) annotation;
+ if (!ann.bodyMediaType().isEmpty()) {
+ answer.setBodyMediaType(MediaType.valueOf(ann.bodyMediaType()));
+ }
+ if (!ann.outputMediaType().isEmpty()) {
+ answer.setOutputMediaType(MediaType.valueOf(ann.outputMediaType()));
+ }
+ }
+
+ return ExpressionBuilder.convertToExpression(answer, resultType);
+ }
+
+ protected Class<?> getResultType(Annotation annotation) {
+ return (Class<?>) getAnnotationObjectValue(annotation, "resultType");
+ }
+
+ protected String getSource(Annotation annotation) {
+ String answer = null;
+ try {
+ answer = (String) getAnnotationObjectValue(annotation, "source");
+ } catch (Exception e) {
+ // Do Nothing
+ }
+ if (answer != null && answer.isBlank()) {
+ return null;
+ }
+ return answer;
+ }
+}
diff --git a/components/camel-datasonnet/src/test/java/org/apache/camel/language/datasonnet/DatasonnetAnnotationTest.java b/components/camel-datasonnet/src/test/java/org/apache/camel/language/datasonnet/DatasonnetAnnotationTest.java
new file mode 100644
index 00000000000..0fec292e8c8
--- /dev/null
+++ b/components/camel-datasonnet/src/test/java/org/apache/camel/language/datasonnet/DatasonnetAnnotationTest.java
@@ -0,0 +1,54 @@
+/*
+ * 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.language.datasonnet;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+public class DatasonnetAnnotationTest extends CamelTestSupport {
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("direct:start")
+ .bean(MyBean.class)
+ .to("mock:result");
+ }
+ };
+ }
+
+ @Test
+ public void testAnnotation() throws Exception {
+ getMockEndpoint("mock:result").expectedBodiesReceived("Hello, DataSonnet. I am Camel");
+
+ template.sendBody("direct:start", "DataSonnet");
+
+ MockEndpoint.assertIsSatisfied(context);
+ }
+
+ public static class MyBean {
+
+ public String hello(@Datasonnet("'Hello, ' + payload") String payload) {
+ return payload + ". I am Camel";
+ }
+ }
+
+}