From 03981502cfae7c15b5f7cd507e0a1e23c73efe6d Mon Sep 17 00:00:00 2001 From: David Kocher Date: Fri, 13 Dec 2024 16:05:13 +0100 Subject: [PATCH 1/2] Allow to register for serialization events. --- .../core/MonitorFolderHostCollection.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/core/src/main/java/ch/cyberduck/core/MonitorFolderHostCollection.java b/core/src/main/java/ch/cyberduck/core/MonitorFolderHostCollection.java index 4812aad587d..f8fded7cdab 100644 --- a/core/src/main/java/ch/cyberduck/core/MonitorFolderHostCollection.java +++ b/core/src/main/java/ch/cyberduck/core/MonitorFolderHostCollection.java @@ -30,6 +30,8 @@ import java.io.IOException; import java.util.Comparator; +import java.util.Set; +import java.util.concurrent.CopyOnWriteArraySet; public class MonitorFolderHostCollection extends AbstractFolderHostCollection implements FileWatcherListener { private static final Logger log = LogManager.getLogger(MonitorFolderHostCollection.class); @@ -37,6 +39,8 @@ public class MonitorFolderHostCollection extends AbstractFolderHostCollection im private final Preferences preferences = PreferencesFactory.get(); private final FileWatcher monitor = new FileWatcher(WatchServiceFactory.get()); + private final Set listeners = new CopyOnWriteArraySet<>(); + public MonitorFolderHostCollection(final Local f) { super(f); } @@ -110,6 +114,9 @@ public void fileDeleted(final Local file) { if(bookmark != null) { log.warn("Delete bookmark {}", bookmark); this.remove(bookmark); + for(HostFileListener listener : listeners) { + listener.fileDeleted(bookmark); + } } } } @@ -124,10 +131,27 @@ public void fileCreated(final Local file) { final Host bookmark = HostReaderFactory.get().read(file); log.warn("Add bookmark {}", bookmark); this.add(bookmark); + for(HostFileListener listener : listeners) { + listener.fileCreated(bookmark); + } } catch(AccessDeniedException e) { log.warn("Failure reading file {}", file); } } } + + public void addListener(final HostFileListener listener) { + listeners.add(listener); + } + + public void removeListener(final HostFileListener listener) { + listeners.remove(listener); + } + + public interface HostFileListener { + void fileDeleted(Host host); + + void fileCreated(Host host); + } } From 8f3b92e71a7497af77fe3edb6c7897fd73925772 Mon Sep 17 00:00:00 2001 From: David Kocher Date: Fri, 13 Dec 2024 16:25:32 +0100 Subject: [PATCH 2/2] Lock collection when saving bookmark. --- .../java/ch/cyberduck/core/AbstractFolderHostCollection.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/src/main/java/ch/cyberduck/core/AbstractFolderHostCollection.java b/core/src/main/java/ch/cyberduck/core/AbstractFolderHostCollection.java index 729f0560d19..0821de44833 100644 --- a/core/src/main/java/ch/cyberduck/core/AbstractFolderHostCollection.java +++ b/core/src/main/java/ch/cyberduck/core/AbstractFolderHostCollection.java @@ -89,6 +89,7 @@ public void sort(final Comparator c) { } protected void save(final Host bookmark) { + this.lock(); try { if(!folder.exists()) { new DefaultLocalDirectoryFeature().mkdir(folder); @@ -100,6 +101,9 @@ protected void save(final Host bookmark) { catch(AccessDeniedException e) { log.warn("Failure saving item in collection {}", e.getMessage()); } + finally { + this.unlock(); + } } @Override