1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
package org.springframework.boot.devtools.filewatch;
import java.io.File; import java.io.FileFilter; import java.time.Duration; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.atomic.AtomicInteger;
import org.springframework.util.Assert;
public class FileSystemWatcher {
private static final Duration DEFAULT_POLL_INTERVAL = Duration.ofMillis(1000);
private static final Duration DEFAULT_QUIET_PERIOD = Duration.ofMillis(400);
private final List<FileChangeListener> listeners = new ArrayList<>();
private final boolean daemon;
private final long pollInterval;
private final long quietPeriod;
private final SnapshotStateRepository snapshotStateRepository;
private final AtomicInteger remainingScans = new AtomicInteger(-1);
private final Map<File, DirectorySnapshot> directories = new HashMap<>();
private Thread watchThread;
private FileFilter triggerFilter;
private final Object monitor = new Object();
public FileSystemWatcher() { this(true, DEFAULT_POLL_INTERVAL, DEFAULT_QUIET_PERIOD); }
public FileSystemWatcher(boolean daemon, Duration pollInterval, Duration quietPeriod) { this(daemon, pollInterval, quietPeriod, null); }
public FileSystemWatcher(boolean daemon, Duration pollInterval, Duration quietPeriod, SnapshotStateRepository snapshotStateRepository) { Assert.notNull(pollInterval, "PollInterval must not be null"); Assert.notNull(quietPeriod, "QuietPeriod must not be null"); Assert.isTrue(pollInterval.toMillis() > 0, "PollInterval must be positive"); Assert.isTrue(quietPeriod.toMillis() > 0, "QuietPeriod must be positive"); Assert.isTrue(pollInterval.toMillis() > quietPeriod.toMillis(), "PollInterval must be greater than QuietPeriod"); this.daemon = daemon; this.pollInterval = pollInterval.toMillis(); this.quietPeriod = quietPeriod.toMillis(); this.snapshotStateRepository = (snapshotStateRepository != null) ? snapshotStateRepository : SnapshotStateRepository.NONE; }
public void addListener(FileChangeListener fileChangeListener) { Assert.notNull(fileChangeListener, "FileChangeListener must not be null"); synchronized (this.monitor) { checkNotStarted(); this.listeners.add(fileChangeListener); } }
public void addSourceDirectories(Iterable<File> directories) { Assert.notNull(directories, "Directories must not be null"); synchronized (this.monitor) { directories.forEach(this::addSourceDirectory); } }
public void addSourceDirectory(File directory) { Assert.notNull(directory, "Directory must not be null"); Assert.isTrue(!directory.isFile(), () -> "Directory '" + directory + "' must not be a file"); synchronized (this.monitor) { checkNotStarted(); this.directories.put(directory, null); } }
public void setTriggerFilter(FileFilter triggerFilter) { synchronized (this.monitor) { this.triggerFilter = triggerFilter; } }
private void checkNotStarted() { Assert.state(this.watchThread == null, "FileSystemWatcher already started"); }
public void start() { synchronized (this.monitor) { createOrRestoreInitialSnapshots(); if (this.watchThread == null) { Map<File, DirectorySnapshot> localDirectories = new HashMap<>(this.directories); Watcher watcher = new Watcher(this.remainingScans, new ArrayList<>(this.listeners), this.triggerFilter, this.pollInterval, this.quietPeriod, localDirectories, this.snapshotStateRepository); this.watchThread = new Thread(watcher); this.watchThread.setName("File Watcher"); this.watchThread.setDaemon(this.daemon); this.watchThread.start(); } } }
@SuppressWarnings("unchecked") private void createOrRestoreInitialSnapshots() { Map<File, DirectorySnapshot> restored = (Map<File, DirectorySnapshot>) this.snapshotStateRepository.restore(); this.directories.replaceAll((f, v) -> { DirectorySnapshot restoredSnapshot = (restored != null) ? restored.get(f) : null; return (restoredSnapshot != null) ? restoredSnapshot : new DirectorySnapshot(f); }); }
public void stop() { stopAfter(0); }
void stopAfter(int remainingScans) { Thread thread; synchronized (this.monitor) { thread = this.watchThread; if (thread != null) { this.remainingScans.set(remainingScans); if (remainingScans <= 0) { thread.interrupt(); } } this.watchThread = null; } if (thread != null && Thread.currentThread() != thread) { try { thread.join(); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } } }
private static final class Watcher implements Runnable {
private final AtomicInteger remainingScans;
private final List<FileChangeListener> listeners;
private final FileFilter triggerFilter;
private final long pollInterval;
private final long quietPeriod;
private Map<File, DirectorySnapshot> directories;
private final SnapshotStateRepository snapshotStateRepository;
private Watcher(AtomicInteger remainingScans, List<FileChangeListener> listeners, FileFilter triggerFilter, long pollInterval, long quietPeriod, Map<File, DirectorySnapshot> directories, SnapshotStateRepository snapshotStateRepository) { this.remainingScans = remainingScans; this.listeners = listeners; this.triggerFilter = triggerFilter; this.pollInterval = pollInterval; this.quietPeriod = quietPeriod; this.directories = directories; this.snapshotStateRepository = snapshotStateRepository;
}
@Override public void run() { int remainingScans = this.remainingScans.get(); while (remainingScans > 0 || remainingScans == -1) { try { if (remainingScans > 0) { this.remainingScans.decrementAndGet(); } scan(); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } remainingScans = this.remainingScans.get(); } }
private void scan() throws InterruptedException { Thread.sleep(this.pollInterval - this.quietPeriod); Map<File, DirectorySnapshot> previous; Map<File, DirectorySnapshot> current = this.directories; do { previous = current; current = getCurrentSnapshots(); Thread.sleep(this.quietPeriod); } while (isDifferent(previous, current)); if (isDifferent(this.directories, current)) { updateSnapshots(current.values()); } }
private boolean isDifferent(Map<File, DirectorySnapshot> previous, Map<File, DirectorySnapshot> current) { if (!previous.keySet().equals(current.keySet())) { return true; } for (Map.Entry<File, DirectorySnapshot> entry : previous.entrySet()) { DirectorySnapshot previousDirectory = entry.getValue(); DirectorySnapshot currentDirectory = current.get(entry.getKey()); if (!previousDirectory.equals(currentDirectory, this.triggerFilter)) { return true; } } return false; }
private Map<File, DirectorySnapshot> getCurrentSnapshots() { Map<File, DirectorySnapshot> snapshots = new LinkedHashMap<>(); for (File directory : this.directories.keySet()) { snapshots.put(directory, new DirectorySnapshot(directory)); } return snapshots; }
private void updateSnapshots(Collection<DirectorySnapshot> snapshots) { Map<File, DirectorySnapshot> updated = new LinkedHashMap<>(); Set<ChangedFiles> changeSet = new LinkedHashSet<>(); for (DirectorySnapshot snapshot : snapshots) { DirectorySnapshot previous = this.directories.get(snapshot.getDirectory()); updated.put(snapshot.getDirectory(), snapshot); ChangedFiles changedFiles = previous.getChangedFiles(snapshot, this.triggerFilter); if (!changedFiles.getFiles().isEmpty()) { changeSet.add(changedFiles); } } this.directories = updated; this.snapshotStateRepository.save(updated); if (!changeSet.isEmpty()) { fireListeners(Collections.unmodifiableSet(changeSet)); } }
private void fireListeners(Set<ChangedFiles> changeSet) { for (FileChangeListener listener : this.listeners) { listener.onChange(changeSet); } }
}
}
|