Java Code Examples for org.apache.commons.vfs2.impl.DefaultFileMonitor

The following examples show how to use org.apache.commons.vfs2.impl.DefaultFileMonitor. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: datawave   Source File: TypeMetadataProvider.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * set up the monitor so that when the file system data is changed, our singleton will be refreshed
 */
public void init() {
    for (String metadataTableName : this.metadataTableNames) {
        DefaultFileMonitor monitor = new DefaultFileMonitor(this);
        try {
            monitor.setDelay(delay);
            monitor.setRecursive(false);
            monitor.addFile(this.bridge.getFileObject(metadataTableName));
            log.debug("monitoring " + this.bridge.getFileObject(metadataTableName));
            monitor.start();
            this.monitors.put(metadataTableName, monitor);
        } catch (Exception ex) {
            monitor.stop();
            throw new RuntimeException("Failed to create TypeMetadataProvider with " + this.bridge.getUri() + this.bridge.getDir() + "/"
                            + this.bridge.getFileName(), ex);
        }
    }
}
 
Example 2
Source Project: jbake   Source File: BakeWatcher.java    License: MIT License 6 votes vote down vote up
/**
 * Starts watching the file system for changes to trigger a bake.
 *
 * @param config JBakeConfiguration settings
 */
public void start(JBakeConfiguration config) {
    try {
        FileSystemManager fsMan = VFS.getManager();
        FileObject listenPath = fsMan.resolveFile(config.getContentFolder().toURI());
        FileObject templateListenPath = fsMan.resolveFile(config.getTemplateFolder().toURI());
        FileObject assetPath = fsMan.resolveFile(config.getAssetFolder().toURI());

        logger.info("Watching for (content, template, asset) changes in [{}]", config.getSourceFolder().getPath());
        DefaultFileMonitor monitor = new DefaultFileMonitor(new CustomFSChangeListener(config));
        monitor.setRecursive(true);
        monitor.addFile(listenPath);
        monitor.addFile(templateListenPath);
        monitor.addFile(assetPath);
        monitor.start();
    } catch (FileSystemException e) {
        logger.error("Problems watching filesystem changes", e);
    }
}
 
Example 3
Source Project: commons-vfs   Source File: DefaultFileMonitorTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testFileCreated() throws Exception {
    final FileObject fileObj = fsManager.resolveFile(testFile.toURI().toURL().toString());
    final DefaultFileMonitor monitor = new DefaultFileMonitor(new TestFileListener());
    // TestFileListener manipulates changeStatus
    monitor.setDelay(100);
    monitor.addFile(fileObj);
    monitor.start();
    try {
        writeToFile(testFile);
        Thread.sleep(300);
        assertTrue("No event occurred", changeStatus != 0);
        assertEquals("Incorrect event", 3, changeStatus);
    } finally {
        monitor.stop();
    }
}
 
Example 4
Source Project: commons-vfs   Source File: DefaultFileMonitorTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testFileDeleted() throws Exception {
    writeToFile(testFile);
    final FileObject fileObj = fsManager.resolveFile(testFile.toURI().toString());
    final DefaultFileMonitor monitor = new DefaultFileMonitor(new TestFileListener());
    // TestFileListener manipulates changeStatus
    monitor.setDelay(100);
    monitor.addFile(fileObj);
    monitor.start();
    try {
        testFile.delete();
        Thread.sleep(300);
        assertTrue("No event occurred", changeStatus != 0);
        assertEquals("Incorrect event", 2, changeStatus);
    } finally {
        monitor.stop();
    }
}
 
Example 5
Source Project: commons-vfs   Source File: DefaultFileMonitorTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testFileModified() throws Exception {
    writeToFile(testFile);
    final FileObject fileObj = fsManager.resolveFile(testFile.toURI().toURL().toString());
    final DefaultFileMonitor monitor = new DefaultFileMonitor(new TestFileListener());
    // TestFileListener manipulates changeStatus
    monitor.setDelay(100);
    monitor.addFile(fileObj);
    monitor.start();
    try {
        // Need a long delay to insure the new timestamp doesn't truncate to be the same as
        // the current timestammp. Java only guarantees the timestamp will be to 1 second.
        Thread.sleep(1000);
        final long value = System.currentTimeMillis();
        final boolean rc = testFile.setLastModified(value);
        assertTrue("setLastModified succeeded", rc);
        Thread.sleep(300);
        assertTrue("No event occurred", changeStatus != 0);
        assertEquals("Incorrect event", 1, changeStatus);
    } finally {
        monitor.stop();
    }
}
 
Example 6
Source Project: commons-vfs   Source File: DefaultFileMonitorTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testChildFileRecreated() throws Exception {
    writeToFile(testFile);
    final FileObject fileObj = fsManager.resolveFile(testDir.toURI().toURL().toString());
    final DefaultFileMonitor monitor = new DefaultFileMonitor(new TestFileListener());
    monitor.setDelay(2000);
    monitor.setRecursive(true);
    monitor.addFile(fileObj);
    monitor.start();
    try {
        changeStatus = 0;
        Thread.sleep(300);
        testFile.delete();
        Thread.sleep(3000);
        assertTrue("No event occurred", changeStatus != 0);
        assertEquals("Incorrect event " + changeStatus, 2, changeStatus);
        changeStatus = 0;
        Thread.sleep(300);
        writeToFile(testFile);
        Thread.sleep(3000);
        assertTrue("No event occurred", changeStatus != 0);
        assertEquals("Incorrect event " + changeStatus, 3, changeStatus);
    } finally {
        monitor.stop();
    }
}
 
Example 7
Source Project: commons-vfs   Source File: DefaultFileMonitorTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testChildFileDeletedWithoutRecursiveChecking() throws Exception {
    writeToFile(testFile);
    final FileObject fileObj = fsManager.resolveFile(testDir.toURI().toURL().toString());
    final DefaultFileMonitor monitor = new DefaultFileMonitor(new TestFileListener());
    monitor.setDelay(2000);
    monitor.setRecursive(false);
    monitor.addFile(fileObj);
    monitor.start();
    try {
        changeStatus = 0;
        Thread.sleep(300);
        testFile.delete();
        Thread.sleep(3000);
        assertEquals("Event should not have occurred", 0, changeStatus);
    } finally {
        monitor.stop();
    }
}
 
Example 8
Source Project: commons-vfs   Source File: DefaultFileMonitorTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testFileMonitorRestarted() throws Exception {
    final FileObject fileObj = fsManager.resolveFile(testFile.toURI().toString());
    final DefaultFileMonitor monitor = new DefaultFileMonitor(new TestFileListener());
    // TestFileListener manipulates changeStatus
    monitor.setDelay(100);
    monitor.addFile(fileObj);

    monitor.start();
    writeToFile(testFile);
    Thread.sleep(300);
    monitor.stop();

    monitor.start();
    try {
        testFile.delete();
        Thread.sleep(300);
        assertTrue("No event occurred", changeStatus != 0);
        assertEquals("Incorrect event", 2, changeStatus);
    } finally {
        monitor.stop();
    }
}
 
Example 9
Source Project: commons-vfs   Source File: DefaultFileMonitorTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * VFS-299: Handlers are not removed. One instance is {@link DefaultFileMonitor#removeFile(FileObject)}.
 *
 * As a result, the file monitor will fire two created events.
 */
@Ignore("VFS-299")
public void ignore_testAddRemove() throws Exception {
    final FileObject file = fsManager.resolveFile(testFile.toURI().toString());
    final CountingListener listener = new CountingListener();
    final DefaultFileMonitor monitor = new DefaultFileMonitor(listener);
    monitor.setDelay(100);

    try {
        monitor.addFile(file);
        monitor.removeFile(file);
        monitor.addFile(file);
        monitor.start();
        writeToFile(testFile);
        Thread.sleep(300);
        assertEquals("Created event is only fired once", 1, listener.created.get());
    } finally {
        monitor.stop();
    }
}
 
Example 10
Source Project: commons-vfs   Source File: DefaultFileMonitorTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testFileRecreated() throws Exception {
    final FileObject fileObj = fsManager.resolveFile(testFile.toURI().toURL().toString());
    final DefaultFileMonitor monitor = new DefaultFileMonitor(new TestFileListener());
    // TestFileListener manipulates changeStatus
    monitor.setDelay(100);
    monitor.addFile(fileObj);
    monitor.start();
    try {
        writeToFile(testFile);
        Thread.sleep(300);
        assertTrue("No event occurred", changeStatus != 0);
        assertEquals("Incorrect event " + changeStatus, 3, changeStatus);
        changeStatus = 0;
        testFile.delete();
        Thread.sleep(300);
        assertTrue("No event occurred", changeStatus != 0);
        assertEquals("Incorrect event " + changeStatus, 2, changeStatus);
        changeStatus = 0;
        Thread.sleep(500);
        monitor.addFile(fileObj);
        writeToFile(testFile);
        Thread.sleep(300);
        assertTrue("No event occurred", changeStatus != 0);
        assertEquals("Incorrect event " + changeStatus, 3, changeStatus);
    } finally {
        monitor.stop();
    }
}
 
Example 11
Source Project: commons-vfs   Source File: DefaultFileMonitorTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * VFS-299: Handlers are not removed. There is no API for properly decommissioning a file monitor.
 *
 * As a result, listeners of stopped monitors still receive events.
 */
@Ignore("VFS-299")
public void ignore_testStartStop() throws Exception {
    final FileObject file = fsManager.resolveFile(testFile.toURI().toString());

    final CountingListener stoppedListener = new CountingListener();
    final DefaultFileMonitor stoppedMonitor = new DefaultFileMonitor(stoppedListener);
    stoppedMonitor.start();
    stoppedMonitor.addFile(file);
    stoppedMonitor.stop();

    // Variant 1: it becomes documented behavior to manually remove all files after stop() such that all listeners are removed
    // This currently does not work, see DefaultFileMonitorTests#testAddRemove above.
    // stoppedMonitor.removeFile(file);

    // Variant 2: change behavior of stop(), which then removes all handlers.
    // This would remove the possibility to pause watching files. Resuming watching for the same files via start(); stop(); start(); would not work.

    // Variant 3: introduce new method DefaultFileMonitor#close which definitely removes all resources held by DefaultFileMonitor.

    final CountingListener activeListener = new CountingListener();
    final DefaultFileMonitor activeMonitor = new DefaultFileMonitor(activeListener);
    activeMonitor.setDelay(100);
    activeMonitor.addFile(file);
    activeMonitor.start();
    try {
        writeToFile(testFile);
        Thread.sleep(1000);

        assertEquals("The listener of the active monitor received one created event", 1, activeListener.created.get());
        assertEquals("The listener of the stopped monitor received no events", 0, stoppedListener.created.get());
    } finally {
        activeMonitor.stop();
    }
}
 
Example 12
Source Project: datawave   Source File: TypeMetadataProvider.java    License: Apache License 2.0 4 votes vote down vote up
public void close() {
    for (DefaultFileMonitor monitor : this.monitors.values()) {
        monitor.stop();
    }
}