filebot/source/net/filebot/HistorySpooler.java

145 lines
4.3 KiB
Java
Raw Normal View History

2014-04-19 02:30:29 -04:00
package net.filebot;
2016-09-24 07:51:03 -04:00
import static java.nio.channels.Channels.*;
2016-03-09 15:36:28 -05:00
import static net.filebot.Logging.*;
import java.io.File;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
2014-11-06 23:54:21 -05:00
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;
2013-12-03 21:37:56 -05:00
import org.apache.commons.io.input.CloseShieldInputStream;
import org.apache.commons.io.output.CloseShieldOutputStream;
2016-08-04 03:05:54 -04:00
import net.filebot.History.Element;
public final class HistorySpooler {
private static final HistorySpooler instance = new HistorySpooler();
public static HistorySpooler getInstance() {
return instance;
}
static {
2016-09-24 07:51:03 -04:00
Runtime.getRuntime().addShutdownHook(new Thread(HistorySpooler.getInstance()::commit, "HistorySpoolerShutdownHook")); // commit session history on shutdown
}
2016-11-25 10:59:26 -05:00
private final File persistentHistoryFile = ApplicationFolder.AppData.resolve("history.xml");
2016-09-24 07:51:03 -04:00
private int sessionHistoryTotalSize = 0;
private int persistentHistoryTotalSize = -1;
private boolean persistentHistoryEnabled = true;
2016-09-24 07:51:03 -04:00
private final History sessionHistory = new History();
public synchronized History getCompleteHistory() throws IOException {
2016-03-10 11:37:29 -05:00
if (persistentHistoryFile.length() <= 0) {
2014-07-28 15:20:34 -04:00
return new History(sessionHistory.sequences());
}
2014-11-06 23:54:21 -05:00
try (FileChannel channel = FileChannel.open(persistentHistoryFile.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE)) {
try (FileLock lock = channel.lock()) {
2016-09-24 07:51:03 -04:00
History history = History.importHistory(new CloseShieldInputStream(newInputStream(channel))); // keep JAXB from closing the stream
2014-11-06 23:54:21 -05:00
history.addAll(sessionHistory.sequences());
return history;
}
}
}
public synchronized void commit() {
2016-09-24 07:51:03 -04:00
if (sessionHistory.sequences().isEmpty() || !persistentHistoryEnabled) {
return;
}
try {
2014-11-06 23:54:21 -05:00
try (FileChannel channel = FileChannel.open(persistentHistoryFile.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE)) {
try (FileLock lock = channel.lock()) {
History history = new History();
// load existing history from previous sessions
if (channel.size() > 0) {
try {
channel.position(0);
2016-09-24 07:51:03 -04:00
history = History.importHistory(new CloseShieldInputStream(newInputStream(channel))); // keep JAXB from closing the stream
2014-11-06 23:54:21 -05:00
} catch (Exception e) {
2016-09-24 07:51:03 -04:00
debug.log(Level.SEVERE, "Failed to read history file", e);
2014-11-06 23:54:21 -05:00
}
2013-12-03 21:37:56 -05:00
}
2014-11-06 23:54:21 -05:00
// write new combined history
history.addAll(sessionHistory.sequences());
channel.position(0);
2016-09-24 07:51:03 -04:00
History.exportHistory(history, new CloseShieldOutputStream(newOutputStream(channel))); // keep JAXB from closing the stream
channel.truncate(channel.position());
2014-11-06 23:54:21 -05:00
sessionHistory.clear();
sessionHistoryTotalSize = 0;
2014-11-06 23:54:21 -05:00
persistentHistoryTotalSize = history.totalSize();
}
}
} catch (Exception e) {
2016-09-24 07:51:03 -04:00
debug.log(Level.SEVERE, "Failed to write history file", e);
}
}
public synchronized void clear() throws IOException {
log.fine("* Delete " + persistentHistoryFile);
persistentHistoryFile.delete();
sessionHistory.clear();
sessionHistoryTotalSize = 0;
persistentHistoryTotalSize = 0;
}
public synchronized void append(Map<File, File> elements) {
append(elements.entrySet());
}
2011-10-10 12:21:54 -04:00
public synchronized void append(Iterable<Entry<File, File>> elements) {
List<Element> sequence = new ArrayList<Element>();
2011-10-10 12:21:54 -04:00
for (Entry<File, File> element : elements) {
File k = element.getKey();
File v = element.getValue();
if (k != null && v != null) {
sequence.add(new Element(k.getName(), v.getPath(), k.getParentFile()));
}
}
if (sequence.size() > 0) {
2016-09-24 07:51:03 -04:00
sessionHistory.add(sequence); // append to session history
sessionHistoryTotalSize += sequence.size();
}
}
2016-09-24 07:51:03 -04:00
public synchronized void append(History importHistory) {
sessionHistory.merge(importHistory);
}
public synchronized History getSessionHistory() {
return new History(sessionHistory.sequences());
}
public synchronized int getSessionHistoryTotalSize() {
return sessionHistoryTotalSize;
}
2016-09-24 07:51:03 -04:00
public synchronized int getPersistentHistoryTotalSize() {
return persistentHistoryTotalSize;
}
2016-09-24 07:51:03 -04:00
public synchronized void setPersistentHistoryEnabled(boolean persistentHistoryEnabled) {
this.persistentHistoryEnabled = persistentHistoryEnabled;
}
}