1 package org.apache.maven.surefire.booter.spi;
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.booter.Command;
23 import org.apache.maven.surefire.api.booter.MasterProcessChannelDecoder;
24 import org.apache.maven.surefire.api.fork.ForkNodeArguments;
25 import org.apache.maven.surefire.api.stream.AbstractStreamDecoder.Memento;
26 import org.apache.maven.surefire.api.stream.MalformedChannelException;
27 import org.apache.maven.surefire.booter.stream.CommandDecoder;
28
29 import javax.annotation.Nonnull;
30 import java.io.IOException;
31 import java.nio.channels.ReadableByteChannel;
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 {
42 private final CommandDecoder decoder;
43 private Memento memento;
44
45 public CommandChannelDecoder( @Nonnull ReadableByteChannel channel,
46 @Nonnull ForkNodeArguments arguments )
47 {
48 decoder = new CommandDecoder( channel, arguments );
49 }
50
51 @Override
52 @Nonnull
53 @SuppressWarnings( "checkstyle:innerassignment" )
54 public Command decode() throws IOException
55 {
56 if ( memento == null )
57 {
58 // do not create memento in constructor because the constructor is called in another thread
59 // memento is the thread confinement object
60 memento = decoder.new Memento();
61 }
62
63 do
64 {
65 try
66 {
67 Command command = decoder.decode( memento );
68 if ( command != null )
69 {
70 return command;
71 }
72 }
73 catch ( MalformedChannelException e )
74 {
75 // a bad stream, already logged the stream down to a dump file or console, and continue till OEF
76 }
77 }
78 while ( true );
79 }
80
81 @Override
82 public void close() throws IOException
83 {
84 decoder.close();
85 }
86 }