View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin.surefire.extensions;
20  
21  import javax.annotation.Nonnull;
22  
23  import java.io.IOException;
24  import java.nio.channels.ClosedChannelException;
25  import java.nio.channels.NonWritableChannelException;
26  import java.nio.channels.WritableByteChannel;
27  
28  import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
29  import org.apache.maven.surefire.api.booter.Command;
30  import org.apache.maven.surefire.extensions.CloseableDaemonThread;
31  import org.apache.maven.surefire.extensions.CommandReader;
32  import org.apache.maven.surefire.stream.CommandEncoder;
33  
34  /**
35   * Commands which are sent from plugin to the forked jvm.
36   * <br>
37   *     <br>
38   * magic number : opcode [: opcode specific data]*
39   * <br>
40   *     or data encoded with Base64
41   * <br>
42   * magic number : opcode [: Base64(opcode specific data)]*
43   *
44   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
45   * @since 3.0.0-M5
46   */
47  public class StreamFeeder extends CloseableDaemonThread {
48      private final WritableByteChannel channel;
49      private final CommandReader commandReader;
50      private final ConsoleLogger logger;
51  
52      private volatile boolean disabled;
53      private volatile Throwable exception;
54  
55      public StreamFeeder(
56              @Nonnull String threadName,
57              @Nonnull WritableByteChannel channel,
58              @Nonnull CommandReader commandReader,
59              @Nonnull ConsoleLogger logger) {
60          super(threadName);
61          this.channel = channel;
62          this.commandReader = commandReader;
63          this.logger = logger;
64      }
65  
66      @Override
67      @SuppressWarnings("checkstyle:innerassignment")
68      public void run() {
69          try (CommandEncoder encoder = new CommandEncoder(channel)) {
70              for (Command cmd; (cmd = commandReader.readNextCommand()) != null; ) {
71                  if (!disabled) {
72                      switch (cmd.getCommandType()) {
73                          case RUN_CLASS:
74                              encoder.sendRunClass(cmd.getData());
75                              break;
76                          case TEST_SET_FINISHED:
77                              encoder.sendTestSetFinished();
78                              break;
79                          case SKIP_SINCE_NEXT_TEST:
80                              encoder.sendSkipSinceNextTest();
81                              break;
82                          case SHUTDOWN:
83                              encoder.sendShutdown(cmd.getData());
84                              break;
85                          case NOOP:
86                              encoder.sendNoop();
87                              break;
88                          case BYE_ACK:
89                              encoder.sendByeAck();
90                              break;
91                          default:
92                              logger.error("Unknown enum " + cmd.getCommandType().name());
93                      }
94                  }
95              }
96          } catch (ClosedChannelException e) {
97              // closed externally
98          } catch (IOException | NonWritableChannelException e) {
99              exception = e.getCause() == null ? e : e.getCause();
100         } catch (IllegalArgumentException e) {
101             logger.error(e.getLocalizedMessage());
102         }
103     }
104 
105     public void disable() {
106         disabled = true;
107     }
108 
109     public Throwable getException() {
110         return exception;
111     }
112 
113     @Override
114     public void close() throws IOException {
115         channel.close();
116     }
117 }