View Javadoc
1   /**
2    * This file Copyright (c) 2014-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.dirwatch;
35  
36  import static java.nio.file.LinkOption.NOFOLLOW_LINKS;
37  import static java.nio.file.StandardWatchEventKinds.*;
38  
39  import info.magnolia.cms.util.ExceptionUtil;
40  import info.magnolia.init.MagnoliaConfigurationProperties;
41  
42  import java.io.IOException;
43  import java.nio.file.FileSystems;
44  import java.nio.file.FileVisitOption;
45  import java.nio.file.FileVisitResult;
46  import java.nio.file.Files;
47  import java.nio.file.LinkOption;
48  import java.nio.file.Path;
49  import java.nio.file.SimpleFileVisitor;
50  import java.nio.file.WatchEvent;
51  import java.nio.file.WatchKey;
52  import java.nio.file.WatchService;
53  import java.nio.file.attribute.BasicFileAttributes;
54  import java.util.ArrayList;
55  import java.util.Collections;
56  import java.util.HashMap;
57  import java.util.List;
58  import java.util.Map;
59  import java.util.function.Predicate;
60  
61  import org.apache.commons.lang3.EnumUtils;
62  import org.apache.commons.lang3.StringUtils;
63  import org.slf4j.Logger;
64  import org.slf4j.LoggerFactory;
65  
66  /**
67   * The {@link DirectoryWatcher} covers implementation of the low level WatchService API.
68   * <p>
69   * Highly inspired by http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/essential/io/examples/WatchDir.java
70   * Since java.nio.file.Watch* is not guaranteed to be using native events, we'll probably want to extract an interface for this. (non-native implementations use polling and can be quite slow)
71   * JDK on OSX doesn't have a native impl for 7 or 8: https://bugs.openjdk.java.net/browse/JDK-7133447
72   * See http://wiki.netbeans.org/NativeFileNotifications
73   * https://code.google.com/p/barbarywatchservice/
74   */
75  public class DirectoryWatcher implements Runnable {
76  
77      private static final Logger log = LoggerFactory.getLogger(DirectoryWatcher.class);
78  
79      private static final String WATCHER_SENSITIVITY = "magnolia.resources.watcher.sensitivity";
80      private static final String DEFAULT_WATCHER_SENSITIVITY_VALUE = "high";
81  
82      private final WatchService watcher;
83      private final Map<WatchKey, Path> keys = new HashMap<>();
84  
85      private final boolean recursive;
86      private final boolean followSymLinks;
87      private final List<WatcherRegistration> registrations = Collections.synchronizedList(new ArrayList<>());
88      private final boolean devMode;
89      private final String watcherSensitivity;
90  
91      public DirectoryWatcher(boolean recursive, boolean followLinks, MagnoliaConfigurationProperties properties) throws IOException {
92          this.recursive = recursive;
93          this.followSymLinks = followLinks;
94          this.watcher = FileSystems.getDefault().newWatchService();
95          this.devMode = properties.getBooleanProperty("magnolia.develop");
96          this.watcherSensitivity = getWatcherSensitivity(properties.getProperty(WATCHER_SENSITIVITY), DEFAULT_WATCHER_SENSITIVITY_VALUE);
97      }
98  
99      public void register(Path dir, Predicate<Path> filterPredicate, WatcherCallback callback) throws IOException {
100         if (recursive) {
101             log.debug("Scanning {}", dir);
102             registerRecursively(dir, filterPredicate);
103             log.debug("Done scanning {}", dir);
104         } else {
105             registerDirectory(dir);
106         }
107         registrations.add(new WatcherRegistration(dir, filterPredicate, callback));
108     }
109 
110     /**
111      * Register the given directory with the WatchService.
112      */
113     protected void registerDirectory(Path dir) throws IOException {
114         final WatchKey key = dir.register(watcher, new WatchEvent.Kind[]{ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY}, getWatchEventModifiers(watcher));
115         // Would have used ExtendedWatchEventModifier.FILE_TREE, but it's not supported on osx nor linux;
116         // AFAIK, there is no way to query the WatchService to see if it supports a given modifier without passing it and getting an UnsupportedOperationException thrown at us, so...
117         // Presumably, this is why we do recursion ourselves in here
118 
119         if (log.isDebugEnabled()) {
120             Path prev = keys.get(key);
121             if (prev == null) {
122                 log.debug("* register: {}", dir);
123             } else {
124                 log.debug("* update: {} -> {}", prev, dir);
125             }
126         }
127         keys.put(key, dir);
128     }
129 
130     protected WatchEvent.Modifier[] getWatchEventModifiers(WatchService watchService) {
131         // Improve watcher performance on OSX by using com.sun.nio.file.SensitivityWatchEventModifier if available.
132         try {
133             // If the WatchService is a PollingWatchService, which it is on OS X, AIX and Solaris prior to version 11
134             Class<?> pollingWatchService = Class.forName("sun.nio.fs.PollingWatchService");
135             if (pollingWatchService.isInstance(watchService)) {
136                 // Try to find enum value of SensitivityWatchEventModifier in magnolia.properties (high, medium, low)
137                 // Use Class.forName to avoid importing sun package for the SensitivityWatchEventModifier
138                 Class clazz = Class.forName("com.sun.nio.file.SensitivityWatchEventModifier");
139                 // Get enum value of the modifier or if the value is wrong set it to high sensitivity
140                 WatchEvent.Modifier sensitivityModifier = (WatchEvent.Modifier) EnumUtils.getEnum(clazz, StringUtils.upperCase(watcherSensitivity));
141                 if (sensitivityModifier == null) {
142                     sensitivityModifier = (WatchEvent.Modifier) EnumUtils.getEnum(clazz, "HIGH");
143                 }
144                 return new WatchEvent.Modifier[] { sensitivityModifier };
145             }
146         } catch (ClassNotFoundException ignored) {
147             // This is expected on JVMs where PollingWatchService or SensitivityWatchEventModifier are not available
148         }
149         return new WatchEvent.Modifier[0];
150     }
151 
152     /**
153      * Register the given directory, and all its sub-directories, with the WatchService.
154      */
155     protected void registerRecursively(final Path start, final Predicate<Path> filterPredicate) throws IOException {
156 
157         // register directory and sub-directories
158         Files.walkFileTree(start, this.followSymLinks ? Collections.singleton(FileVisitOption.FOLLOW_LINKS) : Collections.emptySet(), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {
159 
160             @Override
161             public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
162                 if (!filterPredicate.test(dir)) {
163                     return FileVisitResult.SKIP_SUBTREE;
164                 }
165 
166                 registerDirectory(dir);
167                 return FileVisitResult.CONTINUE;
168             }
169 
170             @Override
171             public FileVisitResult visitFileFailed(Path file, IOException e) throws IOException {
172                 log.warn("Visiting failed for {}", file);
173                 return FileVisitResult.SKIP_SUBTREE;
174             }
175         });
176     }
177 
178     @Override
179     public void run() {
180         try {
181             while (!Thread.currentThread().isInterrupted()) {
182 
183                 // 1. wait for key to be signaled
184                 final WatchKey key;
185                 try {
186                     key = watcher.take();
187                 } catch (InterruptedException x) {
188                     Thread.currentThread().interrupt();
189                     return;
190                 }
191 
192                 final Path dir = keys.get(key);
193                 if (dir == null) {
194                     log.debug("WatchKey not recognized!!");
195                     continue;
196                 }
197 
198                 // 2. retrieve each pending event for the key
199                 final List<WatchEvent<?>> watchEvents = key.pollEvents();
200                 log.debug("   {} events", watchEvents.size());
201 
202                 for (WatchEvent<?> event : watchEvents) {
203                     final WatchEvent.Kind<?> kind = event.kind();
204                     log.debug("event: {}", event);
205                     log.debug("kind: {}", kind);
206 
207                     // TODO OVERFLOW indicates that events may been lost or discarded.
208                     if (kind == OVERFLOW) {
209                         log.warn("WatchEvent OVERFLOWN. Some file system events are lost and won't be processed. Check your 'fs.inotify.max_user_watches' and 'fs.inotify.max_user_instances' system  parameter values and consider to raise them.");
210                         continue;
211                     }
212 
213                     final Path completePath = getCompletePath(dir, event);
214                     log.debug("--> {} : {}: {}", Thread.currentThread(), event.kind().name(), completePath);
215 
216                     // 2a. register new directories
217                     if (Files.isDirectory(completePath, this.followSymLinks ? new LinkOption[0] : new LinkOption[] {NOFOLLOW_LINKS})) {
218                         if (recursive && (kind == ENTRY_CREATE || kind == ENTRY_MODIFY)) {
219                             // MAGNOLIA-6944 multiple threads may try to iterate and modify the collection
220                             // https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#synchronizedList-java.util.List-
221                             synchronized (registrations) {
222                                 // Loop over registrations to re-register accordingly
223                                 try {
224                                     for (WatcherRegistration registration : registrations) {
225                                         if (!completePath.startsWith(registration.getRootPath())) {
226                                             continue;
227                                         }
228                                         Predicate<Path> filterPredicate = registration.getFilterPredicate();
229 
230                                         if (!filterPredicate.test(dir)) {
231                                             continue;
232                                         }
233 
234                                         registerRecursively(completePath, filterPredicate);
235                                     }
236                                 } catch (IOException x) {
237                                     log.warn("Unable to register all subdirectories because of the following exception:", x);
238                                 }
239                             }
240                         }
241                         // TODO shouldn't we unregister deleted directories ?
242                         log.debug(" --> watch keys {}", keys.values());
243                     }
244 
245                     // 2b. process event
246                     try {
247                         processEvent(kind, completePath, event);
248                     } catch (Throwable t) {
249                         log.error("Exception when executing callback for {}: {}", event.context(), ExceptionUtil.exceptionToWords(t), t);
250                         // if we throw here we kill the thread
251                     }
252                 }
253 
254                 // 3. reset key and remove from set if directory no longer accessible
255                 boolean valid = key.reset();
256                 if (!valid) {
257                     keys.remove(key);
258 
259                     // all directories are inaccessible
260                     if (keys.isEmpty()) {
261                         break;
262                     }
263                 }
264             }
265 
266         } catch (Throwable t) {
267             log.error("Exception occurred in DirectoryWatcher: {}", t, t);
268             throw t; // This kills the thread
269         }
270     }
271 
272     /**
273      * Process given pending {@link WatchEvent} as needed.
274      *
275      * @param kind the event {@link WatchEvent.Kind kind}
276      * @param completePath an absolute {@link Path} where the event occurred
277      * @param watchEvent the raw event for further custom processing
278      */
279     protected void processEvent(final WatchEvent.Kind<?> kind, final Path completePath, final WatchEvent<?> watchEvent) {
280         logEvent(kind, completePath);
281 
282         if (kind != ENTRY_CREATE && kind != ENTRY_MODIFY && kind != ENTRY_DELETE) {
283             throw new RuntimeException("Unknown event type " + kind + " for " + completePath.toAbsolutePath().toString());
284         }
285         // MAGNOLIA-6944 multiple threads may try to iterate and modify the collection
286         // https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#synchronizedList-java.util.List-
287         synchronized (registrations) {
288             for (WatcherRegistration registration : registrations) {
289                 if (!completePath.startsWith(registration.getRootPath())) {
290                     continue;
291                 }
292 
293                 if (!registration.getFilterPredicate().test(completePath)) {
294                     continue;
295                 }
296 
297                 WatcherCallback callback = registration.getCallback();
298                 if (kind == ENTRY_CREATE) {
299                     callback.added(completePath);
300                 } else if (kind == ENTRY_DELETE) {
301                     callback.removed(completePath);
302                 } else if (kind == ENTRY_MODIFY) {
303                     callback.modified(completePath);
304                 }
305             }
306         }
307     }
308 
309     /**
310      * Logs file system events, the severity of log statements depends on the state of {@link #devMode dev mode flag}:
311      * <ul>
312      * <li>if it is on - severity level is {@code INFO};</li>
313      * <li>otherwise - severity level is {@code DEBUG}.</li>
314      * </ul>
315      */
316     private void logEvent(WatchEvent.Kind<?> kind, Path completePath) {
317         String message;
318         final String resourceType;
319         if (!Files.exists(completePath)) {
320             resourceType = "File resource(s)";
321         } else {
322             resourceType = Files.isDirectory(completePath) ? "Directory" : "File resource";
323         }
324 
325         if (kind == ENTRY_CREATE) {
326             message = "{} added at [{}]";
327         } else if (kind == ENTRY_DELETE) {
328             message = "{} deleted at [{}]";
329         } else if (kind == ENTRY_MODIFY) {
330             message = "{} modified at [{}]";
331         } else {
332             message = String.format("{} event of unhandled type (%s) occurred at [{}]", kind);
333         }
334 
335         if (devMode) {
336             log.info(message, resourceType, completePath);
337         } else {
338             log.debug(message, resourceType, completePath);
339         }
340     }
341 
342     /**
343      * WatchEvent.context() returns a Path relative to the watchKey it was created for; we reconstruct it here.
344      */
345     private Path getCompletePath(Path parent, WatchEvent<?> watchEvent) {
346         // Theoretically, there could be events with other types than Path. Practically, not so sure.
347         final Path path = cast(watchEvent).context();
348         return parent.resolve(path);
349     }
350 
351     private String getWatcherSensitivity(String property, String defaultValue) {
352         if (StringUtils.isBlank(property)) {
353             return defaultValue;
354         }
355         if (StringUtils.equalsIgnoreCase(property, "high") || StringUtils.equalsIgnoreCase(property, "medium") || StringUtils.equalsIgnoreCase(property, "low")) {
356             return property;
357         }
358         return defaultValue;
359     }
360 
361     @SuppressWarnings("unchecked")
362     private WatchEvent<Path> cast(WatchEvent<?> event) {
363         return (WatchEvent<Path>) event;
364     }
365 
366     private static class WatcherRegistration {
367 
368         private final Path rootPath;
369         private final Predicate<Path> filterPredicate;
370         private final WatcherCallback callback;
371 
372         public WatcherRegistration(Path rootPath, Predicate<Path> filterPredicate, WatcherCallback callback) {
373             this.rootPath = rootPath;
374             this.filterPredicate = filterPredicate;
375             this.callback = callback;
376         }
377 
378         public Path getRootPath() {
379             return rootPath;
380         }
381 
382         public Predicate<Path> getFilterPredicate() {
383             return filterPredicate;
384         }
385 
386         public WatcherCallback getCallback() {
387             return callback;
388         }
389     }
390 }