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.nio.channels.ReadableByteChannel;
24  import java.nio.channels.WritableByteChannel;
25  
26  import org.apache.maven.surefire.api.event.Event;
27  import org.apache.maven.surefire.api.fork.ForkNodeArguments;
28  import org.apache.maven.surefire.extensions.CloseableDaemonThread;
29  import org.apache.maven.surefire.extensions.CommandReader;
30  import org.apache.maven.surefire.extensions.EventHandler;
31  import org.apache.maven.surefire.extensions.ForkChannel;
32  import org.apache.maven.surefire.extensions.util.CountdownCloseable;
33  
34  /**
35   * The main purpose of this class is to bind the
36   * {@link #bindCommandReader(CommandReader, WritableByteChannel) command reader} reading the commands from
37   * {@link CommandReader}, serializing them and writing the stream to the
38   * {@link WritableByteChannel sub-process}. It binds the
39   * {@link #bindEventHandler(EventHandler, CountdownCloseable, ReadableByteChannel) event handler} deserializing
40   * a received event and sends the event object to the {@link EventHandler event handler}.
41   */
42  final class LegacyForkChannel extends ForkChannel {
43      private CloseableDaemonThread commandReaderBindings;
44      private CloseableDaemonThread eventHandlerBindings;
45  
46      LegacyForkChannel(@Nonnull ForkNodeArguments arguments) {
47          super(arguments);
48      }
49  
50      @Override
51      public void tryConnectToClient() {}
52  
53      @Override
54      public String getForkNodeConnectionString() {
55          return "pipe://" + getArguments().getForkChannelId();
56      }
57  
58      @Override
59      public int getCountdownCloseablePermits() {
60          return 2;
61      }
62  
63      @Override
64      public void bindCommandReader(@Nonnull CommandReader commands, WritableByteChannel stdIn) {
65          ForkNodeArguments args = getArguments();
66          String threadName = "commands-fork-" + args.getForkChannelId();
67          commandReaderBindings = new StreamFeeder(threadName, stdIn, commands, args.getConsoleLogger());
68          commandReaderBindings.start();
69      }
70  
71      @Override
72      public void bindEventHandler(
73              @Nonnull EventHandler<Event> eventHandler,
74              @Nonnull CountdownCloseable countdownCloseable,
75              ReadableByteChannel stdOut) {
76          ForkNodeArguments args = getArguments();
77          String threadName = "fork-" + args.getForkChannelId() + "-event-thread";
78          eventHandlerBindings = new EventConsumerThread(threadName, stdOut, eventHandler, countdownCloseable, args);
79          eventHandlerBindings.start();
80      }
81  
82      @Override
83      public void disable() {
84          if (eventHandlerBindings != null) {
85              eventHandlerBindings.disable();
86          }
87  
88          if (commandReaderBindings != null) {
89              commandReaderBindings.disable();
90          }
91      }
92  
93      @Override
94      public void close() {}
95  }