camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject git commit: CAMEL-7980 Setup the ClassResolver on the OGNL context
Date Wed, 29 Oct 2014 04:27:59 GMT
Repository: camel
Updated Branches:
  refs/heads/master c10a91ace -> 94b7cba4e


CAMEL-7980 Setup the ClassResolver on the OGNL context


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

Branch: refs/heads/master
Commit: 94b7cba4eec635f4464fb8258d81074752e528f0
Parents: c10a91a
Author: Willem Jiang <willem.jiang@gmail.com>
Authored: Wed Oct 29 12:27:04 2014 +0800
Committer: Willem Jiang <willem.jiang@gmail.com>
Committed: Wed Oct 29 12:27:29 2014 +0800

----------------------------------------------------------------------
 .../camel/language/ognl/CamelClassResolver.java | 39 ++++++++++++++++++++
 .../camel/language/ognl/OgnlExpression.java     |  2 +
 .../org/apache/camel/language/ognl/Animal.java  |  4 ++
 .../camel/language/ognl/MyClassResolver.java    | 29 +++++++++++++++
 .../apache/camel/language/ognl/OgnlTest.java    | 12 ++++++
 5 files changed, 86 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/94b7cba4/components/camel-ognl/src/main/java/org/apache/camel/language/ognl/CamelClassResolver.java
----------------------------------------------------------------------
diff --git a/components/camel-ognl/src/main/java/org/apache/camel/language/ognl/CamelClassResolver.java
b/components/camel-ognl/src/main/java/org/apache/camel/language/ognl/CamelClassResolver.java
new file mode 100644
index 0000000..0a0912f
--- /dev/null
+++ b/components/camel-ognl/src/main/java/org/apache/camel/language/ognl/CamelClassResolver.java
@@ -0,0 +1,39 @@
+/**
+ * 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.ognl;
+
+import java.util.Map;
+
+import ognl.ClassResolver;
+/**
+ * This class is used to wrap the org.apache.camel.spi.ClassResolver with ClassResolver interface
+ */
+public class CamelClassResolver implements ClassResolver {
+    
+    private org.apache.camel.spi.ClassResolver delegateClassResolver;
+    
+    public CamelClassResolver(org.apache.camel.spi.ClassResolver resolver) {
+        delegateClassResolver = resolver;
+    }
+
+    @Override
+    public Class<?> classForName(String className, Map context) throws ClassNotFoundException
{
+       
+        return delegateClassResolver.resolveClass(className);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/94b7cba4/components/camel-ognl/src/main/java/org/apache/camel/language/ognl/OgnlExpression.java
----------------------------------------------------------------------
diff --git a/components/camel-ognl/src/main/java/org/apache/camel/language/ognl/OgnlExpression.java
b/components/camel-ognl/src/main/java/org/apache/camel/language/ognl/OgnlExpression.java
index 96b3e52..973e35c 100644
--- a/components/camel-ognl/src/main/java/org/apache/camel/language/ognl/OgnlExpression.java
+++ b/components/camel-ognl/src/main/java/org/apache/camel/language/ognl/OgnlExpression.java
@@ -51,6 +51,8 @@ public class OgnlExpression extends ExpressionSupport {
 
     public <T> T evaluate(Exchange exchange, Class<T> tClass) {
         OgnlContext oglContext = new OgnlContext();
+        // setup the class resolver from camel
+        oglContext.setClassResolver(new CamelClassResolver(exchange.getContext().getClassResolver()));
         try {
             Object value = Ognl.getValue(expression, oglContext, new RootObject(exchange));
             return exchange.getContext().getTypeConverter().convertTo(tClass, value);

http://git-wip-us.apache.org/repos/asf/camel/blob/94b7cba4/components/camel-ognl/src/test/java/org/apache/camel/language/ognl/Animal.java
----------------------------------------------------------------------
diff --git a/components/camel-ognl/src/test/java/org/apache/camel/language/ognl/Animal.java
b/components/camel-ognl/src/test/java/org/apache/camel/language/ognl/Animal.java
index c3266ba..d55d3a2 100644
--- a/components/camel-ognl/src/test/java/org/apache/camel/language/ognl/Animal.java
+++ b/components/camel-ognl/src/test/java/org/apache/camel/language/ognl/Animal.java
@@ -53,5 +53,9 @@ public class Animal {
     public String toString() {
         return name;
     }
+    
+    public static String getClassName() {
+        return "Animal";
+    }
 }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/94b7cba4/components/camel-ognl/src/test/java/org/apache/camel/language/ognl/MyClassResolver.java
----------------------------------------------------------------------
diff --git a/components/camel-ognl/src/test/java/org/apache/camel/language/ognl/MyClassResolver.java
b/components/camel-ognl/src/test/java/org/apache/camel/language/ognl/MyClassResolver.java
new file mode 100644
index 0000000..5aff767
--- /dev/null
+++ b/components/camel-ognl/src/test/java/org/apache/camel/language/ognl/MyClassResolver.java
@@ -0,0 +1,29 @@
+/**
+ * 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.ognl;
+
+import org.apache.camel.impl.DefaultClassResolver;
+
+public class MyClassResolver extends DefaultClassResolver {
+    public Class<?> resolveClass(String name) {
+        if (name.equals("org.apache.camel.language.ognl.Animal1")) {
+            name = "org.apache.camel.language.ognl.Animal";
+        }
+        return loadClass(name, DefaultClassResolver.class.getClassLoader());
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/94b7cba4/components/camel-ognl/src/test/java/org/apache/camel/language/ognl/OgnlTest.java
----------------------------------------------------------------------
diff --git a/components/camel-ognl/src/test/java/org/apache/camel/language/ognl/OgnlTest.java
b/components/camel-ognl/src/test/java/org/apache/camel/language/ognl/OgnlTest.java
index acf7659..e0fb612 100644
--- a/components/camel-ognl/src/test/java/org/apache/camel/language/ognl/OgnlTest.java
+++ b/components/camel-ognl/src/test/java/org/apache/camel/language/ognl/OgnlTest.java
@@ -36,6 +36,18 @@ public class OgnlTest extends LanguageTestSupport {
     }
 
     @Test
+    public void testClassMethodExpression() throws Exception {
+        try {
+            assertExpression("@org.apache.camel.language.ognl.Animal1@getClassName()", "Animal");
+            fail("Expect exception here.");
+        } catch (Exception ex) {
+            assertTrue("We should get the ClassNotFoundException", ex.getMessage().indexOf("ClassNotFoundException")
> 0);
+        }
+        // setup the class resolver to load the right class for us
+        exchange.getContext().setClassResolver(new MyClassResolver());
+        assertExpression("@org.apache.camel.language.ognl.Animal1@getClassName()", "Animal");
+    }
+    @Test
     public void testGetOutFalseKeepsNullOutMessage() throws Exception {
         assertExpression("exchange.hasOut()", false);
         assertFalse(exchange.hasOut());


Mime
View raw message