Example usage for org.apache.commons.io.monitor FileAlterationObserver checkAndNotify

List of usage examples for org.apache.commons.io.monitor FileAlterationObserver checkAndNotify

Introduction

In this page you can find the example usage for org.apache.commons.io.monitor FileAlterationObserver checkAndNotify.

Prototype

public void checkAndNotify() 

Source Link

Document

Check whether the file and its chlidren have been created, modified or deleted.

Usage

From source file:net.ion.framework.db.manager.script.FileAlterationMonitor.java

public synchronized void start() throws Exception {

    Callable<Void> sjob = new Callable<Void>() {
        @Override//  ww w.  ja v  a 2 s . c  om
        public Void call() throws Exception {
            for (FileAlterationObserver o : observers) {
                o.checkAndNotify();
            }
            ses.schedule(this, interval, TimeUnit.MILLISECONDS);
            return null;
        }
    };
    ses.schedule(sjob, interval, TimeUnit.MILLISECONDS);
}

From source file:net.ion.radon.cload.monitor.FileAlterationMonitor.java

public synchronized void start() throws Exception {

    Callable<Void> sjob = new Callable<Void>() {
        @Override//from www. jav  a 2  s .com
        public Void call() throws Exception {
            try {
                for (FileAlterationObserver o : observers) {
                    o.checkAndNotify();
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            } finally {
                ses.schedule(this, interval, TimeUnit.MILLISECONDS);
            }
            return null;
        }
    };
    ses.schedule(sjob, interval, TimeUnit.MILLISECONDS);
}

From source file:com.enonic.cms.core.plugin.deploy.HotDeployTask.java

@PostConstruct
public void start() {
    try {//from   www  . jav  a2s .c  om
        final JarFileFilter filter = new JarFileFilter();

        final FileAlterationObserver observer = new FileAlterationObserver(this.deployDir, filter);
        observer.addListener(new HotDeployListener(this.pluginManager));
        observer.checkAndNotify();

        this.monitor = new FileAlterationMonitor(this.scanPeriod, observer);
        this.monitor.start();

        LOG.info("Hot deploying plugins from [{}]. Scanning every [{}] ms.", this.deployDir.getAbsolutePath(),
                this.scanPeriod);
    } catch (Exception e) {
        LOG.error("cannot start monitor.", e);
    }
}

From source file:info.fetter.logstashforwarder.FileWatcher.java

public void checkFiles() throws IOException {
    logger.trace("Checking files");
    logger.trace("==============");
    for (FileAlterationObserver observer : observerList) {
        observer.checkAndNotify();
    }//  w w w  .j  a va  2  s . c o m
    processModifications();
    printWatchMap();
}

From source file:de.adorsys.forge.plugins.ct.ContinuousTestingPlugin.java

@Command(help = "listens to file-changes in project-root/src and triggers mvn test")
public void run(final PipeOut out) {

    String sourceDir = project.getProjectRoot().getFullyQualifiedName() + "/src";

    if (!new File(sourceDir).exists()) {
        ShellMessages.error(out, "SourceDirectory " + sourceDir + " is missing!");
    }//from w w  w.j  a v  a2  s.  c  om

    final FileAlterationObserver observer = new FileAlterationObserver(sourceDir);
    observer.addListener(listener);

    try {
        observer.initialize();
    } catch (Exception ex) {
        handleError(out, ex);
    }

    ShellMessages.info(out, "Monitoring " + sourceDir);
    ShellMessages.info(out, "Cancel with CTRL+C");

    new Thread(new Runnable() {
        @Override
        public void run() {
            while (!shouldExit) {
                observer.checkAndNotify();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    handleError(out, ex);
                }
            }
        }
    }).start();

    if (shell.scan() == 10) {
        shouldExit = true;
    }

}

From source file:it.geosolutions.filesystemmonitor.neutral.monitorpolling.GBFileSystemMonitorJob.java

/**
 * the job. You'll never may call this method manually, (this is executed by the quartz
 * Scheduler).//  w ww .  ja v  a 2 s.co m
 */
public void execute(JobExecutionContext context) throws JobExecutionException {

    final JobDetail detail = context.getJobDetail();

    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Starting FSM job named: " + detail.getKey()); // TODO or key?
    }

    final JobDataMap jdm = detail.getJobDataMap();

    // WORKAROUND
    /*
     * 1Giu2011 Carlo: 
     * Added POLLING_EVENT to implement a Quartz EventGenerator using quartz file system.
     * The returned file path represent the instant when the event is executed (in millisecs)
     */
    if (jdm.get(FileSystemMonitorSPI.TYPE_KEY) == FileSystemEventType.POLLING_EVENT) {
        final GBEventNotifier notifier = (GBEventNotifier) jdm.get(EVENT_NOTIFIER_KEY);
        notifier.notifyEvent(new File(Long.toString(System.currentTimeMillis())),
                FileSystemEventType.POLLING_EVENT);
        return;
    }
    //WORKAROUND

    FileAlterationObserver observer = null;

    if ((observer = getObserver(jdm)) == null) {
        // System.out.println("if1 done sync");
        try {
            long wait = WAITING_LOCK_TIME_DEFAULT; // default wait in seconds
            try {
                wait = jdm.getLong(WAITING_LOCK_TIME_KEY);
            } catch (ClassCastException cce) {
                // NOT HANDLED: using default value
            }
            // System.out.println("WAIT"+wait);
            lock.tryLock(wait, TimeUnit.MILLISECONDS);
            // System.out.println("if1 + synch done if2");
            if ((observer = getObserver(jdm)) == null) {
                // System.out.println("all is done building");
                if (LOGGER.isDebugEnabled())
                    LOGGER.debug("Building the observer tree...");

                observer = buildObserver(jdm);

                if (LOGGER.isDebugEnabled())
                    LOGGER.debug("Observer tree complete.");
            }
        } catch (InterruptedException ex) {
            LOGGER.error("GBFileSystemMonitorJob interrupted during setup", ex);

        } catch (Exception ex) {
            LOGGER.error("GBFileSystemMonitorJob interrupted during setup", ex);

        } finally {
            lock.unlock();
        }

    } // first time initializer ends

    // do the job
    observer.checkAndNotify();
    // DEBUG
    // System.out.println("DOTHEJOB");

    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("job named: " + detail.getKey() + " completed");
    }
}

From source file:org.apache.hadoop.gateway.services.topology.DefaultTopologyServiceTest.java

private void kickMonitor(FileAlterationMonitor monitor) {
    for (FileAlterationObserver observer : monitor.getObservers()) {
        observer.checkAndNotify();
    }/*w  w w.  j  a  v  a 2  s. c o m*/
}

From source file:org.bonitasoft.web.designer.livebuild.ObserverFactoryTest.java

@Test
public void should_create_an_observer_that_notify_when_a_file_is_created() throws Exception {

    FileAlterationObserver observer = factory.create(monitoredFolder, listener);
    Path aFile = Files.write(monitoredFolder.resolve("aFile"), "coucou".getBytes());
    observer.checkAndNotify();

    assertThat(listener.getChanged()).containsExactly(aFile);
}

From source file:org.bonitasoft.web.designer.livebuild.ObserverFactoryTest.java

@Test
public void should_create_an_observer_that_notify_when_a_file_is_modified() throws Exception {
    Files.write(monitoredFolder.resolve("aFile"), "coucou".getBytes());

    FileAlterationObserver observer = factory.create(monitoredFolder, listener);
    Files.write(monitoredFolder.resolve("aFile"), "modified content".getBytes());
    observer.checkAndNotify();

    assertThat(listener.getChanged()).containsExactly(monitoredFolder.resolve("aFile"));
}

From source file:org.codice.ddf.catalog.content.monitor.DurableFileSystemFileConsumer.java

private AsyncFileAlterationObserver backwardsCompatibility(String fileName) {

    String sha1 = DigestUtils.sha1Hex(fileName);
    AsyncFileAlterationObserver newObserver = new AsyncFileAlterationObserver(new File(fileName),
            jsonSerializer);//w w w.  j a va  2  s .  c o  m
    FileAlterationObserver oldObserver = (FileAlterationObserver) fileSystemPersistenceProvider
            .loadFromPersistence(sha1);

    try {
        newObserver.initialize();
    } catch (IllegalStateException e) {
        //  There was an IO error setting up the initial state of the observer
        LOGGER.info("Error initializing the new state of the CDM. retrying on next poll");
        return null;
    }
    oldObserver.addListener(listener);
    oldObserver.checkAndNotify();
    oldObserver.removeListener(listener);

    return newObserver;
}