001package apps.util.issuereporter;
002
003import java.io.File;
004import java.io.IOException;
005import java.nio.file.*;
006
007import jmri.util.FileUtil;
008import jmri.util.startup.StartupRunnable;
009
010import org.apiguardian.api.API;
011import org.openide.util.lookup.ServiceProvider;
012
013/**
014 * Remove remnants of prior issue reports.
015 *
016 * @author Randall Wood Copyright 2020
017 */
018@API(status = API.Status.INTERNAL)
019@ServiceProvider(service = StartupRunnable.class)
020public final class IssueReporterStartupRunnable implements StartupRunnable {
021
022    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(IssueReporterStartupRunnable.class);
023
024    @Override
025    @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE",
026                justification="Null check in stream.forEach() is embedded, beyond our control")
027    public void run() {
028        Path tempDir = new File(System.getProperty("java.io.tmpdir")).toPath();
029        try (DirectoryStream<Path> stream = Files.newDirectoryStream(tempDir, entry -> entry.toFile().getName().startsWith("jmri-issue-report-"))) {
030                    stream.forEach(entry -> FileUtil.delete(entry.toFile()));
031        } catch (IOException ex) {
032            log.error("Exception cleaning up from issue reporter", ex);
033        }
034    }
035}