use of java.nio.file.WatchService in project spring-cloud-gcp by spring-cloud.
the class PubSubEmulator method startEmulator.
private void startEmulator() throws IOException, InterruptedException {
boolean configPresent = Files.exists(EMULATOR_CONFIG_PATH);
WatchService watchService = null;
if (configPresent) {
watchService = FileSystems.getDefault().newWatchService();
EMULATOR_CONFIG_DIR.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
}
try {
this.emulatorProcess = new ProcessBuilder("gcloud", "beta", "emulators", "pubsub", "start").start();
} catch (IOException ex) {
fail("Gcloud not found; leaving host/port uninitialized.");
}
if (configPresent) {
updateConfig(watchService);
watchService.close();
} else {
createConfig();
}
}
use of java.nio.file.WatchService in project smarthome by eclipse.
the class AbstractFileTransformationService method transform.
/**
* <p>
* Transforms the input <code>source</code> by the according method defined in subclass to another string.
* It expects the transformation to be read from a file which is stored
* under the 'conf/transform'
*
* @param filename the name of the file which contains the transformation definition.
* The name may contain subfoldernames
* as well
* @param source the input to transform
* @throws TransformationException
*/
@Override
@Nullable
public String transform(String filename, String source) throws TransformationException {
if (filename == null || source == null) {
throw new TransformationException("the given parameters 'filename' and 'source' must not be null");
}
final WatchService watchService = getWatchService();
processFolderEvents(watchService);
String transformFile = getLocalizedProposedFilename(filename, watchService);
T transform = cachedFiles.get(transformFile);
if (transform == null) {
transform = internalLoadTransform(transformFile);
cachedFiles.put(transformFile, transform);
}
try {
return internalTransform(transform, source);
} catch (TransformationException e) {
logger.warn("Could not transform '{}' with the file '{}' : {}", source, filename, e.getMessage());
return "";
}
}
use of java.nio.file.WatchService in project smarthome by eclipse.
the class AbstractFileTransformationService method getWatchService.
private synchronized WatchService getWatchService() throws TransformationException {
WatchService watchService = this.watchService;
if (watchService != null) {
return watchService;
}
try {
watchService = this.watchService = FileSystems.getDefault().newWatchService();
} catch (IOException e) {
logger.error("Unable to start transformation directory monitoring");
throw new TransformationException("Cannot get a new watch service.");
}
watchSubDirectory("", watchService);
return watchService;
}
use of java.nio.file.WatchService in project smarthome by eclipse.
the class WatchQueueReader method stopWatchService.
public synchronized void stopWatchService(AbstractWatchService service) {
if (watchService != null) {
List<WatchKey> keys = new LinkedList<>();
for (WatchKey key : keyToService.keySet()) {
if (keyToService.get(key) == service) {
keys.add(key);
}
}
if (keys.size() == keyToService.size()) {
try {
watchService.close();
} catch (IOException e) {
logger.warn("Cannot deactivate folder watcher", e);
}
watchService = null;
keyToService.clear();
registeredKeys.clear();
hashes.clear();
futures.values().forEach(keyFutures -> keyFutures.values().forEach(future -> future.cancel(true)));
futures.clear();
} else {
for (WatchKey key : keys) {
key.cancel();
keyToService.remove(key);
registeredKeys.remove(key);
hashes.remove(service);
Map<Path, @Nullable ScheduledFuture<?>> keyFutures = futures.remove(key);
if (keyFutures != null) {
keyFutures.values().forEach(future -> future.cancel(true));
}
}
}
}
}
use of java.nio.file.WatchService in project karaf by apache.
the class AutoEncryptionSupport method run.
@Override
public void run() {
WatchService watchService = null;
try {
watchService = FileSystems.getDefault().newWatchService();
Path dir = null;
Path file = null;
if (usersFileName == null) {
dir = Paths.get(System.getProperty("karaf.etc"));
file = dir.resolve("users.properties");
} else {
file = new File(usersFileName).toPath();
dir = file.getParent();
}
dir.register(watchService, ENTRY_MODIFY);
encryptedPassword(new Properties(file.toFile()));
while (running) {
try {
WatchKey key = watchService.poll(1, TimeUnit.SECONDS);
if (key == null) {
continue;
}
for (WatchEvent<?> event : key.pollEvents()) {
@SuppressWarnings("unchecked") WatchEvent<Path> ev = (WatchEvent<Path>) event;
// Context for directory entry event is the file name of entry
Path name = dir.resolve(ev.context());
if (file.equals(name)) {
encryptedPassword(new Properties(file.toFile()));
}
}
key.reset();
} catch (IOException e) {
LOGGER.warn(e.getMessage(), e);
} catch (InterruptedException e) {
// Ignore as this happens on shutdown
}
}
} catch (IOException e) {
LOGGER.warn(e.getMessage(), e);
} finally {
StreamUtils.close(watchService);
}
}
Aggregations