Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add interface to register listeners for file system events. #16669

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public void sort(final Comparator<? super Host> c) {
}

protected void save(final Host bookmark) {
this.lock();
try {
if(!folder.exists()) {
new DefaultLocalDirectoryFeature().mkdir(folder);
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@

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);

private final Preferences preferences = PreferencesFactory.get();
private final FileWatcher monitor = new FileWatcher(WatchServiceFactory.get());

private final Set<HostFileListener> listeners = new CopyOnWriteArraySet<>();

public MonitorFolderHostCollection(final Local f) {
super(f);
}
Expand Down Expand Up @@ -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);
}
}
}
}
Expand All @@ -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);
}
}
Loading