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.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 volatile JdkAttributes jdk;
42
43 public Platform()
44 {
45 // the job may take 50 or 80 ms
46 this( new FutureTask<Long>( pidJob() ), null );
47 newDaemonThread( pluginPidJob ).start();
48 }
49
50 private Platform( RunnableFuture<Long> pluginPidJob, JdkAttributes jdk )
51 {
52 this.pluginPidJob = pluginPidJob;
53 this.jdk = jdk;
54 }
55
56 public Long getPluginPid()
57 {
58 try
59 {
60 return pluginPidJob.get();
61 }
62 catch ( Exception e )
63 {
64 return null;
65 }
66 }
67
68 public JdkAttributes getJdkExecAttributesForTests()
69 {
70 return jdk;
71 }
72
73 public Platform withJdkExecAttributesForTests( JdkAttributes jdk )
74 {
75 return new Platform( pluginPidJob, jdk );
76 }
77
78 private static Callable<Long> pidJob()
79 {
80 return new Callable<Long>()
81 {
82 @Override
83 public Long call() throws Exception
84 {
85 return SystemUtils.pid();
86 }
87 };
88 }
89 }