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:43 UTC
(camel) 07/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 96fa45644c26982b0dca08ae5ed968bb6ecca2d3
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Feb 8 14:17:31 2024 +0100
CAMEL-20378: Languages should be thread-safe and be configured only via properties array, all in the same way.
---
.../org/apache/camel/component/hl7/Hl7Terser.java | 19 ++++++-
.../hl7/Hl7TerserAnnotationExpressionFactory.java | 65 ++++++++++++++++++++++
.../camel/component/hl7/Hl7TerserLanguage.java | 2 +-
3 files changed, 84 insertions(+), 2 deletions(-)
diff --git a/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/Hl7Terser.java b/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/Hl7Terser.java
index 6bf2d5fbef4..48c6b1072fb 100644
--- a/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/Hl7Terser.java
+++ b/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/Hl7Terser.java
@@ -27,7 +27,24 @@ import org.apache.camel.support.language.LanguageAnnotation;
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
-@LanguageAnnotation(language = "hl7terser")
+@LanguageAnnotation(language = "hl7terser", factory = Hl7TerserAnnotationExpressionFactory.class)
public @interface Hl7Terser {
+
+ /**
+ * The HL7 terser which will be applied
+ */
String value();
+
+ /**
+ * The desired return type from the evaluated HL7 terser.
+ */
+ 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 "";
+
}
diff --git a/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/Hl7TerserAnnotationExpressionFactory.java b/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/Hl7TerserAnnotationExpressionFactory.java
new file mode 100644
index 00000000000..8e969d050e3
--- /dev/null
+++ b/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/Hl7TerserAnnotationExpressionFactory.java
@@ -0,0 +1,65 @@
+/*
+ * 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.component.hl7;
+
+import java.lang.annotation.Annotation;
+
+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 HL7 terser expression annotations.
+ */
+public class Hl7TerserAnnotationExpressionFactory extends DefaultAnnotationExpressionFactory {
+
+ @Override
+ public Expression createExpression(
+ CamelContext camelContext, Annotation annotation, LanguageAnnotation languageAnnotation,
+ Class<?> expressionReturnType) {
+
+ String hl7 = getExpressionFromAnnotation(annotation);
+ Class<?> resultType = getResultType(annotation);
+ if (resultType.equals(Object.class)) {
+ resultType = expressionReturnType;
+ }
+ String source = getSource(annotation);
+ Expression input = ExpressionBuilder.singleInputExpression(source);
+ Expression exp = Hl7TerserLanguage.terser(input, hl7);
+
+ return ExpressionBuilder.convertToExpression(exp, 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-hl7/src/main/java/org/apache/camel/component/hl7/Hl7TerserLanguage.java b/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/Hl7TerserLanguage.java
index 0732c2e308a..a4170bbd65c 100644
--- a/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/Hl7TerserLanguage.java
+++ b/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/Hl7TerserLanguage.java
@@ -35,7 +35,7 @@ public class Hl7TerserLanguage extends SingleInputTypedLanguageSupport {
return terser(ExpressionBuilder.bodyExpression(), expression);
}
- private static Expression terser(final Expression source, final String expression) {
+ public static Expression terser(final Expression source, final String expression) {
ObjectHelper.notNull(expression, "expression");
return new ExpressionAdapter() {