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.booterclient.lazytestprovider;
20
21 import java.io.IOException;
22
23 import org.apache.maven.surefire.api.booter.Command;
24
25 /**
26 * Reader stream sends commands to forked jvm std-{@link java.io.InputStream input-stream}.
27 *
28 * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
29 * @since 2.19
30 * @see Command
31 */
32 public abstract class DefaultCommandReader extends AbstractCommandReader {
33 /**
34 * Opposite to {@link #isClosed()}.
35 * @return {@code true} if not closed
36 */
37 protected boolean canContinue() {
38 return !isClosed();
39 }
40
41 /**
42 * Possibly waiting for next command (see {@link #nextCommand()}) unless the stream is atomically
43 * closed (see {@link #isClosed()} returns {@code true}) before this method has returned.
44 *
45 * @throws IOException stream error while waiting for notification regarding next test required by forked jvm
46 */
47 protected void beforeNextCommand() throws IOException {}
48
49 protected abstract Command nextCommand();
50
51 /**
52 * Used by single thread in StreamFeeder class.
53 *
54 * @return {@inheritDoc}
55 * @throws IOException {@inheritDoc}
56 */
57 @Override
58 public Command readNextCommand() throws IOException {
59 if (isClosed()) {
60 return null;
61 }
62
63 if (!canContinue()) {
64 close();
65 return null;
66 }
67
68 beforeNextCommand();
69
70 if (isClosed()) {
71 return null;
72 }
73
74 return nextCommand();
75 }
76 }