You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2009/06/06 20:03:53 UTC

svn commit: r782291 - in /myfaces/tomahawk/trunk/core/src: main/java/org/apache/myfaces/custom/fileupload/ test/java/org/apache/myfaces/custom/fileupload/

Author: lu4242
Date: Sat Jun  6 18:03:52 2009
New Revision: 782291

URL: http://svn.apache.org/viewvc?rev=782291&view=rev
Log:
TOMAHAWK-1208 NotSerializableException for org.apache.myfaces.custom.fileupload.UploadedFileDefaultMemoryImpl$1

Modified:
    myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/fileupload/UploadedFileDefaultFileImpl.java
    myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/fileupload/UploadedFileDefaultMemoryImpl.java
    myfaces/tomahawk/trunk/core/src/test/java/org/apache/myfaces/custom/fileupload/HtmlFileUploadRendererTest.java

Modified: myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/fileupload/UploadedFileDefaultFileImpl.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/fileupload/UploadedFileDefaultFileImpl.java?rev=782291&r1=782290&r2=782291&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/fileupload/UploadedFileDefaultFileImpl.java (original)
+++ myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/fileupload/UploadedFileDefaultFileImpl.java Sat Jun  6 18:03:52 2009
@@ -22,6 +22,7 @@
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.Serializable;
 
 import org.apache.commons.fileupload.FileItem;
 import org.apache.commons.fileupload.disk.DiskFileItem;
@@ -41,19 +42,43 @@
     {
         super(fileItem.getName(), fileItem.getContentType());
         this.fileItem = (DiskFileItem) fileItem;
-        storageStrategy = new DiskStorageStrategy() {
+        storageStrategy = new DefaultDiskStorageStrategy();
+    }
 
-        public File getTempFile() {
-          return UploadedFileDefaultFileImpl.this.fileItem.getStoreLocation();
+    private class DefaultDiskStorageStrategy 
+        extends DiskStorageStrategy implements Serializable
+    {
+        private static final long serialVersionUID = 5191237379179109587L;
+        
+        public DefaultDiskStorageStrategy()
+        {
         }
 
-        public void deleteFileContents() {
-          UploadedFileDefaultFileImpl.this.fileItem.delete();
+        public File getTempFile()
+        {
+            if (UploadedFileDefaultFileImpl.this.fileItem != null)
+            {
+                return UploadedFileDefaultFileImpl.this.fileItem.getStoreLocation();
+            }
+            else
+            {
+                return null;
+            }
         }
-        
-      };
-    }
 
+        public void deleteFileContents()
+        {
+            // UploadedFileDefaultFileImpl.this.fileItem becomes null 
+            // when the parent class is serialized and deserialized.
+            // In this case, the instance contained by the original
+            // object is garbage collected, so we don't have to 
+            // worry about it.
+            if (UploadedFileDefaultFileImpl.this.fileItem != null)
+            {
+                UploadedFileDefaultFileImpl.this.fileItem.delete();
+            }
+        }
+    }
 
     /**
      * Answer the uploaded file contents.

Modified: myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/fileupload/UploadedFileDefaultMemoryImpl.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/fileupload/UploadedFileDefaultMemoryImpl.java?rev=782291&r1=782290&r2=782291&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/fileupload/UploadedFileDefaultMemoryImpl.java (original)
+++ myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/fileupload/UploadedFileDefaultMemoryImpl.java Sat Jun  6 18:03:52 2009
@@ -19,10 +19,12 @@
 package org.apache.myfaces.custom.fileupload;
 
 import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.fileupload.disk.DiskFileItem;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.Serializable;
 
 
 /**
@@ -34,25 +36,38 @@
     private static final long serialVersionUID = -6006333070975059090L;
     private byte[] bytes;
     private StorageStrategy storageStrategy;
-
+    private transient FileItem fileItem = null;
 
     public UploadedFileDefaultMemoryImpl(final FileItem fileItem) throws IOException
     {
         super(fileItem.getName(), fileItem.getContentType());
         int sizeInBytes = (int)fileItem.getSize();
         bytes = new byte[sizeInBytes];
+        this.fileItem = fileItem;
         fileItem.getInputStream().read(bytes);
-      this.storageStrategy = new StorageStrategy() {
+        this.storageStrategy = new DefaultMemoryStorageStrategy();
+    }
+    
+    private class DefaultMemoryStorageStrategy 
+        extends StorageStrategy implements Serializable
+    {
+        private static final long serialVersionUID = 3610866246514636068L;
 
-        public void deleteFileContents() {
-          fileItem.delete();
+        public void deleteFileContents()
+        {
+          // UploadedFileDefaultMemoryImpl.this.fileItem becomes null 
+          // when the parent class is serialized and deserialized.
+          // In this case, the instance contained by the original
+          // object is garbage collected, so we don't have to 
+          // worry about it.
+          if (UploadedFileDefaultMemoryImpl.this.fileItem != null)
+          {
+              UploadedFileDefaultMemoryImpl.this.fileItem.delete();
+          }
           bytes = null;
         }
-        
-      };
     }
 
-
     /**
      * Answer the uploaded file contents.
      *
@@ -75,7 +90,6 @@
         return new ByteArrayInputStream( bytes );
     }
 
-
     /**
      * Answer the size of this file.
      * @return long
@@ -86,10 +100,7 @@
         return bytes.length;
     }
 
-
     public StorageStrategy getStorageStrategy() {
       return storageStrategy;
-    }
-    
-    
+    }    
 }

Modified: myfaces/tomahawk/trunk/core/src/test/java/org/apache/myfaces/custom/fileupload/HtmlFileUploadRendererTest.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core/src/test/java/org/apache/myfaces/custom/fileupload/HtmlFileUploadRendererTest.java?rev=782291&r1=782290&r2=782291&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/core/src/test/java/org/apache/myfaces/custom/fileupload/HtmlFileUploadRendererTest.java (original)
+++ myfaces/tomahawk/trunk/core/src/test/java/org/apache/myfaces/custom/fileupload/HtmlFileUploadRendererTest.java Sat Jun  6 18:03:52 2009
@@ -18,13 +18,24 @@
  */
 package org.apache.myfaces.custom.fileupload;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+
 import junit.framework.Test;
 import junit.framework.TestSuite;
 
+import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.fileupload.disk.DiskFileItem;
 import org.apache.myfaces.test.AbstractTomahawkViewControllerTestCase;
 import org.apache.myfaces.test.utils.HtmlCheckAttributesUtil;
 import org.apache.myfaces.test.utils.HtmlRenderedAttr;
 import org.apache.shale.test.mock.MockResponseWriter;
+import org.easymock.MockControl;
 
 public class HtmlFileUploadRendererTest extends AbstractTomahawkViewControllerTestCase
 {
@@ -65,4 +76,81 @@
             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
         }
     }
+    
+    public void testUploadedFileDefaultFileImplSerializable() throws Exception
+    {
+        String fieldName = "inputFile";
+        String contentType = "someType";
+        boolean isFormField = true;
+        String fileName = "tempFile";
+        int sizeThreshold = 10000;
+        File repository = new File(System.getProperty("java.io.tmpdir"));
+        DiskFileItem item = new DiskFileItem(fieldName, contentType, isFormField, fileName, sizeThreshold, repository);
+
+        UploadedFileDefaultFileImpl original = new UploadedFileDefaultFileImpl(item);
+        
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        ObjectOutputStream oos = new ObjectOutputStream(out);
+        oos.writeObject(original);
+        oos.close();
+
+        byte[] serializedArray = out.toByteArray();
+        InputStream in = new ByteArrayInputStream(serializedArray);
+        ObjectInputStream ois = new ObjectInputStream(in);
+        UploadedFileDefaultFileImpl copy = (UploadedFileDefaultFileImpl) ois.readObject();
+        
+        assertEquals(original.getName(), copy.getName());
+        assertEquals(original.getContentType(), copy.getContentType());
+        assertEquals(copy.getSize(),0);
+        
+        copy.getStorageStrategy().deleteFileContents();
+    }
+    
+    public void testUploadedFileDefaultMemoryImplSerializable() throws Exception
+    {
+        String fieldName = "inputFile";
+        String contentType = "someType";
+        
+        MockControl control = MockControl.createControl(FileItem.class);
+        FileItem item = (FileItem) control.getMock();
+        
+        item.getName();
+        control.setReturnValue(fieldName,1);
+        item.getContentType();
+        control.setReturnValue(contentType,1);
+        item.getSize();
+        control.setReturnValue(0,1);
+        item.getInputStream();
+        control.setReturnValue(new InputStream()
+        {
+            public int read() throws IOException
+            {
+                return -1;
+            }
+        },1);
+
+        item.delete();
+        control.setVoidCallable(1);
+        
+        control.replay();
+        
+        UploadedFileDefaultMemoryImpl original = new UploadedFileDefaultMemoryImpl(item);
+        
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        ObjectOutputStream oos = new ObjectOutputStream(out);
+        oos.writeObject(original);
+        oos.close();
+
+        byte[] serializedArray = out.toByteArray();
+        InputStream in = new ByteArrayInputStream(serializedArray);
+        ObjectInputStream ois = new ObjectInputStream(in);
+        UploadedFileDefaultMemoryImpl copy = (UploadedFileDefaultMemoryImpl) ois.readObject();
+        
+        assertEquals(original.getName(), copy.getName());
+        assertEquals(original.getContentType(), copy.getContentType());
+        assertEquals(copy.getSize(),0);
+        
+        copy.getStorageStrategy().deleteFileContents();
+    }
+
 }