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.surefire.extensions;
20
21 import javax.annotation.Nonnull;
22
23 import java.io.Closeable;
24 import java.io.IOException;
25 import java.nio.channels.ReadableByteChannel;
26 import java.nio.channels.WritableByteChannel;
27
28 import org.apache.maven.surefire.api.event.Event;
29 import org.apache.maven.surefire.api.fork.ForkNodeArguments;
30 import org.apache.maven.surefire.extensions.util.CountdownCloseable;
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 @Nonnull
46 private final ForkNodeArguments arguments;
47
48 /**
49 * @param arguments data necessary for command reader and event handler.
50 */
51 protected ForkChannel(@Nonnull ForkNodeArguments arguments) {
52 this.arguments = arguments;
53 }
54
55 /**
56 * Asynchronously connects to the client.
57 *
58 * @throws IOException if stream fails
59 * @throws InterruptedException if interrupted thread
60 */
61 public abstract void tryConnectToClient() throws IOException, InterruptedException;
62
63 /**
64 * This is server related class, which if binds to a TCP port, determines the connection string for the client.
65 *
66 * @return a connection string utilized by the client in the fork JVM
67 */
68 public abstract String getForkNodeConnectionString();
69
70 /**
71 * the permits in {@link CountdownCloseable}.
72 */
73 public abstract int getCountdownCloseablePermits();
74
75 /**
76 * Binds command handler to the channel. Starts a Thread streaming out the commands.
77 *
78 * @param commands command reader, see {@link CommandReader#readNextCommand()}
79 * @param stdIn optional standard input stream of the JVM to write the encoded commands into it
80 * @throws IOException if an error in the fork channel
81 * @throws InterruptedException channel interrupted
82 */
83 public abstract void bindCommandReader(@Nonnull CommandReader commands, WritableByteChannel stdIn)
84 throws IOException, InterruptedException;
85
86 /**
87 * Starts a Thread reading the events.
88 *
89 * @param eventHandler event eventHandler
90 * @param countdownCloseable count down of the final call of {@link Closeable#close()}
91 * @param stdOut optional standard output stream of the JVM
92 * @throws IOException if an error in the fork channel
93 * @throws InterruptedException channel interrupted
94 */
95 public abstract void bindEventHandler(
96 @Nonnull EventHandler<Event> eventHandler,
97 @Nonnull CountdownCloseable countdownCloseable,
98 ReadableByteChannel stdOut)
99 throws IOException, InterruptedException;
100
101 @Nonnull
102 protected ForkNodeArguments getArguments() {
103 return arguments;
104 }
105
106 public abstract void disable();
107
108 @Override
109 public abstract void close() throws IOException;
110 }