camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1211368 - in /camel/branches/camel-2.7.x: ./ components/camel-stream/src/main/java/org/apache/camel/component/stream/StreamConsumer.java components/camel-stream/src/test/java/org/apache/camel/component/stream/ScanStreamFileTest.java
Date Wed, 07 Dec 2011 10:12:26 GMT
Author: davsclaus
Date: Wed Dec  7 10:12:26 2011
New Revision: 1211368

URL: http://svn.apache.org/viewvc?rev=1211368&view=rev
Log:
CAMEL-4748: Ensure old stream is closed when re-initializing or closing consumer

Modified:
    camel/branches/camel-2.7.x/   (props changed)
    camel/branches/camel-2.7.x/components/camel-stream/src/main/java/org/apache/camel/component/stream/StreamConsumer.java
    camel/branches/camel-2.7.x/components/camel-stream/src/test/java/org/apache/camel/component/stream/ScanStreamFileTest.java

Propchange: camel/branches/camel-2.7.x/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Wed Dec  7 10:12:26 2011
@@ -1,2 +1,2 @@
-/camel/branches/camel-2.8.x:1170965,1171400,1174571,1175323,1176329,1176787,1177397,1177946,1177949,1180598,1187226,1189704,1199138,1199732,1199766,1199807,1200867,1201638-1201639,1202171,1202222,1202662,1204355,1205709,1208933,1210779,1210916
-/camel/trunk:1146608,1146903,1147216,1170956,1171396,1174565,1175321,1176274,1176781-1176782,1177394,1177945,1177948,1180597,1187221,1189693,1199137,1199703,1199739,1199804,1200861,1201623,1201637,1202167,1202215,1202659,1204338,1205412,1208930,1210771,1210830
+/camel/branches/camel-2.8.x:1170965,1171400,1174571,1175323,1176329,1176787,1177397,1177946,1177949,1180598,1187226,1189704,1199138,1199732,1199766,1199807,1200867,1201638-1201639,1202171,1202222,1202662,1204355,1205709,1208933,1210779,1210916,1211366
+/camel/trunk:1146608,1146903,1147216,1170956,1171396,1174565,1175321,1176274,1176781-1176782,1177394,1177945,1177948,1180597,1187221,1189693,1199137,1199703,1199739,1199804,1200861,1201623,1201637,1202167,1202215,1202659,1204338,1205412,1208930,1210771,1210830,1211363

Propchange: camel/branches/camel-2.7.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: camel/branches/camel-2.7.x/components/camel-stream/src/main/java/org/apache/camel/component/stream/StreamConsumer.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.7.x/components/camel-stream/src/main/java/org/apache/camel/component/stream/StreamConsumer.java?rev=1211368&r1=1211367&r2=1211368&view=diff
==============================================================================
--- camel/branches/camel-2.7.x/components/camel-stream/src/main/java/org/apache/camel/component/stream/StreamConsumer.java
(original)
+++ camel/branches/camel-2.7.x/components/camel-stream/src/main/java/org/apache/camel/component/stream/StreamConsumer.java
Wed Dec  7 10:12:26 2011
@@ -36,6 +36,7 @@ import org.apache.camel.Message;
 import org.apache.camel.Processor;
 import org.apache.camel.impl.DefaultConsumer;
 import org.apache.camel.impl.DefaultMessage;
+import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.ObjectHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -50,7 +51,8 @@ public class StreamConsumer extends Defa
     private static final String INVALID_URI = "Invalid uri, valid form: 'stream:{" + TYPES
+ "}'";
     private static final List<String> TYPES_LIST = Arrays.asList(TYPES.split(","));
     private ExecutorService executor;
-    private InputStream inputStream = System.in;
+    private volatile InputStream inputStream = System.in;
+    private volatile InputStream inputStreamToClose;
     private StreamEndpoint endpoint;
     private String uri;
     private boolean initialPromptDone;
@@ -86,6 +88,9 @@ public class StreamConsumer extends Defa
             executor = null;
         }
         lines.clear();
+
+        // do not close regular inputStream as it may be System.in etc.
+        IOHelper.close(inputStreamToClose);
         super.doStop();
     }
 
@@ -98,16 +103,21 @@ public class StreamConsumer extends Defa
     }
 
     private BufferedReader initializeStream() throws Exception {
+        // close old stream, before obtaining a new stream
+        IOHelper.close(inputStreamToClose);
+
         if ("in".equals(uri)) {
             inputStream = System.in;
+            inputStreamToClose = null;
         } else if ("file".equals(uri)) {
             inputStream = resolveStreamFromFile();
+            inputStreamToClose = inputStream;
         } else if ("url".equals(uri)) {
             inputStream = resolveStreamFromUrl();
+            inputStreamToClose = inputStream;
         }
         Charset charset = endpoint.getCharset();
-        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, charset));
-        return br;
+        return new BufferedReader(new InputStreamReader(inputStream, charset));
     }
 
     private void readFromStream() throws Exception {

Modified: camel/branches/camel-2.7.x/components/camel-stream/src/test/java/org/apache/camel/component/stream/ScanStreamFileTest.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.7.x/components/camel-stream/src/test/java/org/apache/camel/component/stream/ScanStreamFileTest.java?rev=1211368&r1=1211367&r2=1211368&view=diff
==============================================================================
--- camel/branches/camel-2.7.x/components/camel-stream/src/test/java/org/apache/camel/component/stream/ScanStreamFileTest.java
(original)
+++ camel/branches/camel-2.7.x/components/camel-stream/src/test/java/org/apache/camel/component/stream/ScanStreamFileTest.java
Wed Dec  7 10:12:26 2011
@@ -51,10 +51,13 @@ public class ScanStreamFileTest extends 
         mock.expectedMinimumMessageCount(2);
 
         FileOutputStream fos = new FileOutputStream(file);
-        fos.write("Hello\n".getBytes());
-        Thread.sleep(150);
-        fos.write("World\n".getBytes());
-        fos.close();
+        try {
+            fos.write("Hello\n".getBytes());
+            Thread.sleep(150);
+            fos.write("World\n".getBytes());
+        } finally {
+            fos.close();
+        }
         
         assertMockEndpointsSatisfied();
     }
@@ -65,17 +68,20 @@ public class ScanStreamFileTest extends 
         mock.expectedMinimumMessageCount(3);
 
         FileOutputStream fos = refreshFile(null);
-        fos.write("Hello\n".getBytes());        
-        Thread.sleep(150);
-        fos = refreshFile(fos);
-        fos.write("there\n".getBytes());
-        Thread.sleep(150);
-        fos = refreshFile(fos);
-        fos.write("World\n".getBytes());
-        Thread.sleep(150);
-        fos = refreshFile(fos);
-        fos.write("!\n".getBytes());
-        fos.close();
+        try {
+            fos.write("Hello\n".getBytes());
+            Thread.sleep(150);
+            fos = refreshFile(fos);
+            fos.write("there\n".getBytes());
+            Thread.sleep(150);
+            fos = refreshFile(fos);
+            fos.write("World\n".getBytes());
+            Thread.sleep(150);
+            fos = refreshFile(fos);
+            fos.write("!\n".getBytes());
+        } finally {
+            fos.close();
+        }
 
         assertMockEndpointsSatisfied();
     }



Mime
View raw message