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.plugin.surefire.booterclient;
20  
21  import java.util.concurrent.Callable;
22  import java.util.concurrent.FutureTask;
23  import java.util.concurrent.RunnableFuture;
24  
25  import org.apache.maven.plugin.surefire.JdkAttributes;
26  import org.apache.maven.surefire.booter.SystemUtils;
27  
28  import static org.apache.maven.surefire.api.util.internal.DaemonThreadFactory.newDaemonThread;
29  
30  /**
31   * Loads platform specifics.
32   *
33   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
34   * @since 2.20.1
35   */
36  public final class Platform {
37      private final RunnableFuture<Long> pluginPidJob;
38  
39      private final JdkAttributes jdk;
40  
41      private volatile boolean shutdown;
42  
43      public Platform() {
44          // the job may take 50 or 80 ms
45          this(new FutureTask<>(pidJob()), null);
46          newDaemonThread(pluginPidJob).start();
47      }
48  
49      private Platform(RunnableFuture<Long> pluginPidJob, JdkAttributes jdk) {
50          this.pluginPidJob = pluginPidJob;
51          this.jdk = jdk;
52      }
53  
54      public boolean isShutdown() {
55          return shutdown;
56      }
57  
58      public void setShutdownState() {
59          this.shutdown = true;
60      }
61  
62      public void clearShutdownState() {
63          this.shutdown = false;
64      }
65  
66      public Long getPluginPid() {
67          try {
68              return pluginPidJob.get();
69          } catch (Exception e) {
70              return null;
71          }
72      }
73  
74      public JdkAttributes getJdkExecAttributesForTests() {
75          return jdk;
76      }
77  
78      public Platform withJdkExecAttributesForTests(JdkAttributes jdk) {
79          return new Platform(pluginPidJob, jdk);
80      }
81  
82      private static Callable<Long> pidJob() {
83          return new Callable<Long>() {
84              @Override
85              public Long call() throws Exception {
86                  return SystemUtils.pid();
87              }
88          };
89      }
90  }