Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving executable-plugin stream reading into thread #2

Open
coudot opened this issue Jan 19, 2017 · 1 comment
Open

Moving executable-plugin stream reading into thread #2

coudot opened this issue Jan 19, 2017 · 1 comment
Milestone

Comments

@coudot
Copy link
Member

coudot commented Jan 19, 2017

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);
 			}

@coudot coudot added this to the 1.1 milestone Jan 19, 2017
@coudot
Copy link
Member Author

coudot commented Jan 7, 2022

May be the same as #10 ?

@coudot coudot modified the milestones: 1.1, 1.2 Jan 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant