1 package org.apache.maven.surefire.booter;
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 static org.apache.maven.surefire.util.internal.ObjectUtils.requireNonNull;
23
24 /**
25 * Immutable object which encapsulates PID and elapsed time (Unix) or start time (Windows).
26 * <br>
27 * Methods
28 * ({@link #getPID()}, {@link #getTime()}, {@link #isTimeAfter(ProcessInfo)}, {@link #isTimeEqualTo(ProcessInfo)})
29 * throw {@link IllegalStateException}
30 * if {@link #isValid()} returns {@code false} or {@link #isError()} returns {@code true}.
31 *
32 * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
33 * @since 2.20.1
34 */
35 final class ProcessInfo
36 {
37 static final ProcessInfo INVALID_PROCESS_INFO = new ProcessInfo( null, null );
38 static final ProcessInfo ERR_PROCESS_INFO = new ProcessInfo( null, null );
39
40 /**
41 * On Unix we do not get PID due to the command is interested only to etime of PPID:
42 * <br>
43 * <pre>/bin/ps -o etime= -p 123</pre>
44 */
45 static ProcessInfo unixProcessInfo( long pid, long etime )
46 {
47 return new ProcessInfo( pid, etime );
48 }
49
50 static ProcessInfo windowsProcessInfo( long pid, String startTimestamp )
51 {
52 return new ProcessInfo( pid, requireNonNull( startTimestamp, "startTimestamp is NULL" ) );
53 }
54
55 private final Long pid;
56 private final Comparable time;
57
58 private ProcessInfo( Long pid, Comparable time )
59 {
60 this.pid = pid;
61 this.time = time;
62 }
63
64 boolean isValid()
65 {
66 return this != INVALID_PROCESS_INFO;
67 }
68
69 boolean isError()
70 {
71 return this == ERR_PROCESS_INFO;
72 }
73
74 long getPID()
75 {
76 checkValid();
77 return pid;
78 }
79
80 Comparable getTime()
81 {
82 checkValid();
83 return time;
84 }
85
86 @SuppressWarnings( "unchecked" )
87 boolean isTimeEqualTo( ProcessInfo that )
88 {
89 checkValid();
90 that.checkValid();
91 return this.time.compareTo( that.time ) == 0;
92 }
93
94 @SuppressWarnings( "unchecked" )
95 boolean isTimeAfter( ProcessInfo that )
96 {
97 checkValid();
98 that.checkValid();
99 return this.time.compareTo( that.time ) > 0;
100 }
101
102 private void checkValid()
103 {
104 if ( !isValid() || isError() )
105 {
106 throw new IllegalStateException( "invalid process info" );
107 }
108 }
109 }