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.booter.spi;
20
21 import javax.annotation.Nonnull;
22
23 import java.io.IOException;
24 import java.nio.channels.ReadableByteChannel;
25
26 import org.apache.maven.surefire.api.booter.Command;
27 import org.apache.maven.surefire.api.booter.MasterProcessChannelDecoder;
28 import org.apache.maven.surefire.api.fork.ForkNodeArguments;
29 import org.apache.maven.surefire.api.stream.AbstractStreamDecoder.Memento;
30 import org.apache.maven.surefire.api.stream.MalformedChannelException;
31 import org.apache.maven.surefire.booter.stream.CommandDecoder;
32
33 /**
34 * magic number : opcode [: opcode specific data]*
35 * <br>
36 *
37 * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
38 * @since 3.0.0-M5
39 */
40 public class CommandChannelDecoder implements MasterProcessChannelDecoder {
41 private final CommandDecoder decoder;
42 private Memento memento;
43
44 public CommandChannelDecoder(@Nonnull ReadableByteChannel channel, @Nonnull ForkNodeArguments arguments) {
45 decoder = new CommandDecoder(channel, arguments);
46 }
47
48 @Override
49 @Nonnull
50 @SuppressWarnings("checkstyle:innerassignment")
51 public Command decode() throws IOException {
52 if (memento == null) {
53 // do not create memento in constructor because the constructor is called in another thread
54 // memento is the thread confinement object
55 memento = decoder.new Memento();
56 }
57
58 do {
59 try {
60 Command command = decoder.decode(memento);
61 if (command != null) {
62 return command;
63 }
64 } catch (MalformedChannelException e) {
65 // a bad stream, already logged the stream down to a dump file or console, and continue till OEF
66 }
67 } while (true);
68 }
69
70 @Override
71 public void close() throws IOException {
72 decoder.close();
73 }
74 }