Top Description Fields Constructors Methods
org.jabref.gui.autosaveandbackup

public Class BackupManager

extends Object
Class Inheritance
Imports
java.io.IOException, .Writer, java.nio.charset.Charset, .StandardCharsets, java.nio.file.Files, .Path, .StandardCopyOption, java.nio.file.attribute.FileTime, java.util.HashSet, .List, .Optional, .Queue, .Set, java.util.concurrent.BlockingQueue, .ExecutorService, .LinkedBlockingQueue, .ScheduledThreadPoolExecutor, .TimeUnit, javafx.scene.control.TableColumn, org.jabref.gui.LibraryTab, org.jabref.gui.maintable.BibEntryTableViewModel, org.jabref.gui.maintable.columns.MainTableColumn, org.jabref.logic.bibtex.InvalidFieldValueException, org.jabref.logic.exporter.AtomicFileWriter, .BibWriter, .BibtexDatabaseWriter, .SelfContainedSaveConfiguration, org.jabref.logic.util.BackupFileType, .CoarseChangeFilter, org.jabref.logic.util.io.BackupFileUtil, org.jabref.model.database.BibDatabase, .BibDatabaseContext, org.jabref.model.database.event.BibDatabaseContextChangedEvent, org.jabref.model.entry.BibEntry, .BibEntryTypesManager, org.jabref.model.metadata.SaveOrder, .SelfContainedSaveOrder, org.jabref.preferences.PreferencesService, com.google.common.eventbus.Subscribe, org.slf4j.Logger, .LoggerFactory

Backups the given bib database file from BibDatabaseContext on every BibDatabaseContextChangedEvent. An intelligent ExecutorService with a BlockingQueue prevents a high load while making backups and rejects all redundant backup tasks. This class does not manage the .bak file which is created when opening a database.

Field Summary

Modifier and TypeField and Description
private final Queue<Path>
private final BibDatabaseContext
private final CoarseChangeFilter
private static final int
private final BibEntryTypesManager
private final ScheduledThreadPoolExecutor
private final LibraryTab
private static final Logger
private static final int
private boolean
private final PreferencesService
private static Set<BackupManager>

Constructor Summary

AccessConstructor and Description
pack-priv
BackupManager(LibraryTab libraryTab, BibDatabaseContext bibDatabaseContext, BibEntryTypesManager entryTypesManager, PreferencesService preferences)

Method Summary

Modifier and TypeMethod and Description
public static boolean

Returns:

true if backup file exists AND differs from originalPath. false is the "default" return value in the good case. In case a discarded file exists, false is returned, too. In the case of an exception true is returned to ensure that the user checks the output.
backupFileDiffers
(Path
Path to the file a backup should be checked for. Example: jabref.bib.
originalPath
,
Path backupDir)

Checks whether a backup file exists for the given database file.

pack-priv Optional<Path>
private static Path
determineDiscardedFile(Path file, Path backupDir)

public static void
discardBackup(BibDatabaseContext bibDatabaseContext, Path backupDir)

Marks the backup as discarded at the library which is associated with the given BibDatabaseContext.

public void
discardBackup(Path backupDir)

Marks the backups as discarded.

private void
fillQueue(Path backupDir)

pack-priv static Path
getBackupPathForNewBackup(Path originalPath, Path backupDir)

Determines the most recent backup file name

pack-priv static Optional<Path>
getLatestBackupPath(Path originalPath, Path backupDir)

Determines the most recent existing backup file name

public synchronized void
private void
pack-priv void
performBackup(Path
the full path to the file where the library should be backed up to
backupPath
)

This method is called as soon as the scheduler says: "Do the backup" SIDE EFFECT: Deletes oldest backup file

public static void
restoreBackup(Path
Path to the file which should be equalized to the backup file.
originalPath
,
Path backupDir)

Restores the backup file by copying and overwriting the original one.

public static void
shutdown(BibDatabaseContext bibDatabaseContext, Path
The path to the backup directory
backupDir
,
boolean
True, if a backup should be created
createBackup
)

Shuts down the BackupManager which is associated with the given BibDatabaseContext.

private void
shutdown(Path
The backup directory
backupDir
,
boolean
If the backup manager should still perform a backup
createBackup
)

Unregisters the BackupManager from the eventBus of BibDatabaseContext.

public static BackupManager
start(LibraryTab libraryTab, BibDatabaseContext bibDatabaseContext, BibEntryTypesManager entryTypesManager, PreferencesService preferences)

Starts the BackupManager which is associated with the given BibDatabaseContext.

private void
startBackupTask(Path backupDir)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

backupFilesQueueback to summary
private final Queue<Path> backupFilesQueue
bibDatabaseContextback to summary
private final BibDatabaseContext bibDatabaseContext
changeFilterback to summary
private final CoarseChangeFilter changeFilter
DELAY_BETWEEN_BACKUP_ATTEMPTS_IN_SECONDSback to summary
private static final int DELAY_BETWEEN_BACKUP_ATTEMPTS_IN_SECONDS
entryTypesManagerback to summary
private final BibEntryTypesManager entryTypesManager
executorback to summary
private final ScheduledThreadPoolExecutor executor
libraryTabback to summary
private final LibraryTab libraryTab
LOGGERback to summary
private static final Logger LOGGER
MAXIMUM_BACKUP_FILE_COUNTback to summary
private static final int MAXIMUM_BACKUP_FILE_COUNT
needsBackupback to summary
private boolean needsBackup
preferencesback to summary
private final PreferencesService preferences
runningInstancesback to summary
private static Set<BackupManager> runningInstances

Constructor Detail

BackupManagerback to summary
pack-priv BackupManager(LibraryTab libraryTab, BibDatabaseContext bibDatabaseContext, BibEntryTypesManager entryTypesManager, PreferencesService preferences)

Method Detail

backupFileDiffersback to summary
public static boolean backupFileDiffers(Path originalPath, Path backupDir)

Checks whether a backup file exists for the given database file. If it exists, it is checked whether it is newer and different from the original. In case a discarded file is present, the method also returns false, See also discardBackup(Path).

Parameters
originalPath:Path

Path to the file a backup should be checked for. Example: jabref.bib.

Returns:boolean

true if backup file exists AND differs from originalPath. false is the "default" return value in the good case. In case a discarded file exists, false is returned, too. In the case of an exception true is returned to ensure that the user checks the output.

determineBackupPathForNewBackupback to summary
pack-priv Optional<Path> determineBackupPathForNewBackup(Path backupDir)
determineDiscardedFileback to summary
private static Path determineDiscardedFile(Path file, Path backupDir)
discardBackupback to summary
public static void discardBackup(BibDatabaseContext bibDatabaseContext, Path backupDir)

Marks the backup as discarded at the library which is associated with the given BibDatabaseContext.

Parameters
bibDatabaseContext:BibDatabaseContext

Associated BibDatabaseContext

discardBackupback to summary
public void discardBackup(Path backupDir)

Marks the backups as discarded. We do not delete any files, because the user might want to recover old backup files. Therefore, we mark discarded backups by a --discarded file.

fillQueueback to summary
private void fillQueue(Path backupDir)
getBackupPathForNewBackupback to summary
pack-priv static Path getBackupPathForNewBackup(Path originalPath, Path backupDir)

Determines the most recent backup file name

getLatestBackupPathback to summary
pack-priv static Optional<Path> getLatestBackupPath(Path originalPath, Path backupDir)

Determines the most recent existing backup file name

listenback to summary
public synchronized void listen(BibDatabaseContextChangedEvent event)
Annotations
@Subscribe
logIfCriticalback to summary
private void logIfCritical(Path backupPath, IOException e)
performBackupback to summary
pack-priv void performBackup(Path backupPath)

This method is called as soon as the scheduler says: "Do the backup" SIDE EFFECT: Deletes oldest backup file

Parameters
backupPath:Path

the full path to the file where the library should be backed up to

restoreBackupback to summary
public static void restoreBackup(Path originalPath, Path backupDir)

Restores the backup file by copying and overwriting the original one.

Parameters
originalPath:Path

Path to the file which should be equalized to the backup file.

shutdownback to summary
public static void shutdown(BibDatabaseContext bibDatabaseContext, Path backupDir, boolean createBackup)

Shuts down the BackupManager which is associated with the given BibDatabaseContext.

Parameters
bibDatabaseContext:BibDatabaseContext

Associated BibDatabaseContext

backupDir:Path

The path to the backup directory

createBackup:boolean

True, if a backup should be created

shutdownback to summary
private void shutdown(Path backupDir, boolean createBackup)

Unregisters the BackupManager from the eventBus of BibDatabaseContext. This method should only be used when closing a database/JabRef in a normal way.

Parameters
backupDir:Path

The backup directory

createBackup:boolean

If the backup manager should still perform a backup

startback to summary
public static BackupManager start(LibraryTab libraryTab, BibDatabaseContext bibDatabaseContext, BibEntryTypesManager entryTypesManager, PreferencesService preferences)

Starts the BackupManager which is associated with the given BibDatabaseContext. As long as no database file is present in BibDatabaseContext, the BackupManager will do nothing. This method is not thread-safe. The caller has to ensure that this method is not called in parallel.

Parameters
bibDatabaseContext:BibDatabaseContext

Associated BibDatabaseContext

startBackupTaskback to summary
private void startBackupTask(Path backupDir)