2014-04-19 02:30:29 -04:00
package net.filebot ;
2009-05-11 20:17:53 -04:00
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.* ;
2009-05-17 13:14:03 -04:00
2009-05-11 20:17:53 -04:00
import java.io.File ;
2009-05-17 13:14:03 -04:00
import java.io.IOException ;
2013-03-26 04:43:02 -04:00
import java.nio.channels.FileChannel ;
import java.nio.channels.FileLock ;
2014-11-06 23:54:21 -05:00
import java.nio.file.StandardOpenOption ;
2009-07-20 07:03:24 -04:00
import java.util.ArrayList ;
import java.util.List ;
2014-04-19 12:54:25 -04:00
import java.util.Map ;
2009-05-11 20:17:53 -04:00
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 ;
2009-07-20 07:03:24 -04:00
2016-08-04 03:05:54 -04:00
import net.filebot.History.Element ;
2011-09-09 10:50:01 -04:00
public final class HistorySpooler {
2013-11-17 14:05:45 -05:00
2009-05-11 20:17:53 -04:00
private static final HistorySpooler instance = new HistorySpooler ( ) ;
2013-11-17 14:05:45 -05:00
2009-05-11 20:17:53 -04:00
public static HistorySpooler getInstance ( ) {
return instance ;
}
2013-11-17 14:05:45 -05:00
2014-10-09 13:30:43 -04:00
static {
2016-09-24 07:51:03 -04:00
Runtime . getRuntime ( ) . addShutdownHook ( new Thread ( HistorySpooler . getInstance ( ) : : commit , " HistorySpoolerShutdownHook " ) ) ; // commit session history on shutdown
2014-10-09 13:30:43 -04:00
}
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
2017-03-10 15:37:52 -05:00
private int sessionHistoryTotalSize = 0 ;
2013-03-26 04:43:02 -04:00
private int persistentHistoryTotalSize = - 1 ;
2013-03-30 12:46:25 -04:00
private boolean persistentHistoryEnabled = true ;
2013-11-17 14:05:45 -05:00
2016-09-24 07:51:03 -04:00
private final History sessionHistory = new History ( ) ;
2013-11-17 14:05:45 -05:00
2013-03-26 04:43:02 -04:00
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 ( ) ) ;
2013-03-26 04:43:02 -04:00
}
2013-11-17 14:05:45 -05:00
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 ;
}
2009-05-11 20:17:53 -04:00
}
}
2013-11-17 14:05:45 -05:00
2013-03-26 04:43:02 -04:00
public synchronized void commit ( ) {
2016-09-24 07:51:03 -04:00
if ( sessionHistory . sequences ( ) . isEmpty ( ) | | ! persistentHistoryEnabled ) {
2013-03-26 04:43:02 -04:00
return ;
}
2013-11-17 14:05:45 -05:00
2013-03-26 04:43:02 -04:00
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
}
2013-11-17 14:05:45 -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 ( ) ;
persistentHistoryTotalSize = history . totalSize ( ) ;
}
2013-03-26 04:43:02 -04:00
}
} catch ( Exception e ) {
2016-09-24 07:51:03 -04:00
debug . log ( Level . SEVERE , " Failed to write history file " , e ) ;
2013-03-26 04:43:02 -04:00
}
2012-07-09 06:50:18 -04:00
}
2013-11-17 14:05:45 -05:00
2014-04-19 12:54:25 -04:00
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 ) {
2009-07-20 07:03:24 -04:00
List < Element > sequence = new ArrayList < Element > ( ) ;
2013-11-17 14:05:45 -05:00
2011-10-10 12:21:54 -04:00
for ( Entry < File , File > element : elements ) {
2017-04-05 04:15:34 -04:00
File k = element . getKey ( ) ;
File v = element . getValue ( ) ;
if ( k ! = null & & v ! = null ) {
sequence . add ( new Element ( k . getName ( ) , v . getPath ( ) , k . getParentFile ( ) ) ) ;
}
2009-07-20 07:03:24 -04:00
}
2013-11-17 14:05:45 -05:00
2011-09-09 10:50:01 -04:00
if ( sequence . size ( ) > 0 ) {
2016-09-24 07:51:03 -04:00
sessionHistory . add ( sequence ) ; // append to session history
2017-03-10 15:37:52 -05:00
sessionHistoryTotalSize + = sequence . size ( ) ;
2011-09-09 10:50:01 -04:00
}
2009-05-11 20:17:53 -04:00
}
2013-11-17 14:05:45 -05:00
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 ( ) {
2017-03-10 15:37:52 -05:00
return sessionHistoryTotalSize ;
2013-02-25 12:27:34 -05:00
}
2013-11-17 14:05:45 -05:00
2016-09-24 07:51:03 -04:00
public synchronized int getPersistentHistoryTotalSize ( ) {
2013-03-26 04:43:02 -04:00
return persistentHistoryTotalSize ;
2009-05-11 20:17:53 -04:00
}
2013-11-17 14:05:45 -05:00
2016-09-24 07:51:03 -04:00
public synchronized void setPersistentHistoryEnabled ( boolean persistentHistoryEnabled ) {
2013-03-30 12:46:25 -04:00
this . persistentHistoryEnabled = persistentHistoryEnabled ;
}
2013-11-17 14:05:45 -05:00
2009-05-11 20:17:53 -04:00
}