camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r631503 - in /activemq/camel/trunk/components/camel-juel/src: main/java/org/apache/camel/language/juel/ test/java/org/apache/camel/language/juel/
Date Wed, 27 Feb 2008 06:42:26 GMT
Author: jstrachan
Date: Tue Feb 26 22:42:19 2008
New Revision: 631503

URL: http://svn.apache.org/viewvc?rev=631503&view=rev
Log:
added fix for https://issues.apache.org/activemq/browse/CAMEL-347 to enable JUEL to invoke
methods in expressions

Added:
    activemq/camel/trunk/components/camel-juel/src/main/java/org/apache/camel/language/juel/BeanAndMethodELResolver.java
  (with props)
Modified:
    activemq/camel/trunk/components/camel-juel/src/main/java/org/apache/camel/language/juel/JuelExpression.java
    activemq/camel/trunk/components/camel-juel/src/test/java/org/apache/camel/language/juel/JuelLanguageTest.java

Added: activemq/camel/trunk/components/camel-juel/src/main/java/org/apache/camel/language/juel/BeanAndMethodELResolver.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-juel/src/main/java/org/apache/camel/language/juel/BeanAndMethodELResolver.java?rev=631503&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-juel/src/main/java/org/apache/camel/language/juel/BeanAndMethodELResolver.java
(added)
+++ activemq/camel/trunk/components/camel-juel/src/main/java/org/apache/camel/language/juel/BeanAndMethodELResolver.java
Tue Feb 26 22:42:19 2008
@@ -0,0 +1,84 @@
+/**
+ *
+ * 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.juel;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.el.BeanELResolver;
+import javax.el.ELContext;
+import javax.el.PropertyNotFoundException;
+
+/**
+ * An extension of the JUEL {@link BeanELResolver} which also supports the resolving of methods
+ *
+ * @version $Revision: 1.1 $
+ */
+public class BeanAndMethodELResolver extends BeanELResolver {
+    public BeanAndMethodELResolver() {
+        super(false);
+    }
+
+    @Override
+    public Object getValue(ELContext elContext, Object base, Object property) {
+        try {
+            return super.getValue(elContext, base, property);
+        }
+        catch (PropertyNotFoundException e) {
+            // lets see if its a method call...
+            Method method = findMethod(elContext, base, property);
+            if (method != null) {
+                elContext.setPropertyResolved(true);
+                return method;
+            }
+            else {
+                throw e;
+            }
+        }
+    }
+
+    protected Method findMethod(ELContext elContext, Object base, Object property) {
+        if (base != null && property instanceof String) {
+            Method[] methods = base.getClass().getMethods();
+            List<Method> matching = new ArrayList<Method>();
+            for (Method method : methods) {
+                if (method.getName().equals(property) && Modifier.isPublic(method.getModifiers()))
{
+                    matching.add(method);
+                }
+            }
+            int size = matching.size();
+            if (size > 0) {
+                if (size > 1) {
+                    // TODO there's currently no way for JUEL to tell us how many parameters
there are
+                    // so lets just pick the first one that has a single param by default
+                    for (Method method : matching) {
+                        Class<?>[] paramTypes = method.getParameterTypes();
+                        if (paramTypes.length == 1) {
+                            return method;
+                        }
+                    }
+                }
+                // lets default to the first one
+                return matching.get(0);
+            }
+        }
+        return null;
+    }
+}

Propchange: activemq/camel/trunk/components/camel-juel/src/main/java/org/apache/camel/language/juel/BeanAndMethodELResolver.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: activemq/camel/trunk/components/camel-juel/src/main/java/org/apache/camel/language/juel/JuelExpression.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-juel/src/main/java/org/apache/camel/language/juel/JuelExpression.java?rev=631503&r1=631502&r2=631503&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-juel/src/main/java/org/apache/camel/language/juel/JuelExpression.java
(original)
+++ activemq/camel/trunk/components/camel-juel/src/main/java/org/apache/camel/language/juel/JuelExpression.java
Tue Feb 26 22:42:19 2008
@@ -16,12 +16,11 @@
  */
 package org.apache.camel.language.juel;
 
-import javax.el.ELContext;
-import javax.el.ExpressionFactory;
-import javax.el.ValueExpression;
+import java.util.Properties;
 
-import de.odysseus.el.util.SimpleContext;
+import javax.el.*;
 
+import de.odysseus.el.util.SimpleContext;
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
 import org.apache.camel.impl.ExpressionSupport;
@@ -29,14 +28,14 @@
 /**
  * The <a href="http://activemq.apache.org/camel/el.html">EL Language from JSP and
JSF</a>
  * using the <a href="http://activemq.apache.org/camel/juel.html">JUEL library</a>
- * 
+ *
  * @version $Revision$
  */
 public class JuelExpression extends ExpressionSupport<Exchange> {
-
     private final String expression;
     private final Class<?> type;
     private ExpressionFactory expressionFactory;
+    private Properties expressionFactoryProperties;
 
     public JuelExpression(String expression, Class<?> type) {
         this.expression = expression;
@@ -57,7 +56,8 @@
 
     public ExpressionFactory getExpressionFactory() {
         if (expressionFactory == null) {
-            expressionFactory = ExpressionFactory.newInstance();
+            Properties properties = getExpressionFactoryProperties();
+            expressionFactory = ExpressionFactory.newInstance(properties);
         }
         return expressionFactory;
     }
@@ -66,6 +66,18 @@
         this.expressionFactory = expressionFactory;
     }
 
+    public Properties getExpressionFactoryProperties() {
+        if (expressionFactoryProperties == null) {
+            expressionFactoryProperties = new Properties();
+            populateDefaultExpressionProperties(expressionFactoryProperties);
+        }
+        return expressionFactoryProperties;
+    }
+
+    public void setExpressionFactoryProperties(Properties expressionFactoryProperties) {
+        this.expressionFactoryProperties = expressionFactoryProperties;
+    }
+
     protected ELContext populateContext(ELContext context, Exchange exchange) {
         setVariable(context, "exchange", exchange, Exchange.class);
         setVariable(context, "in", exchange.getIn(), Message.class);
@@ -74,6 +86,14 @@
         return context;
     }
 
+    /**
+     * A Strategy Method to populate the default properties used to create the expression
factory
+     */
+    protected void populateDefaultExpressionProperties(Properties properties) {
+        // lets enable method invocations
+        properties.setProperty("javax.el.methodInvocations", "true");
+    }
+
     protected void setVariable(ELContext context, String name, Object value, Class<?>
type) {
         ValueExpression valueExpression = getExpressionFactory().createValueExpression(value,
type);
         SimpleContext simpleContext = (SimpleContext) context;
@@ -84,7 +104,17 @@
      * Factory method to create the EL context
      */
     protected ELContext createContext() {
-        return new SimpleContext();
+        ELResolver resolver = new CompositeELResolver() {
+            {
+                //add(methodResolver);
+                add(new ArrayELResolver(false));
+                add(new ListELResolver(false));
+                add(new MapELResolver(false));
+                add(new ResourceBundleELResolver());
+                add(new BeanAndMethodELResolver());
+            }
+        };
+        return new SimpleContext(resolver);
     }
 
     protected String assertionFailureMessage(Exchange exchange) {

Modified: activemq/camel/trunk/components/camel-juel/src/test/java/org/apache/camel/language/juel/JuelLanguageTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-juel/src/test/java/org/apache/camel/language/juel/JuelLanguageTest.java?rev=631503&r1=631502&r2=631503&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-juel/src/test/java/org/apache/camel/language/juel/JuelLanguageTest.java
(original)
+++ activemq/camel/trunk/components/camel-juel/src/test/java/org/apache/camel/language/juel/JuelLanguageTest.java
Tue Feb 26 22:42:19 2008
@@ -28,6 +28,10 @@
         assertExpression("${in.body}", "<hello id='m123'>world!</hello>");
     }
 
+    public void testElPredicates() throws Exception {
+        assertPredicate("${in.headers.foo.startsWith('a')}");
+    }
+
     protected String getLanguageName() {
         return "el";
     }



Mime
View raw message