You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by gn...@apache.org on 2008/11/02 01:46:07 UTC

svn commit: r709820 - in /servicemix/components/engines/servicemix-bean/trunk/src: main/java/org/apache/servicemix/bean/support/TransformBeanSupport.java test/java/org/apache/servicemix/bean/TransformBeanSupportTest.java

Author: gnodet
Date: Sat Nov  1 17:46:07 2008
New Revision: 709820

URL: http://svn.apache.org/viewvc?rev=709820&view=rev
Log:
SM-1667: TransformBeanSupport does not correctly report errors for in-only exchanges

Added:
    servicemix/components/engines/servicemix-bean/trunk/src/test/java/org/apache/servicemix/bean/TransformBeanSupportTest.java
Modified:
    servicemix/components/engines/servicemix-bean/trunk/src/main/java/org/apache/servicemix/bean/support/TransformBeanSupport.java

Modified: servicemix/components/engines/servicemix-bean/trunk/src/main/java/org/apache/servicemix/bean/support/TransformBeanSupport.java
URL: http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-bean/trunk/src/main/java/org/apache/servicemix/bean/support/TransformBeanSupport.java?rev=709820&r1=709819&r2=709820&view=diff
==============================================================================
--- servicemix/components/engines/servicemix-bean/trunk/src/main/java/org/apache/servicemix/bean/support/TransformBeanSupport.java (original)
+++ servicemix/components/engines/servicemix-bean/trunk/src/main/java/org/apache/servicemix/bean/support/TransformBeanSupport.java Sat Nov  1 17:46:07 2008
@@ -21,10 +21,14 @@
 import javax.jbi.messaging.MessageExchange;
 import javax.jbi.messaging.MessagingException;
 import javax.jbi.messaging.NormalizedMessage;
+import javax.annotation.PostConstruct;
 
 import org.apache.servicemix.common.JbiConstants;
 import org.apache.servicemix.jbi.listener.MessageExchangeListener;
 import org.apache.servicemix.jbi.transformer.CopyTransformer;
+import org.apache.servicemix.store.StoreFactory;
+import org.apache.servicemix.store.Store;
+import org.apache.servicemix.store.memory.MemoryStoreFactory;
 
 /**
  * A useful base class for a transform component.
@@ -37,10 +41,15 @@
 
     private boolean copyProperties = true;
     private boolean copyAttachments = true;
+    private StoreFactory storeFactory;
+    private Store store;
 
     protected TransformBeanSupport() {
     }
 
+    // Getters / Setters
+    //-------------------------------------------------------------------------
+
     public ExchangeTarget getTarget() {
         return target;
     }
@@ -49,7 +58,77 @@
         this.target = target;
     }
 
-    public void onMessageExchange(MessageExchange exchange) {
+    public boolean isCopyProperties() {
+        return copyProperties;
+    }
+
+
+    public void setCopyProperties(boolean copyProperties) {
+        this.copyProperties = copyProperties;
+        if (getMessageTransformer() instanceof CopyTransformer) {
+            ((CopyTransformer) getMessageTransformer()).setCopyProperties(copyProperties);
+        }
+    }
+
+
+    public boolean isCopyAttachments() {
+        return copyAttachments;
+    }
+
+
+    public void setCopyAttachments(boolean copyAttachments) {
+        this.copyAttachments = copyAttachments;
+        if (getMessageTransformer() instanceof CopyTransformer) {
+            ((CopyTransformer) getMessageTransformer()).setCopyAttachments(copyAttachments);
+        }
+    }
+
+    public StoreFactory getStoreFactory() {
+        return storeFactory;
+    }
+
+    public void setStoreFactory(StoreFactory storeFactory) {
+        this.storeFactory = storeFactory;
+    }
+
+    public Store getStore() {
+        return store;
+    }
+
+    public void setStore(Store store) {
+        this.store = store;
+    }
+
+    // Implementation methods
+    //-------------------------------------------------------------------------
+
+    @PostConstruct
+    public void initialize() throws Exception {
+        if (store == null) {
+            if (storeFactory == null) {
+                storeFactory = new MemoryStoreFactory();
+            }
+            store = storeFactory.open(getService().toString() + getEndpoint());
+        }
+    }
+
+    public void onMessageExchange(MessageExchange exchange) throws MessagingException {
+        // Handle consumer exchanges
+        if (exchange.getRole() == MessageExchange.Role.CONSUMER) {
+            MessageExchange original = null;
+            try {
+                original = (MessageExchange) store.load(exchange.getExchangeId());
+            } catch (Exception e) {
+                // We can't do, so just return
+                return;
+            }
+            if (exchange.getStatus() == ExchangeStatus.ERROR) {
+                original.setStatus(ExchangeStatus.ERROR);
+                original.setError(exchange.getError());
+                send(original);
+            }
+            return;
+        }
         // Skip done exchanges
         if (exchange.getStatus() == ExchangeStatus.DONE) {
             return;
@@ -82,23 +161,30 @@
                 if (isInAndOut(exchange)) {
                     exchange.setMessage(out, "out");
                     if (txSync) {
-                        getDeliveryChannel().sendSync(exchange);
+                        sendSync(exchange);
                     } else {
-                        getDeliveryChannel().send(exchange);
+                        send(exchange);
                     }
                 } else {
                     outExchange.setMessage(out, "in");
                     if (txSync) {
-                        getDeliveryChannel().sendSync(outExchange);
+                        sendSync(outExchange);
+                        if (outExchange.getStatus() == ExchangeStatus.ERROR) {
+                            exchange.setStatus(ExchangeStatus.ERROR);
+                            exchange.setError(outExchange.getError());
+                            send(exchange);
+                        } else {
+                            exchange.setStatus(ExchangeStatus.DONE);
+                            send(exchange);
+                        }
                     } else {
-                        getDeliveryChannel().send(outExchange);
+                        store.store(outExchange.getExchangeId(), exchange);
+                        send(outExchange);
                     }
-                    exchange.setStatus(ExchangeStatus.DONE);
-                    getDeliveryChannel().send(exchange);
                 }
             } else {
                 exchange.setStatus(ExchangeStatus.DONE);
-                getDeliveryChannel().send(exchange);
+                send(exchange);
             }
         } catch (Exception e) {
             try {
@@ -113,41 +199,12 @@
     }
 
 
-    // Implementation methods
-    //-------------------------------------------------------------------------
-
     /**
      * Transforms the given out message
      */
     protected abstract boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws Exception;
 
 
-    public boolean isCopyProperties() {
-        return copyProperties;
-    }
-
-
-    public void setCopyProperties(boolean copyProperties) {
-        this.copyProperties = copyProperties;
-        if (getMessageTransformer() instanceof CopyTransformer) {
-            ((CopyTransformer) getMessageTransformer()).setCopyProperties(copyProperties);
-        }
-    }
-
-
-    public boolean isCopyAttachments() {
-        return copyAttachments;
-    }
-
-
-    public void setCopyAttachments(boolean copyAttachments) {
-        this.copyAttachments = copyAttachments;
-        if (getMessageTransformer() instanceof CopyTransformer) {
-            ((CopyTransformer) getMessageTransformer()).setCopyAttachments(copyAttachments);
-        }
-    }
-
-
     /**
      * If enabled the properties and attachments are copied to the destination message
      */
@@ -160,4 +217,5 @@
             CopyTransformer.copyAttachments(in, out);
         }
     }
+
 }

Added: servicemix/components/engines/servicemix-bean/trunk/src/test/java/org/apache/servicemix/bean/TransformBeanSupportTest.java
URL: http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-bean/trunk/src/test/java/org/apache/servicemix/bean/TransformBeanSupportTest.java?rev=709820&view=auto
==============================================================================
--- servicemix/components/engines/servicemix-bean/trunk/src/test/java/org/apache/servicemix/bean/TransformBeanSupportTest.java (added)
+++ servicemix/components/engines/servicemix-bean/trunk/src/test/java/org/apache/servicemix/bean/TransformBeanSupportTest.java Sat Nov  1 17:46:07 2008
@@ -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.servicemix.bean;
+
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.jbi.messaging.MessagingException;
+import javax.jbi.messaging.ExchangeStatus;
+import javax.jbi.messaging.InOnly;
+import javax.jbi.component.Component;
+import javax.xml.namespace.QName;
+
+import junit.framework.TestCase;
+import org.apache.servicemix.bean.support.TransformBeanSupport;
+import org.apache.servicemix.bean.pojos.LoggingPojo;
+import org.apache.servicemix.common.util.MessageUtil;
+import org.apache.servicemix.jbi.container.JBIContainer;
+import org.apache.servicemix.jbi.listener.MessageExchangeListener;
+import org.apache.servicemix.jbi.jaxp.StringSource;
+import org.apache.servicemix.components.util.EchoComponent;
+import org.apache.servicemix.components.util.TransformComponentSupport;
+import org.apache.servicemix.components.util.ComponentSupport;
+import org.apache.servicemix.bean.support.ExchangeTarget;
+import org.apache.servicemix.client.ServiceMixClient;
+import org.apache.servicemix.client.DefaultServiceMixClient;
+
+public class TransformBeanSupportTest extends TestCase {
+
+    protected JBIContainer container;
+    protected BeanComponent component;
+
+    protected void setUp() throws Exception {
+        container = new JBIContainer();
+        container.setEmbedded(true);
+        container.init();
+
+        component = new BeanComponent();
+        container.activateComponent(component, "servicemix-bean");
+
+        container.start();
+    }
+
+    protected void tearDown() throws Exception {
+        container.shutDown();
+    }
+
+    public void testInOnlyWithError() throws Exception {
+        MyTransformer transformer = new MyTransformer();
+        ExchangeTarget target = new ExchangeTarget();
+        target.setService(new QName("error"));
+        transformer.setTarget(target);
+        BeanEndpoint transformEndpoint = new BeanEndpoint();
+        transformEndpoint.setBean(transformer);
+        transformEndpoint.setService(new QName("transform"));
+        transformEndpoint.setEndpoint("endpoint");
+        component.addEndpoint(transformEndpoint);
+
+        SendErrorComponent sendErrorComponent = new SendErrorComponent();
+        container.activateComponent(sendErrorComponent, "error");
+
+        ServiceMixClient client = new DefaultServiceMixClient(container);
+        MessageExchange io = client.createInOnlyExchange();
+        io.setService(new QName("transform"));
+        io.getMessage("in").setContent(new StringSource("<hello/>"));
+        client.send(io);
+        io = client.receive();
+
+        assertEquals(ExchangeStatus.ERROR, io.getStatus());
+    }
+
+    public static class MyTransformer extends TransformBeanSupport {
+        protected boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws Exception {
+            MessageUtil.transfer(in, out);
+            return true;
+        }
+    }
+
+    public static class SendErrorComponent extends ComponentSupport implements MessageExchangeListener {
+        public SendErrorComponent() {
+            setService(new QName("error"));
+        }
+
+        public void onMessageExchange(MessageExchange exchange) throws MessagingException {
+            exchange.setStatus(ExchangeStatus.ERROR);
+            exchange.setError(new Exception());
+            send(exchange);
+        }
+    }
+
+}