Search in sources :

Example 1 with DefaultFileMonitor

use of org.apache.commons.vfs2.impl.DefaultFileMonitor in project motech by motech.

the class ConfigFileMonitor method init.

/**
 * Initializes the configuration file monitor. This method will be automatically called after creation and
 * dependency injection. It is done to make sure that injected dependencies are set and ready to use.
 */
@PostConstruct
public void init() throws IOException {
    BootstrapConfig bootstrapConfig = configurationService.loadBootstrapConfig();
    if (bootstrapConfig != null && bootstrapConfig.getConfigSource() == ConfigSource.FILE) {
        // allow custom monitors to be injected
        if (fileMonitor == null) {
            fileMonitor = new DefaultFileMonitor(this);
            // allow raw configs, which are one directory down, under /raw/, to be monitored
            fileMonitor.setRecursive(true);
        }
        fileMonitor.setDelay(DELAY);
        final List<File> files = new ArrayList<>();
        try {
            files.addAll(configLoader.findExistingConfigs());
        } catch (MotechConfigurationException ex) {
            LOGGER.error(ex.getMessage(), ex);
            return;
        }
        configurationService.processExistingConfigs(files);
        startFileMonitor();
    }
}
Also used : BootstrapConfig(org.motechproject.config.core.domain.BootstrapConfig) ArrayList(java.util.ArrayList) MotechConfigurationException(org.motechproject.config.core.exception.MotechConfigurationException) DefaultFileMonitor(org.apache.commons.vfs2.impl.DefaultFileMonitor) File(java.io.File) PostConstruct(javax.annotation.PostConstruct)

Example 2 with DefaultFileMonitor

use of org.apache.commons.vfs2.impl.DefaultFileMonitor in project accumulo by apache.

the class VfsClassLoaderTest method testFileMonitor.

@Test
public void testFileMonitor() throws Exception {
    MyFileMonitor listener = new MyFileMonitor();
    DefaultFileMonitor monitor = new DefaultFileMonitor(listener);
    monitor.setRecursive(true);
    FileObject testDir = vfs.resolveFile(TEST_DIR.toUri().toString());
    monitor.addFile(testDir);
    monitor.start();
    // Copy jar file to a new file name
    URL jarPath = this.getClass().getResource("/HelloWorld.jar");
    Path src = new Path(jarPath.toURI().toString());
    Path dst = new Path(TEST_DIR, "HelloWorld2.jar");
    this.hdfs.copyFromLocalFile(src, dst);
    // VFS-487 significantly wait to avoid failure
    Thread.sleep(7000);
    Assert.assertTrue(listener.isFileCreated());
    // Update the jar
    jarPath = this.getClass().getResource("/HelloWorld.jar");
    src = new Path(jarPath.toURI().toString());
    dst = new Path(TEST_DIR, "HelloWorld2.jar");
    this.hdfs.copyFromLocalFile(src, dst);
    // VFS-487 significantly wait to avoid failure
    Thread.sleep(7000);
    Assert.assertTrue(listener.isFileChanged());
    this.hdfs.delete(dst, false);
    // VFS-487 significantly wait to avoid failure
    Thread.sleep(7000);
    Assert.assertTrue(listener.isFileDeleted());
    monitor.stop();
}
Also used : Path(org.apache.hadoop.fs.Path) FileObject(org.apache.commons.vfs2.FileObject) DefaultFileMonitor(org.apache.commons.vfs2.impl.DefaultFileMonitor) URL(java.net.URL) Test(org.junit.Test)

Aggregations

DefaultFileMonitor (org.apache.commons.vfs2.impl.DefaultFileMonitor)2 File (java.io.File)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 PostConstruct (javax.annotation.PostConstruct)1 FileObject (org.apache.commons.vfs2.FileObject)1 Path (org.apache.hadoop.fs.Path)1 Test (org.junit.Test)1 BootstrapConfig (org.motechproject.config.core.domain.BootstrapConfig)1 MotechConfigurationException (org.motechproject.config.core.exception.MotechConfigurationException)1