Skip to content

Commit ff2ec15

Browse files
committed
Minor improvements.
1 parent 7c37705 commit ff2ec15

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

tools/bamm-cli/src/main/java/io/openmanufacturing/sds/CommandExecutor.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
package io.openmanufacturing.sds;
1515

1616
import java.io.IOException;
17+
import java.io.InputStream;
1718
import java.util.Scanner;
1819
import java.util.StringTokenizer;
1920

2021
import io.openmanufacturing.sds.exception.CommandException;
2122

23+
// Executes an external resolver via the underlying OS command and returns the stdout from the command as result.
2224
public class CommandExecutor {
2325

2426
public static String executeCommand( String command ) {
@@ -30,14 +32,12 @@ public static String executeCommand( String command ) {
3032
try {
3133
final Process p = Runtime.getRuntime().exec( command );
3234
final int result = p.waitFor();
33-
final Scanner s = new Scanner( p.getInputStream() ).useDelimiter( "\\A" );
34-
final String output = s.hasNext() ? s.next() : "";
3535
if ( result != 0 ) {
36-
throw new CommandException( output );
36+
throw new CommandException( getOutputFrom( p.getErrorStream() ) );
3737
}
38-
return output;
38+
return getOutputFrom( p.getInputStream() );
3939
} 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 );
4141
}
4242
}
4343

@@ -48,4 +48,9 @@ private static boolean isJarInvocation( final String command ) {
4848
}
4949
return false;
5050
}
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+
}
5156
}

tools/bamm-cli/src/main/java/io/openmanufacturing/sds/ExternalResolverStrategy.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.openmanufacturing.sds.aspectmodel.urn.AspectModelUrn;
2424
import io.vavr.control.Try;
2525

26+
// Specialized resolution strategy: use external resolver to resolve the URNs passed as argument into models.
2627
class ExternalResolverStrategy implements ResolutionStrategy {
2728

2829
private final String command;
@@ -35,10 +36,6 @@ class ExternalResolverStrategy implements ResolutionStrategy {
3536
public Try<Model> apply( final AspectModelUrn aspectModelUrn ) {
3637
final String commandWithParameters = command + " " + aspectModelUrn.toString();
3738
final String result = CommandExecutor.executeCommand( commandWithParameters );
38-
if ( null == result ) {
39-
return Try.failure( new Exception() );
40-
}
41-
// TODO encodings
4239
return TurtleLoader.loadTurtle( new ByteArrayInputStream( result.getBytes( StandardCharsets.UTF_8 ) ) );
4340
}
4441
}

0 commit comments

Comments
 (0)