View Javadoc
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.api.booter;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.apache.maven.surefire.api.stream.AbstractStreamDecoder.Segment;
25  
26  import static java.nio.charset.StandardCharsets.US_ASCII;
27  import static java.util.Objects.requireNonNull;
28  
29  /**
30   * Commands which are sent from plugin to the forked jvm.
31   *
32   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
33   * @since 2.19
34   */
35  public enum MasterProcessCommand {
36      RUN_CLASS("run-testclass", String.class),
37      TEST_SET_FINISHED("testset-finished", Void.class),
38      SKIP_SINCE_NEXT_TEST("skip-since-next-test", Void.class),
39      SHUTDOWN("shutdown", String.class),
40  
41      /** To tell a forked process that the master process is still alive. Repeated after 10 seconds. */
42      NOOP("noop", Void.class),
43      BYE_ACK("bye-ack", Void.class);
44  
45      // due to have fast and thread-safe Map
46      public static final Map<Segment, MasterProcessCommand> COMMAND_TYPES = segmentsToCmds();
47  
48      private final String opcode;
49      private final byte[] opcodeBinary;
50      private final Class<?> dataType;
51  
52      MasterProcessCommand(String opcode, Class<?> dataType) {
53          this.opcode = requireNonNull(opcode, "value cannot be null");
54          opcodeBinary = opcode.getBytes(US_ASCII);
55          this.dataType = requireNonNull(dataType, "dataType cannot be null");
56      }
57  
58      public byte[] getOpcodeBinary() {
59          return opcodeBinary;
60      }
61  
62      public int getOpcodeLength() {
63          return opcodeBinary.length;
64      }
65  
66      public Class<?> getDataType() {
67          return dataType;
68      }
69  
70      public boolean hasDataType() {
71          return dataType != Void.class;
72      }
73  
74      @Override
75      public String toString() {
76          return opcode;
77      }
78  
79      private static Map<Segment, MasterProcessCommand> segmentsToCmds() {
80          Map<Segment, MasterProcessCommand> commands = new HashMap<>();
81          for (MasterProcessCommand command : MasterProcessCommand.values()) {
82              byte[] array = command.toString().getBytes(US_ASCII);
83              commands.put(new Segment(array, 0, array.length), command);
84          }
85          return commands;
86      }
87  }