Skip to content

Commit 033d7ae

Browse files
committed
Refactor ProcessWrapper to conditionally, based on System property, to forcibly destroy a forked JVM (child) process on shutdown.
1 parent 8afbb0c commit 033d7ae

File tree

4 files changed

+37
-42
lines changed

4 files changed

+37
-42
lines changed

spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/process/ProcessWrapper.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.springframework.data.gemfire.tests.util.ThreadUtils;
4141
import org.springframework.data.gemfire.tests.util.ThrowableUtils;
4242
import org.springframework.lang.NonNull;
43+
import org.springframework.lang.Nullable;
4344
import org.springframework.util.Assert;
4445
import org.springframework.util.StringUtils;
4546

@@ -59,6 +60,7 @@
5960
* @see java.util.concurrent.Future
6061
* @see org.springframework.data.gemfire.tests.process.ProcessConfiguration
6162
* @see org.springframework.data.gemfire.tests.process.ProcessInputStreamListener
63+
* @see org.springframework.data.gemfire.tests.process.ProcessUtils
6264
* @since 0.0.1
6365
*/
6466
@SuppressWarnings("unused")
@@ -72,6 +74,8 @@ public class ProcessWrapper {
7274

7375
protected static final String DEFAULT_HOST = "localhost";
7476

77+
protected static final String PROCESS_DESTROY_FORCIBLY_PROPERTY = "spring.data.gemfire.test.process.destroy-forcibly";
78+
7579
private final List<ProcessInputStreamListener> listeners = new CopyOnWriteArrayList<>();
7680

7781
protected final Logger log = Logger.getLogger(getClass().getName());
@@ -220,12 +224,12 @@ public int safeExitValue() {
220224
}
221225
}
222226

223-
public ProcessWrapper listeningOn(int port) {
227+
public @NonNull ProcessWrapper listeningOn(int port) {
224228
this.port = Math.max(port, DEFAULT_PORT);
225229
return this;
226230
}
227231

228-
public String readLogFile() throws IOException {
232+
public @NonNull String readLogFile() throws IOException {
229233

230234
File[] logFiles = FileSystemUtils.listFiles(getWorkingDirectory(),
231235
path -> (path != null && (path.isDirectory() || path.getAbsolutePath().endsWith(".log"))));
@@ -239,20 +243,20 @@ public String readLogFile() throws IOException {
239243
}
240244
}
241245

242-
public String readLogFile(File log) throws IOException {
246+
public @NonNull String readLogFile(@NonNull File log) throws IOException {
243247
return FileUtils.read(log);
244248
}
245249

246-
public boolean register(ProcessInputStreamListener listener) {
250+
public boolean register(@Nullable ProcessInputStreamListener listener) {
247251
return listener != null && listeners.add(listener);
248252
}
249253

250-
public ProcessWrapper registerShutdownHook() {
254+
public @NonNull ProcessWrapper registerShutdownHook() {
251255
Runtime.getRuntime().addShutdownHook(new Thread(this::shutdown));
252256
return this;
253257
}
254258

255-
public ProcessWrapper runningOn(String host) {
259+
public @NonNull ProcessWrapper runningOn(@Nullable String host) {
256260
this.host = StringUtils.hasText(host) ? host : DEFAULT_HOST;
257261
return this;
258262
}
@@ -331,6 +335,7 @@ public int stop(long milliseconds) {
331335
interrupted = true;
332336
}
333337
}
338+
334339
}
335340
catch (TimeoutException cause) {
336341
exitValue = -1;
@@ -361,14 +366,18 @@ public int shutdown() {
361366

362367
if (isRunning()) {
363368
stop();
364-
if (isRunning()) {
369+
if (isRunning() && isShutdownForciblyEnabled()) {
365370
this.process.destroyForcibly();
366371
}
367372
}
368373

369374
return safeExitValue();
370375
}
371376

377+
private boolean isShutdownForciblyEnabled() {
378+
return Boolean.getBoolean(PROCESS_DESTROY_FORCIBLY_PROPERTY);
379+
}
380+
372381
public boolean unregister(ProcessInputStreamListener listener) {
373382
return this.listeners.remove(listener);
374383
}

spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/util/FileSystemUtils.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030
import org.springframework.util.Assert;
3131

3232
/**
33-
* The {@link FileSystemUtils} class is a utility class encapsulating functionality to process
34-
* file system directories and files collectively.
33+
* Abstract utility class encapsulating functionality to process file system directories and files collectively.
3534
*
3635
* @author John Blum
3736
* @see java.io.File
@@ -51,11 +50,11 @@ public abstract class FileSystemUtils extends FileUtils {
5150

5251
public static final File[] NO_FILES = new File[0];
5352

54-
public static boolean deleteRecursive(File path) {
53+
public static boolean deleteRecursive(@Nullable File path) {
5554
return deleteRecursive(path, AllFilesFilter.INSTANCE);
5655
}
5756

58-
public static boolean deleteRecursive(File path, FileFilter fileFilter) {
57+
public static boolean deleteRecursive(@Nullable File path, @Nullable FileFilter fileFilter) {
5958

6059
boolean success = true;
6160

@@ -72,7 +71,7 @@ public static boolean exists(@Nullable File path) {
7271
return path != null && path.exists();
7372
}
7473

75-
// returns sub-directory just below working directory
74+
// returns subdirectory just below working directory
7675
public static @Nullable File getRootRelativeToWorkingDirectoryOrPath(@Nullable File path) {
7776

7877
File localPath = path;
@@ -83,12 +82,10 @@ public static boolean exists(@Nullable File path) {
8382
}
8483
}
8584

86-
return localPath != null
87-
? localPath
88-
: path;
85+
return localPath != null ? localPath : path;
8986
}
9087

91-
public static boolean isEmpty(File path) {
88+
public static boolean isEmpty(@Nullable File path) {
9289

9390
return isDirectory(path)
9491
? ArrayUtils.isEmpty(path.listFiles())
@@ -120,13 +117,9 @@ public static boolean isEmpty(File path) {
120117

121118
public static @NonNull File[] safeListFiles(@Nullable File directory, @Nullable FileFilter fileFilter) {
122119

123-
FileFilter resolvedFileFilter = fileFilter != null
124-
? fileFilter
125-
: AllFilesFilter.INSTANCE;
120+
FileFilter resolvedFileFilter = fileFilter != null ? fileFilter : AllFilesFilter.INSTANCE;
126121

127-
File[] files = isDirectory(directory)
128-
? directory.listFiles(resolvedFileFilter)
129-
: null;
122+
File[] files = isDirectory(directory) ? directory.listFiles(resolvedFileFilter) : null;
130123

131124
return files != null
132125
? files

spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/util/FileUtils.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,15 @@
2222
import java.io.FileWriter;
2323
import java.io.IOException;
2424

25+
import org.springframework.lang.NonNull;
2526
import org.springframework.util.Assert;
2627
import org.springframework.util.StringUtils;
2728

2829
/**
29-
* The {@link FileUtils} class is an abstract utility class for processing file system files
30-
* by working with {@link File} objects.
30+
* Abstract utility class for processing file system files by using {@link File} objects.
3131
*
3232
* @author John Blum
3333
* @see java.io.File
34-
* @see java.io.FileReader
35-
* @see java.io.FileWriter
3634
* @see org.springframework.data.gemfire.tests.util.IOUtils
3735
* @since 0.0.1
3836
*/
@@ -50,25 +48,23 @@ public static boolean isFile(File path) {
5048
return path != null && path.isFile();
5149
}
5250

53-
public static File newFile(String pathname) {
51+
public static @NonNull File newFile(String pathname) {
5452
return new File(pathname);
5553
}
5654

57-
public static File newFile(File parent, String pathname) {
55+
public static @NonNull File newFile(File parent, String pathname) {
5856
return new File(parent, pathname);
5957
}
6058

6159
public static long nullSafeLength(File path) {
6260
return path != null ? path.length() : 0L;
6361
}
6462

65-
public static String read(File file) throws IOException {
63+
public static @NonNull String read(@NonNull File file) throws IOException {
6664

6765
Assert.isTrue(isFile(file), String.format("The File [%s] to read the contents from is not a valid file", file));
6866

69-
BufferedReader fileReader = new BufferedReader(new FileReader(file));
70-
71-
try {
67+
try (BufferedReader fileReader = new BufferedReader(new FileReader(file))) {
7268

7369
StringBuilder buffer = new StringBuilder();
7470

@@ -79,12 +75,9 @@ public static String read(File file) throws IOException {
7975

8076
return buffer.toString().trim();
8177
}
82-
finally {
83-
close(fileReader);
84-
}
8578
}
8679

87-
public static void write(File file, String contents) throws IOException {
80+
public static void write(@NonNull File file, @NonNull String contents) throws IOException {
8881

8982
Assert.notNull(file, "File is required");
9083

spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/util/IOUtils.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* or implied. See the License for the specific language governing
1414
* permissions and limitations under the License.
1515
*/
16-
1716
package org.springframework.data.gemfire.tests.util;
1817

1918
import java.io.ByteArrayInputStream;
@@ -26,8 +25,11 @@
2625
import java.util.logging.Level;
2726
import java.util.logging.Logger;
2827

28+
import org.springframework.lang.NonNull;
29+
import org.springframework.lang.Nullable;
30+
2931
/**
30-
* The {@link IOUtils} class is an abstract utility class for working with IO operations.
32+
* Abstract utility class used to process IO operations.
3133
*
3234
* @author John Blum
3335
* @see java.io.Closeable
@@ -39,7 +41,7 @@ public abstract class IOUtils {
3941

4042
protected static final Logger log = Logger.getLogger(IOUtils.class.getName());
4143

42-
public static boolean close(Closeable closeable) {
44+
public static boolean close(@Nullable Closeable closeable) {
4345

4446
if (closeable != null) {
4547
try {
@@ -67,7 +69,7 @@ public static boolean close(Closeable closeable) {
6769
* threw an {@link IOException}.
6870
* @see IOException
6971
*/
70-
public static boolean doSafeIo(IoExceptionThrowingOperation operation) {
72+
public static boolean doSafeIo(@NonNull IoExceptionThrowingOperation operation) {
7173

7274
try {
7375
operation.doIo();
@@ -87,7 +89,6 @@ public static <T> T deserializeObject(byte[] objectBytes) throws IOException, Cl
8789

8890
try {
8991
objectInputStream = new ObjectInputStream(byteArrayInputStream);
90-
9192
return (T) objectInputStream.readObject();
9293
}
9394
finally {
@@ -105,7 +106,6 @@ public static byte[] serializeObject(Serializable obj) throws IOException {
105106
objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
106107
objectOutputStream.writeObject(obj);
107108
objectOutputStream.flush();
108-
109109
return byteArrayOutputStream.toByteArray();
110110
}
111111
finally {

0 commit comments

Comments
 (0)