Search in sources :

Example 51 with WatchService

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();
    }
}
Also used : IOException(java.io.IOException) WatchService(java.nio.file.WatchService)

Example 52 with WatchService

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 "";
    }
}
Also used : WatchService(java.nio.file.WatchService) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 53 with WatchService

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;
}
Also used : IOException(java.io.IOException) WatchService(java.nio.file.WatchService)

Example 54 with 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));
                }
            }
        }
    }
}
Also used : NoSuchFileException(java.nio.file.NoSuchFileException) Arrays(java.util.Arrays) ScheduledFuture(java.util.concurrent.ScheduledFuture) MessageDigest(java.security.MessageDigest) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) WatchKey(java.nio.file.WatchKey) StandardWatchEventKinds(java.nio.file.StandardWatchEventKinds) Kind(java.nio.file.WatchEvent.Kind) Nullable(org.eclipse.jdt.annotation.Nullable) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) LinkedList(java.util.LinkedList) Path(java.nio.file.Path) EnumSet(java.util.EnumSet) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) ThreadPoolManager(org.eclipse.smarthome.core.common.ThreadPoolManager) Logger(org.slf4j.Logger) Files(java.nio.file.Files) WatchEvent(java.nio.file.WatchEvent) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) FileVisitResult(java.nio.file.FileVisitResult) WatchService(java.nio.file.WatchService) List(java.util.List) FileVisitOption(java.nio.file.FileVisitOption) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AccessDeniedException(java.nio.file.AccessDeniedException) FileSystems(java.nio.file.FileSystems) InputStream(java.io.InputStream) Path(java.nio.file.Path) WatchKey(java.nio.file.WatchKey) IOException(java.io.IOException) LinkedList(java.util.LinkedList) ScheduledFuture(java.util.concurrent.ScheduledFuture)

Example 55 with WatchService

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);
    }
}
Also used : Path(java.nio.file.Path) WatchKey(java.nio.file.WatchKey) WatchEvent(java.nio.file.WatchEvent) IOException(java.io.IOException) Properties(org.apache.felix.utils.properties.Properties) WatchService(java.nio.file.WatchService) File(java.io.File)

Aggregations

WatchService (java.nio.file.WatchService)56 Path (java.nio.file.Path)40 WatchKey (java.nio.file.WatchKey)32 WatchEvent (java.nio.file.WatchEvent)23 IOException (java.io.IOException)21 Test (org.junit.Test)18 File (java.io.File)16 FileSystem (java.nio.file.FileSystem)6 ClosedWatchServiceException (java.nio.file.ClosedWatchServiceException)5 Kind (java.nio.file.WatchEvent.Kind)5 FileSystems (java.nio.file.FileSystems)4 StandardWatchEventKinds (java.nio.file.StandardWatchEventKinds)4 SensitivityWatchEventModifier (com.sun.nio.file.SensitivityWatchEventModifier)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)3 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 TimeUnit (java.util.concurrent.TimeUnit)3 BufferedReader (java.io.BufferedReader)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2