Skip to content

Commit 3085b25

Browse files
author
Ryan O'Meara
authored
Merge pull request #23 from blackducksoftware/improvement/romeara/22-automatic-temporary-file-cleanup
Resolves GH-22: Add automatic cleanup of temporary files
2 parents cb7e838 + a9fe640 commit 3085b25

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

CHANGE_LOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

77
## [Unreleased]
8+
### Changed
9+
- (GH-22) Added automatic cleanup of temporary files
10+
11+
## [0.1.0]
812
### Added
913
- Ability to analyze built source for external method calls, outputting to a formatted report

method-analyzer-core/src/main/java/com/synopsys/method/analyzer/core/report/ReportGenerator.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,30 @@
2323
package com.synopsys.method.analyzer.core.report;
2424

2525
import java.io.BufferedWriter;
26+
import java.io.File;
2627
import java.io.FileInputStream;
2728
import java.io.FileOutputStream;
2829
import java.io.IOException;
2930
import java.nio.file.Files;
3031
import java.nio.file.Path;
3132
import java.nio.file.Paths;
3233
import java.util.Collection;
34+
import java.util.Comparator;
3335
import java.util.HashMap;
3436
import java.util.LinkedList;
3537
import java.util.List;
3638
import java.util.Map;
3739
import java.util.Map.Entry;
3840
import java.util.Objects;
41+
import java.util.stream.Stream;
3942
import java.util.zip.ZipEntry;
4043
import java.util.zip.ZipOutputStream;
4144

4245
import javax.annotation.Nullable;
4346

47+
import org.slf4j.Logger;
48+
import org.slf4j.LoggerFactory;
49+
4450
import com.google.common.collect.Lists;
4551
import com.google.common.collect.Multimap;
4652
import com.google.gson.Gson;
@@ -69,6 +75,9 @@ public class ReportGenerator {
6975

7076
private static final Gson GSON = new Gson();
7177

78+
/** Logger reference to output information to the application log files */
79+
private final Logger logger = LoggerFactory.getLogger(getClass());
80+
7281
private final MetaDataReportJson metaDataReport;
7382

7483
/**
@@ -178,6 +187,17 @@ private Path writeReport(Path destinationFile, List<MethodIdJson> uniqueMethodKe
178187

179188
writeZipFile(destinationFile, outputFileMapping);
180189

190+
// GH-22: Explicitly clean up temporary files once use of them is complete
191+
try {
192+
try (Stream<Path> walk = Files.walk(workingDirectory)) {
193+
walk.sorted(Comparator.reverseOrder())
194+
.map(Path::toFile)
195+
.forEach(File::delete);
196+
}
197+
} catch (IOException e) {
198+
logger.warn("Error cleaning up temporary report files", e);
199+
}
200+
181201
return destinationFile;
182202
}
183203

0 commit comments

Comments
 (0)