
br.com.objectos.io.WatchServiceJava7 Maven / Gradle / Ivy
/*
* Copyright (C) 2011-2021 Objectos Software LTDA.
*
* 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 br.com.objectos.io;
import static br.com.objectos.collections.Collections.newGrowableMap;
import static br.com.objectos.collections.Collections.toImmutableSet;
import static br.com.objectos.lang.Lang.checkNotNull;
import br.com.objectos.collections.GrowableMap;
import br.com.objectos.collections.ImmutableMap;
import br.com.objectos.collections.ImmutableSet;
import br.com.objectos.lang.Lang;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.WatchKey;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Set;
final class WatchServiceJava7 implements WatchService {
private final Set directories;
private Worker worker;
WatchServiceJava7(Set directories) {
this.directories = directories;
}
@Override
public final void onShutdownHook() throws Exception {
stopService();
}
@Override
public final synchronized void startService() throws IOException {
FileSystem fs;
fs = FileSystems.getDefault();
java.nio.file.WatchService delegate;
try {
delegate = fs.newWatchService();
} catch (IOException e) {
throw e;
}
IOException ioException;
ioException = null;
GrowableMap keys;
keys = newGrowableMap();
for (WatchDirectoryJava7 option : directories) {
try {
WatchKey watchKey;
watchKey = option.register(delegate);
keys.put(watchKey, option);
} catch (IOException e) {
ioException = e;
break;
}
}
if (ioException != null) {
ioException = Lang.close(ioException, delegate);
throw ioException;
}
worker = new Worker(delegate, keys.toImmutableMap());
worker.start();
}
@Override
public final void stopService() throws IOException {
worker.shutdown();
}
static class Builder implements WatchServiceBuilder {
private final GrowableMap directories = newGrowableMap();
public final WatchService build() {
Collection values;
values = directories.values();
ImmutableSet set;
set = toImmutableSet(values);
return new WatchServiceJava7(set);
}
@Override
public final void watchDirectory(
Directory directory, WatchServiceListener listener, EventKind event) {
checkNotNull(directory, "directory == null");
if (directories.containsKey(directory)) {
throw new IllegalArgumentException(directory.getPath() + " has already been registered");
}
checkNotNull(listener, "listener == null");
checkNotNull(event, "event == null");
EnumSet events;
events = EnumSet.of(event);
WatchDirectoryJava7 option;
option = new WatchDirectoryJava7(directory, listener, events);
directories.put(directory, option);
}
}
private static class Worker extends Thread {
private final java.nio.file.WatchService delegate;
private final ImmutableMap keys;
Worker(java.nio.file.WatchService delegate, ImmutableMap keys) {
super("WatchService");
this.delegate = delegate;
this.keys = keys;
}
@Override
public final void run() {
while (!Thread.interrupted()) {
try {
WatchKey currentKey;
currentKey = delegate.take();
WatchDirectoryJava7 watchDirectory;
watchDirectory = keys.get(currentKey);
if (watchDirectory != null) {
watchDirectory.consume(currentKey);
}
currentKey.reset();
} catch (InterruptedException e) {
return;
}
}
}
public final void shutdown() throws IOException {
try {
interrupt();
} finally {
delegate.close();
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy