From 9e70409ab22030eaf94ff63247606d0d03682eee Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sat, 24 Sep 2016 19:51:03 +0800 Subject: [PATCH] Fix various HistorySpooler issues --- source/net/filebot/History.java | 6 +-- source/net/filebot/HistorySpooler.java | 48 ++++++++++--------- source/net/filebot/ui/SupportDialog.java | 2 +- .../net/filebot/ui/rename/HistoryDialog.java | 33 +++++-------- 4 files changed, 41 insertions(+), 48 deletions(-) diff --git a/source/net/filebot/History.java b/source/net/filebot/History.java index 525d2616..f0ac0d62 100644 --- a/source/net/filebot/History.java +++ b/source/net/filebot/History.java @@ -210,10 +210,10 @@ public class History { return ((History) unmarshaller.unmarshal(stream)); } catch (Exception e) { debug.log(Level.SEVERE, "Failed to read history", e); - - // fail-safe => default to empty history - return new History(); } + + // default to empty history + return new History(); } } diff --git a/source/net/filebot/HistorySpooler.java b/source/net/filebot/HistorySpooler.java index 8f21c812..2d4c9d0c 100644 --- a/source/net/filebot/HistorySpooler.java +++ b/source/net/filebot/HistorySpooler.java @@ -1,10 +1,10 @@ package net.filebot; +import static java.nio.channels.Channels.*; import static net.filebot.Logging.*; import java.io.File; import java.io.IOException; -import java.nio.channels.Channels; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; import java.nio.file.StandardOpenOption; @@ -27,22 +27,16 @@ public final class HistorySpooler { return instance; } - // commit session history on shutdown static { - Runtime.getRuntime().addShutdownHook(new Thread() { - - @Override - public void run() { - HistorySpooler.getInstance().commit(); - } - }); + Runtime.getRuntime().addShutdownHook(new Thread(HistorySpooler.getInstance()::commit, "HistorySpoolerShutdownHook")); // commit session history on shutdown } - private File persistentHistoryFile = ApplicationFolder.AppData.resolve("history.xml"); + private final File persistentHistoryFile = ApplicationFolder.AppData.resolve("history.xml"); + private int persistentHistoryTotalSize = -1; private boolean persistentHistoryEnabled = true; - private History sessionHistory = new History(); + private final History sessionHistory = new History(); public synchronized History getCompleteHistory() throws IOException { if (persistentHistoryFile.length() <= 0) { @@ -51,7 +45,7 @@ public final class HistorySpooler { try (FileChannel channel = FileChannel.open(persistentHistoryFile.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE)) { try (FileLock lock = channel.lock()) { - History history = History.importHistory(new CloseShieldInputStream(Channels.newInputStream(channel))); // keep JAXB from closing the stream + History history = History.importHistory(new CloseShieldInputStream(newInputStream(channel))); // keep JAXB from closing the stream history.addAll(sessionHistory.sequences()); return history; } @@ -59,7 +53,7 @@ public final class HistorySpooler { } public synchronized void commit() { - if (!persistentHistoryEnabled || sessionHistory.sequences().isEmpty()) { + if (sessionHistory.sequences().isEmpty() || !persistentHistoryEnabled) { return; } @@ -72,9 +66,9 @@ public final class HistorySpooler { if (channel.size() > 0) { try { channel.position(0); - history = History.importHistory(new CloseShieldInputStream(Channels.newInputStream(channel))); // keep JAXB from closing the stream + history = History.importHistory(new CloseShieldInputStream(newInputStream(channel))); // keep JAXB from closing the stream } catch (Exception e) { - debug.log(Level.SEVERE, "Failed to load rename history", e); + debug.log(Level.SEVERE, "Failed to read history file", e); } } @@ -82,14 +76,15 @@ public final class HistorySpooler { history.addAll(sessionHistory.sequences()); channel.position(0); - History.exportHistory(history, new CloseShieldOutputStream(Channels.newOutputStream(channel))); // keep JAXB from closing the stream + History.exportHistory(history, new CloseShieldOutputStream(newOutputStream(channel))); // keep JAXB from closing the stream + channel.truncate(channel.position()); sessionHistory.clear(); persistentHistoryTotalSize = history.totalSize(); } } } catch (Exception e) { - debug.log(Level.SEVERE, "Failed to write rename history", e); + debug.log(Level.SEVERE, "Failed to write history file", e); } } @@ -104,21 +99,28 @@ public final class HistorySpooler { sequence.add(new Element(element.getKey().getName(), element.getValue().getPath(), element.getKey().getParentFile())); } - // append to session history if (sequence.size() > 0) { - sessionHistory.add(sequence); + sessionHistory.add(sequence); // append to session history } } - public History getSessionHistory() { - return sessionHistory; + public synchronized void append(History importHistory) { + sessionHistory.merge(importHistory); } - public int getPersistentHistoryTotalSize() { + public synchronized History getSessionHistory() { + return new History(sessionHistory.sequences()); + } + + public synchronized int getSessionHistoryTotalSize() { + return sessionHistory.totalSize(); + } + + public synchronized int getPersistentHistoryTotalSize() { return persistentHistoryTotalSize; } - public void setPersistentHistoryEnabled(boolean persistentHistoryEnabled) { + public synchronized void setPersistentHistoryEnabled(boolean persistentHistoryEnabled) { this.persistentHistoryEnabled = persistentHistoryEnabled; } diff --git a/source/net/filebot/ui/SupportDialog.java b/source/net/filebot/ui/SupportDialog.java index dd678c22..7d948024 100644 --- a/source/net/filebot/ui/SupportDialog.java +++ b/source/net/filebot/ui/SupportDialog.java @@ -133,7 +133,7 @@ public enum SupportDialog { int lastSupportRevision = supportRevision.stream().max(Integer::compare).orElse(0); int currentRevision = getApplicationRevisionNumber(); - int sessionRenameCount = HistorySpooler.getInstance().getSessionHistory().totalSize(); + int sessionRenameCount = HistorySpooler.getInstance().getSessionHistoryTotalSize(); int totalRenameCount = HistorySpooler.getInstance().getPersistentHistoryTotalSize(); // show donation / review reminders to power users diff --git a/source/net/filebot/ui/rename/HistoryDialog.java b/source/net/filebot/ui/rename/HistoryDialog.java index 83ab8f95..7c3f64c6 100644 --- a/source/net/filebot/ui/rename/HistoryDialog.java +++ b/source/net/filebot/ui/rename/HistoryDialog.java @@ -10,6 +10,7 @@ import static net.filebot.Logging.*; import static net.filebot.Settings.*; import static net.filebot.UserFiles.*; import static net.filebot.media.XattrMetaInfo.*; +import static net.filebot.util.FileUtilities.*; import static net.filebot.util.RegularExpressions.*; import static net.filebot.util.ui.SwingUI.*; @@ -71,6 +72,7 @@ import javax.swing.table.TableRowSorter; import net.filebot.History; import net.filebot.History.Element; import net.filebot.History.Sequence; +import net.filebot.HistorySpooler; import net.filebot.ResourceManager; import net.filebot.StandardRenameAction; import net.filebot.mac.MacAppUtilities; @@ -79,8 +81,6 @@ import net.filebot.ui.transfer.FileTransferablePolicy; import net.filebot.ui.transfer.LoadAction; import net.filebot.ui.transfer.SaveAction; import net.filebot.ui.transfer.TransferablePolicy; -import net.filebot.ui.transfer.TransferablePolicy.TransferAction; -import net.filebot.util.FileUtilities; import net.filebot.util.FileUtilities.ExtensionFileFilter; import net.filebot.util.ui.GradientStyle; import net.filebot.util.ui.LazyDocumentListener; @@ -126,15 +126,7 @@ class HistoryDialog extends JDialog { content.add(createScrollPaneGroup("Sequences", sequenceTable), "growx, wrap paragraph"); content.add(createScrollPaneGroup("Elements", elementTable), "growx, wrap paragraph"); - // use ADD by default - Action importAction = new LoadAction("Import", ResourceManager.getIcon("action.load"), this::getTransferablePolicy) { - - @Override - public TransferAction getTransferAction(ActionEvent evt) { - // if SHIFT was pressed when the button was clicked, assume PUT action, use ADD by default - return ((evt.getModifiers() & ActionEvent.SHIFT_MASK) != 0) ? TransferAction.PUT : TransferAction.ADD; - } - }; + Action importAction = new LoadAction("Import", ResourceManager.getIcon("action.load"), this::getTransferablePolicy); content.add(new JButton(importAction), "wmin button, hmin 25px, gap indent, sg button"); content.add(new JButton(new SaveAction("Export", ResourceManager.getIcon("action.save"), exportHandler)), "gap rel, sg button"); @@ -597,26 +589,25 @@ class HistoryDialog extends JDialog { @Override protected boolean accept(List files) { - return FileUtilities.containsOnly(files, new ExtensionFileFilter("xml")); + return containsOnly(files, new ExtensionFileFilter("xml")); } @Override protected void clear() { - setModel(new History()); + // do nothing } @Override protected void load(List files, TransferAction action) throws IOException { - History history = getModel(); - - try { - for (File file : files) { - history.merge(History.importHistory(new FileInputStream(file))); + for (File file : files) { + try { + HistorySpooler.getInstance().append(History.importHistory(new FileInputStream(file))); + } catch (Exception e) { + log.log(Level.SEVERE, "Failed to import history: " + file, e); } - } finally { - // update view - setModel(history); } + + setModel(HistorySpooler.getInstance().getCompleteHistory()); // update view } @Override