Skip to content

Improved macOS support: Small fixes #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@ jobs:
test:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
os:
- image: ubuntu-latest
- image: macos-latest
mac-backend: jdk
- image: macos-latest
mac-backend: jna
- image: windows-latest
jdk: [11, 17, 21]

fail-fast: false
runs-on: ${{ matrix.os }}
runs-on: ${{ matrix.os.image }}
steps:
- uses: actions/checkout@v4
- run: echo " " >> pom.xml # make sure the cache is slightly different for these runners
Expand All @@ -27,7 +34,7 @@ jobs:
cache: 'maven'

- name: test
run: mvn -B clean test
run: mvn -B clean test -DargLine="-Dengineering.swat.java-watch.mac=${{ matrix.os.mac-backend }}"
env:
DELAY_FACTOR: 3

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ try(var active = watcherSetup.start()) {
// no new events will be scheduled on the threadpool
```

## Internals
## Backends

On all platforms except macOS, the library internally uses the JDK default implementation of the Java NIO [`WatchService`](https://docs.oracle.com/javase/8/docs/api/java/nio/file/WatchService.html) API.

On macOS, the library internally uses our custom `WatchService` implementation based on macOS's native [file system event streams](https://developer.apple.com/documentation/coreservices/file_system_events?language=objc) (using JNA).
Generally, it offers better performance than the JDK default implementation (because the latter uses a polling loop to detect changes only once every two seconds).
To force the library to use the JDK default implementation on macOS, set system property `engineering.swat.watch.impl` to `default`.
To force the library to use the JDK default implementation on macOS, set system property `engineering.swat.java-watch.mac` to `jdk`.

## Related work

Expand Down
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<checkerframework.version>3.49.2</checkerframework.version>
<junit.version>5.12.2</junit.version>
<log4j.version>2.24.3</log4j.version>
<jna.version>5.16.0</jna.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
Expand Down Expand Up @@ -226,12 +227,12 @@
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.16.0</version>
<version>${jna.version}</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>5.16.0</version>
<version>${jna.version}</version>
</dependency>
</dependencies>

Expand Down
24 changes: 13 additions & 11 deletions src/main/java/engineering/swat/watch/impl/jdk/JDKPoller.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,20 +190,22 @@ public Watchable newWatchable(Path path) {
static final Platform CURRENT = current(); // Assumption: the platform doesn't change

private static Platform current() {
var key = "engineering.swat.watch.impl";
var val = System.getProperty(key);
if (val != null) {
if (val.equals("mac")) {
return MAC;
} else if (val.equals("default")) {
return DEFAULT;
} else {
logger.warn("Unexpected value \"{}\" for system property \"{}\". Using value \"default\" instead.", val, key);
return DEFAULT;
if (com.sun.jna.Platform.isMac()) {
var key = "engineering.swat.java-watch.mac";
var val = System.getProperty(key);
if (val != null) {
if (val.equals("jna")) {
return MAC;
} else if (val.equals("jdk")) {
return DEFAULT;
} else {
logger.warn("Unexpected value \"{}\" for system property \"{}\". Using value \"jdk\" instead.", val, key);
return DEFAULT;
}
}
}

return com.sun.jna.Platform.isMac() ? MAC : DEFAULT;
return DEFAULT;
}

static Platform get() {
Expand Down