You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2010/04/16 14:44:50 UTC

svn commit: r934852 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/converter/stream/ camel-core/src/test/java/org/apache/camel/converter/stream/ tests/camel-itest/src/test/java/org/apache/camel/itest/issues/

Author: ningjiang
Date: Fri Apr 16 12:44:50 2010
New Revision: 934852

URL: http://svn.apache.org/viewvc?rev=934852&view=rev
Log:
CAMEL-2636 Fixed the issue of IOException: Bad file descriptor

Added:
    camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/issues/JettyHttpFileCacheTest.java   (with props)
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/FileInputStreamCache.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/converter/stream/CachedOutputStreamTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java?rev=934852&r1=934851&r2=934852&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java Fri Apr 16 12:44:50 2010
@@ -68,37 +68,7 @@ public class CachedOutputStream extends 
         if (dir != null) {
             this.outputDir = exchange.getContext().getTypeConverter().convertTo(File.class, dir);
         }
-
-        // add on completion so we can cleanup after the exchange is done such as deleting temporary files
-        exchange.addOnCompletion(new SynchronizationAdapter() {
-            @Override
-            public void onDone(Exchange exchange) {
-                try {
-                    // close the stream and FileInputStreamCache
-                    close();
-                    for (FileInputStreamCache cache : fileInputStreamCaches) {
-                        cache.close();
-                    }
-                    // cleanup temporary file
-                    if (tempFile != null) {
-                        boolean deleted = tempFile.delete();
-                        if (!deleted) {
-                            LOG.warn("Cannot delete temporary cache file: " + tempFile);
-                        } else if (LOG.isTraceEnabled()) {
-                            LOG.trace("Deleted temporary cache file: " + tempFile);
-                        }
-                        tempFile = null;
-                    }
-                } catch (Exception e) {
-                    LOG.warn("Error deleting temporary cache file: " + tempFile, e);
-                }
-            }
-
-            @Override
-            public String toString() {
-                return "OnCompletion[CachedOutputStream]";
-            }
-        });
+        
     }
 
     public void flush() throws IOException {
@@ -107,6 +77,20 @@ public class CachedOutputStream extends 
 
     public void close() throws IOException {
         currentStream.close();
+        try {
+            // cleanup temporary file
+            if (tempFile != null) {
+                boolean deleted = tempFile.delete();
+                if (!deleted) {
+                    LOG.warn("Cannot delete temporary cache file: " + tempFile);
+                } else if (LOG.isTraceEnabled()) {
+                    LOG.trace("Deleted temporary cache file: " + tempFile);
+                }
+                tempFile = null;
+            }
+        } catch (Exception e) {
+            LOG.warn("Error deleting temporary cache file: " + tempFile, e);
+        }
     }
 
     public boolean equals(Object obj) {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/FileInputStreamCache.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/FileInputStreamCache.java?rev=934852&r1=934851&r2=934852&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/FileInputStreamCache.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/FileInputStreamCache.java Fri Apr 16 12:44:50 2010
@@ -41,19 +41,24 @@ public class FileInputStreamCache extend
     @Override
     public void close() {
         try {
-            getInputStream().close();
+            if (isSteamOpened()) {
+                getInputStream().close();
+            }
+            // when close the FileInputStreamCache we should also close the cachedOutputStream
             if (cachedOutputStream != null) {
                 cachedOutputStream.close();
             }
         } catch (Exception e) {
             throw new RuntimeCamelException(e);
-        } 
+        }
     }
 
     @Override
     public void reset() {
         try {
-            getInputStream().close();
+            if (isSteamOpened()) {
+                getInputStream().close();
+            }
             // reset by creating a new stream based on the file
             stream = new FileInputStream(file);
         } catch (Exception e) {
@@ -78,5 +83,13 @@ public class FileInputStreamCache extend
     protected InputStream getInputStream() {
         return stream;
     }
+    
+    private boolean isSteamOpened() {
+        if (stream != null && stream instanceof FileInputStream) {
+            return ((FileInputStream) stream).getChannel().isOpen();            
+        } else {
+            return stream != null;
+        }
+    }
 
 }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/stream/CachedOutputStreamTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/stream/CachedOutputStreamTest.java?rev=934852&r1=934851&r2=934852&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/converter/stream/CachedOutputStreamTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/converter/stream/CachedOutputStreamTest.java Fri Apr 16 12:44:50 2010
@@ -78,8 +78,6 @@ public class CachedOutputStreamTest exte
         ((InputStream)cache).close();
         assertEquals("Cached a wrong file", temp, TEST_STRING);
 
-        exchange.getUnitOfWork().done(exchange);
-
         try {
             cache.reset();
             // The stream is closed, so the temp file is gone.
@@ -110,7 +108,6 @@ public class CachedOutputStreamTest exte
         temp = toString((InputStream)cache);
         assertEquals("Cached a wrong file", temp, TEST_STRING);
         
-        exchange.getUnitOfWork().done(exchange);
         ((InputStream)cache).close();
         
         files = file.list();
@@ -131,8 +128,6 @@ public class CachedOutputStreamTest exte
         assertTrue("Should get the InputStreamCache", cache instanceof InputStreamCache);
         String temp = IOConverter.toString((InputStream)cache, null);
         assertEquals("Cached a wrong file", temp, TEST_STRING);
-
-        exchange.getUnitOfWork().done(exchange);
     }
 
     public void testCacheStreamToMemoryAsDiskIsdisabled() throws IOException {

Added: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/issues/JettyHttpFileCacheTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/issues/JettyHttpFileCacheTest.java?rev=934852&view=auto
==============================================================================
--- camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/issues/JettyHttpFileCacheTest.java (added)
+++ camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/issues/JettyHttpFileCacheTest.java Fri Apr 16 12:44:50 2010
@@ -0,0 +1,83 @@
+/**
+ * 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.camel.itest.issues;
+
+import java.io.File;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.converter.stream.CachedOutputStream;
+import org.apache.camel.impl.DefaultExchange;
+import org.apache.camel.impl.DefaultUnitOfWork;
+import org.apache.camel.spi.UnitOfWork;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Before;
+import org.junit.Test;
+
+public class JettyHttpFileCacheTest extends CamelTestSupport {
+    private static final String TEST_STRING = "This is a test string and it has enough" 
+        + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ";
+    
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+        
+        context.getProperties().put(CachedOutputStream.TEMP_DIR, "./target/cachedir");
+        context.getProperties().put(CachedOutputStream.THRESHOLD, "16");
+        deleteDirectory("./target/cachedir");
+        createDirectory("./target/cachedir");
+    }
+
+    @Test
+    public void testGetWithRelativePath() throws Exception {
+        
+        String response = template.requestBody("http://localhost:8201/clipboard/download/file", "   ", String.class);
+        assertEquals("should get the right response", TEST_STRING, response);
+        
+        File file = new File("./target/cachedir");
+        String[] files = file.list();
+        assertTrue("There should not have any temp file", files.length == 0);
+        
+    }
+    
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                
+
+                from("jetty:http://localhost:8201/clipboard/download?chunked=true&matchOnUriPrefix=true")
+                    .to("http://localhost:9101?bridgeEndpoint=true");
+                
+                from("jetty:http://localhost:9101?chunked=true&matchOnUriPrefix=true")
+                    .process(new Processor() {
+
+                        public void process(Exchange exchange) throws Exception {
+                            exchange.getOut().setBody(TEST_STRING);
+                        }
+                        
+                    });
+
+               
+            }
+        };
+    }
+
+}

Propchange: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/issues/JettyHttpFileCacheTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/issues/JettyHttpFileCacheTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date