Search in sources :

Example 21 with WatchService

use of java.nio.file.WatchService in project j2objc by google.

the class MacOSXPathTest method test_register$WatchService$WatchEvent_Kind_Exception_NPE.

@Test
public void test_register$WatchService$WatchEvent_Kind_Exception_NPE() throws IOException {
    WatchService watchService = FileSystems.getDefault().newWatchService();
    Path directory = Paths.get(filesSetup.getTestDir(), "directory1");
    Files.createDirectories(directory);
    // When file is not a directory.
    try {
        directory.register(null, ENTRY_CREATE);
        fail();
    } catch (NullPointerException expected) {
    }
    try {
        directory.register(watchService, (Kind<?>) null);
        fail();
    } catch (NullPointerException expected) {
    }
}
Also used : Path(java.nio.file.Path) WatchService(java.nio.file.WatchService) Test(org.junit.Test)

Example 22 with WatchService

use of java.nio.file.WatchService in project j2objc by google.

the class MacOSXPathTest method test_register$WatchService$WatchEvent_Kind$WatchEvent_Modifier_NPE.

@Test
public void test_register$WatchService$WatchEvent_Kind$WatchEvent_Modifier_NPE() throws IOException {
    WatchService watchService = FileSystems.getDefault().newWatchService();
    WatchEvent.Kind<?>[] events = { ENTRY_CREATE };
    Path dirRoot = Paths.get(filesSetup.getTestDir(), "dir");
    Files.createDirectories(dirRoot);
    try {
        WatchKey key = dirRoot.register(null, events, ExtendedWatchEventModifier.FILE_TREE);
        fail();
    } catch (NullPointerException expected) {
    }
    try {
        WatchKey key = dirRoot.register(watchService, null, ExtendedWatchEventModifier.FILE_TREE);
        fail();
    } catch (NullPointerException expected) {
    }
}
Also used : Path(java.nio.file.Path) Kind(java.nio.file.WatchEvent.Kind) WatchKey(java.nio.file.WatchKey) WatchService(java.nio.file.WatchService) Test(org.junit.Test)

Example 23 with WatchService

use of java.nio.file.WatchService in project j2objc by google.

the class MacOSXPathTest method test_register$WatchService$WatchEvent_Kind_Exception.

@Test
public void test_register$WatchService$WatchEvent_Kind_Exception() throws IOException {
    WatchService watchService = FileSystems.getDefault().newWatchService();
    Path directory = Paths.get(filesSetup.getTestDir(), "directory1");
    Files.createFile(directory);
    // When file is not a directory.
    try {
        directory.register(watchService, ENTRY_CREATE);
        fail();
    } catch (NotDirectoryException expected) {
    }
    // When the events are not supported.
    Files.deleteIfExists(directory);
    Files.createDirectories(directory);
    WatchEvent.Kind<?>[] events = { new NonStandardEvent<>() };
    try {
        directory.register(watchService, events);
        fail();
    } catch (UnsupportedOperationException expected) {
    }
    // When the watch service is closed.
    watchService.close();
    try {
        directory.register(watchService, ENTRY_CREATE);
        fail();
    } catch (ClosedWatchServiceException expected) {
    }
}
Also used : Path(java.nio.file.Path) NotDirectoryException(java.nio.file.NotDirectoryException) Kind(java.nio.file.WatchEvent.Kind) ClosedWatchServiceException(java.nio.file.ClosedWatchServiceException) WatchService(java.nio.file.WatchService) Test(org.junit.Test)

Example 24 with WatchService

use of java.nio.file.WatchService in project dubbo by alibaba.

the class FileSystemDynamicConfiguration method newWatchService.

private static Optional<WatchService> newWatchService() {
    Optional<WatchService> watchService = null;
    FileSystem fileSystem = FileSystems.getDefault();
    try {
        watchService = Optional.of(fileSystem.newWatchService());
    } catch (IOException e) {
        if (logger.isErrorEnabled()) {
            logger.error(e.getMessage(), e);
        }
        watchService = Optional.empty();
    }
    return watchService;
}
Also used : FileSystem(java.nio.file.FileSystem) IOException(java.io.IOException) WatchService(java.nio.file.WatchService)

Example 25 with WatchService

use of java.nio.file.WatchService in project jPOS by jpos.

the class Q2 method run.

public void run() {
    started = true;
    Thread.currentThread().setName("Q2-" + getInstanceId().toString());
    Path dir = Paths.get(deployDir.getAbsolutePath());
    FileSystem fs = dir.getFileSystem();
    try (WatchService service = fs.newWatchService()) {
        watchServiceClassname = service.getClass().getName();
        dir.register(service, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
        /*
            * The following code determines whether a MBeanServer exists 
            * already. If so then the first one in the list is used. 
            * I have not yet find a way to interrogate the server for 
            * information other than MBeans so to pick a specific one 
            * would be difficult.
            */
        ArrayList mbeanServerList = MBeanServerFactory.findMBeanServer(null);
        if (mbeanServerList.isEmpty()) {
            server = MBeanServerFactory.createMBeanServer(JMX_NAME);
        } else {
            server = (MBeanServer) mbeanServerList.get(0);
        }
        final ObjectName loaderName = new ObjectName(Q2_CLASS_LOADER);
        try {
            loader = (QClassLoader) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {

                public Object run() {
                    return new QClassLoader(server, libDir, loaderName, mainClassLoader);
                }
            });
            if (server.isRegistered(loaderName))
                server.unregisterMBean(loaderName);
            server.registerMBean(loader, loaderName);
            loader = loader.scan(false);
        } catch (Throwable t) {
            if (log != null)
                log.error("initial-scan", t);
            else
                t.printStackTrace();
        }
        factory = new QFactory(loaderName, this);
        writePidFile();
        initSystemLogger();
        if (bundleContext == null)
            addShutdownHook();
        q2Thread = Thread.currentThread();
        q2Thread.setContextClassLoader(loader);
        if (cli != null)
            cli.start();
        initConfigDecorator();
        if (startOSGI)
            startOSGIFramework();
        if (enableSsh) {
            deployElement(SshService.createDescriptor(sshPort, sshUser, sshAuthorizedKeys, sshHostKeyFile), "05_sshd-" + getInstanceId() + ".xml", false, true);
        }
        deployInternal();
        for (int i = 1; shutdown.getCount() > 0; i++) {
            try {
                if (i > 1 && disableDeployScan) {
                    shutdown.await();
                    break;
                }
                boolean forceNewClassLoader = scan() && i > 1;
                QClassLoader oldClassLoader = loader;
                loader = loader.scan(forceNewClassLoader);
                if (loader != oldClassLoader) {
                    // We want this to be null so it gets GCed.
                    oldClassLoader = null;
                    // force a GC
                    System.gc();
                    log.info("new classloader [" + Integer.toString(loader.hashCode(), 16) + "] has been created");
                    q2Thread.setContextClassLoader(loader);
                }
                logVersion();
                deploy();
                checkModified();
                ready.countDown();
                if (!waitForChanges(service))
                    break;
            } catch (InterruptedException | IllegalAccessError ignored) {
            // NOPMD
            } catch (Throwable t) {
                log.error("start", t.getMessage());
                relax();
            }
        }
        undeploy();
        try {
            if (server.isRegistered(loaderName))
                server.unregisterMBean(loaderName);
        } catch (InstanceNotFoundException e) {
            log.error(e);
        }
        if (decorator != null) {
            decorator.uninitialize();
        }
        if (exit && !shuttingDown)
            System.exit(0);
    } catch (IllegalAccessError ignored) {
    // NOPMD OK to happen
    } catch (Exception e) {
        if (log != null)
            log.error(e);
        else
            e.printStackTrace();
        System.exit(1);
    }
}
Also used : Path(java.nio.file.Path) InstanceNotFoundException(javax.management.InstanceNotFoundException) JDOMException(org.jdom2.JDOMException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) GeneralSecurityException(java.security.GeneralSecurityException) BundleException(org.osgi.framework.BundleException) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) SAXException(org.xml.sax.SAXException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ISOException(org.jpos.iso.ISOException) MissingArgumentException(org.apache.commons.cli.MissingArgumentException) ObjectName(javax.management.ObjectName) FileSystem(java.nio.file.FileSystem) WatchService(java.nio.file.WatchService)

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