org.gradle.internal.filewatch.jdk7.WatchServiceFileWatcherBacking Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gradle-api Show documentation
Show all versions of gradle-api Show documentation
Gradle 6.9.1 API redistribution.
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.internal.filewatch.jdk7;
import com.google.common.base.Throwables;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import org.gradle.api.Action;
import org.gradle.api.internal.file.FileSystemSubset;
import org.gradle.internal.filewatch.FileWatcher;
import org.gradle.internal.filewatch.FileWatcherEvent;
import org.gradle.internal.filewatch.FileWatcherListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.lang.ref.SoftReference;
import java.nio.file.ClosedWatchServiceException;
import java.nio.file.WatchService;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
public class WatchServiceFileWatcherBacking {
private static final Logger LOGGER = LoggerFactory.getLogger(WatchServiceFileWatcherBacking.class);
private final AtomicBoolean started = new AtomicBoolean();
private final AtomicBoolean running = new AtomicBoolean();
private final AtomicBoolean stopped = new AtomicBoolean();
private final AtomicReference> pollerThreadReference = new AtomicReference>();
private final Action super Throwable> onError;
private final WatchServiceRegistrar watchServiceRegistrar;
private final WatchService watchService;
private final WatchServicePoller poller;
private final FileWatcher fileWatcher = new FileWatcher() {
@Override
public boolean isRunning() {
return running.get();
}
@Override
public void watch(FileSystemSubset fileSystemSubset) throws IOException {
WatchServiceFileWatcherBacking.this.watchServiceRegistrar.watch(fileSystemSubset);
}
@Override
public void stop() {
WatchServiceFileWatcherBacking.this.stop();
}
};
WatchServiceFileWatcherBacking(Action super Throwable> onError, FileWatcherListener listener, WatchService watchService) throws IOException {
this(onError, listener, watchService, new WatchServiceRegistrar(watchService, listener));
}
WatchServiceFileWatcherBacking(Action super Throwable> onError, FileWatcherListener listener, WatchService watchService, WatchServiceRegistrar watchServiceRegistrar) throws IOException {
this.onError = onError;
this.watchServiceRegistrar = watchServiceRegistrar;
this.watchService = watchService;
this.poller = new WatchServicePoller(watchService);
}
public FileWatcher start(ListeningExecutorService executorService) {
if (started.compareAndSet(false, true)) {
final ListenableFuture> runLoopFuture = executorService.submit(new Runnable() {
@Override
public void run() {
if (!stopped.get()) {
pollerThreadReference.set(new SoftReference(Thread.currentThread()));
running.set(true);
try {
try {
pumpEvents();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (Throwable t) {
if (!(Throwables.getRootCause(t) instanceof InterruptedException)) {
stop();
onError.execute(t);
}
}
} finally {
stop();
}
}
}
});
// This is necessary so that the watcher indicates its not running if the runnable gets cancelled
Futures.addCallback(runLoopFuture, new FutureCallback
© 2015 - 2025 Weber Informatics LLC | Privacy Policy