/** * Register new resource watcher that will be checked in default {@link Frequency#MEDIUM MEDIUM} frequency */ public <W extends ResourceWatcher> WatcherHandle<W> add(W watcher) throws IOException { return add(watcher, Frequency.MEDIUM); }
private FileObserver createChild(Path file, boolean initial) throws IOException { FileObserver child = new FileObserver(file); child.init(initial); return child; }
@Override public void init() throws IOException { if (!initialized) { doInit(); initialized = true; } }
private void onDirectoryCreated(boolean initial) throws IOException { for (FileChangesListener listener : listeners()) { try { if (initial) { listener.onDirectoryInit(file); } else { listener.onDirectoryCreated(file); } } catch (Exception e) { logger.warn("cannot notify file changes listener", e); } } children = listChildren(initial); }
private void onDirectoryDeleted() { // First delete all children for (int child = 0; child < children.length; child++) { deleteChild(child); } for (FileChangesListener listener : listeners()) { try { listener.onDirectoryDeleted(file); } catch (Exception e) { logger.warn("cannot notify file changes listener", e); } } }
private void onFileCreated(boolean initial) { for (FileChangesListener listener : listeners()) { try { if (initial) { listener.onFileInit(file); } else { listener.onFileCreated(file); } } catch (Exception e) { logger.warn("cannot notify file changes listener", e); } } }
/** * Register new resource watcher that will be checked in the given frequency */ public <W extends ResourceWatcher> WatcherHandle<W> add(W watcher, Frequency frequency) throws IOException { watcher.init(); switch (frequency) { case LOW: return lowMonitor.add(watcher); case MEDIUM: return mediumMonitor.add(watcher); case HIGH: return highMonitor.add(watcher); default: throw new IllegalArgumentException("Unknown frequency [" + frequency + "]"); } }
private FileObserver[] listChildren(boolean initial) throws IOException { Path[] files = listFiles(); if (files != null && files.length > 0) { FileObserver[] children = new FileObserver[files.length]; for (int i = 0; i < files.length; i++) { children[i] = createChild(files[i], initial); } return children; } else { return EMPTY_DIRECTORY; } }
private void deleteChild(int child) { if (children[child].exists) { if (children[child].isDirectory) { children[child].onDirectoryDeleted(); } else { children[child].onFileDeleted(); } } }
@Override public void checkAndNotify() throws IOException { init(); doCheckAndNotify(); }
private void onFileChanged() { for (FileChangesListener listener : listeners()) { try { listener.onFileChanged(file); } catch (Exception e) { logger.warn("cannot notify file changes listener", e); } } }
private void onFileDeleted() { for (FileChangesListener listener : listeners()) { try { listener.onFileDeleted(file); } catch (Exception e) { logger.warn("cannot notify file changes listener", e); } } }
/** * Creates new file watcher on the given directory */ public FileWatcher(Path file) { this.file = file; rootFileObserver = new FileObserver(file); }
@Override protected void doCheckAndNotify() throws IOException { rootFileObserver.checkAndNotify(); }
public void notifyNow() { notifyNow(Frequency.MEDIUM); }
@Override public synchronized void run() { for(ResourceWatcher watcher : watchers) { try { watcher.checkAndNotify(); } catch (IOException e) { logger.trace("failed to check resource watcher", e); } } } }
private <W extends ResourceWatcher> WatcherHandle<W> add(W watcher) { watchers.add(watcher); return new WatcherHandle<>(this, watcher); }
@Inject public ResourceWatcherService(Settings settings, ThreadPool threadPool) { super(settings); this.enabled = ENABLED.get(settings); this.threadPool = threadPool; TimeValue interval = RELOAD_INTERVAL_LOW.get(settings); lowMonitor = new ResourceMonitor(interval, Frequency.LOW); interval = RELOAD_INTERVAL_MEDIUM.get(settings); mediumMonitor = new ResourceMonitor(interval, Frequency.MEDIUM); interval = RELOAD_INTERVAL_HIGH.get(settings); highMonitor = new ResourceMonitor(interval, Frequency.HIGH); }
/** * Clears any state with the FileWatcher, making all files show up as new */ public void clearState() { rootFileObserver = new FileObserver(file); try { rootFileObserver.init(false); } catch (IOException e) { // ignore IOException } }