3434import java .io .Closeable ;
3535import java .io .IOException ;
3636import java .nio .file .FileSystems ;
37+ import java .nio .file .Path ;
3738import java .nio .file .WatchEvent ;
3839import java .nio .file .WatchKey ;
3940import java .nio .file .WatchService ;
41+ import java .nio .file .Watchable ;
4042import java .util .List ;
4143import java .util .Map ;
4244import java .util .concurrent .CompletableFuture ;
5355
5456import com .sun .nio .file .ExtendedWatchEventModifier ;
5557
58+ import engineering .swat .watch .impl .mac .MacWatchService ;
59+ import engineering .swat .watch .impl .mac .MacWatchable ;
5660import engineering .swat .watch .impl .util .SubscriptionKey ;
5761
5862/**
@@ -73,7 +77,7 @@ private JDKPoller() {}
7377
7478 static {
7579 try {
76- service = FileSystems . getDefault ().newWatchService ();
80+ service = Platform . get ().newWatchService ();
7781 } catch (IOException e ) {
7882 throw new RuntimeException ("Could not start watcher" , e );
7983 }
@@ -122,11 +126,12 @@ public static Closeable register(SubscriptionKey path, Consumer<List<WatchEvent<
122126 return CompletableFuture .supplyAsync (() -> {
123127 try {
124128 WatchEvent .Kind <?>[] kinds = new WatchEvent .Kind []{ ENTRY_CREATE , ENTRY_MODIFY , OVERFLOW , ENTRY_DELETE };
129+ var watchable = Platform .get ().newWatchable (path .getPath ());
125130 if (path .isRecursive ()) {
126- return path . getPath () .register (service , kinds , ExtendedWatchEventModifier .FILE_TREE );
131+ return watchable .register (service , kinds , ExtendedWatchEventModifier .FILE_TREE );
127132 }
128133 else {
129- return path . getPath () .register (service , kinds );
134+ return watchable .register (service , kinds );
130135 }
131136 } catch (IOException e ) {
132137 throw new RuntimeException (e );
@@ -156,4 +161,38 @@ public void close() throws IOException {
156161 throw new IOException ("The registration was canceled" );
157162 }
158163 }
164+
165+ private static interface Platform {
166+ WatchService newWatchService () throws IOException ;
167+ Watchable newWatchable (Path path );
168+
169+ static final Platform MAC = new Platform () {
170+ @ Override
171+ public WatchService newWatchService () throws IOException {
172+ return new MacWatchService ();
173+ }
174+ @ Override
175+ public Watchable newWatchable (Path path ) {
176+ return new MacWatchable (path );
177+ }
178+ };
179+
180+ static final Platform DEFAULT = new Platform () {
181+ @ Override
182+ public WatchService newWatchService () throws IOException {
183+ return FileSystems .getDefault ().newWatchService ();
184+ }
185+ @ Override
186+ public Watchable newWatchable (Path path ) {
187+ return path ;
188+ }
189+ };
190+
191+ static final Platform CURRENT = // Assumption: the platform doesn't change
192+ com .sun .jna .Platform .isMac () ? MAC : DEFAULT ;
193+
194+ static Platform get () {
195+ return CURRENT ;
196+ }
197+ }
159198}
0 commit comments