/
SurefireForkChannel.java
302 lines (271 loc) · 11.2 KB
/
SurefireForkChannel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package org.apache.maven.plugin.surefire.extensions;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.maven.plugin.surefire.booterclient.output.NativeStdOutStreamConsumer;
import org.apache.maven.surefire.api.event.Event;
import org.apache.maven.surefire.api.fork.ForkNodeArguments;
import org.apache.maven.surefire.extensions.CloseableDaemonThread;
import org.apache.maven.surefire.extensions.CommandReader;
import org.apache.maven.surefire.extensions.EventHandler;
import org.apache.maven.surefire.extensions.ForkChannel;
import org.apache.maven.surefire.extensions.util.CountDownLauncher;
import org.apache.maven.surefire.extensions.util.CountdownCloseable;
import org.apache.maven.surefire.extensions.util.LineConsumerThread;
import javax.annotation.Nonnull;
import java.io.Closeable;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketOption;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import static java.net.StandardSocketOptions.SO_KEEPALIVE;
import static java.net.StandardSocketOptions.SO_REUSEADDR;
import static java.net.StandardSocketOptions.TCP_NODELAY;
import static java.nio.channels.AsynchronousChannelGroup.withThreadPool;
import static java.nio.channels.AsynchronousServerSocketChannel.open;
import static java.nio.charset.StandardCharsets.US_ASCII;
import static org.apache.maven.surefire.api.util.internal.Channels.newBufferedChannel;
import static org.apache.maven.surefire.api.util.internal.Channels.newChannel;
import static org.apache.maven.surefire.api.util.internal.Channels.newInputStream;
import static org.apache.maven.surefire.api.util.internal.Channels.newOutputStream;
import static org.apache.maven.surefire.api.util.internal.DaemonThreadFactory.newDaemonThreadFactory;
import static org.apache.maven.surefire.shared.lang3.StringUtils.isBlank;
import static org.apache.maven.surefire.shared.lang3.StringUtils.isNotBlank;
/**
* The TCP/IP server accepting only one client connection. The forked JVM connects to the server using the
* {@link #getForkNodeConnectionString() connection string}.
* The main purpose of this class is to {@link #tryConnectToClient() conect with tthe client}, bind the
* {@link #bindCommandReader(CommandReader, WritableByteChannel) command reader} to the internal socket's
* {@link java.io.InputStream}, and bind the
* {@link #bindEventHandler(EventHandler, CountdownCloseable, ReadableByteChannel) event handler} writing the event
* objects to the {@link EventHandler event handler}.
* <br>
* The objects {@link WritableByteChannel} and {@link ReadableByteChannel} are forked process streams
* (standard input and output). Both are ignored in this implementation but they are used in {@link LegacyForkChannel}.
* <br>
* The channel is closed after the forked JVM has finished normally or the shutdown hook is executed in the plugin.
*/
final class SurefireForkChannel extends ForkChannel
{
private static final ExecutorService THREAD_POOL = Executors.newCachedThreadPool( newDaemonThreadFactory() );
private final AsynchronousServerSocketChannel server;
private final String localHost;
private final int localPort;
private final String sessionId;
private final Bindings bindings = new Bindings( 2 );
private volatile Future<AsynchronousSocketChannel> session;
private volatile LineConsumerThread out;
private volatile CloseableDaemonThread commandReaderBindings;
private volatile CloseableDaemonThread eventHandlerBindings;
private volatile EventBindings eventBindings;
private volatile CommandBindings commandBindings;
SurefireForkChannel( @Nonnull ForkNodeArguments arguments ) throws IOException
{
super( arguments );
server = open( withThreadPool( THREAD_POOL ) );
setTrueOptions( SO_REUSEADDR, TCP_NODELAY, SO_KEEPALIVE );
InetAddress ip = InetAddress.getLoopbackAddress();
server.bind( new InetSocketAddress( ip, 0 ), 1 );
InetSocketAddress localAddress = (InetSocketAddress) server.getLocalAddress();
localHost = localAddress.getHostString();
localPort = localAddress.getPort();
sessionId = arguments.getSessionId();
}
@Override
public void tryConnectToClient()
{
if ( session != null )
{
throw new IllegalStateException( "already accepted TCP client connection" );
}
session = server.accept();
}
@Override
public String getForkNodeConnectionString()
{
return "tcp://" + localHost + ":" + localPort + ( isBlank( sessionId ) ? "" : "?sessionId=" + sessionId );
}
@Override
public int getCountdownCloseablePermits()
{
return 3;
}
@Override
public void bindCommandReader( @Nonnull CommandReader commands, WritableByteChannel stdIn )
throws IOException, InterruptedException
{
commandBindings = new CommandBindings( commands );
bindings.countDown();
}
@Override
public void bindEventHandler( @Nonnull EventHandler<Event> eventHandler,
@Nonnull CountdownCloseable countdown,
ReadableByteChannel stdOut )
throws IOException, InterruptedException
{
ForkNodeArguments args = getArguments();
out = new LineConsumerThread( "fork-" + args.getForkChannelId() + "-out-thread", stdOut,
new NativeStdOutStreamConsumer( args.getConsoleLock() ), countdown );
out.start();
eventBindings = new EventBindings( eventHandler, countdown );
bindings.countDown();
}
@Override
public void disable()
{
if ( eventHandlerBindings != null )
{
eventHandlerBindings.disable();
}
if ( commandReaderBindings != null )
{
commandReaderBindings.disable();
}
}
@Override
public void close() throws IOException
{
//noinspection unused,EmptyTryBlock,EmptyTryBlock
try ( Closeable c1 = getChannel(); Closeable c2 = server; Closeable c3 = out )
{
// only close all channels
}
catch ( InterruptedException e )
{
Throwable cause = e.getCause();
throw cause instanceof IOException ? (IOException) cause : new IOException( cause );
}
}
private void verifySessionId() throws InterruptedException, IOException
{
try
{
ByteBuffer buffer = ByteBuffer.allocate( sessionId.length() );
int read;
do
{
read = getChannel().read( buffer ).get();
} while ( read != -1 && buffer.hasRemaining() );
if ( read == -1 )
{
throw new IOException( "Channel closed while verifying the client." );
}
( (Buffer) buffer ).flip();
String clientSessionId = new String( buffer.array(), US_ASCII );
if ( !clientSessionId.equals( sessionId ) )
{
throw new InvalidSessionIdException( clientSessionId, sessionId );
}
}
catch ( ExecutionException e )
{
Throwable cause = e.getCause();
throw cause instanceof IOException ? (IOException) cause : new IOException( cause );
}
}
@SafeVarargs
private final void setTrueOptions( SocketOption<Boolean>... options )
throws IOException
{
for ( SocketOption<Boolean> option : options )
{
if ( server.supportedOptions().contains( option ) )
{
server.setOption( option, true );
}
}
}
private class EventBindings
{
private final EventHandler<Event> eventHandler;
private final CountdownCloseable countdown;
private EventBindings( EventHandler<Event> eventHandler, CountdownCloseable countdown )
{
this.eventHandler = eventHandler;
this.countdown = countdown;
}
void bindEventHandler( AsynchronousSocketChannel source )
{
ForkNodeArguments args = getArguments();
String threadName = "fork-" + args.getForkChannelId() + "-event-thread";
ReadableByteChannel channel = newBufferedChannel( newInputStream( source ) );
eventHandlerBindings = new EventConsumerThread( threadName, channel, eventHandler, countdown, args );
eventHandlerBindings.start();
}
}
private class CommandBindings
{
private final CommandReader commands;
private CommandBindings( CommandReader commands )
{
this.commands = commands;
}
void bindCommandSender( AsynchronousSocketChannel source )
{
// don't use newBufferedChannel here - may cause the command is not sent and the JVM hangs
// only newChannel flushes the message
// newBufferedChannel does not flush
ForkNodeArguments args = getArguments();
WritableByteChannel channel = newChannel( newOutputStream( source ) );
String threadName = "commands-fork-" + args.getForkChannelId();
commandReaderBindings = new StreamFeeder( threadName, channel, commands, args.getConsoleLogger() );
commandReaderBindings.start();
}
}
private class Bindings extends CountDownLauncher
{
private Bindings( int count )
{
super( count );
}
@Override
protected void job() throws IOException, InterruptedException
{
AsynchronousSocketChannel channel = getChannel();
if ( isNotBlank( sessionId ) )
{
verifySessionId();
}
eventBindings.bindEventHandler( channel );
commandBindings.bindCommandSender( channel );
}
}
private AsynchronousSocketChannel getChannel()
throws InterruptedException, IOException
{
try
{
return session == null ? null : session.get();
}
catch ( ExecutionException e )
{
Throwable cause = e.getCause();
throw cause instanceof IOException ? (IOException) cause : new IOException( cause );
}
}
}