You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ha...@apache.org on 2010/07/07 04:48:51 UTC

svn commit: r961060 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/model/PolicyDefinition.java main/java/org/apache/camel/spi/DefinitionAwarePolicy.java test/java/org/apache/camel/processor/DefinitionPolicyPerProcessorTest.java

Author: hadrian
Date: Wed Jul  7 02:48:50 2010
New Revision: 961060

URL: http://svn.apache.org/viewvc?rev=961060&view=rev
Log:
CAMEL-2914. Patch applied with thanks to Mark

Added:
    camel/trunk/camel-core/src/main/java/org/apache/camel/spi/DefinitionAwarePolicy.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DefinitionPolicyPerProcessorTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/PolicyDefinition.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/PolicyDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/PolicyDefinition.java?rev=961060&r1=961059&r2=961060&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/PolicyDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/PolicyDefinition.java Wed Jul  7 02:48:50 2010
@@ -23,7 +23,9 @@ import javax.xml.bind.annotation.XmlRoot
 import javax.xml.bind.annotation.XmlTransient;
 
 import org.apache.camel.Processor;
+import org.apache.camel.processor.DelegateProcessor;
 import org.apache.camel.processor.WrapProcessor;
+import org.apache.camel.spi.DefinitionAwarePolicy;
 import org.apache.camel.spi.Policy;
 import org.apache.camel.spi.RouteContext;
 import org.apache.camel.spi.TransactedPolicy;
@@ -120,11 +122,18 @@ public class PolicyDefinition extends Ou
 
     @Override
     public Processor createProcessor(RouteContext routeContext) throws Exception {
-        Processor childProcessor = this.createChildProcessor(routeContext, true);
+        DelegateProcessor childProcessor = new DelegateProcessor();
 
         Policy policy = resolvePolicy(routeContext);
         ObjectHelper.notNull(policy, "policy", this);
-        Processor target = policy.wrap(routeContext, childProcessor);
+        Processor target;
+        if (policy instanceof DefinitionAwarePolicy) {
+            target = ((DefinitionAwarePolicy)policy).wrap(routeContext, childProcessor, this);
+        } else {
+            target = policy.wrap(routeContext, childProcessor);
+        }
+        
+        childProcessor.setProcessor(this.createChildProcessor(routeContext, true));
 
         // wrap the target so it becomes a service and we can manage its lifecycle
         WrapProcessor wrap = new WrapProcessor(target, childProcessor);

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/spi/DefinitionAwarePolicy.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/DefinitionAwarePolicy.java?rev=961060&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/spi/DefinitionAwarePolicy.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/spi/DefinitionAwarePolicy.java Wed Jul  7 02:48:50 2010
@@ -0,0 +1,38 @@
+/**
+ * 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.spi;
+
+import org.apache.camel.Processor;
+import org.apache.camel.model.ProcessorDefinition;
+
+/**
+ * A strategy capable of applying interceptors to a processor
+ *
+ * @version $Revision: 761894 $
+ */
+public interface DefinitionAwarePolicy extends Policy {
+
+    /**
+     * Wraps any applicable interceptors around the given processor
+     *
+     * @param routeContext the route context
+     * @param  ProcessorDefinition<ProcessorDefinition>
+     * @param processor the processor to be intercepted
+     * @return either the original processor or a processor wrapped in one or more interceptors
+     */
+    Processor wrap(RouteContext routeContext, Processor processor, ProcessorDefinition<?> processorDefinition);
+}

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DefinitionPolicyPerProcessorTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DefinitionPolicyPerProcessorTest.java?rev=961060&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DefinitionPolicyPerProcessorTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DefinitionPolicyPerProcessorTest.java Wed Jul  7 02:48:50 2010
@@ -0,0 +1,104 @@
+/**
+ * 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.processor;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.ExpressionClause;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.JndiRegistry;
+import org.apache.camel.model.ProcessorDefinition;
+import org.apache.camel.model.SetBodyDefinition;
+import org.apache.camel.model.language.ExpressionDefinition;
+import org.apache.camel.spi.DefinitionAwarePolicy;
+import org.apache.camel.spi.RouteContext;
+
+/**
+ * @version $Revision: 937165 $
+ */
+public class DefinitionPolicyPerProcessorTest extends ContextTestSupport {
+
+    public void testDefintionAugmentationPolicy() throws Exception {
+        getMockEndpoint("mock:foo").expectedMessageCount(1);
+        getMockEndpoint("mock:foo").expectedHeaderReceived("foo", "was wrapped");
+        getMockEndpoint("mock:foo").expectedBodyReceived().constant("body was altered");        
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+
+        MyPolicy foo = context.getRegistry().lookup("foo", MyPolicy.class);
+        assertEquals("Should only be invoked 1 time", 1, foo.getInvoked());
+    }
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+        jndi.bind("foo", new MyPolicy("foo"));
+        return jndi;
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                // START SNIPPET: e1
+                from("direct:start")
+                    // only wrap policy foo around the to(mock:foo) - notice the end()
+                    .policy("foo").setBody().constant("body not altered").to("mock:foo").end();
+            }
+        };
+    }
+
+    public static class MyPolicy implements DefinitionAwarePolicy {
+
+        private final String name;
+        private int invoked;
+
+        public MyPolicy(String name) {
+            this.name = name;
+        }
+
+        public Processor wrap(RouteContext routeContext, final Processor processor) {
+            throw new UnsupportedOperationException("This method should not be called");
+        }
+
+        public int getInvoked() {
+            return invoked;
+        }
+
+        public Processor wrap(final RouteContext routeContext,
+            final Processor processor,
+            final ProcessorDefinition<?> processorDefinition) {
+
+            // alter the child definition
+            SetBodyDefinition bodyDef = (SetBodyDefinition) processorDefinition.getOutputs().get(0);
+            ((ExpressionClause<?>)((ExpressionDefinition)bodyDef.getExpression()).getExpressionValue()).constant("body was altered");
+
+            // make sure the normal "wrap" still works.
+            return new Processor() {
+                public void process(Exchange exchange) throws Exception {
+                    invoked++;
+                    exchange.getIn().setHeader(name, "was wrapped");
+                    processor.process(exchange);
+                }
+            };
+        }
+    }
+}