23
23
import java .io .InputStream ;
24
24
import java .io .InputStreamReader ;
25
25
import java .util .List ;
26
+ import java .util .concurrent .Callable ;
27
+ import java .util .concurrent .ExecutorService ;
28
+ import java .util .concurrent .Executors ;
29
+ import java .util .concurrent .Future ;
26
30
31
+ import org .neo4j .driver .v1 .util .DaemonThreadFactory ;
27
32
import org .neo4j .driver .v1 .util .ProcessEnvConfigurator ;
28
33
29
34
import static java .lang .System .lineSeparator ;
30
35
import static java .util .Arrays .asList ;
36
+ import static java .util .concurrent .TimeUnit .MINUTES ;
31
37
32
38
public class CommandLineUtil
33
39
{
40
+ private static final ExecutorService executor = Executors .newCachedThreadPool (
41
+ new DaemonThreadFactory ( "command-line-thread-" ) );
42
+
34
43
public static boolean boltKitAvailable ()
35
44
{
36
45
try
@@ -59,8 +68,7 @@ public static String executeCommand( List<String> commands )
59
68
catch ( InterruptedException e )
60
69
{
61
70
Thread .currentThread ().interrupt ();
62
- throw new CommandLineException ( "Interrupted while waiting for command " +
63
- commands , e );
71
+ throw new CommandLineException ( "Interrupted while waiting for command " + commands , e );
64
72
}
65
73
}
66
74
@@ -73,17 +81,31 @@ private static String executeAndGetStdOut( ProcessBuilder processBuilder )
73
81
throws IOException , InterruptedException
74
82
{
75
83
Process process = processBuilder .start ();
84
+ Future <String > stdOutFuture = read ( process .getInputStream () );
85
+ Future <String > stdErrFuture = read ( process .getErrorStream () );
76
86
int exitCode = process .waitFor ();
77
- String stdOut = asString ( process . getInputStream () );
78
- String stdErr = asString ( process . getErrorStream () );
87
+ String stdOut = get ( stdOutFuture );
88
+ String stdErr = get ( stdErrFuture );
79
89
if ( exitCode != 0 )
80
90
{
81
91
throw new CommandLineException ( "Non-zero exit code\n STDOUT:\n " + stdOut + "\n STDERR:\n " + stdErr );
82
92
}
83
93
return stdOut ;
84
94
}
85
95
86
- private static String asString ( InputStream input )
96
+ private static Future <String > read ( final InputStream input )
97
+ {
98
+ return executor .submit ( new Callable <String >()
99
+ {
100
+ @ Override
101
+ public String call () throws Exception
102
+ {
103
+ return readToString ( input );
104
+ }
105
+ } );
106
+ }
107
+
108
+ private static String readToString ( InputStream input )
87
109
{
88
110
StringBuilder result = new StringBuilder ();
89
111
try ( BufferedReader reader = new BufferedReader ( new InputStreamReader ( input ) ) )
@@ -100,4 +122,16 @@ private static String asString( InputStream input )
100
122
}
101
123
return result .toString ();
102
124
}
125
+
126
+ private static <T > T get ( Future <T > future )
127
+ {
128
+ try
129
+ {
130
+ return future .get ( 10 , MINUTES );
131
+ }
132
+ catch ( Exception e )
133
+ {
134
+ throw new RuntimeException ( e );
135
+ }
136
+ }
103
137
}
0 commit comments