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

commit 58ad1e0fc85d20608bb4b792162d33103e105a83
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Tue Jan 18 08:40:36 2022 +0100

    CAMEL-17508: camel-core - Process ref should support #class syntax and 
other EIPs as well
---
 .../resources/org/apache/camel/model/process.json  |  2 +-
 .../org/apache/camel/model/ProcessDefinition.java  |  8 +++-
 .../org/apache/camel/reifier/AbstractReifier.java  | 14 ++++++-
 .../reifier/language/XPathExpressionReifier.java   |  4 +-
 .../reifier/language/XQueryExpressionReifier.java  |  4 +-
 .../org/apache/camel/processor/EchoProcessor.java  | 30 +++++++++++++++
 .../camel/processor/ProcessRefClassTest.java       | 45 ++++++++++++++++++++++
 7 files changed, 98 insertions(+), 9 deletions(-)

diff --git 
a/core/camel-core-model/src/generated/resources/org/apache/camel/model/process.json
 
b/core/camel-core-model/src/generated/resources/org/apache/camel/model/process.json
index 7d9fa22..dfd6f92 100644
--- 
a/core/camel-core-model/src/generated/resources/org/apache/camel/model/process.json
+++ 
b/core/camel-core-model/src/generated/resources/org/apache/camel/model/process.json
@@ -12,7 +12,7 @@
     "output": false
   },
   "properties": {
-    "ref": { "kind": "attribute", "displayName": "Ref", "required": true, 
"type": "string", "javaType": "java.lang.String", "deprecated": false, 
"autowired": false, "secret": false, "description": "Reference to the Processor 
to lookup in the registry to use." },
+    "ref": { "kind": "attribute", "displayName": "Ref", "required": true, 
"type": "string", "javaType": "java.lang.String", "deprecated": false, 
"autowired": false, "secret": false, "description": "Reference to the Processor 
to lookup in the registry to use. Can also be used for creating new beans by 
their class name by prefixing with #class, eg #class:com.foo.MyClassType. And 
it is also possible to refer to singleton beans by their type in the registry 
by prefixing with #type: syntax, e [...]
     "id": { "kind": "attribute", "displayName": "Id", "required": false, 
"type": "string", "javaType": "java.lang.String", "deprecated": false, 
"autowired": false, "secret": false, "description": "Sets the id of this node" 
},
     "description": { "kind": "element", "displayName": "Description", 
"required": false, "type": "object", "javaType": 
"org.apache.camel.model.DescriptionDefinition", "deprecated": false, 
"autowired": false, "secret": false, "description": "Sets the description of 
this node" }
   }
diff --git 
a/core/camel-core-model/src/main/java/org/apache/camel/model/ProcessDefinition.java
 
b/core/camel-core-model/src/main/java/org/apache/camel/model/ProcessDefinition.java
index 87e92f5..c123335 100644
--- 
a/core/camel-core-model/src/main/java/org/apache/camel/model/ProcessDefinition.java
+++ 
b/core/camel-core-model/src/main/java/org/apache/camel/model/ProcessDefinition.java
@@ -85,7 +85,13 @@ public class ProcessDefinition extends 
NoOutputDefinition<ProcessDefinition> {
     }
 
     /**
-     * Reference to the {@link Processor} to lookup in the registry to use.
+     * Reference to the Processor to lookup in the registry to use.
+     *
+     * Can also be used for creating new beans by their class name by 
prefixing with #class, eg
+     * #class:com.foo.MyClassType.
+     *
+     * And it is also possible to refer to singleton beans by their type in 
the registry by prefixing with #type:
+     * syntax, eg #type:com.foo.MyClassType
      */
     public void setRef(String ref) {
         this.ref = ref;
diff --git 
a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/AbstractReifier.java
 
b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/AbstractReifier.java
index 8269e69..cca43de 100644
--- 
a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/AbstractReifier.java
+++ 
b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/AbstractReifier.java
@@ -142,7 +142,19 @@ public abstract class AbstractReifier implements 
BeanRepository {
     }
 
     public <T> T mandatoryLookup(String name, Class<T> beanType) {
-        return CamelContextHelper.mandatoryLookup(camelContext, name, 
beanType);
+        // lookup in registry first
+        Object obj = CamelContextHelper.lookup(camelContext, name, beanType);
+        if (obj == null) {
+            // fallback in case the name uses #class #type or other syntax
+            obj = lookupByName(name);
+        }
+        if (obj != null) {
+            obj = camelContext.getTypeConverter().convertTo(beanType, obj);
+        }
+        if (obj == null) {
+            throw new NoSuchBeanException(name, beanType.getName());
+        }
+        return beanType.cast(obj);
     }
 
     public <T> T findSingleByType(Class<T> type) {
diff --git 
a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/language/XPathExpressionReifier.java
 
b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/language/XPathExpressionReifier.java
index 8498067..4d0e714 100644
--- 
a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/language/XPathExpressionReifier.java
+++ 
b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/language/XPathExpressionReifier.java
@@ -27,7 +27,6 @@ import org.apache.camel.model.language.ExpressionDefinition;
 import org.apache.camel.model.language.XPathExpression;
 import org.apache.camel.spi.Language;
 import org.apache.camel.spi.NamespaceAware;
-import org.apache.camel.support.CamelContextHelper;
 
 public class XPathExpressionReifier extends ExpressionReifier<XPathExpression> 
{
 
@@ -113,8 +112,7 @@ public class XPathExpressionReifier extends 
ExpressionReifier<XPathExpression> {
             }
         }
         if (definition.getXPathFactory() == null && definition.getFactoryRef() 
!= null) {
-            definition.setXPathFactory(
-                    CamelContextHelper.mandatoryLookupAndConvert(camelContext, 
definition.getFactoryRef(), XPathFactory.class));
+            
definition.setXPathFactory(mandatoryLookup(definition.getFactoryRef(), 
XPathFactory.class));
         }
     }
 
diff --git 
a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/language/XQueryExpressionReifier.java
 
b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/language/XQueryExpressionReifier.java
index e9f98f0..8c7bcf8 100644
--- 
a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/language/XQueryExpressionReifier.java
+++ 
b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/language/XQueryExpressionReifier.java
@@ -24,7 +24,6 @@ import org.apache.camel.model.language.ExpressionDefinition;
 import org.apache.camel.model.language.XQueryExpression;
 import org.apache.camel.spi.Language;
 import org.apache.camel.spi.NamespaceAware;
-import org.apache.camel.support.CamelContextHelper;
 
 public class XQueryExpressionReifier extends 
ExpressionReifier<XQueryExpression> {
 
@@ -77,8 +76,7 @@ public class XQueryExpressionReifier extends 
ExpressionReifier<XQueryExpression>
             }
         }
         if (definition.getConfiguration() == null && 
definition.getConfigurationRef() != null) {
-            definition.setConfiguration(
-                    CamelContextHelper.mandatoryLookupAndConvert(camelContext, 
definition.getConfigurationRef(), Object.class));
+            
definition.setConfiguration(mandatoryLookup(definition.getConfigurationRef(), 
Object.class));
         }
     }
 
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/processor/EchoProcessor.java 
b/core/camel-core/src/test/java/org/apache/camel/processor/EchoProcessor.java
new file mode 100644
index 0000000..8802644
--- /dev/null
+++ 
b/core/camel-core/src/test/java/org/apache/camel/processor/EchoProcessor.java
@@ -0,0 +1,30 @@
+/*
+ * 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.processor;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+
+public class EchoProcessor implements Processor {
+
+    @Override
+    public void process(Exchange exchange) throws Exception {
+        String s = exchange.getMessage().getBody(String.class);
+        exchange.getMessage().setBody(s + s);
+    }
+
+}
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/processor/ProcessRefClassTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/processor/ProcessRefClassTest.java
new file mode 100644
index 0000000..041716f26
--- /dev/null
+++ 
b/core/camel-core/src/test/java/org/apache/camel/processor/ProcessRefClassTest.java
@@ -0,0 +1,45 @@
+/*
+ * 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.processor;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.Test;
+
+public class ProcessRefClassTest extends ContextTestSupport {
+
+    @Test
+    public void testProcessRefClass() throws Exception {
+        getMockEndpoint("mock:result").expectedBodiesReceived("CamelCamel");
+
+        template.sendBody("direct:start", "Camel");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                        
.process("#class:org.apache.camel.processor.EchoProcessor")
+                        .to("mock:result");
+            }
+        };
+    }
+}

Reply via email to