1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
36
37
38
39
40
41
42
43
44
45
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
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 }