camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [camel] 02/02: CAMEL-12309: Optimised SpEL to eager lookup bean resolver.
Date Mon, 05 Mar 2018 09:50:28 GMT
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 06d6ab6d21a8b1331d66ebd9b4c34353b7a67742
Author: Claus Ibsen <claus.ibsen@gmail.com>
AuthorDate: Mon Mar 5 10:40:37 2018 +0100

    CAMEL-12309: Optimised SpEL to eager lookup bean resolver.
---
 .../apache/camel/language/spel/SpelExpression.java | 16 ++++++++---
 .../apache/camel/language/spel/SpelLanguage.java   | 31 +++++++++++++++++++---
 2 files changed, 40 insertions(+), 7 deletions(-)

diff --git a/components/camel-spring/src/main/java/org/apache/camel/language/spel/SpelExpression.java
b/components/camel-spring/src/main/java/org/apache/camel/language/spel/SpelExpression.java
index e410e1d..4ac573e 100644
--- a/components/camel-spring/src/main/java/org/apache/camel/language/spel/SpelExpression.java
+++ b/components/camel-spring/src/main/java/org/apache/camel/language/spel/SpelExpression.java
@@ -23,6 +23,7 @@ import org.apache.camel.spring.SpringCamelContext;
 import org.apache.camel.spring.util.RegistryBeanResolver;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.expression.BeanFactoryResolver;
+import org.springframework.expression.BeanResolver;
 import org.springframework.expression.EvaluationContext;
 import org.springframework.expression.Expression;
 import org.springframework.expression.ParserContext;
@@ -31,22 +32,27 @@ import org.springframework.expression.spel.standard.SpelExpressionParser;
 import org.springframework.expression.spel.support.StandardEvaluationContext;
 
 /**
- * Class responsible for evaluating <a href="http://static.springsource.org
- * /spring/docs/current/spring-framework-reference/html/expressions.html">
- * Spring Expression Language</a> in the context of Camel.
+ * Class responsible for evaluating <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#expressions">
+ * Spring Expression Language (SpEL)</a> in the context of Camel.
  */
 @SuppressWarnings("deprecation")
 public class SpelExpression extends ExpressionSupport {
 
     private final String expressionString;
     private final Class<?> type;
+    private final BeanResolver beanResolver;
 
     // SpelExpressionParser is thread-safe according to the docs
     private final SpelExpressionParser expressionParser;
 
     public SpelExpression(String expressionString, Class<?> type) {
+        this(expressionString, type, null);
+    }
+
+    public SpelExpression(String expressionString, Class<?> type, BeanResolver beanResolver)
{
         this.expressionString = expressionString;
         this.type = type;
+        this.beanResolver = beanResolver;
         this.expressionParser = new SpelExpressionParser();
     }
 
@@ -68,7 +74,9 @@ public class SpelExpression extends ExpressionSupport {
 
     private EvaluationContext createEvaluationContext(Exchange exchange) {
         StandardEvaluationContext evaluationContext = new StandardEvaluationContext(new RootObject(exchange));
-        if (exchange.getContext() instanceof SpringCamelContext) {
+        if (beanResolver != null) {
+            evaluationContext.setBeanResolver(beanResolver);
+        } else if (exchange.getContext() instanceof SpringCamelContext) {
             // Support references (like @foo) in expressions to beans defined in the Registry/ApplicationContext
             ApplicationContext applicationContext = ((SpringCamelContext) exchange.getContext()).getApplicationContext();
             evaluationContext.setBeanResolver(new BeanFactoryResolver(applicationContext));
diff --git a/components/camel-spring/src/main/java/org/apache/camel/language/spel/SpelLanguage.java
b/components/camel-spring/src/main/java/org/apache/camel/language/spel/SpelLanguage.java
index 5e7c57f..e9a06e0 100644
--- a/components/camel-spring/src/main/java/org/apache/camel/language/spel/SpelLanguage.java
+++ b/components/camel-spring/src/main/java/org/apache/camel/language/spel/SpelLanguage.java
@@ -18,22 +18,47 @@ package org.apache.camel.language.spel;
 
 import org.apache.camel.Expression;
 import org.apache.camel.Predicate;
+import org.apache.camel.Service;
 import org.apache.camel.spi.Language;
+import org.apache.camel.spring.SpringCamelContext;
+import org.apache.camel.spring.util.RegistryBeanResolver;
 import org.apache.camel.support.LanguageSupport;
+import org.apache.camel.util.ObjectHelper;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.expression.BeanFactoryResolver;
+import org.springframework.expression.BeanResolver;
 
 /**
  * A Spring Expression {@link Language} plugin
  */
-public class SpelLanguage extends LanguageSupport {
+public class SpelLanguage extends LanguageSupport implements Service {
+
+    private BeanResolver beanResolver;
 
     public Predicate createPredicate(String expression) {
         expression = loadResource(expression);
-        return new SpelExpression(expression, Boolean.class);
+        return new SpelExpression(expression, Boolean.class, beanResolver);
     }
 
     public Expression createExpression(String expression) {
         expression = loadResource(expression);
-        return new SpelExpression(expression, Object.class);
+        return new SpelExpression(expression, Object.class, beanResolver);
     }
 
+    @Override
+    public void start() throws Exception {
+        ObjectHelper.notNull(getCamelContext(), "CamelContext", this);
+
+        if (getCamelContext() instanceof SpringCamelContext) {
+            ApplicationContext applicationContext = ((SpringCamelContext) getCamelContext()).getApplicationContext();
+            beanResolver = new BeanFactoryResolver(applicationContext);
+        } else {
+            beanResolver = new RegistryBeanResolver(getCamelContext().getRegistry());
+        }
+    }
+
+    @Override
+    public void stop() throws Exception {
+        // noop
+    }
 }

-- 
To stop receiving notification emails like this one, please contact
davsclaus@apache.org.

Mime
View raw message