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 * @see Command
30 * @since 2.19
31 */
32 public abstract class DefaultCommandReader extends AbstractCommandReader {
33 /**
34 * Opposite to {@link #isClosed()}.
35 *
36 * @return {@code true} if not closed
37 */
38 protected boolean canContinue() {
39 return !isClosed();
40 }
41
42 /**
43 * Possibly waiting for next command (see {@link #nextCommand()}) unless the stream is atomically
44 * closed (see {@link #isClosed()} returns {@code true}) before this method has returned.
45 *
46 * @throws IOException stream error while waiting for notification regarding next test required by forked jvm
47 */
48 protected void beforeNextCommand() throws IOException {}
49
50 protected abstract Command nextCommand();
51
52 /**
53 * Used by single thread in StreamFeeder class.
54 *
55 * @return {@inheritDoc}
56 * @throws IOException {@inheritDoc}
57 */
58 @Override
59 public Command readNextCommand() throws IOException {
60 if (isClosed()) {
61 return null;
62 }
63
64 if (!canContinue()) {
65 close();
66 return null;
67 }
68
69 beforeNextCommand();
70
71 if (isClosed()) {
72 return null;
73 }
74
75 return nextCommand();
76 }
77 }