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 2006/04/09 21:19:51 UTC

svn commit: r392808 - in /incubator/servicemix/trunk/servicemix-core/src: main/java/org/apache/servicemix/jbi/messaging/ main/java/org/apache/servicemix/jbi/util/ test/java/org/apache/servicemix/jbi/messaging/

Author: gnodet
Date: Sun Apr  9 12:19:48 2006
New Revision: 392808

URL: http://svn.apache.org/viewcvs?rev=392808&view=rev
Log:
SM-391: Exchange with attachments can not be serialized (therefore unusable on clustered flows)

Modified:
    incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/messaging/NormalizedMessageImpl.java
    incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/util/ByteArrayDataSource.java
    incubator/servicemix/trunk/servicemix-core/src/test/java/org/apache/servicemix/jbi/messaging/MessageExchangeImplTest.java

Modified: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/messaging/NormalizedMessageImpl.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/messaging/NormalizedMessageImpl.java?rev=392808&r1=392807&r2=392808&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/messaging/NormalizedMessageImpl.java (original)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/messaging/NormalizedMessageImpl.java Sun Apr  9 12:19:48 2006
@@ -15,14 +15,19 @@
  */
 package org.apache.servicemix.jbi.messaging;
 
-import org.apache.servicemix.client.Message;
-import org.apache.servicemix.jbi.RuntimeJBIException;
-import org.apache.servicemix.jbi.jaxp.BytesSource;
-import org.apache.servicemix.jbi.jaxp.ResourceSource;
-import org.apache.servicemix.jbi.jaxp.SourceTransformer;
-import org.apache.servicemix.jbi.jaxp.StringSource;
+import java.io.ByteArrayOutputStream;
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
 
 import javax.activation.DataHandler;
+import javax.activation.DataSource;
 import javax.jbi.messaging.Fault;
 import javax.jbi.messaging.MessageExchange;
 import javax.jbi.messaging.MessagingException;
@@ -33,15 +38,14 @@
 import javax.xml.transform.sax.SAXSource;
 import javax.xml.transform.stream.StreamSource;
 
-import java.io.Externalizable;
-import java.io.IOException;
-import java.io.ObjectInput;
-import java.io.ObjectOutput;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Set;
+import org.apache.servicemix.client.Message;
+import org.apache.servicemix.jbi.RuntimeJBIException;
+import org.apache.servicemix.jbi.jaxp.BytesSource;
+import org.apache.servicemix.jbi.jaxp.ResourceSource;
+import org.apache.servicemix.jbi.jaxp.SourceTransformer;
+import org.apache.servicemix.jbi.jaxp.StringSource;
+import org.apache.servicemix.jbi.util.ByteArrayDataSource;
+import org.apache.servicemix.jbi.util.FileUtil;
 
 /**
  * Represents a JBI NormalizedMessage.
@@ -166,7 +170,7 @@
      * @param content
      */
     public void addAttachment(String id, DataHandler content) {
-        getAttachments().put(id, content);
+        getAttachments().put(id, content.getDataSource());
     }
 
     /**
@@ -177,7 +181,7 @@
      */
     public DataHandler getAttachment(String id) {
         if (attachments != null) {
-            return (DataHandler) attachments.get(id);
+            return new DataHandler((DataSource) attachments.get(id));
         }
         return null;
     }
@@ -295,6 +299,7 @@
      */
     public void writeExternal(ObjectOutput out) throws IOException {
         try {
+            convertAttachments();
             out.writeObject(attachments);
             out.writeObject(properties);
             String src = transformer.toString(content);
@@ -310,6 +315,26 @@
             }
         } catch (TransformerException e) {
             throw (IOException) new IOException("Could not transform content to string").initCause(e);
+        }
+    }
+
+    private void convertAttachments() throws IOException {
+        if (attachments != null) {
+            Map newAttachments = createAttachmentsMap();
+            for (Iterator it = attachments.keySet().iterator(); it.hasNext();) {
+                String name = (String) it.next();
+                DataSource ds = (DataSource) attachments.get(name);
+                if (ds instanceof ByteArrayDataSource) {
+                    newAttachments.put(name, ds);
+                } else {
+                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
+                    FileUtil.copyInputStream(ds.getInputStream(), baos);
+                    ByteArrayDataSource bads = new ByteArrayDataSource(baos.toByteArray(), ds.getContentType());
+                    bads.setName(ds.getName());
+                    newAttachments.put(name, bads);
+                }
+            }
+            attachments = newAttachments;
         }
     }
 

Modified: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/util/ByteArrayDataSource.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/util/ByteArrayDataSource.java?rev=392808&r1=392807&r2=392808&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/util/ByteArrayDataSource.java (original)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/util/ByteArrayDataSource.java Sun Apr  9 12:19:48 2006
@@ -19,6 +19,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.Serializable;
 
 import javax.activation.DataSource;
 
@@ -27,7 +28,10 @@
  * @author George Gastaldi
  * @since 3.0
  */
-public class ByteArrayDataSource implements DataSource {
+public class ByteArrayDataSource implements DataSource, Serializable {
+
+    private static final long serialVersionUID = 1L;
+    
     private byte[] data;
     private String type;
     private String name = "unused";

Modified: incubator/servicemix/trunk/servicemix-core/src/test/java/org/apache/servicemix/jbi/messaging/MessageExchangeImplTest.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/test/java/org/apache/servicemix/jbi/messaging/MessageExchangeImplTest.java?rev=392808&r1=392807&r2=392808&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/test/java/org/apache/servicemix/jbi/messaging/MessageExchangeImplTest.java (original)
+++ incubator/servicemix/trunk/servicemix-core/src/test/java/org/apache/servicemix/jbi/messaging/MessageExchangeImplTest.java Sun Apr  9 12:19:48 2006
@@ -21,7 +21,9 @@
 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
 import org.apache.servicemix.jbi.jaxp.StringSource;
 import org.apache.servicemix.jbi.messaging.InOnlyImpl;
+import org.apache.servicemix.jbi.util.StreamDataSource;
 
+import javax.activation.DataHandler;
 import javax.jbi.messaging.MessageExchange;
 import javax.jbi.messaging.NormalizedMessage;
 import javax.xml.namespace.QName;
@@ -46,7 +48,7 @@
         NormalizedMessage msg = me.createMessage();
         msg.setProperty("myMsgProp", "myMsgValue");
         msg.setContent(src);
-        //msg.addAttachment("myAttachment", null);
+        msg.addAttachment("myAttachment", new DataHandler(new StreamDataSource(new ByteArrayInputStream("hello".getBytes()))));
         me.setMessage(msg, "in");
         assertNotNull(((NormalizedMessageImpl) msg).getBody());
         
@@ -73,6 +75,7 @@
         assertNotNull(outStr);
         assertNotNull(((NormalizedMessageImpl) msgOut).getBody());
         log.info(outStr);
+        assertNotNull(msgOut.getAttachment("myAttachment"));
     }