Repository: camel
Updated Branches:
  refs/heads/master 2386efa7c -> 661f6f392


CAMEL-11377: Optimise - Bean expression invoking bean can use static method 
instead of creating new objects


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a05fde83
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a05fde83
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a05fde83

Branch: refs/heads/master
Commit: a05fde838c95eb0e8e9227947c8ca947012dcb95
Parents: 2386efa
Author: Claus Ibsen <davscl...@apache.org>
Authored: Thu Jun 1 16:02:32 2017 +0200
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Thu Jun 1 16:02:32 2017 +0200

----------------------------------------------------------------------
 .../camel/language/bean/BeanExpression.java     | 86 ++++++++------------
 1 file changed, 35 insertions(+), 51 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/a05fde83/camel-core/src/main/java/org/apache/camel/language/bean/BeanExpression.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/language/bean/BeanExpression.java 
b/camel-core/src/main/java/org/apache/camel/language/bean/BeanExpression.java
index 020733f..9ff76ba 100644
--- 
a/camel-core/src/main/java/org/apache/camel/language/bean/BeanExpression.java
+++ 
b/camel-core/src/main/java/org/apache/camel/language/bean/BeanExpression.java
@@ -122,10 +122,8 @@ public class BeanExpression implements Expression, 
Predicate {
             }
         } else {
             // regular non ognl invocation
-            InvokeProcessor invoke = new InvokeProcessor(beanHolder, beanName, 
method);
             try {
-                invoke.process(exchange);
-                return invoke.getResult();
+                return invokeBean(beanHolder, beanName, method, exchange);
             } catch (Exception e) {
                 if (e instanceof RuntimeBeanExpressionException) {
                     throw (RuntimeBeanExpressionException) e;
@@ -181,59 +179,46 @@ public class BeanExpression implements Expression, 
Predicate {
     }
 
     /**
-     * Invokes a given bean holder. The method name is optional.
+     * Invokes the bean and returns the result. If an exception was thrown 
while invoking the bean, then the
+     * exception is set on the exchange.
      */
-    private static final class InvokeProcessor implements Processor {
-
-        private final BeanHolder beanHolder;
-        private final String methodName;
-        private final String beanName;
-        private Object result;
-
-        private InvokeProcessor(BeanHolder beanHolder, String beanName, String 
methodName) {
-            this.beanHolder = beanHolder;
-            this.methodName = methodName;
-            this.beanName = beanName;
+    private static Object invokeBean(BeanHolder beanHolder, String beanName, 
String methodName, Exchange exchange) {
+        Object result;
+
+        BeanExpressionProcessor processor = new 
BeanExpressionProcessor(beanHolder);
+        if (methodName != null) {
+            processor.setMethod(methodName);
+            // enable OGNL like invocation
+            processor.setShorthandMethod(true);
         }
+        try {
+            // copy the original exchange to avoid side effects on it
+            Exchange resultExchange = exchange.copy();
+            // remove any existing exception in case we do OGNL on the 
exception
+            resultExchange.setException(null);
 
-        public void process(Exchange exchange) throws Exception {
-            BeanExpressionProcessor processor = new 
BeanExpressionProcessor(beanHolder);
-            if (methodName != null) {
-                processor.setMethod(methodName);
-                // enable OGNL like invocation
-                processor.setShorthandMethod(true);
+            // force to use InOut to retrieve the result on the OUT message
+            resultExchange.setPattern(ExchangePattern.InOut);
+            processor.process(resultExchange);
+            result = resultExchange.getOut().getBody();
+
+            // propagate properties and headers from result
+            if (resultExchange.hasProperties()) {
+                
exchange.getProperties().putAll(resultExchange.getProperties());
+            }
+            if (resultExchange.getOut().hasHeaders()) {
+                
exchange.getIn().getHeaders().putAll(resultExchange.getOut().getHeaders());
             }
-            try {
-                // copy the original exchange to avoid side effects on it
-                Exchange resultExchange = exchange.copy();
-                // remove any existing exception in case we do OGNL on the 
exception
-                resultExchange.setException(null);
-
-                // force to use InOut to retrieve the result on the OUT message
-                resultExchange.setPattern(ExchangePattern.InOut);
-                processor.process(resultExchange);
-                result = resultExchange.getOut().getBody();
-
-                // propagate properties and headers from result
-                if (resultExchange.hasProperties()) {
-                    
exchange.getProperties().putAll(resultExchange.getProperties());
-                }
-                if (resultExchange.getOut().hasHeaders()) {
-                    
exchange.getIn().getHeaders().putAll(resultExchange.getOut().getHeaders());
-                }
 
-                // propagate exceptions
-                if (resultExchange.getException() != null) {
-                    exchange.setException(resultExchange.getException());
-                }
-            } catch (Exception e) {
-                throw new RuntimeBeanExpressionException(exchange, beanName, 
methodName, e);
+            // propagate exceptions
+            if (resultExchange.getException() != null) {
+                exchange.setException(resultExchange.getException());
             }
+        } catch (Throwable e) {
+            throw new RuntimeBeanExpressionException(exchange, beanName, 
methodName, e);
         }
 
-        public Object getResult() {
-            return result;
-        }
+        return result;
     }
 
     /**
@@ -323,15 +308,14 @@ public class BeanExpression implements Expression, 
Predicate {
 
                 // only invoke if we have a method name to use to invoke
                 if (methodName != null) {
-                    InvokeProcessor invoke = new InvokeProcessor(holder, 
beanName, methodName);
-                    invoke.process(resultExchange);
+                    Object newResult = invokeBean(holder, beanName, 
methodName, resultExchange);
 
                     // check for exception and rethrow if we failed
                     if (resultExchange.getException() != null) {
                         throw new RuntimeBeanExpressionException(exchange, 
beanName, methodName, resultExchange.getException());
                     }
 
-                    result = invoke.getResult();
+                    result = newResult;
                 }
 
                 // if there was a key then we need to lookup using the key

Reply via email to