Skip to content

Commit 32c268e

Browse files
committed
added shutdown hook
1 parent 468ff3d commit 32c268e

File tree

5 files changed

+47
-1
lines changed

5 files changed

+47
-1
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### Updates from version 1.5.4 to 1.5.5
2+
* added shutdown hook for resource cleanup on hard shutdowns
3+
14
### Updates from version 1.5.3 to 1.5.4
25
* updated to use SNAP version 8.0.5 / S3TBX version 8.0.3
36
* update Postgres/PostGIS drivers
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.bc.fiduceo.tool;
2+
3+
import com.bc.fiduceo.db.Storage;
4+
import com.bc.fiduceo.log.FiduceoLogger;
5+
import com.bc.fiduceo.util.TempFileUtils;
6+
7+
import java.sql.SQLException;
8+
9+
public class ShutdownHook extends Thread {
10+
11+
private final ToolContext context;
12+
13+
public ShutdownHook(ToolContext context) {
14+
this.context = context;
15+
}
16+
17+
@Override
18+
public void run() {
19+
final TempFileUtils tempFileUtils = context.getTempFileUtils();
20+
if (tempFileUtils != null) {
21+
tempFileUtils.cleanup();
22+
}
23+
24+
final Storage storage = context.getStorage();
25+
if (storage != null) {
26+
try {
27+
storage.close();
28+
} catch (SQLException e) {
29+
FiduceoLogger.getLogger().severe(e.getMessage());
30+
e.printStackTrace();
31+
}
32+
}
33+
}
34+
}

ingestion-tool/src/main/java/com/bc/fiduceo/ingest/IngestionTool.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.bc.fiduceo.reader.AcquisitionInfo;
3434
import com.bc.fiduceo.reader.Reader;
3535
import com.bc.fiduceo.reader.ReaderFactory;
36+
import com.bc.fiduceo.tool.ShutdownHook;
3637
import com.bc.fiduceo.tool.ToolContext;
3738
import com.bc.fiduceo.util.TempFileUtils;
3839
import com.bc.fiduceo.util.TimeUtils;
@@ -76,6 +77,8 @@ void run(CommandLine commandLine) throws IOException, SQLException {
7677

7778
logger.info("Successfully initialized tool");
7879

80+
Runtime.getRuntime().addShutdownHook(new ShutdownHook(context));
81+
7982
try {
8083
ingestMetadata(context, sensorType, processingVersion);
8184
} finally {

matchup-tool/src/main/java/com/bc/fiduceo/matchup/MatchupTool.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.bc.fiduceo.matchup.strategy.MatchupStrategyFactory;
3333
import com.bc.fiduceo.matchup.writer.*;
3434
import com.bc.fiduceo.reader.ReaderFactory;
35+
import com.bc.fiduceo.tool.ShutdownHook;
3536
import com.bc.fiduceo.tool.ToolContext;
3637
import com.bc.fiduceo.util.NetCDFUtils;
3738
import com.bc.fiduceo.util.TempFileUtils;
@@ -297,6 +298,9 @@ static String createDistanceVariableName(VariablesConfiguration variablesConfigu
297298

298299
void run(CommandLine commandLine) throws IOException, SQLException, InvalidRangeException {
299300
final ToolContext context = initialize(commandLine);
301+
302+
Runtime.getRuntime().addShutdownHook(new ShutdownHook(context));
303+
300304
final MmdWriterConfig mmdWriterConfig = loadWriterConfig(commandLine);
301305

302306
try {
@@ -340,7 +344,7 @@ private ToolContext initialize(CommandLine commandLine) throws IOException, SQLE
340344
final ValidationResult validationResult = useCaseConfig.checkValid();
341345
if (!validationResult.isValid()) {
342346
final StringBuilder builder = createErrorMessage(validationResult);
343-
throw new IllegalArgumentException("Use case configuration errors: " + builder.toString());
347+
throw new IllegalArgumentException("Use case configuration errors: " + builder);
344348
}
345349
context.setUseCaseConfig(useCaseConfig);
346350

post-processing-tool/src/main/java/com/bc/fiduceo/post/PostProcessingToolMain.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323

2424
import com.bc.fiduceo.log.FiduceoLogger;
25+
import com.bc.fiduceo.tool.ShutdownHook;
2526
import org.apache.commons.cli.CommandLine;
2627
import org.apache.commons.cli.CommandLineParser;
2728
import org.apache.commons.cli.ParseException;
@@ -47,6 +48,7 @@ public static void main(String[] args) {
4748

4849
try {
4950
final PostProcessingContext context = PostProcessingTool.initializeContext(commandLine);
51+
Runtime.getRuntime().addShutdownHook(new ShutdownHook(context));
5052
final PostProcessingTool tool = new PostProcessingTool(context);
5153
tool.runPostProcessing();
5254
} catch (Throwable e) {

0 commit comments

Comments
 (0)