mirror of
https://github.com/jenkinsci/jenkins.git
synced 2026-11-17 05:52:26 +00:00
Migrate from deprecated finalization API to Java 9+ API (#8486)
* Migrate from deprecated finalization API to Java 9+ API * Use non-deprecated JTH method
This commit is contained in:
1 parent
09d8e23c22
commit
febca34a3d
4 files changed
+115
-18
No files matched your search
@@ -30,6 +30,7 @@ import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.lang.ref.Cleaner;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.AtomicMoveNotSupportedException;
|
||||
@@ -55,6 +56,9 @@ public class AtomicFileWriter extends Writer {
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(AtomicFileWriter.class.getName());
|
||||
|
||||
private static final Cleaner CLEANER = Cleaner.create(
|
||||
new NamingThreadFactory(new DaemonThreadFactory(), AtomicFileWriter.class.getName() + ".cleaner"));
|
||||
|
||||
private static /* final */ boolean DISABLE_FORCED_FLUSH = SystemProperties.getBoolean(
|
||||
AtomicFileWriter.class.getName() + ".DISABLE_FORCED_FLUSH");
|
||||
|
||||
@@ -64,7 +68,7 @@ public class AtomicFileWriter extends Writer {
|
||||
}
|
||||
}
|
||||
|
||||
private final Writer core;
|
||||
private final FileChannelWriter core;
|
||||
private final Path tmpPath;
|
||||
private final Path destPath;
|
||||
|
||||
@@ -151,6 +155,8 @@ public class AtomicFileWriter extends Writer {
|
||||
}
|
||||
|
||||
core = new FileChannelWriter(tmpPath, charset, integrityOnFlush, integrityOnClose, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
|
||||
|
||||
CLEANER.register(this, new CleanupChecker(core, tmpPath, destPath));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -185,7 +191,12 @@ public class AtomicFileWriter extends Writer {
|
||||
* the {@link #commit()} is called, to simplify coding.
|
||||
*/
|
||||
public void abort() throws IOException {
|
||||
closeAndDeleteTempFile();
|
||||
// One way or another, the temporary file should be deleted.
|
||||
try {
|
||||
close();
|
||||
} finally {
|
||||
Files.deleteIfExists(tmpPath);
|
||||
}
|
||||
}
|
||||
|
||||
public void commit() throws IOException {
|
||||
@@ -225,21 +236,32 @@ public class AtomicFileWriter extends Writer {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
try {
|
||||
closeAndDeleteTempFile();
|
||||
} finally {
|
||||
super.finalize();
|
||||
}
|
||||
}
|
||||
private static final class CleanupChecker implements Runnable {
|
||||
private final FileChannelWriter core;
|
||||
private final Path tmpPath;
|
||||
private final Path destPath;
|
||||
|
||||
private void closeAndDeleteTempFile() throws IOException {
|
||||
// one way or the other, temporary file should be deleted.
|
||||
try {
|
||||
close();
|
||||
} finally {
|
||||
Files.deleteIfExists(tmpPath);
|
||||
CleanupChecker(final FileChannelWriter core, final Path tmpPath, final Path destPath) {
|
||||
this.core = core;
|
||||
this.tmpPath = tmpPath;
|
||||
this.destPath = destPath;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (core.isOpen()) {
|
||||
LOGGER.log(Level.WARNING, "AtomicFileWriter for " + destPath + " was not closed before being released");
|
||||
try {
|
||||
core.close();
|
||||
} catch (IOException e) {
|
||||
LOGGER.log(Level.WARNING, "Failed to close " + tmpPath + " for destination file " + destPath, e);
|
||||
}
|
||||
}
|
||||
try {
|
||||
Files.deleteIfExists(tmpPath);
|
||||
} catch (IOException e) {
|
||||
LOGGER.log(Level.WARNING, "Failed to delete temporary file " + tmpPath + " for destination file " + destPath, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.CharBuffer;
|
||||
import java.nio.channels.Channel;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.OpenOption;
|
||||
@@ -26,7 +27,7 @@ import org.kohsuke.accmod.restrictions.NoExternalUse;
|
||||
* @see <a href="https://github.com/jenkinsci/jenkins/pull/2548">PR-2548</a>
|
||||
*/
|
||||
@Restricted(NoExternalUse.class)
|
||||
public class FileChannelWriter extends Writer {
|
||||
public class FileChannelWriter extends Writer implements Channel {
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(FileChannelWriter.class.getName());
|
||||
|
||||
@@ -82,6 +83,11 @@ public class FileChannelWriter extends Writer {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOpen() {
|
||||
return channel.isOpen();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
if (channel.isOpen()) {
|
||||
|
||||
Reference in new issue
Block a user