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