servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ge...@apache.org
Subject svn commit: r774372 - in /servicemix/components/engines/servicemix-camel/trunk/src: main/java/org/apache/servicemix/camel/ test/java/org/apache/servicemix/camel/
Date Wed, 13 May 2009 14:03:28 GMT
Author: gertv
Date: Wed May 13 14:03:28 2009
New Revision: 774372

URL: http://svn.apache.org/viewvc?rev=774372&view=rev
Log:
SMCOMP-495: servicemix-camel doesn't forward CamelCaughtException property

Added:
    servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiExchangeTest.java
  (with props)
Modified:
    servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/JbiBinding.java
    servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/JbiExchange.java
    servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiBindingTest.java
    servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyWithErrorHandledTrueSpringDSLTest.java
    servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyWithFaultHandledTrueSpringDSLTest.java

Modified: servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/JbiBinding.java
URL: http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/JbiBinding.java?rev=774372&r1=774371&r2=774372&view=diff
==============================================================================
--- servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/JbiBinding.java
(original)
+++ servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/JbiBinding.java
Wed May 13 14:03:28 2009
@@ -82,7 +82,7 @@
             jbiExchange.setMessage(normalizedMessage, "in");
         }
         normalizedMessage.setContent(getJbiInContent(camelExchange));
-        addJbiHeaders(jbiExchange, normalizedMessage, camelExchange);
+        addJbiHeaders(jbiExchange, normalizedMessage, camelExchange.getIn());
         addJbiAttachments(jbiExchange, normalizedMessage, camelExchange);
         return jbiExchange;
     }
@@ -165,10 +165,10 @@
     }
 
     protected void addJbiHeaders(MessageExchange jbiExchange, NormalizedMessage normalizedMessage,
-                                 Exchange camelExchange) {
-        Set<Map.Entry<String, Object>> entries = camelExchange.getIn().getHeaders().entrySet();
+                                 Message camelMessage) {
+        // get headers from the Camel in message
+        Set<Map.Entry<String, Object>> entries = camelMessage.getHeaders().entrySet();
         for (Map.Entry<String, Object> entry : entries) {
-            normalizedMessage.setProperty(entry.getKey(), entry.getValue());
             //check if value is Serializable, and if value is Map or collection,
             //just exclude it since the entry of it may not be Serializable as well
             if (entry.getValue() instanceof Serializable
@@ -177,6 +177,29 @@
                 normalizedMessage.setProperty(entry.getKey(), entry.getValue());
             }
         }
+        
+        // if there's a NormalizedMessage inside the Camel Message, copy those headers over
as well
+        NormalizedMessage camelNormalizedMessage = getNormalizedMessage(camelMessage);
+        if (camelNormalizedMessage != null) {
+            copyNormalizedMessageHeaders(normalizedMessage, camelNormalizedMessage);
+        }
+    }
+
+    @SuppressWarnings("unchecked")
+    private void copyNormalizedMessageHeaders(NormalizedMessage from, NormalizedMessage to)
{
+        Set<String> propertyNames = to.getPropertyNames();
+        for (String propertyName : propertyNames) {
+            if (from.getProperty(propertyName) == null) {
+                Object propertyValue = to.getProperty(propertyName);
+                //check if value is Serializable, and if value is Map or collection,
+                //just exclude it since the entry of it may not be Serializable as well
+                if (propertyValue instanceof Serializable
+                        && !(propertyValue instanceof Map)
+                        && !(propertyValue instanceof Collection)) {
+                    from.setProperty(propertyName, propertyValue);
+                }
+            }
+        }
     }
 
     protected void addJbiAttachments(MessageExchange jbiExchange, NormalizedMessage normalizedMessage,
@@ -189,4 +212,11 @@
         }
     }
 
+    protected NormalizedMessage getNormalizedMessage(Message message) {
+        if (message instanceof JbiMessage) {
+            return ((JbiMessage) message).getNormalizedMessage();
+        }
+        return null;
+    }
+
 }

Modified: servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/JbiExchange.java
URL: http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/JbiExchange.java?rev=774372&r1=774371&r2=774372&view=diff
==============================================================================
--- servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/JbiExchange.java
(original)
+++ servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/JbiExchange.java
Wed May 13 14:03:28 2009
@@ -85,6 +85,14 @@
     public org.apache.camel.Exchange newInstance() {        
         return new JbiExchange(this.getContext(), this.getBinding(), this.getMessageExchange());
     }
+    
+    @Override
+    public final void setProperty(String key, Object value) {
+        if (messageExchange != null && messageExchange.getProperty(key) == null)
{
+            messageExchange.setProperty(key, value);
+        }
+        super.setProperty(key, value);
+    }
 
     /**
      * @return the Camel <-> JBI binding

Modified: servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiBindingTest.java
URL: http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiBindingTest.java?rev=774372&r1=774371&r2=774372&view=diff
==============================================================================
--- servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiBindingTest.java
(original)
+++ servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiBindingTest.java
Wed May 13 14:03:28 2009
@@ -16,12 +16,17 @@
  */
 package org.apache.servicemix.camel;
 
+import javax.jbi.messaging.NormalizedMessage;
+
 import junit.framework.TestCase;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Exchange;
+import org.apache.camel.Message;
 import org.apache.camel.impl.DefaultCamelContext;
 import org.apache.camel.impl.DefaultExchange;
+import org.apache.camel.impl.DefaultMessage;
+import org.apache.servicemix.tck.mock.MockNormalizedMessage;
 
 public class JbiBindingTest extends TestCase {
 
@@ -38,5 +43,19 @@
         exchange.getIn().setBody("<hello>world!</hello>");
         assertNotNull(binding.getJbiInContent(exchange));
     }
+    
+    public void testGetNormalizedMessageForDefaultCamelMessage() {
+        Message message = new DefaultMessage();
+        assertNull(binding.getNormalizedMessage(message));
+    }
+    
+    public void testGetNormalizedMessageForJbiCamelMessage() {
+        JbiMessage camelMessage = new JbiMessage();
+        assertNull(binding.getNormalizedMessage(camelMessage));
+        
+        NormalizedMessage jbiMessage = new MockNormalizedMessage();
+        camelMessage.setNormalizedMessage(jbiMessage);
+        assertSame(jbiMessage, binding.getNormalizedMessage(camelMessage));
+    }
 
 }

Added: servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiExchangeTest.java
URL: http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiExchangeTest.java?rev=774372&view=auto
==============================================================================
--- servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiExchangeTest.java
(added)
+++ servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiExchangeTest.java
Wed May 13 14:03:28 2009
@@ -0,0 +1,79 @@
+/*
+ * 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;
+
+import java.net.URI;
+
+import javax.jbi.messaging.MessageExchange;
+
+import junit.framework.TestCase;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.servicemix.jbi.messaging.MessageExchangeSupport;
+import org.apache.servicemix.tck.mock.MockMessageExchange;
+
+/**
+ * Test case for {@link JbiExchange}
+ */
+public class JbiExchangeTest extends TestCase {
+    
+    private static final String KEY = "org.apache.servicemix.camel.TEST_KEY";
+    private static final String VALUE = "TEST_VALUE";
+    
+    /*
+     * Test setting the property on both the Camel Exchange and the underlying JBI MessageExchange
+     */
+    public void testSetPropertyOnMessageExchange() throws Exception {
+        MessageExchange jbiExchange = createMockExchange();
+        Exchange camelExchange = new JbiExchange(new DefaultCamelContext(), new JbiBinding(),
jbiExchange); 
+        camelExchange.setProperty(KEY, VALUE);
+        assertEquals(VALUE, camelExchange.getProperty(KEY));
+        assertEquals(VALUE, jbiExchange.getProperty(KEY));
+    }
+    
+    /*
+     * Test setting the property without overriding the existing JBI MessageExchange one
+     */
+    public void testSetPropertyNoOverrideMessageExchange() throws Exception {
+        MessageExchange jbiExchange = createMockExchange();
+        jbiExchange.setProperty(KEY, VALUE);
+        Exchange camelExchange = new JbiExchange(new DefaultCamelContext(), new JbiBinding(),
jbiExchange); 
+        camelExchange.setProperty(KEY, "OVERRIDE_TEST_VALUE");
+        assertEquals(VALUE, jbiExchange.getProperty(KEY));
+    }
+    
+    /*
+     * Test setting a property without having a MessageExchange in there
+     */
+    public void testSetPropertyWithoutMessageExchange() throws Exception {
+        Exchange camelExchange = new JbiExchange(new DefaultCamelContext(), new JbiBinding());

+        camelExchange.setProperty(KEY, VALUE);
+        assertEquals(VALUE, camelExchange.getProperty(KEY));
+    }
+    
+    private MessageExchange createMockExchange() {
+        return new MockMessageExchange() {
+            @Override
+            public URI getPattern() {
+                return MessageExchangeSupport.IN_OUT;
+            }
+        };
+    }
+
+
+}

Propchange: servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiExchangeTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyWithErrorHandledTrueSpringDSLTest.java
URL: http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyWithErrorHandledTrueSpringDSLTest.java?rev=774372&r1=774371&r2=774372&view=diff
==============================================================================
--- servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyWithErrorHandledTrueSpringDSLTest.java
(original)
+++ servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyWithErrorHandledTrueSpringDSLTest.java
Wed May 13 14:03:28 2009
@@ -23,11 +23,13 @@
 import javax.jbi.messaging.MessagingException;
 import javax.xml.namespace.QName;
 
+import org.apache.camel.processor.DeadLetterChannel;
 import org.apache.servicemix.MessageExchangeListener;
 import org.apache.servicemix.client.ServiceMixClient;
 import org.apache.servicemix.components.util.ComponentSupport;
 import org.apache.servicemix.jbi.container.ActivationSpec;
 import org.apache.servicemix.tck.ReceiverComponent;
+import org.springframework.util.Assert;
 
 /**
  * Tests on handling fault messages with the Camel Exception handler
@@ -41,7 +43,13 @@
 
     @Override
     protected void setUp() throws Exception {
-        receiver = new ReceiverComponent();
+        receiver = new ReceiverComponent() {
+            public void onMessageExchange(MessageExchange exchange) throws MessagingException
{
+                Object value = getInMessage(exchange).getProperty(DeadLetterChannel.CAUGHT_EXCEPTION_HEADER);
+                Assert.notNull(value, DeadLetterChannel.CAUGHT_EXCEPTION_HEADER + " property
not set");
+                super.onMessageExchange(exchange);
+            }
+        };
         deadLetter = new ReceiverComponent();
 
         super.setUp();
@@ -52,13 +60,14 @@
         MessageExchange[] exchanges = new MessageExchange[] {smxClient.createInOnlyExchange(),
smxClient.createRobustInOnlyExchange()};
         for (MessageExchange exchange : exchanges) {
             exchange.setService(TEST_SERVICE);
-    
+
             smxClient.send(exchange);
-    
+
             exchange = smxClient.receive();
             assertEquals(ExchangeStatus.DONE, exchange.getStatus());
+            assertNotNull(exchange.getMessage("in").getProperty(DeadLetterChannel.CAUGHT_EXCEPTION_HEADER));
         }
-    
+
         receiver.getMessageList().assertMessagesReceived(2);
         deadLetter.getMessageList().assertMessagesReceived(0);
     }
@@ -70,8 +79,8 @@
 
     @Override
     protected void appendJbiActivationSpecs(List<ActivationSpec> activationSpecList)
{
-        activationSpecList.add(createActivationSpec(new ReturnNullPointerExceptionErrorComponent(),

-                                                    new QName("urn:test", "npe-error-service")));
+        activationSpecList.add(createActivationSpec(new ReturnNullPointerExceptionErrorComponent(),
new QName("urn:test",
+                                                                                        
                     "npe-error-service")));
 
         activationSpecList.add(createActivationSpec(receiver, new QName("urn:test", "receiver-service")));
         activationSpecList.add(createActivationSpec(deadLetter, new QName("urn:test", "deadLetter-service")));

Modified: servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyWithFaultHandledTrueSpringDSLTest.java
URL: http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyWithFaultHandledTrueSpringDSLTest.java?rev=774372&r1=774371&r2=774372&view=diff
==============================================================================
--- servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyWithFaultHandledTrueSpringDSLTest.java
(original)
+++ servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/JbiInOnlyWithFaultHandledTrueSpringDSLTest.java
Wed May 13 14:03:28 2009
@@ -27,11 +27,13 @@
 import javax.xml.namespace.QName;
 
 import org.apache.camel.converter.jaxp.StringSource;
+import org.apache.camel.processor.DeadLetterChannel;
 import org.apache.servicemix.MessageExchangeListener;
 import org.apache.servicemix.client.ServiceMixClient;
 import org.apache.servicemix.components.util.ComponentSupport;
 import org.apache.servicemix.jbi.container.ActivationSpec;
 import org.apache.servicemix.tck.ReceiverComponent;
+import org.springframework.util.Assert;
 
 /**
  * Tests on handling fault messages with the Camel Exception handler
@@ -45,7 +47,13 @@
 
     @Override
     protected void setUp() throws Exception {
-        receiver = new ReceiverComponent();
+        receiver = new ReceiverComponent() {
+            public void onMessageExchange(MessageExchange exchange) throws MessagingException
{
+                Object value = getInMessage(exchange).getProperty(DeadLetterChannel.CAUGHT_EXCEPTION_HEADER);
+                Assert.notNull(value, DeadLetterChannel.CAUGHT_EXCEPTION_HEADER + " property
not set");
+                super.onMessageExchange(exchange);
+            }
+        };
         deadLetter = new ReceiverComponent();
 
         super.setUp();
@@ -58,8 +66,9 @@
 
         smxClient.send(exchange);
 
-        exchange = (InOnly) smxClient.receive();
+        exchange = (InOnly)smxClient.receive();
         assertEquals(ExchangeStatus.DONE, exchange.getStatus());
+        assertNotNull(exchange.getMessage("in").getProperty(DeadLetterChannel.CAUGHT_EXCEPTION_HEADER));
 
         receiver.getMessageList().assertMessagesReceived(1);
         deadLetter.getMessageList().assertMessagesReceived(0);
@@ -72,13 +81,14 @@
 
         smxClient.send(exchange);
 
-        exchange = (RobustInOnly) smxClient.receive();
+        exchange = (RobustInOnly)smxClient.receive();
         assertEquals(ExchangeStatus.DONE, exchange.getStatus());
+        assertNotNull(exchange.getMessage("in").getProperty(DeadLetterChannel.CAUGHT_EXCEPTION_HEADER));
 
         receiver.getMessageList().assertMessagesReceived(1);
         deadLetter.getMessageList().assertMessagesReceived(0);
     }
-    
+
     @Override
     protected String getServiceUnitName() {
         return "su9";
@@ -86,8 +96,7 @@
 
     @Override
     protected void appendJbiActivationSpecs(List<ActivationSpec> activationSpecList)
{
-        activationSpecList.add(createActivationSpec(new ReturnFaultComponent(), 
-                                                    new QName("urn:test", "faulty-service")));
+        activationSpecList.add(createActivationSpec(new ReturnFaultComponent(), new QName("urn:test",
"faulty-service")));
 
         activationSpecList.add(createActivationSpec(receiver, new QName("urn:test", "receiver-service")));
         activationSpecList.add(createActivationSpec(deadLetter, new QName("urn:test", "deadLetter-service")));



Mime
View raw message