Skip to content

Moving executable-plugin stream reading into thread #2

@coudot

Description

@coudot

From http://tools.lsc-project.org/issues/716

Hello,

In the executable plugin, the streams from the executed script are read after the script return. This can lead to the script to stop due to full buffer and the process never end(see http://docs.oracle.com/javase/6/docs/api/java/lang/Process.html)

I've made a small patch which create two tread for reading the output and error streams of the script.

I've also changer the entryToBean method to use value.getString() instead of value.getValue() because it was incorrectly storing base64 encoded string in the entry, but maybe there is another problem elsewhere.



Index: src/main/java/org/lsc/plugins/connectors/executable/AbstractExecutableLdifService.java
===================================================================
--- src/main/java/org/lsc/plugins/connectors/executable/AbstractExecutableLdifService.java	(révision 2011)
+++ src/main/java/org/lsc/plugins/connectors/executable/AbstractExecutableLdifService.java	(copie de travail)
@@ -2,6 +2,7 @@
 
 import java.io.IOException;
 import java.io.OutputStream;
+import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -133,6 +134,40 @@
             //TODO: need to check for max time
             LOGGER.debug("Waiting for command to stop ... ");
 
+			//Read stream with separate threads
+			class StreamGobbler extends Thread
+			{
+				InputStream is;
+				StringBuffer type;
+		
+				StreamGobbler(InputStream is, StringBuffer type)
+				{
+					this.is = is;
+					this.type = type;
+				}
+												
+				public void run()
+				{
+					try {
+						type.append(IOUtils.toString(is));
+					} catch (IOException e) {
+						// Failing to read the complete string causes null return
+						LOGGER.error("Fail to read complete data from script stream");
+						LOGGER.debug(e.toString(), e);
+					}
+				}
+			}
+
+			// any error message?
+			StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), messages);
+			// any output?
+			StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(), datas);
+
+			// starting threads.
+			errorGobbler.start();
+			outputGobbler.start();
+
+
             p.waitFor();
         } catch (IOException e) {
             // Encountered an error while reading data from output
@@ -144,22 +179,6 @@
             LOGGER.debug(e.toString(), e);
         }
 
-        try {
-        	datas.append(IOUtils.toString(p.getInputStream()));
-        } catch (IOException e) {
-            // Failing to read the complete string causes null return
-            LOGGER.error("Fail to read complete data from script output stream: {}", runtime);
-            LOGGER.debug(e.toString(), e);
-        }
-
-        try {
-        	messages.append(IOUtils.toString(p.getErrorStream()));
-        } catch (IOException e) {
-            // Failing to read the complete string causes null return
-            LOGGER.error("Fail to read complete messages from script stderr stream: {}", runtime);
-            LOGGER.debug(e.toString(), e);
-        }
-        
         if (p.exitValue() != 0) {
             // A non zero value causes null return
             LOGGER.error("Non zero exit code for runtime: {}, exit code={}", runtime[0], p.exitValue());
@@ -252,7 +271,7 @@
 				String attributeId = attribute.getId().toLowerCase();
 				HashSet<Object> values = new HashSet<Object>();
 				for (Value<?> value: attribute) {
-					values.add(value.getValue());
+					values.add(value.getString());
 				}
 				bean.setDataset(attributeId, values);
 			}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions