You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2012/01/02 09:01:58 UTC

svn commit: r1226364 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/model/ camel-core/src/test/java/org/apache/camel/component/properties/ camel-core/src/test/java/org/apache/camel/language/simple/ camel-core/src/test/resources/org/apache...

Author: davsclaus
Date: Mon Jan  2 08:01:57 2012
New Revision: 1226364

URL: http://svn.apache.org/viewvc?rev=1226364&view=rev
Log:
CAMEL-4844: Expressions in Camel routes is now also resolving property placeholders on route creation.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentSetHeaderSimpleTest.java
      - copied, changed from r1226049, camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentSimpleLanguageTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
    camel/trunk/camel-core/src/test/resources/org/apache/camel/component/properties/cheese.properties
    camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/GroovySetHeaderPropertyComponentTest.java
    camel/trunk/components/camel-script/src/test/resources/org/apache/camel/builder/script/myproperties.properties

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java?rev=1226364&r1=1226363&r2=1226364&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java Mon Jan  2 08:01:57 2012
@@ -418,14 +418,28 @@ public abstract class ProcessorDefinitio
     protected Processor makeProcessor(RouteContext routeContext) throws Exception {
         Processor processor = null;
 
+        // allow any custom logic before we create the processor
+        preCreateProcessor();
+
         // resolve properties before we create the processor
         resolvePropertyPlaceholders(routeContext, this);
 
         // resolve constant fields (eg Exchange.FILE_NAME)
         resolveKnownConstantFields(this);
 
-        // allow any custom logic before we create the processor
-        preCreateProcessor();
+        // also resolve properties and constant fields on embedded expressions
+        ProcessorDefinition me = (ProcessorDefinition) this;
+        if (me instanceof ExpressionNode) {
+            ExpressionNode exp = (ExpressionNode) me;
+            ExpressionDefinition expressionDefinition = exp.getExpression();
+            if (expressionDefinition != null) {
+                // resolve properties before we create the processor
+                resolvePropertyPlaceholders(routeContext, expressionDefinition);
+
+                // resolve constant fields (eg Exchange.FILE_NAME)
+                resolveKnownConstantFields(expressionDefinition);
+            }
+        }
 
         // at first use custom factory
         if (routeContext.getCamelContext().getProcessorFactory() != null) {
@@ -444,32 +458,36 @@ public abstract class ProcessorDefinitio
     }
 
     /**
-     * Inspects the given processor definition and resolves any property placeholders from its properties.
+     * Inspects the given definition and resolves any property placeholders from its properties.
      * <p/>
      * This implementation will check all the getter/setter pairs on this instance and for all the values
      * (which is a String type) will be property placeholder resolved.
      *
      * @param routeContext the route context
-     * @param definition   the processor definition
+     * @param definition   the definition
      * @throws Exception is thrown if property placeholders was used and there was an error resolving them
      * @see org.apache.camel.CamelContext#resolvePropertyPlaceholders(String)
      * @see org.apache.camel.component.properties.PropertiesComponent
      */
-    protected void resolvePropertyPlaceholders(RouteContext routeContext, ProcessorDefinition definition) throws Exception {
+    protected void resolvePropertyPlaceholders(RouteContext routeContext, Object definition) throws Exception {
         log.trace("Resolving property placeholders for: {}", definition);
 
         // find all getter/setter which we can use for property placeholders
         Map<Object, Object> properties = new HashMap<Object, Object>();
         IntrospectionSupport.getProperties(definition, properties, null);
 
+        ProcessorDefinition processorDefinition = null;
+        if (definition instanceof ProcessorDefinition) {
+            processorDefinition = (ProcessorDefinition) definition;
+        }
         // include additional properties which have the Camel placeholder QName
         // and when the definition parameter is this (otherAttributes belong to this)
-        if (definition.getOtherAttributes() != null) {
-            for (Object key : definition.getOtherAttributes().keySet()) {
+        if (processorDefinition != null && processorDefinition.getOtherAttributes() != null) {
+            for (Object key : processorDefinition.getOtherAttributes().keySet()) {
                 QName qname = (QName) key;
                 if (Constants.PLACEHOLDER_QNAME.equals(qname.getNamespaceURI())) {
                     String local = qname.getLocalPart();
-                    Object value = definition.getOtherAttributes().get(key);
+                    Object value = processorDefinition.getOtherAttributes().get(key);
                     if (value != null && value instanceof String) {
                         // value must be enclosed with placeholder tokens
                         String s = (String) value;
@@ -519,14 +537,14 @@ public abstract class ProcessorDefinitio
     }
 
     /**
-     * Inspects the given processor definition and resolves known fields
+     * Inspects the given definition and resolves known fields
      * <p/>
      * This implementation will check all the getter/setter pairs on this instance and for all the values
      * (which is a String type) will check if it refers to a known field (such as on Exchange).
      *
-     * @param definition   the processor definition
+     * @param definition   the definition
      */
-    protected void resolveKnownConstantFields(ProcessorDefinition definition) throws Exception {
+    protected void resolveKnownConstantFields(Object definition) throws Exception {
         log.trace("Resolving known fields for: {}", definition);
 
         // find all String getter/setter

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentSetHeaderSimpleTest.java (from r1226049, camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentSimpleLanguageTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentSetHeaderSimpleTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentSetHeaderSimpleTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentSimpleLanguageTest.java&r1=1226049&r2=1226364&rev=1226364&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentSimpleLanguageTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentSetHeaderSimpleTest.java Mon Jan  2 08:01:57 2012
@@ -17,113 +17,20 @@
 package org.apache.camel.component.properties;
 
 import org.apache.camel.CamelContext;
-import org.apache.camel.CamelExecutionException;
 import org.apache.camel.ContextTestSupport;
-import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.builder.RouteBuilder;
 
 /**
  * @version 
  */
-public class PropertiesComponentSimpleLanguageTest extends ContextTestSupport {
+public class PropertiesComponentSetHeaderSimpleTest extends ContextTestSupport {
+    
+    public void testPropertiesAndSimple() throws Exception {
+        getMockEndpoint("mock:result").expectedHeaderReceived("foo", "http://mycoolserver/myapp");
 
-    @Override
-    public boolean isUseRouteBuilder() {
-        return false;
-    }
-
-    public void testPropertiesComponentSimpleLanguage() throws Exception {
-        context.addRoutes(new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                from("direct:start")
-                    .transform().simple("Hi ${body} do you think ${properties:cheese.quote}?");
-            }
-        });
-        context.start();
-
-        String reply = template.requestBody("direct:start", "Claus", String.class);
-        assertEquals("Hi Claus do you think Camel rocks?", reply);
-    }
-
-    public void testPropertiesComponentDualSimpleLanguage() throws Exception {
-        context.addRoutes(new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                from("direct:start")
-                    .transform().simple("Hi ${body} do you think ${properties:cheese.quote}? And do you like ${properties:cheese.type} cheese?");
-            }
-        });
-        context.start();
-
-        String reply = template.requestBody("direct:start", "Claus", String.class);
-        assertEquals("Hi Claus do you think Camel rocks? And do you like Gouda cheese?", reply);
-    }
-
-    public void testPropertiesComponentSimpleLanguageWithLocations() throws Exception {
-        context.addRoutes(new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                from("direct:start")
-                    .transform().simple("Hi ${body}. ${properties:org/apache/camel/component/properties/bar.properties:bar.quote}.");
-            }
-        });
-        context.start();
-
-        String reply = template.requestBody("direct:start", "Claus", String.class);
-        assertEquals("Hi Claus. Beer taste good.", reply);
-    }
-
-    public void testNoExistingPropertiesComponentWithLocation() throws Exception {
-        context.removeComponent("properties");
-        context.addRoutes(new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                from("direct:start")
-                    .transform().simple("Hi ${body}. ${properties:org/apache/camel/component/properties/bar.properties:bar.quote}.");
-            }
-        });
-        context.start();
-
-        String reply = template.requestBody("direct:start", "Claus", String.class);
-        assertEquals("Hi Claus. Beer taste good.", reply);
-    }
-
-    public void testNoExistingPropertiesComponentWithLocations() throws Exception {
-        context.removeComponent("properties");
-        context.addRoutes(new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                from("direct:start")
-                    .transform().simple("Hi ${body}. ${properties:org/apache/camel/component/properties/bar.properties,"
-                        + "org/apache/camel/component/properties/cheese.properties:cheese.quote}.");
-            }
-        });
-        context.start();
-
-        String reply = template.requestBody("direct:start", "Claus", String.class);
-        assertEquals("Hi Claus. Camel rocks.", reply);
-    }
-
-    public void testNoExistingPropertiesComponentWithoutLocation() throws Exception {
-        context.removeComponent("properties");
-        context.addRoutes(new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                from("direct:start")
-                    .transform().simple("Hi ${body} do you think ${properties:cheese.quote}?");
-            }
-        });
-        context.start();
+        template.sendBodyAndHeader("direct:start", "Hello World", "app", "myapp");
 
-        try {
-            template.requestBody("direct:start", "Claus", String.class);
-            fail("Should have thrown exception");
-        } catch (CamelExecutionException e) {
-            RuntimeCamelException rce = assertIsInstanceOf(RuntimeCamelException.class, e.getCause());
-            IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, rce.getCause());
-            assertEquals("PropertiesComponent with name properties must be defined in CamelContext to support property placeholders in expressions", iae.getMessage());
-        }
+        assertMockEndpointsSatisfied();
     }
 
     @Override
@@ -138,4 +45,15 @@ public class PropertiesComponentSimpleLa
         return context;
     }
 
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .setHeader("foo").simple("{{cheese.server}}/${header.app}")
+                    .to("mock:result");
+            }
+        };
+    }
 }
\ No newline at end of file

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java?rev=1226364&r1=1226363&r2=1226364&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java Mon Jan  2 08:01:57 2012
@@ -1016,6 +1016,11 @@ public class SimpleTest extends Language
         assertExpression("${body.getClass.getSimpleName}", "Animal");
         assertExpression("${body.class.simpleName}", "Animal");
     }
+    
+    public void testSlashBeforeHeader() throws Exception {
+        assertExpression("foo/${header.foo}", "foo/abc");
+        assertExpression("foo\\${header.foo}", "foo\\abc");
+    }
 
     protected String getLanguageName() {
         return "simple";

Modified: camel/trunk/camel-core/src/test/resources/org/apache/camel/component/properties/cheese.properties
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/resources/org/apache/camel/component/properties/cheese.properties?rev=1226364&r1=1226363&r2=1226364&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/resources/org/apache/camel/component/properties/cheese.properties (original)
+++ camel/trunk/camel-core/src/test/resources/org/apache/camel/component/properties/cheese.properties Mon Jan  2 08:01:57 2012
@@ -18,6 +18,7 @@
 cheese.end=mock:cheese
 cheese.quote=Camel rocks
 cheese.type=Gouda
+cheese.server=http://mycoolserver
 
 bean.foo=foo
 bean.bar=bar
\ No newline at end of file

Modified: camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/GroovySetHeaderPropertyComponentTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/GroovySetHeaderPropertyComponentTest.java?rev=1226364&r1=1226363&r2=1226364&view=diff
==============================================================================
--- camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/GroovySetHeaderPropertyComponentTest.java (original)
+++ camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/GroovySetHeaderPropertyComponentTest.java Mon Jan  2 08:01:57 2012
@@ -45,6 +45,20 @@ public class GroovySetHeaderPropertyComp
         assertMockEndpointsSatisfied();
     }
 
+    @Test
+    public void testNumber() throws Exception {
+        if (!ScriptTestHelper.canRunTestOnThisPlatform()) {
+            return;
+        }
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived(5);
+
+        template.sendBody("direct:number", 3);
+
+        assertMockEndpointsSatisfied();
+    }
+
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             public void configure() throws Exception {
@@ -54,6 +68,10 @@ public class GroovySetHeaderPropertyComp
                 from("direct:start")
                     .setHeader("myHeader").groovy("context.resolvePropertyPlaceholders('{{' + request.headers.get('foo') + '}}')")
                     .to("mock:result");
+                
+                from("direct:number")
+                    .transform().groovy("{{myscript}}")
+                    .to("mock:result");
             }
         };
     }

Modified: camel/trunk/components/camel-script/src/test/resources/org/apache/camel/builder/script/myproperties.properties
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-script/src/test/resources/org/apache/camel/builder/script/myproperties.properties?rev=1226364&r1=1226363&r2=1226364&view=diff
==============================================================================
--- camel/trunk/components/camel-script/src/test/resources/org/apache/camel/builder/script/myproperties.properties (original)
+++ camel/trunk/components/camel-script/src/test/resources/org/apache/camel/builder/script/myproperties.properties Mon Jan  2 08:01:57 2012
@@ -16,4 +16,6 @@
 ## ---------------------------------------------------------------------------
 
 foo=Camel
-bar=Kong
\ No newline at end of file
+bar=Kong
+
+myscript=2 + request.body
\ No newline at end of file