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

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

Introduction

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

Prototype

public FileAlterationObserver(File directory) 

Source Link

Document

Construct an observer for the specified directory.

Usage

From source file:com.ctt.client.Client.java

public static void main(String[] args) throws Exception {

    Client.prop = new Properties();
    FileReader reader = new FileReader("../config.properties");
    Client.prop.load(reader);//from  w  w w.  ja v a2s . co m

    String directory = Client.prop.getProperty("directory");

    FilesMonitor listener = new FilesMonitor();
    File folder = new File(directory);

    try {
        FileAlterationObserver observer = new FileAlterationObserver(directory);
        FileAlterationMonitor monitor = new FileAlterationMonitor(1000);
        observer.addListener(listener);
        monitor.addObserver(observer);
        monitor.start();
    } catch (Exception io) {
    }
}

From source file:com.ctt.sshlistener.Main.java

public static void main(String[] args) throws Exception {

    Main.prop = new Properties();
    FileReader reader = new FileReader("../config.properties");
    Main.prop.load(reader);/* ww  w.  j a va 2 s .c  o  m*/

    String directory = Main.prop.getProperty("directory");

    FilesMonitor listener = new FilesMonitor();
    File folder = new File(directory);

    try {
        FileAlterationObserver observer = new FileAlterationObserver(directory);
        FileAlterationMonitor monitor = new FileAlterationMonitor(10000);
        observer.addListener(listener);
        monitor.addObserver(observer);
        monitor.start();
    } catch (Exception io) {
    }
}

From source file:it.univaq.disim.connectorOTF.utils.FileMonitor.java

public void monitor(String path) throws Exception {
    // The monitor will perform polling on the folder every 5 seconds
    final long pollingInterval = 5 * 1000;

    File folder = new File(FOLDER);

    if (!folder.exists()) {
        // Test to see if monitored folder exists
        throw new RuntimeException("Directory not found: " + FOLDER);
    }//from  w  ww.  j a va 2  s. c  om

    FileAlterationObserver observer = new FileAlterationObserver(folder);
    FileAlterationMonitor monitor = new FileAlterationMonitor(pollingInterval);
    FileAlterationListener listener = new SpringAlterationListener();

    observer.addListener(listener);
    monitor.addObserver(observer);
    monitor.start();
}

From source file:com.moneydance.modules.features.importlist.io.TransactionFileListenerTest.java

@Before
public void setUp() {
    this.file = new File(".");
    this.listener = new TransactionFileListener();
    this.fileAlterationObserver = new FileAlterationObserver(this.file);
    this.listener.addObserver(this);
    this.listener.onStart(this.fileAlterationObserver);
}

From source file:com.bbytes.jfilesync.sync.FileMonitor.java

public void start() throws Exception {
    FileAlterationObserver observer = new FileAlterationObserver(sourceFolderToMonitor);
    observer.addListener(this);
    monitor = new FileAlterationMonitor(intervalInSeconds);
    monitor.addObserver(observer);/*from   ww w.  j a  v a2  s.c  o m*/
    monitor.start();

    StructureSyncThread structureSyncThread = new StructureSyncThread();
    structureSyncThread.start();

}

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  va 2 s.  c  o m

    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:at.medevit.elexis.gdt.tools.DirectoryWatcher.java

public void addDirectoryToWatch(File directory) {
    if (directory.isDirectory()) {
        logger.log("Adding directory: " + directory, Log.DEBUGMSG);
        FileAlterationObserver observer = new FileAlterationObserver(directory);
        directories.add(directory);//w ww  .  j a va2 s.  com
        observer.addListener(this);
        observers.add(observer);
        monitor.addObserver(observer);
        this.unwatch();
        this.watch();
    } else {
        logger.log("Invalid directory entry passed: " + directory, Log.DEBUGMSG);
    }
}

From source file:io.fabric8.vertx.maven.plugin.utils.IncrementalBuilder.java

/**
 * Adds an observer listening for changes in the given path.
 *
 * @param path the path to observe/*from   w  ww. j  a v  a  2  s .c  om*/
 */
protected synchronized void buildObserver(Path path) {

    logger.info("Observing path:" + path.toString());

    FileAlterationObserver observer = new FileAlterationObserver(path.toFile());

    observer.addListener(this);

    observers.put(path, observer);

    this.monitor.addObserver(observer);
}

From source file:com.lxd.client.monitor.MonitorDir.java

private void fileWatch() {
    //?  /* w w w. ja v a 2  s.  com*/
    FileAlterationMonitor monitor = new FileAlterationMonitor(Define.SCANNING); //??
    //  
    FileAlterationObserver observer = new FileAlterationObserver(new File(filePath));

    ///< ??
    observer.addListener(new AdamFileListener());
    ///< ??
    monitor.addObserver(observer);
    try {
        monitor.start();
    } catch (Exception e) {
        e.printStackTrace();
    }

    ///< ??
    MonitorMsg msg = null;
    while ((msg = ClientResource.getSingleton().takeMonitorMsg()) != null) {
        if (msg.getStart() == 0 || msg.getStart() <= System.currentTimeMillis()) {
            ///< ?
            if (msg.getStart() == 0 && msg.getType() == Type.UPDATE) {
                ///< , ??, ?
                msg.setStart(System.currentTimeMillis() + Define.UPDATETIME);
                ClientResource.getSingleton().submitMonitorMsg(msg);
            } else {
                ///< ??
                if (msg.getType() == Type.DELETE || fileCanRead(msg.getFile())) {
                    ///< 
                    if (msg.getType() == Type.ADD
                            && msg.getState().equals(com.lxd.client.monitor.MonitorMsg.State.BEGIN)) {
                        msg.setStart(System.currentTimeMillis() + Define.CREATEFILETIME);
                        msg.setState(com.lxd.client.monitor.MonitorMsg.State.ACCESS);
                        ClientResource.getSingleton().submitMonitorMsg(msg);
                        continue;
                    }
                    msg = ClientResource.getSingleton().reviseMsg(msg);
                    msgPacking(msg);
                } else {

                    log.info("" + msg.getFile() + ", ?");
                    msg.setStart(System.currentTimeMillis() + Define.ERRORTIME);
                    ClientResource.getSingleton().submitMonitorMsg(msg);
                }
            }
        } else {
            ///< ??
            ClientResource.getSingleton().submitMonitorMsg(msg);
            try {
                ///< ??, , ?
                log.info("" + msg.getFile() + ", ?");
                sleep(Define.SLEEPTIME);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:jai.file.utils.FileMonitorTest.java

private void sample() {
    System.out.println("begin sample-> observer=" + FILE_TO_MONITOR);
    File directory = new File(FILE_TO_MONITOR);
    if (directory.exists()) {
        FileAlterationObserver observer = new FileAlterationObserver(directory);
        observer.addListener(new FAListener());

        long interval = 1000;
        FileAlterationMonitor monitor = new FileAlterationMonitor(interval);
        monitor.addObserver(observer);//w  w w.j ava  2  s  . c om
        try {
            monitor.start();
            // monitor.stop();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {
        System.out.println("Path " + FILE_TO_MONITOR + " does not exist.");
    }

}