14
14
package io .openmanufacturing .sds ;
15
15
16
16
import java .io .IOException ;
17
+ import java .io .InputStream ;
17
18
import java .util .Scanner ;
18
19
import java .util .StringTokenizer ;
19
20
20
21
import io .openmanufacturing .sds .exception .CommandException ;
21
22
23
+ // Executes an external resolver via the underlying OS command and returns the stdout from the command as result.
22
24
public class CommandExecutor {
23
25
24
26
public static String executeCommand ( String command ) {
@@ -30,14 +32,12 @@ public static String executeCommand( String command ) {
30
32
try {
31
33
final Process p = Runtime .getRuntime ().exec ( command );
32
34
final int result = p .waitFor ();
33
- final Scanner s = new Scanner ( p .getInputStream () ).useDelimiter ( "\\ A" );
34
- final String output = s .hasNext () ? s .next () : "" ;
35
35
if ( result != 0 ) {
36
- throw new CommandException ( output );
36
+ throw new CommandException ( getOutputFrom ( p . getErrorStream () ) );
37
37
}
38
- return output ;
38
+ return getOutputFrom ( p . getInputStream () ) ;
39
39
} catch ( final IOException | InterruptedException e ) {
40
- throw new CommandException ( e );
40
+ throw new CommandException ( "The attempt to execute external resolver failed with the error:" , e );
41
41
}
42
42
}
43
43
@@ -48,4 +48,9 @@ private static boolean isJarInvocation( final String command ) {
48
48
}
49
49
return false ;
50
50
}
51
+
52
+ private static String getOutputFrom ( final InputStream stream ) {
53
+ final Scanner s = new Scanner ( stream ).useDelimiter ( "\\ A" );
54
+ return s .hasNext () ? s .next () : "" ;
55
+ }
51
56
}
0 commit comments