You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by ge...@apache.org on 2010/04/24 07:33:00 UTC

svn commit: r937593 - in /servicemix/smx4/features/trunk/camel/servicemix-camel/src: main/java/org/apache/servicemix/camel/nmr/ test/java/org/apache/servicemix/camel/nmr/

Author: gertv
Date: Sat Apr 24 05:33:00 2010
New Revision: 937593

URL: http://svn.apache.org/viewvc?rev=937593&view=rev
Log:
SMX4-522: Camel NMR component's response Exchange contains header to itself

Added:
    servicemix/smx4/features/trunk/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/ServiceMixBindingTest.java
Modified:
    servicemix/smx4/features/trunk/camel/servicemix-camel/src/main/java/org/apache/servicemix/camel/nmr/ServiceMixBinding.java
    servicemix/smx4/features/trunk/camel/servicemix-camel/src/main/java/org/apache/servicemix/camel/nmr/ServiceMixConsumer.java
    servicemix/smx4/features/trunk/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/AbstractComponentTest.java

Modified: servicemix/smx4/features/trunk/camel/servicemix-camel/src/main/java/org/apache/servicemix/camel/nmr/ServiceMixBinding.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/features/trunk/camel/servicemix-camel/src/main/java/org/apache/servicemix/camel/nmr/ServiceMixBinding.java?rev=937593&r1=937592&r2=937593&view=diff
==============================================================================
--- servicemix/smx4/features/trunk/camel/servicemix-camel/src/main/java/org/apache/servicemix/camel/nmr/ServiceMixBinding.java (original)
+++ servicemix/smx4/features/trunk/camel/servicemix-camel/src/main/java/org/apache/servicemix/camel/nmr/ServiceMixBinding.java Sat Apr 24 05:33:00 2010
@@ -41,12 +41,14 @@ public class ServiceMixBinding {
     public static final String NMR_OPERATION = "nmrOperation";
     
     public void copyCamelMessageToNmrMessage(org.apache.servicemix.nmr.api.Message nmrMessage, Message camelMessage) {
-        nmrMessage.setBody(camelMessage.getBody());
-        nmrMessage.getHeaders().clear();
-        addNmrHeaders(nmrMessage, camelMessage);
-        nmrMessage.getAttachments().clear();
-        nmrMessage.getAttachments().putAll(camelMessage.getAttachments());
-        addSecuritySubject(nmrMessage, camelMessage);
+        if (nmrMessage != null && camelMessage != null) {
+            nmrMessage.setBody(camelMessage.getBody());
+            nmrMessage.getHeaders().clear();
+            addNmrHeaders(nmrMessage, camelMessage);
+            nmrMessage.getAttachments().clear();
+            nmrMessage.getAttachments().putAll(camelMessage.getAttachments());
+            addSecuritySubject(nmrMessage, camelMessage);
+        }
     }
 
     public void copyNmrMessageToCamelMessage(org.apache.servicemix.nmr.api.Message nmrMessage, Message camelMessage) {
@@ -137,6 +139,15 @@ public class ServiceMixBinding {
         }
         return null;
     }
-    
-    
+
+
+    /**
+     * Extract the NMR Exchange from the Camel Exchange
+     *
+     * @param camel the Camel Exchange
+     * @return the NMR Exchange
+     */
+    public org.apache.servicemix.nmr.api.Exchange extractNmrExchange(Exchange camel) {
+        return (org.apache.servicemix.nmr.api.Exchange) camel.getProperties().remove(NMR_EXCHANGE);
+    }
 }

Modified: servicemix/smx4/features/trunk/camel/servicemix-camel/src/main/java/org/apache/servicemix/camel/nmr/ServiceMixConsumer.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/features/trunk/camel/servicemix-camel/src/main/java/org/apache/servicemix/camel/nmr/ServiceMixConsumer.java?rev=937593&r1=937592&r2=937593&view=diff
==============================================================================
--- servicemix/smx4/features/trunk/camel/servicemix-camel/src/main/java/org/apache/servicemix/camel/nmr/ServiceMixConsumer.java (original)
+++ servicemix/smx4/features/trunk/camel/servicemix-camel/src/main/java/org/apache/servicemix/camel/nmr/ServiceMixConsumer.java Sat Apr 24 05:33:00 2010
@@ -21,6 +21,7 @@ import java.util.Map;
 import org.apache.camel.Consumer;
 import org.apache.camel.Processor;
 import org.apache.camel.impl.DefaultConsumer;
+import org.apache.camel.spi.Synchronization;
 import org.apache.servicemix.nmr.api.Channel;
 import org.apache.servicemix.nmr.api.Endpoint;
 import org.apache.servicemix.nmr.api.Exchange;
@@ -68,7 +69,10 @@ public class ServiceMixConsumer extends 
             try {
             	org.apache.camel.Exchange camelExchange = getEndpoint().createExchange(exchange);
             	getProcessor().process(camelExchange);
-                
+
+                // extract the NMR Exchange from the Camel Exchange
+                getEndpoint().getComponent().getBinding().extractNmrExchange(camelExchange);
+
                 // just copy the camelExchange back to the nmr exchange
             	exchange.getProperties().putAll(camelExchange.getProperties());
                 if (camelExchange.hasOut() && !camelExchange.getOut().isFault()) {
@@ -78,7 +82,7 @@ public class ServiceMixConsumer extends 
                 	getEndpoint().getComponent().getBinding().
             			copyCamelMessageToNmrMessage(exchange.getFault(), camelExchange.getOut());
                 } else if (camelExchange.getException() != null) {
-                	throw (Exception)camelExchange.getException();
+                	throw (Exception) camelExchange.getException();
                 } else {
                     exchange.setStatus(Status.Done);
                 }

Modified: servicemix/smx4/features/trunk/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/AbstractComponentTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/features/trunk/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/AbstractComponentTest.java?rev=937593&r1=937592&r2=937593&view=diff
==============================================================================
--- servicemix/smx4/features/trunk/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/AbstractComponentTest.java (original)
+++ servicemix/smx4/features/trunk/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/AbstractComponentTest.java Sat Apr 24 05:33:00 2010
@@ -18,6 +18,9 @@ package org.apache.servicemix.camel.nmr;
 
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.impl.JndiRegistry;
+import org.apache.servicemix.executors.ExecutorFactory;
+import org.apache.servicemix.executors.impl.ExecutorConfig;
+import org.apache.servicemix.executors.impl.ExecutorFactoryImpl;
 import org.apache.servicemix.nmr.api.Channel;
 import org.apache.servicemix.nmr.core.ServiceMix;
 
@@ -35,6 +38,7 @@ public abstract class AbstractComponentT
     @Override
     protected void setUp() throws Exception {
         nmr = new ServiceMix();
+        nmr.setExecutorFactory(createExecutorFactory());
         nmr.init();
 
         component = new ServiceMixComponent();
@@ -43,6 +47,22 @@ public abstract class AbstractComponentT
         super.setUp();
     }
 
+    /*
+     * Create the ExecutorFactory for the unit test
+     * based on the default configuration used in ServiceMix 4
+     */
+    protected ExecutorFactory createExecutorFactory() {
+        ExecutorFactoryImpl factory = new ExecutorFactoryImpl();
+
+        ExecutorConfig config = factory.getDefaultConfig();
+        config.setCorePoolSize(1);
+        config.setMaximumPoolSize(16);
+        config.setQueueSize(256);
+        config.setBypassIfSynchronous(true);
+
+        return factory;
+    };
+
     @Override
     protected JndiRegistry createRegistry() throws Exception {
         JndiRegistry registry = super.createRegistry();

Added: servicemix/smx4/features/trunk/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/ServiceMixBindingTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/features/trunk/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/ServiceMixBindingTest.java?rev=937593&view=auto
==============================================================================
--- servicemix/smx4/features/trunk/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/ServiceMixBindingTest.java (added)
+++ servicemix/smx4/features/trunk/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/ServiceMixBindingTest.java Sat Apr 24 05:33:00 2010
@@ -0,0 +1,56 @@
+/*
+ * 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.servicemix.camel.nmr;
+
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.servicemix.nmr.api.Exchange;
+import org.apache.servicemix.nmr.api.Pattern;
+
+/**
+ * Test cases for {@link org.apache.servicemix.camel.nmr.ServiceMixBinding}
+ */
+public class ServiceMixBindingTest extends AbstractComponentTest {
+
+    private static final String KEY = "test.key";
+    private static final String VALUE = "test.value";
+    private static final String MESSAGE = "Message content";
+
+    private ServiceMixBinding binding = new ServiceMixBinding();
+
+    public void testToCamelAndBackToNmr() {
+        Exchange nmr = getChannel().createExchange(Pattern.InOnly);
+        nmr.setProperty(KEY, VALUE);
+        nmr.getIn().setBody(MESSAGE);
+        nmr.getIn().setHeader(KEY, VALUE);
+
+        org.apache.camel.Exchange camel =
+                binding.populateCamelExchangeFromNmrExchange(new DefaultCamelContext(), nmr);
+
+        assertEquals(VALUE, camel.getProperty(KEY));
+        assertEquals(VALUE, camel.getIn().getHeader(KEY));
+        assertEquals(MESSAGE, camel.getIn().getBody());
+        assertEquals("NMR Exchange should be available on the Camel Exchange",
+                     nmr, camel.getProperty(ServiceMixBinding.NMR_EXCHANGE));
+
+        assertSame(nmr, binding.extractNmrExchange(camel));
+        assertNull("NMR Exchange should have been removed from the Camel Exchange",
+                   camel.getProperty(ServiceMixBinding.NMR_EXCHANGE));
+        
+
+    }
+
+}