]> source.dussan.org Git - vaadin-framework.git/commitdiff
merged [9463] to 6.2
authorMatti Tahvonen <matti.tahvonen@itmill.com>
Thu, 29 Oct 2009 14:23:29 +0000 (14:23 +0000)
committerMatti Tahvonen <matti.tahvonen@itmill.com>
Thu, 29 Oct 2009 14:23:29 +0000 (14:23 +0000)
svn changeset:9465/svn branch:6.2

src/com/vaadin/terminal/gwt/server/CommunicationManager.java
src/com/vaadin/ui/Upload.java
tests/src/com/vaadin/tests/TestForUpload.java

index 8e73098454351162bebd981b76918c69d398bcd6..4238aab01919b0c641b9120b7f3db82a5c9d0ccc 100644 (file)
@@ -62,6 +62,7 @@ import com.vaadin.ui.AbstractField;
 import com.vaadin.ui.Component;
 import com.vaadin.ui.Upload;
 import com.vaadin.ui.Window;
+import com.vaadin.ui.Upload.UploadException;
 
 /**
  * Application manager processes changes and paints for single application
@@ -203,7 +204,18 @@ public class CommunicationManager implements Paintable.RepaintRequestListener,
                     // file
                     pl.setUpload(uploadComponent);
 
-                    uploadComponent.receiveUpload(upstream);
+                    try {
+                        uploadComponent.receiveUpload(upstream);
+                    } catch (UploadException e) {
+                        // error happened while receiving file. Handle the
+                        // error in the same manner as it would have happened in
+                        // variable change.
+                        synchronized (application) {
+                            handleChangeVariablesError(application,
+                                    uploadComponent, e,
+                                    new HashMap<String, Object>());
+                        }
+                    }
                 }
             }
         } catch (final FileUploadException e) {
index 4789641193db22414a620a354b5feb95d290ec96..dfbbbfc103306589b65c5f70def776ba1dacd3ef 100644 (file)
@@ -122,7 +122,7 @@ public class Upload extends AbstractComponent implements Component.Focusable {
      * 
      * @param upload
      */
-    public void receiveUpload(UploadStream upload) {
+    public void receiveUpload(UploadStream upload) throws UploadException {
         if (!isUploading) {
             throw new IllegalStateException("uploading not started");
         }
@@ -174,7 +174,7 @@ public class Upload extends AbstractComponent implements Component.Focusable {
                     }
                 }
                 if (interrupted) {
-                    throw new Exception("Upload interrupted by other thread");
+                    throw new UploadInterruptedException();
                 }
             }
 
@@ -188,17 +188,22 @@ public class Upload extends AbstractComponent implements Component.Focusable {
 
         } catch (final Exception e) {
             synchronized (application) {
-                // Download interrupted
-                try {
-                    // still try to close output stream
-                    out.close();
-                } catch (IOException ignored) {
+                if (e instanceof UploadInterruptedException) {
+                    // Download interrupted
+                    try {
+                        // still try to close output stream
+                        out.close();
+                    } catch (IOException e1) {
+                        // NOP
+                    }
                 }
                 fireUploadInterrupted(filename, type, totalBytes, e);
                 endUpload();
                 interrupted = false;
-                // throw cause ahead
-                throw new IllegalStateException("Uploading failed", e);
+                if (!(e instanceof UploadInterruptedException)) {
+                    // throw exception for terminal to be handled
+                    throw new UploadException(e);
+                }
             }
         }
     }
@@ -296,6 +301,19 @@ public class Upload extends AbstractComponent implements Component.Focusable {
         }
     }
 
+    private class UploadInterruptedException extends Exception {
+        public UploadInterruptedException() {
+            super("Upload interrupted by other thread");
+        }
+
+    }
+
+    public class UploadException extends Exception {
+        public UploadException(Exception e) {
+            super("Upload failed", e);
+        }
+    }
+
     /**
      * Upload.Received event is sent when the upload receives a file, regardless
      * of whether the reception was successful or failed. If you wish to
index 351a590b2668676a21fe1fb4c26d4d739ebca91d..0774a1a20998eb54bb950984e4d054af426f9b70 100644 (file)
@@ -13,36 +13,35 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.lang.management.ManagementFactory;
+import java.lang.management.MemoryMXBean;
 
 import com.vaadin.data.Property.ValueChangeEvent;
 import com.vaadin.terminal.StreamResource;
 import com.vaadin.ui.AbstractField;
 import com.vaadin.ui.Button;
+import com.vaadin.ui.CheckBox;
 import com.vaadin.ui.CustomComponent;
 import com.vaadin.ui.Label;
 import com.vaadin.ui.Layout;
 import com.vaadin.ui.Link;
-import com.vaadin.ui.OrderedLayout;
 import com.vaadin.ui.Panel;
 import com.vaadin.ui.ProgressIndicator;
 import com.vaadin.ui.Select;
 import com.vaadin.ui.TextField;
 import com.vaadin.ui.Upload;
+import com.vaadin.ui.VerticalLayout;
 import com.vaadin.ui.Button.ClickEvent;
-import com.vaadin.ui.Upload.FailedEvent;
-import com.vaadin.ui.Upload.FailedListener;
 import com.vaadin.ui.Upload.FinishedEvent;
-import com.vaadin.ui.Upload.FinishedListener;
 import com.vaadin.ui.Upload.StartedEvent;
 import com.vaadin.ui.Upload.StartedListener;
-import com.vaadin.ui.Upload.SucceededEvent;
-import com.vaadin.ui.Upload.SucceededListener;
 
 public class TestForUpload extends CustomComponent implements
-        Upload.FinishedListener, FailedListener, SucceededListener,
-        Upload.ProgressListener, StartedListener {
+        Upload.ProgressListener {
 
-    Layout main = new OrderedLayout();
+    private static final long serialVersionUID = -3400119871764256575L;
+
+    Layout main = new VerticalLayout();
 
     Buffer buffer = new MemoryBuffer();
 
@@ -63,6 +62,13 @@ public class TestForUpload extends CustomComponent implements
 
     private Label textFieldValue;
 
+    private CheckBox beSluggish = new CheckBox("Be sluggish");
+
+    private CheckBox throwExecption = new CheckBox(
+            "Throw exception in receiver");
+
+    private Button interrupt = new Button("Interrupt upload");
+
     public TestForUpload() {
         setCompositionRoot(main);
         main.addComponent(new Label(
@@ -86,12 +92,69 @@ public class TestForUpload extends CustomComponent implements
 
         up = new Upload("Upload", buffer);
         up.setImmediate(true);
-        up.addListener((FinishedListener) this);
-        up.addListener((FailedListener) this);
-        up.addListener((SucceededListener) this);
-        up.addListener((StartedListener) this);
+        up.addListener(new Listener() {
+            private static final long serialVersionUID = -8319074730512324303L;
+
+            public void componentEvent(Event event) {
+                // print out all events fired by upload for debug purposes
+                System.out.println("Upload fired event | " + event);
+            }
+        });
+
+        up.addListener(new StartedListener() {
+            private static final long serialVersionUID = 5508883803861085154L;
+
+            public void uploadStarted(StartedEvent event) {
+                pi.setVisible(true);
+                pi2.setVisible(true);
+                l.setValue("Started uploading file " + event.getFilename());
+                textFieldValue
+                        .setValue(" TestFields value at the upload start is:"
+                                + textField.getValue());
+            }
+        });
+
+        up.addListener(new Upload.FinishedListener() {
+            private static final long serialVersionUID = -3773034195991947371L;
+
+            public void uploadFinished(FinishedEvent event) {
+                pi.setVisible(false);
+                pi2.setVisible(false);
+                if (event instanceof Upload.FailedEvent) {
+                    Exception reason = ((Upload.FailedEvent) event).getReason();
+                    l.setValue("Finished with failure ( " + reason
+                            + "  ), idle");
+                } else if (event instanceof Upload.SucceededEvent) {
+                    l.setValue("Finished with succes, idle");
+                } else {
+                    l.setValue("Finished with unknow event");
+                }
+                setBuffer();
+
+                status.removeAllComponents();
+                final InputStream stream = buffer.getStream();
+                if (stream == null) {
+                    status.addComponent(new Label(
+                            "Upload finished, but output buffer is null"));
+                } else {
+                    status.addComponent(new Label("<b>Name:</b> "
+                            + event.getFilename(), Label.CONTENT_XHTML));
+                    status.addComponent(new Label("<b>Mimetype:</b> "
+                            + event.getMIMEType(), Label.CONTENT_XHTML));
+                    status.addComponent(new Label("<b>Size:</b> "
+                            + event.getLength() + " bytes.",
+                            Label.CONTENT_XHTML));
+
+                    status.addComponent(new Link("Download "
+                            + buffer.getFileName(), new StreamResource(buffer,
+                            buffer.getFileName(), getApplication())));
+
+                    status.setVisible(true);
+                }
+
+            }
+        });
 
-        up.setProgressListener(this);
         up.addListener(new Upload.ProgressListener() {
 
             public void updateProgress(long readBytes, long contentLenght) {
@@ -102,12 +165,20 @@ public class TestForUpload extends CustomComponent implements
 
         });
 
-        final Button b = new Button("b", this, "readState");
+        final Button b = new Button("Reed state from upload", this, "readState");
 
-        final Button c = new Button("b with gc", this, "gc");
+        final Button c = new Button("Force GC", this, "gc");
 
         main.addComponent(b);
         main.addComponent(c);
+        main.addComponent(beSluggish);
+        main.addComponent(throwExecption);
+        main.addComponent(interrupt);
+        interrupt.addListener(new Button.ClickListener() {
+            public void buttonClick(ClickEvent event) {
+                up.interruptUpload();
+            }
+        });
 
         uploadBufferSelector = new Select("Receiver type");
         uploadBufferSelector.setImmediate(true);
@@ -163,7 +234,6 @@ public class TestForUpload extends CustomComponent implements
 
     public void gc() {
         Runtime.getRuntime().gc();
-        readState();
     }
 
     public void readState() {
@@ -185,29 +255,6 @@ public class TestForUpload extends CustomComponent implements
         refreshMemUsage();
     }
 
-    public void uploadFinished(FinishedEvent event) {
-        status.removeAllComponents();
-        final InputStream stream = buffer.getStream();
-        if (stream == null) {
-            status.addComponent(new Label(
-                    "Upload finished, but output buffer is null!!"));
-        } else {
-            status
-                    .addComponent(new Label("<b>Name:</b> "
-                            + event.getFilename(), Label.CONTENT_XHTML));
-            status.addComponent(new Label("<b>Mimetype:</b> "
-                    + event.getMIMEType(), Label.CONTENT_XHTML));
-            status.addComponent(new Label("<b>Size:</b> " + event.getLength()
-                    + " bytes.", Label.CONTENT_XHTML));
-
-            status.addComponent(new Link("Download " + buffer.getFileName(),
-                    new StreamResource(buffer, buffer.getFileName(),
-                            getApplication())));
-
-            status.setVisible(true);
-        }
-    }
-
     public interface Buffer extends StreamResource.StreamSource,
             Upload.Receiver {
 
@@ -233,13 +280,20 @@ public class TestForUpload extends CustomComponent implements
         }
 
         /**
-         * @see com.vaadin.ui.Upload.Receiver#receiveUpload(String,
-         *      String)
+         * @see com.vaadin.ui.Upload.Receiver#receiveUpload(String, String)
          */
         public OutputStream receiveUpload(String filename, String MIMEType) {
             fileName = filename;
             mimeType = MIMEType;
-            outputBuffer = new ByteArrayOutputStream();
+            outputBuffer = new ByteArrayOutputStream() {
+                @Override
+                public synchronized void write(byte[] b, int off, int len) {
+                    beSluggish();
+                    throwExecption();
+                    super.write(b, off, len);
+                }
+
+            };
             return outputBuffer;
         }
 
@@ -296,14 +350,23 @@ public class TestForUpload extends CustomComponent implements
         }
 
         /**
-         * @see com.vaadin.ui.Upload.Receiver#receiveUpload(String,
-         *      String)
+         * @see com.vaadin.ui.Upload.Receiver#receiveUpload(String, String)
          */
         public OutputStream receiveUpload(String filename, String MIMEType) {
             fileName = filename;
             mimeType = MIMEType;
             try {
-                return new FileOutputStream(file);
+                return new FileOutputStream(file) {
+
+                    @Override
+                    public void write(byte[] b, int off, int len)
+                            throws IOException {
+                        beSluggish();
+                        throwExecption();
+                        super.write(b, off, len);
+                    }
+
+                };
             } catch (final FileNotFoundException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
@@ -331,21 +394,6 @@ public class TestForUpload extends CustomComponent implements
 
     }
 
-    public void uploadFailed(FailedEvent event) {
-        System.out.println(event);
-
-        System.out.println(event.getSource());
-
-    }
-
-    public void uploadSucceeded(SucceededEvent event) {
-        pi.setVisible(false);
-        pi2.setVisible(false);
-        l.setValue("Finished upload, idle");
-        System.out.println(event);
-        setBuffer();
-    }
-
     public void updateProgress(long readBytes, long contentLenght) {
         pi.setValue(new Float(readBytes / (float) contentLenght));
 
@@ -354,22 +402,33 @@ public class TestForUpload extends CustomComponent implements
 
     private void refreshMemUsage() {
         memoryStatus.setValue("Not available in Java 1.4");
-        /*
-         * StringBuffer mem = new StringBuffer(); MemoryMXBean mmBean =
-         * ManagementFactory.getMemoryMXBean(); mem.append("Heap (M):");
-         * mem.append(mmBean.getHeapMemoryUsage().getUsed() / 1048576);
-         * mem.append(" |�Non-Heap (M):");
-         * mem.append(mmBean.getNonHeapMemoryUsage().getUsed() / 1048576);
-         * memoryStatus.setValue(mem.toString());
-         */
+
+        StringBuffer mem = new StringBuffer();
+        MemoryMXBean mmBean = ManagementFactory.getMemoryMXBean();
+        mem.append("Heap (M):");
+        mem.append(mmBean.getHeapMemoryUsage().getUsed() / 1048576);
+        mem.append(" | Non-Heap (M):");
+        mem.append(mmBean.getNonHeapMemoryUsage().getUsed() / 1048576);
+        memoryStatus.setValue(mem.toString());
+
     }
 
-    public void uploadStarted(StartedEvent event) {
-        pi.setVisible(true);
-        pi2.setVisible(true);
-        l.setValue("Started uploading file " + event.getFilename());
-        textFieldValue.setValue(" TestFields value at the upload start is:"
-                + textField.getValue());
+    private void beSluggish() {
+        if (beSluggish.booleanValue()) {
+            try {
+                Thread.sleep(1000);
+            } catch (InterruptedException e) {
+                // TODO Auto-generated catch block
+                e.printStackTrace();
+            }
+        }
     }
 
+    private void throwExecption() {
+        if (throwExecption.booleanValue()) {
+            throwExecption.setValue(false);
+            throw new RuntimeException("Test execption in receiver.");
+        }
+
+    }
 }