View Javadoc
1   package org.apache.maven.surefire.extensions;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.surefire.api.event.Event;
23  import org.apache.maven.surefire.api.fork.ForkNodeArguments;
24  import org.apache.maven.surefire.extensions.util.CountdownCloseable;
25  
26  import javax.annotation.Nonnull;
27  import java.io.Closeable;
28  import java.io.IOException;
29  import java.nio.channels.ReadableByteChannel;
30  import java.nio.channels.WritableByteChannel;
31  
32  /**
33   * It's a session object used only by a particular Thread in ForkStarter
34   * and communicates with a dedicated forked JVM. It represents a server.
35   * <br>
36   * <br>
37   * It connects to a remote client by {@link #tryConnectToClient()}, provides a connection string
38   * {@link #getForkNodeConnectionString()} needed by the client in forked JVM, binds event handler and command reader.
39   * This object is called in one Thread.
40   *
41   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
42   * @since 3.0.0-M5
43   */
44  public abstract class ForkChannel implements Closeable
45  {
46      @Nonnull
47      private final ForkNodeArguments arguments;
48  
49      /**
50       * @param arguments data necessary for command reader and event handler.
51       */
52      protected ForkChannel( @Nonnull ForkNodeArguments arguments )
53      {
54          this.arguments = arguments;
55      }
56  
57      /**
58       * Asynchronously connects to the client.
59       *
60       * @throws IOException if stream fails
61       * @throws InterruptedException if interrupted thread
62       */
63      public abstract void tryConnectToClient() throws IOException, InterruptedException;
64  
65      /**
66       * This is server related class, which if binds to a TCP port, determines the connection string for the client.
67       *
68       * @return a connection string utilized by the client in the fork JVM
69       */
70      public abstract String getForkNodeConnectionString();
71  
72      /**
73       * the permits in {@link CountdownCloseable}.
74       */
75      public abstract int getCountdownCloseablePermits();
76  
77      /**
78       * Binds command handler to the channel. Starts a Thread streaming out the commands.
79       *
80       * @param commands command reader, see {@link CommandReader#readNextCommand()}
81       * @param stdIn    optional standard input stream of the JVM to write the encoded commands into it
82       * @throws IOException if an error in the fork channel
83       * @throws InterruptedException channel interrupted
84       */
85      public abstract void bindCommandReader( @Nonnull CommandReader commands, WritableByteChannel stdIn )
86          throws IOException, InterruptedException;
87  
88      /**
89       * Starts a Thread reading the events.
90       *
91       * @param eventHandler       event eventHandler
92       * @param countdownCloseable count down of the final call of {@link Closeable#close()}
93       * @param stdOut             optional standard output stream of the JVM
94       * @throws IOException if an error in the fork channel
95       * @throws InterruptedException channel interrupted
96       */
97      public abstract void bindEventHandler( @Nonnull EventHandler<Event> eventHandler,
98                                             @Nonnull CountdownCloseable countdownCloseable,
99                                             ReadableByteChannel stdOut )
100         throws IOException, InterruptedException;
101 
102     @Nonnull
103     protected ForkNodeArguments getArguments()
104     {
105         return arguments;
106     }
107 
108     public abstract void disable();
109 
110     @Override
111     public abstract void close() throws IOException;
112 }