|
| 1 | +package org.jabref.collab; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.nio.file.Files; |
| 5 | +import java.nio.file.Path; |
| 6 | +import java.util.Optional; |
| 7 | + |
| 8 | +import javax.swing.SwingUtilities; |
| 9 | + |
| 10 | +import org.jabref.JabRefExecutorService; |
| 11 | +import org.jabref.gui.BasePanel; |
| 12 | +import org.jabref.gui.SidePaneManager; |
| 13 | +import org.jabref.gui.util.FileUpdateListener; |
| 14 | +import org.jabref.gui.util.FileUpdateMonitor; |
| 15 | +import org.jabref.logic.util.io.FileBasedLock; |
| 16 | +import org.jabref.logic.util.io.FileUtil; |
| 17 | +import org.jabref.model.database.BibDatabaseContext; |
| 18 | + |
| 19 | +import org.apache.commons.logging.Log; |
| 20 | +import org.apache.commons.logging.LogFactory; |
| 21 | + |
| 22 | +public class DatabaseChangeMonitor implements FileUpdateListener { |
| 23 | + private static final Log LOGGER = LogFactory.getLog(DatabaseChangeMonitor.class); |
| 24 | + |
| 25 | + private final BibDatabaseContext database; |
| 26 | + private final FileUpdateMonitor fileMonitor; |
| 27 | + private final BasePanel panel; |
| 28 | + private boolean updatedExternally; |
| 29 | + private Path tmpFile; |
| 30 | + private long timeStamp; |
| 31 | + private long fileSize; |
| 32 | + |
| 33 | + public DatabaseChangeMonitor(BibDatabaseContext database, FileUpdateMonitor fileMonitor, BasePanel panel) { |
| 34 | + this.database = database; |
| 35 | + this.fileMonitor = fileMonitor; |
| 36 | + this.panel = panel; |
| 37 | + |
| 38 | + this.database.getDatabasePath().ifPresent(path -> { |
| 39 | + try { |
| 40 | + fileMonitor.addListenerForFile(path, this); |
| 41 | + timeStamp = Files.getLastModifiedTime(path).toMillis(); |
| 42 | + fileSize = Files.size(path); |
| 43 | + tmpFile = Files.createTempFile("jabref", ".bib"); |
| 44 | + tmpFile.toFile().deleteOnExit(); |
| 45 | + copyToTemp(path); |
| 46 | + } catch (IOException e) { |
| 47 | + LOGGER.error("Error while trying to monitor " + path, e); |
| 48 | + } |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void fileUpdated() { |
| 54 | + if (panel.isSaving()) { |
| 55 | + // We are just saving the file, so this message is most likely due to bad timing. |
| 56 | + // If not, we'll handle it on the next polling. |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + updatedExternally = true; |
| 61 | + |
| 62 | + final ChangeScanner scanner = new ChangeScanner(panel.frame(), panel, database.getDatabaseFile().orElse(null), tmpFile); |
| 63 | + |
| 64 | + // Test: running scan automatically in background |
| 65 | + if (database.getDatabasePath().isPresent() && !FileBasedLock.waitForFileLock(database.getDatabasePath().get())) { |
| 66 | + // The file is locked even after the maximum wait. Do nothing. |
| 67 | + LOGGER.error("File updated externally, but change scan failed because the file is locked."); |
| 68 | + |
| 69 | + // Wait a bit and then try again |
| 70 | + try { |
| 71 | + Thread.sleep(1000); |
| 72 | + } catch (InterruptedException e) { |
| 73 | + // Nothing to do |
| 74 | + } |
| 75 | + fileUpdated(); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + JabRefExecutorService.INSTANCE.executeInterruptableTaskAndWait(scanner); |
| 80 | + |
| 81 | + // Adding the sidepane component is Swing work, so we must do this in the Swing |
| 82 | + // thread: |
| 83 | + Runnable t = () -> { |
| 84 | + |
| 85 | + // Check if there is already a notification about external |
| 86 | + // changes: |
| 87 | + SidePaneManager sidePaneManager = panel.getSidePaneManager(); |
| 88 | + boolean hasAlready = sidePaneManager.hasComponent(FileUpdatePanel.class); |
| 89 | + if (hasAlready) { |
| 90 | + sidePaneManager.hideComponent(FileUpdatePanel.class); |
| 91 | + sidePaneManager.unregisterComponent(FileUpdatePanel.class); |
| 92 | + } |
| 93 | + FileUpdatePanel pan = new FileUpdatePanel(panel, sidePaneManager, |
| 94 | + database.getDatabaseFile().orElse(null), scanner); |
| 95 | + sidePaneManager.register(pan); |
| 96 | + sidePaneManager.show(FileUpdatePanel.class); |
| 97 | + }; |
| 98 | + |
| 99 | + if (scanner.changesFound()) { |
| 100 | + SwingUtilities.invokeLater(t); |
| 101 | + } else { |
| 102 | + updatedExternally = false; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Forces a check on the file, and returns the result. Check if time stamp or the file size has changed. |
| 108 | + * |
| 109 | + * @return boolean true if the file has changed. |
| 110 | + */ |
| 111 | + private boolean hasBeenModified() { |
| 112 | + Optional<Path> file = database.getDatabasePath(); |
| 113 | + if (file.isPresent()) { |
| 114 | + try { |
| 115 | + long modified = Files.getLastModifiedTime(file.get()).toMillis(); |
| 116 | + if (modified == 0L) { |
| 117 | + // File deleted |
| 118 | + return false; |
| 119 | + } |
| 120 | + long fileSizeNow = Files.size(file.get()); |
| 121 | + return (timeStamp != modified) || (fileSize != fileSizeNow); |
| 122 | + } catch (IOException ex) { |
| 123 | + return false; |
| 124 | + } |
| 125 | + } |
| 126 | + return false; |
| 127 | + } |
| 128 | + |
| 129 | + public void unregister() { |
| 130 | + database.getDatabasePath().ifPresent(file -> fileMonitor.removeListener(file, this)); |
| 131 | + } |
| 132 | + |
| 133 | + public boolean hasBeenModifiedExternally() { |
| 134 | + return updatedExternally || hasBeenModified(); |
| 135 | + } |
| 136 | + |
| 137 | + public void markExternalChangesAsResolved() { |
| 138 | + updatedExternally = false; |
| 139 | + } |
| 140 | + |
| 141 | + public void markAsSaved() { |
| 142 | + database.getDatabasePath().ifPresent(file -> { |
| 143 | + try { |
| 144 | + timeStamp = Files.getLastModifiedTime(file).toMillis(); |
| 145 | + fileSize = Files.size(file); |
| 146 | + |
| 147 | + copyToTemp(file); |
| 148 | + } catch (IOException ex) { |
| 149 | + LOGGER.error("Error while getting file information", ex); |
| 150 | + } |
| 151 | + }); |
| 152 | + } |
| 153 | + |
| 154 | + private void copyToTemp(Path file) { |
| 155 | + FileUtil.copyFile(file, tmpFile, true); |
| 156 | + } |
| 157 | + |
| 158 | + public Path getTempFile() { |
| 159 | + return tmpFile; |
| 160 | + } |
| 161 | +} |
0 commit comments