1313// limitations under the License.
1414package com .google .devtools .build .lib .worker ;
1515
16- import static java .nio .charset .StandardCharsets .UTF_8 ;
17-
16+ import com .google .common .collect .ImmutableList ;
1817import com .google .devtools .build .lib .worker .WorkerProtocol .Input ;
1918import com .google .devtools .build .lib .worker .WorkerProtocol .WorkRequest ;
2019import com .google .devtools .build .lib .worker .WorkerProtocol .WorkResponse ;
2322import com .google .protobuf .ByteString ;
2423import com .google .protobuf .util .JsonFormat ;
2524import com .google .protobuf .util .JsonFormat .Printer ;
26- import java .io .BufferedReader ;
2725import java .io .BufferedWriter ;
2826import java .io .EOFException ;
2927import java .io .IOException ;
30- import java .io .InputStream ;
31- import java .io .InputStreamReader ;
32- import java .io .OutputStream ;
33- import java .io .OutputStreamWriter ;
34- import java .util .ArrayList ;
3528import java .util .List ;
3629
37- /** Sample implementation of the Worker Protocol using JSON to communicate with Bazel. */
38- public class JsonExampleWorkerProtocolImpl implements ExampleWorkerProtocol {
39- private final Printer jsonPrinter =
40- JsonFormat .printer ().omittingInsignificantWhitespace ().includingDefaultValueFields ();
30+ /** Implementation of the Worker Protocol using JSON to communicate with Bazel. */
31+ public final class JsonWorkerMessageProcessor implements WorkRequestHandler .WorkerMessageProcessor {
32+ /** Reader for reading the WorkResponse. */
4133 private final JsonReader reader ;
34+ /** Printer for printing the WorkRequest. */
35+ private final Printer jsonPrinter ;
36+ /** Writer for writing the WorkRequest to the worker. */
4237 private final BufferedWriter jsonWriter ;
4338
44- public JsonExampleWorkerProtocolImpl (InputStream stdin , OutputStream stdout ) {
45- reader = new JsonReader (new BufferedReader (new InputStreamReader (stdin , UTF_8 )));
39+ /** Constructs a {@code WorkRequestHandler} that reads and writes JSON. */
40+ public JsonWorkerMessageProcessor (JsonReader reader , BufferedWriter jsonWriter ) {
41+ this .reader = reader ;
4642 reader .setLenient (true );
47- jsonWriter = new BufferedWriter (new OutputStreamWriter (stdout , UTF_8 ));
43+ this .jsonWriter = jsonWriter ;
44+ jsonPrinter =
45+ JsonFormat .printer ().omittingInsignificantWhitespace ().includingDefaultValueFields ();
4846 }
4947
50- private static ArrayList <String > readArguments (JsonReader reader ) throws IOException {
48+ private static ImmutableList <String > readArguments (JsonReader reader ) throws IOException {
5149 reader .beginArray ();
52- ArrayList <String > arguments = new ArrayList <> ();
50+ ImmutableList . Builder <String > argumentsBuilder = ImmutableList . builder ();
5351 while (reader .hasNext ()) {
54- arguments .add (reader .nextString ());
52+ argumentsBuilder .add (reader .nextString ());
5553 }
5654 reader .endArray ();
57- return arguments ;
55+ return argumentsBuilder . build () ;
5856 }
5957
60- private static ArrayList <Input > readInputs (JsonReader reader ) throws IOException {
58+ private static ImmutableList <Input > readInputs (JsonReader reader ) throws IOException {
6159 reader .beginArray ();
62- ArrayList <Input > inputs = new ArrayList <> ();
60+ ImmutableList . Builder <Input > inputsBuilder = ImmutableList . builder ();
6361 while (reader .hasNext ()) {
6462 String digest = null ;
6563 String path = null ;
@@ -81,19 +79,25 @@ private static ArrayList<Input> readInputs(JsonReader reader) throws IOException
8179 path = reader .nextString ();
8280 break ;
8381 default :
84- throw new IOException ( name + " is an incorrect field in input" ) ;
82+ continue ;
8583 }
8684 }
8785 reader .endObject ();
88- inputs .add (
89- Input .newBuilder ().setDigest (ByteString .copyFromUtf8 (digest )).setPath (path ).build ());
86+ Input .Builder inputBuilder = Input .newBuilder ();
87+ if (digest != null ) {
88+ inputBuilder .setDigest (ByteString .copyFromUtf8 (digest ));
89+ }
90+ if (path != null ) {
91+ inputBuilder .setPath (path );
92+ }
93+ inputsBuilder .add (inputBuilder .build ());
9094 }
9195 reader .endArray ();
92- return inputs ;
96+ return inputsBuilder . build () ;
9397 }
9498
9599 @ Override
96- public WorkRequest readRequest () throws IOException {
100+ public WorkRequest readWorkRequest () throws IOException {
97101 List <String > arguments = null ;
98102 List <Input > inputs = null ;
99103 Integer requestId = null ;
@@ -104,24 +108,24 @@ public WorkRequest readRequest() throws IOException {
104108 switch (name ) {
105109 case "arguments" :
106110 if (arguments != null ) {
107- throw new IOException ("Work request cannot have more than one list of arguments" );
111+ throw new IOException ("WorkRequest cannot have more than one ' arguments' field " );
108112 }
109113 arguments = readArguments (reader );
110114 break ;
111115 case "inputs" :
112116 if (inputs != null ) {
113- throw new IOException ("Work request cannot have more than one list of inputs" );
117+ throw new IOException ("WorkRequest cannot have more than one ' inputs' field " );
114118 }
115119 inputs = readInputs (reader );
116120 break ;
117121 case "requestId" :
118122 if (requestId != null ) {
119- throw new IOException ("Work request cannot have more than one requestId" );
123+ throw new IOException ("WorkRequest cannot have more than one requestId" );
120124 }
121125 requestId = reader .nextInt ();
122126 break ;
123127 default :
124- throw new IOException ( name + " is an incorrect field in work request" ) ;
128+ break ;
125129 }
126130 }
127131 reader .endObject ();
@@ -143,17 +147,13 @@ public WorkRequest readRequest() throws IOException {
143147 }
144148
145149 @ Override
146- public void writeResponse (WorkResponse response ) throws IOException {
150+ public void writeWorkResponse (WorkResponse response ) throws IOException {
147151 jsonPrinter .appendTo (response , jsonWriter );
148152 jsonWriter .flush ();
149153 }
150154
151155 @ Override
152- public void close () {
153- try {
154- jsonWriter .close ();
155- } catch (IOException e ) {
156- System .err .printf ("Could not close json writer. %s" , e );
157- }
156+ public void close () throws IOException {
157+ jsonWriter .close ();
158158 }
159159}
0 commit comments