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.extensions.util.CountdownCloseable;
24  
25  import javax.annotation.Nonnull;
26  import java.io.Closeable;
27  import java.io.IOException;
28  import java.nio.channels.ReadableByteChannel;
29  import java.nio.channels.WritableByteChannel;
30  
31  /**
32   * It's a session object used only by a particular Thread in ForkStarter
33   * and dedicated forked JVM. It represents a server.
34   * <br>
35   * <br>
36   * It connects with a client {@link #connectToClient()}, provides a connection string
37   * {@link #getForkNodeConnectionString()} needed by the client in the JVM, binds event handler and command reader.
38   *
39   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
40   * @since 3.0.0-M5
41   */
42  public abstract class ForkChannel implements Closeable
43  {
44      @Nonnull
45      private final ForkNodeArguments arguments;
46  
47      /**
48       * @param arguments data necessary for command reader and event handler.
49       */
50      protected ForkChannel( @Nonnull ForkNodeArguments arguments )
51      {
52          this.arguments = arguments;
53      }
54  
55      public abstract void connectToClient() throws IOException;
56  
57      /**
58       * This is server related class, which if binds to a TCP port, determines the connection string for the client.
59       *
60       * @return a connection string utilized by the client in the fork JVM
61       */
62      public abstract String getForkNodeConnectionString();
63  
64      /**
65       * the permits in {@link CountdownCloseable}.
66       */
67      public abstract int getCountdownCloseablePermits();
68  
69      /**
70       * Binds command handler to the channel.
71       *
72       * @param commands command reader, see {@link CommandReader#readNextCommand()}
73       * @param stdIn    optional standard input stream of the JVM to write the encoded commands into it
74       * @return the thread instance to start up in order to stream out the data
75       * @throws IOException if an error in the fork channel
76       */
77      public abstract CloseableDaemonThread bindCommandReader( @Nonnull CommandReader commands,
78                                                               WritableByteChannel stdIn )
79          throws IOException;
80  
81      /**
82       *
83       * @param eventHandler       event eventHandler
84       * @param countdownCloseable count down of the final call of {@link Closeable#close()}
85       * @param stdOut             optional standard output stream of the JVM
86       * @return the thread instance to start up in order to stream out the data
87       * @throws IOException if an error in the fork channel
88       */
89      public abstract CloseableDaemonThread bindEventHandler( @Nonnull EventHandler<Event> eventHandler,
90                                                              @Nonnull CountdownCloseable countdownCloseable,
91                                                              ReadableByteChannel stdOut )
92          throws IOException;
93  
94      @Nonnull
95      protected ForkNodeArguments getArguments()
96      {
97          return arguments;
98      }
99  }