|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * <p> |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * <p> |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.jooby.filewatcher; |
| 20 | + |
| 21 | +import com.sun.nio.file.SensitivityWatchEventModifier; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.nio.file.*; |
| 25 | +import java.nio.file.StandardWatchEventKinds; |
| 26 | +import java.nio.file.WatchEvent.Modifier; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Optional; |
| 31 | +import java.util.function.Function; |
| 32 | + |
| 33 | +import static java.nio.file.StandardWatchEventKinds.*; |
| 34 | +import static java.util.Objects.requireNonNull; |
| 35 | + |
| 36 | +/** |
| 37 | + * Allow to customize a file watch handler. You can listen for particular event kinds, apply a glob |
| 38 | + * filter, recursive listening for changes and set watcher priority or modifier. |
| 39 | + * <p> |
| 40 | + * Default filter watch recursively listen for all the available kinds using a <code>HIGH</code> |
| 41 | + * modifier. |
| 42 | + * <p> |
| 43 | + * This class can't be instantiated by client code. Intances of this class are provided via |
| 44 | + * configuration callbacks. See |
| 45 | + * {@link FileWatcher#register(Path, Class, java.util.function.Consumer)} |
| 46 | + * |
| 47 | + * @author edgar |
| 48 | + * @since 1.1.0 |
| 49 | + */ |
| 50 | +public class FileEventOptions { |
| 51 | + |
| 52 | + private List<WatchEvent.Kind<Path>> kinds = new ArrayList<>(); |
| 53 | + |
| 54 | + static final PathMatcher TRUE = new PathMatcher() { |
| 55 | + |
| 56 | + @Override |
| 57 | + public boolean matches(final Path path) { |
| 58 | + return true; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public String toString() { |
| 63 | + return "**/*"; |
| 64 | + } |
| 65 | + }; |
| 66 | + |
| 67 | + private static final WatchEvent.Kind<?>[] KINDS = {ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY}; |
| 68 | + |
| 69 | + private final List<PathMatcher> matchers = new ArrayList<>(); |
| 70 | + |
| 71 | + private Modifier modifier = SensitivityWatchEventModifier.HIGH; |
| 72 | + |
| 73 | + private boolean recursive = true; |
| 74 | + |
| 75 | + private final Class<? extends FileEventHandler> handler; |
| 76 | + |
| 77 | + private final Path path; |
| 78 | + |
| 79 | + private FileEventHandler handlerInstance; |
| 80 | + |
| 81 | + FileEventOptions(final Path path, final Class<? extends FileEventHandler> handler) |
| 82 | + throws IOException { |
| 83 | + if (!Files.exists(path)) { |
| 84 | + Files.createDirectories(path); |
| 85 | + } |
| 86 | + this.path = path; |
| 87 | + this.handler = handler; |
| 88 | + this.handlerInstance = null; |
| 89 | + } |
| 90 | + |
| 91 | + FileEventOptions(final Path path, final FileEventHandler handler) throws IOException { |
| 92 | + this(path, handler.getClass()); |
| 93 | + this.handlerInstance = handler; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Append a kind filter. |
| 98 | + * <p> |
| 99 | + * The default filter is: {@link StandardWatchEventKinds#ENTRY_CREATE}, |
| 100 | + * {@link StandardWatchEventKinds#ENTRY_DELETE} and {@link StandardWatchEventKinds#ENTRY_MODIFY}. |
| 101 | + * |
| 102 | + * @param kind Filter type. |
| 103 | + * @return This options. |
| 104 | + */ |
| 105 | + public FileEventOptions kind(final WatchEvent.Kind<Path> kind) { |
| 106 | + requireNonNull(kind, "WatchEvent.Kind required."); |
| 107 | + kinds.add(kind); |
| 108 | + return this; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Turn on/off watching of subfolder. |
| 113 | + * |
| 114 | + * @param recursive True to watch on subfolder. |
| 115 | + * @return This options. |
| 116 | + */ |
| 117 | + public FileEventOptions recursive(final boolean recursive) { |
| 118 | + this.recursive = recursive; |
| 119 | + return this; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Add a path filter using <code>glob</code> expression, like <code><pre>** / *.java</pre><code>, |
| 124 | + * etc... |
| 125 | + * |
| 126 | + * @param expression Glob expression. |
| 127 | + * @return This options. |
| 128 | + */ |
| 129 | + public FileEventOptions includes(final String expression) { |
| 130 | + requireNonNull(expression, "Glob expression required."); |
| 131 | + this.matchers.add(new GlobPathMatcher(expression)); |
| 132 | + return this; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Set a watch modifier. Default is: <code>HIGH</code>. |
| 137 | + * |
| 138 | + * @param modifier Watch modifier. |
| 139 | + * @return This options. |
| 140 | + */ |
| 141 | + public FileEventOptions modifier(final WatchEvent.Modifier modifier) { |
| 142 | + requireNonNull(modifier, "Modifier required."); |
| 143 | + this.modifier = modifier; |
| 144 | + return this; |
| 145 | + } |
| 146 | + |
| 147 | + Modifier modifier() { |
| 148 | + return modifier; |
| 149 | + } |
| 150 | + |
| 151 | + WatchEvent.Kind<?>[] kinds() { |
| 152 | + return kinds.size() == 0 ? KINDS : kinds.toArray(new WatchEvent.Kind[0]); |
| 153 | + } |
| 154 | + |
| 155 | + PathMatcher filter() { |
| 156 | + return matchers.size() == 0 ? TRUE : new FirstOfPathMatcher(matchers); |
| 157 | + } |
| 158 | + |
| 159 | + boolean recursive() { |
| 160 | + return recursive; |
| 161 | + } |
| 162 | + |
| 163 | + Path path() { |
| 164 | + return path; |
| 165 | + } |
| 166 | + |
| 167 | + FileEventHandler handler( |
| 168 | + final Function<Class<? extends FileEventHandler>, FileEventHandler> factory) { |
| 169 | + return Optional.ofNullable(handlerInstance).orElseGet(() -> factory.apply(handler)); |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public String toString() { |
| 174 | + return path + " {kinds: " + Arrays.toString(kinds()) + ", filter: " + filter() |
| 175 | + + ", recursive: " + recursive + ", modifier: " + modifier + "}"; |
| 176 | + } |
| 177 | +} |
0 commit comments