package de.mlessmann.certassist.openssl; import de.mlessmann.certassist.DeleteRecursiveFileVisitor; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.Set; import lombok.AccessLevel; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @Slf4j @RequiredArgsConstructor(access = AccessLevel.PACKAGE) public class OpenSSLCertificateResult implements CertificateUsage { private final Path tmpDir; private final Path certificatePath; private final Path privateKeyPath; private final Path fullchainPath; private final String certificateFingerPrint; @Override public Path certificatePath() { return certificatePath; } @Override public Path certificateKeyPath() { return privateKeyPath; } @Override public Path fullchainPath() { return fullchainPath; } @Override public String fingerprint() { return certificateFingerPrint; } @Override public void close() { cleanupDir(true); } private void cleanupDir(boolean retryOnExit) { try { log.info("Cleaning up temporary output directory {}", tmpDir); Files.walkFileTree(tmpDir, Set.of(), Integer.MAX_VALUE, new DeleteRecursiveFileVisitor()); Files.deleteIfExists(tmpDir); } catch (IOException e) { log.error("Failed to clean up temporary output directory {}! (retry={})", tmpDir, retryOnExit, e); if (retryOnExit) { Runtime.getRuntime().addShutdownHook(new Thread(() -> cleanupDir(false))); } } } }