org.apache.commons.io.monitor.FileAlterationObserver Java Examples

The following examples show how to use org.apache.commons.io.monitor.FileAlterationObserver. 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 File: DefaultHttpRequestHandler.java    From t-io with Apache License 2.0 6 votes vote down vote up
/**
 * 扫描文件变化
 * @throws Exception
 */
public void monitorFileChanged() throws Exception {
	if (httpConfig.monitorFileChange) {
		if (httpConfig.getPageRoot() != null) {
			File directory = new File(httpConfig.getPageRoot());//需要扫描的文件夹路径
			// 测试采用轮询间隔 5 秒
			long interval = TimeUnit.SECONDS.toMillis(5);
			FileAlterationObserver observer = new FileAlterationObserver(directory, new FileFilter() {
				@Override
				public boolean accept(File pathname) {
					return true;
				}
			});
			//设置文件变化监听器
			observer.addListener(new FileChangeListener(this));
			FileAlterationMonitor monitor = new FileAlterationMonitor(interval, observer);
			monitor.start();
		}
	}
}
 
Example #2
Source File: FileMonitor.java    From util4j with Apache License 2.0 6 votes vote down vote up
/**
 * 只监控文件发送变化,如果是子目录的文件改变,则目录会变,由于没有过滤目录发生变化,则目录下的文件改变不会监控到
 * @param dir
 * @throws Exception 
 */
public void check1(String dir) throws Exception
{
	File directory = new File(dir);
    // 轮询间隔 5 秒
    long interval = TimeUnit.SECONDS.toMillis(5);
    // 创建一个文件观察器用于处理文件的格式
    IOFileFilter filter=FileFilterUtils.or(FileFilterUtils.suffixFileFilter(".class"),
    		FileFilterUtils.suffixFileFilter(".jar"));
    FileAlterationObserver observer = new FileAlterationObserver(directory,filter);
    //设置文件变化监听器
    observer.addListener(new MyFileListener());
    FileAlterationMonitor monitor = new FileAlterationMonitor(interval);
    monitor.addObserver(observer);//文件观察
    monitor.start();
}
 
Example #3
Source File: FileMonitor.java    From util4j with Apache License 2.0 6 votes vote down vote up
public void check2(String dir) throws Exception
	{
		File directory = new File(dir);
	    // 轮询间隔 5 秒
	    long interval = TimeUnit.SECONDS.toMillis(5);
	    //后缀过滤器
	    IOFileFilter filefilter=FileFilterUtils.or(FileFilterUtils.suffixFileFilter(".class"),
	    		FileFilterUtils.suffixFileFilter(".jar"));
	    //子目录的后缀
	    IOFileFilter subFilefilter=FileFilterUtils.or(FileFilterUtils.directoryFileFilter(),filefilter);
	    //根目录和子目录变化
	    IOFileFilter filter = FileFilterUtils.or(filefilter,subFilefilter);
	    FileAlterationObserver observer = new FileAlterationObserver(directory,filter);
	    //设置文件变化监听器
	    observer.addListener(new MyFileListener());
	    FileAlterationMonitor monitor = new FileAlterationMonitor(interval);
	    monitor.addObserver(observer);//文件观察
	    monitor.start();
//	    monitor.addObserver(observer);//文件观察,如果在start后面加,则会触发所有文件创建
	}
 
Example #4
Source File: ApplicationConfigMonitor.java    From canal with Apache License 2.0 6 votes vote down vote up
@PostConstruct
public void init() {
    File confDir = Util.getConfDirPath();
    try {
        FileAlterationObserver observer = new FileAlterationObserver(confDir,
            FileFilterUtils.and(FileFilterUtils.fileFileFilter(),
                FileFilterUtils.prefixFileFilter("application"),
                FileFilterUtils.suffixFileFilter("yml")));
        FileListener listener = new FileListener();
        observer.addListener(listener);
        fileMonitor = new FileAlterationMonitor(3000, observer);
        fileMonitor.start();

    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}
 
Example #5
Source File: HbaseConfigMonitor.java    From canal with Apache License 2.0 6 votes vote down vote up
public void init(HbaseAdapter hbaseAdapter, Properties envProperties) {
    this.hbaseAdapter = hbaseAdapter;
    this.envProperties = envProperties;
    File confDir = Util.getConfDirPath(adapterName);
    try {
        FileAlterationObserver observer = new FileAlterationObserver(confDir,
            FileFilterUtils.and(FileFilterUtils.fileFileFilter(), FileFilterUtils.suffixFileFilter("yml")));
        FileListener listener = new FileListener();
        observer.addListener(listener);
        fileMonitor = new FileAlterationMonitor(3000, observer);
        fileMonitor.start();

    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}
 
Example #6
Source File: TemplateWatcher.java    From roboconf-platform with Apache License 2.0 6 votes vote down vote up
@Override
public void onStart( final FileAlterationObserver observer ) {

	if( this.alreadyStarted.getAndSet( true ))
		return;

	this.logger.fine("Initial provisioning of templates...");
	final Collection<File> templateFiles = FileUtils.listFiles(
			this.templateDir,

			// Find readable template files.
			FileFilterUtils.and(
					FileFilterUtils.suffixFileFilter( ".tpl" ),
					CanReadFileFilter.CAN_READ),

			// Directory filter: go through the root template directory and its direct children.
			new TemplateDirectoryFileFilter( this.templateDir ));

	process( templateFiles );
}
 
Example #7
Source File: RdbConfigMonitor.java    From canal with Apache License 2.0 6 votes vote down vote up
public void init(String key, RdbAdapter rdbAdapter, Properties envProperties) {
    this.key = key;
    this.rdbAdapter = rdbAdapter;
    this.envProperties = envProperties;
    File confDir = Util.getConfDirPath(adapterName);
    try {
        FileAlterationObserver observer = new FileAlterationObserver(confDir,
            FileFilterUtils.and(FileFilterUtils.fileFileFilter(), FileFilterUtils.suffixFileFilter("yml")));
        FileListener listener = new FileListener();
        observer.addListener(listener);
        fileMonitor = new FileAlterationMonitor(3000, observer);
        fileMonitor.start();

    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}
 
Example #8
Source File: ESConfigMonitor.java    From canal with Apache License 2.0 6 votes vote down vote up
public void init(ESAdapter esAdapter, Properties envProperties) {
    this.esAdapter = esAdapter;
    this.envProperties = envProperties;
    this.adapterName = envProperties.getProperty("es.version");
    File confDir = Util.getConfDirPath(adapterName);
    try {
        FileAlterationObserver observer = new FileAlterationObserver(confDir,
            FileFilterUtils.and(FileFilterUtils.fileFileFilter(), FileFilterUtils.suffixFileFilter("yml")));
        FileListener listener = new FileListener();
        observer.addListener(listener);
        fileMonitor = new FileAlterationMonitor(3000, observer);
        fileMonitor.start();

    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}
 
Example #9
Source File: ESConfigMonitor.java    From canal-1.1.3 with Apache License 2.0 6 votes vote down vote up
public void init(ESAdapter esAdapter, Properties envProperties) {
        this.esAdapter = esAdapter;
        this.envProperties = envProperties;
        File confDir = Util.getConfDirPath(adapterName);
        try {
            FileAlterationObserver observer = new FileAlterationObserver(confDir,
                FileFilterUtils.and(FileFilterUtils.fileFileFilter(), FileFilterUtils.suffixFileFilter("yml")));
//            文件变化监听采用的common io的类库
            FileListener listener = new FileListener();
            observer.addListener(listener);
            fileMonitor = new FileAlterationMonitor(3000, observer);
            fileMonitor.start();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
 
Example #10
Source File: RdbConfigMonitor.java    From canal-1.1.3 with Apache License 2.0 6 votes vote down vote up
public void init(String key, RdbAdapter rdbAdapter, Properties envProperties) {
    this.key = key;
    this.rdbAdapter = rdbAdapter;
    this.envProperties = envProperties;
    File confDir = Util.getConfDirPath(adapterName);
    try {
        FileAlterationObserver observer = new FileAlterationObserver(confDir,
            FileFilterUtils.and(FileFilterUtils.fileFileFilter(), FileFilterUtils.suffixFileFilter("yml")));
        FileListener listener = new FileListener();
        observer.addListener(listener);
        fileMonitor = new FileAlterationMonitor(3000, observer);
        fileMonitor.start();

    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}
 
Example #11
Source File: HbaseConfigMonitor.java    From canal-1.1.3 with Apache License 2.0 6 votes vote down vote up
public void init(HbaseAdapter hbaseAdapter, Properties envProperties) {
    this.hbaseAdapter = hbaseAdapter;
    this.envProperties = envProperties;
    File confDir = Util.getConfDirPath(adapterName);
    try {
        FileAlterationObserver observer = new FileAlterationObserver(confDir,
            FileFilterUtils.and(FileFilterUtils.fileFileFilter(), FileFilterUtils.suffixFileFilter("yml")));
        FileListener listener = new FileListener();
        observer.addListener(listener);
        fileMonitor = new FileAlterationMonitor(3000, observer);
        fileMonitor.start();

    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}
 
Example #12
Source File: ApplicationConfigMonitor.java    From canal-1.1.3 with Apache License 2.0 6 votes vote down vote up
@PostConstruct
public void init() {
    File confDir = Util.getConfDirPath();
    try {
        FileAlterationObserver observer = new FileAlterationObserver(confDir,
            FileFilterUtils.and(FileFilterUtils.fileFileFilter(),
                FileFilterUtils.prefixFileFilter("application"),
                FileFilterUtils.suffixFileFilter("yml")));
        FileListener listener = new FileListener();
        observer.addListener(listener);
        fileMonitor = new FileAlterationMonitor(3000, observer);
        fileMonitor.start();

    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}
 
Example #13
Source File: FileMonitor.java    From MercuryTrade with MIT License 6 votes vote down vote up
public void start() {
    String gamePath = Configuration.get().applicationConfiguration().get().getGamePath();

    File folder = new File(gamePath + "logs");
    this.fileHandler = new MessageFileHandler(gamePath + "logs/Client.txt");
    FileAlterationObserver observer = new FileAlterationObserver(folder);
    monitor = new FileAlterationMonitor(POLLING_INTERVAL);
    FileAlterationListener listener = new FileAlterationListenerAdaptor() {
        @Override
        public void onFileChange(File file) {
            fileHandler.parse();
        }
    };

    observer.addListener(listener);
    monitor.addObserver(observer);
    try {
        monitor.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #14
Source File: DirectoryMonitor.java    From fuchsia with Apache License 2.0 6 votes vote down vote up
public DirectoryMonitor(String directorypath, long polling, String classname) {

        this.directory = new File(directorypath);
        this.trackedClassName = classname;
        this.polling = polling;

        if (!directory.isDirectory()) {
            LOG.info("Monitored directory {} not existing - creating directory", directory.getAbsolutePath());
            if (!this.directory.mkdirs()) {
                throw new IllegalStateException("Monitored directory doesn't exist and cannot be created.");
            }
        }

        // We observes all files.
        FileAlterationObserver observer = new FileAlterationObserver(directory, TrueFileFilter.INSTANCE);
        observer.checkAndNotify();
        observer.addListener(new FileMonitor());
        monitor = new FileAlterationMonitor(polling, observer);

    }
 
Example #15
Source File: SensitiveWordMonitor.java    From everyone-java-blog with Apache License 2.0 5 votes vote down vote up
public void monitor(String directory, int interval) {
    FileAlterationObserver fileAlterationObserver = new FileAlterationObserver(directory, FileFilterUtils.and(FileFilterUtils.nameFileFilter(SENSITIVE_WORD_FILE_NAME)), null);
    fileAlterationObserver.addListener(this);
    FileAlterationMonitor fileAlterationMonitor = new FileAlterationMonitor(interval, fileAlterationObserver);

    try {
        fileAlterationMonitor.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #16
Source File: ObserverFactoryTest.java    From bonita-ui-designer with GNU General Public License v2.0 5 votes vote down vote up
@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"));
}
 
Example #17
Source File: TorrentFileWatcher.java    From joal with Apache License 2.0 5 votes vote down vote up
TorrentFileWatcher(final FileAlterationListener listener, final Path monitoredFolder, final Integer interval) {
    Preconditions.checkNotNull(listener, "listener cannot be null");
    Preconditions.checkNotNull(monitoredFolder, "monitoredFolder cannot be null");
    Preconditions.checkArgument(Files.exists(monitoredFolder), "Folder '" + monitoredFolder.toAbsolutePath() + "' does not exists.");
    Preconditions.checkNotNull(interval, "interval cannot be null");
    Preconditions.checkArgument(interval > 0, "interval cannot be less than 1");
    this.listener = listener;
    this.monitoredFolder = monitoredFolder.toFile();
    this.monitor = new FileAlterationMonitor(interval);
    this.monitor.setThreadFactory(new ThreadFactoryBuilder().setNameFormat("torrent-file-watcher-%d").build());
    this.observer = new FileAlterationObserver(this.monitoredFolder, TORRENT_FILE_FILTER);

    this.observer.addListener(this.listener);
    monitor.addObserver(this.observer);
}
 
Example #18
Source File: WatchableDirectory.java    From opoopress with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize() {
    this.fileAlterationObserver = new FileAlterationObserver(directory, fileFilter);
    this.fileAlterationObserver.addListener(this);
    try {
        this.fileAlterationObserver.initialize();
    } catch (Exception e) {
    }
}
 
Example #19
Source File: ObserverFactoryTest.java    From bonita-ui-designer with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example #20
Source File: DefaultScriptSource.java    From util4j with Apache License 2.0 5 votes vote down vote up
protected FileAlterationObserver buildFileAlterationObserver(String directory)
{
    //后缀过滤器
    IOFileFilter suffixFileFilter=FileFilterUtils.or(FileFilterUtils.suffixFileFilter(".class"),FileFilterUtils.suffixFileFilter(".jar"));
    //子目录变化
    IOFileFilter rootAndSubFilefilter=FileFilterUtils.or(FileFilterUtils.directoryFileFilter(),suffixFileFilter);
    return new FileAlterationObserver(directory,rootAndSubFilefilter);
}
 
Example #21
Source File: DefaultScriptSource.java    From util4j with Apache License 2.0 5 votes vote down vote up
protected void init() throws Exception
{
	FileAlterationObserver observer=buildFileAlterationObserver(scriptDir);
	observer.addListener(new FileListener());
	FileAlterationMonitor monitor=new FileAlterationMonitor(updateInterval);
	monitor.addObserver(observer);
	monitor.start();
	scanResources();
}
 
Example #22
Source File: DefaultClassSource.java    From util4j with Apache License 2.0 5 votes vote down vote up
/**
 * 监视目录
 * @param directory
 * @param filter
 * @throws Exception 
 */
protected void monitorDir(String directory,IOFileFilter filter) throws Exception
{
	FileAlterationObserver observer=new FileAlterationObserver(directory,filter);
	observer.initialize();
	observer.addListener(fileListener);
	observers.add(observer);
}
 
Example #23
Source File: DefaultClassSource.java    From util4j with Apache License 2.0 5 votes vote down vote up
private void checkFileChange()
{
	for (int i=0;i<observers.size();i++) {
		 FileAlterationObserver observer =observers.get(i);
		 observer.checkAndNotify();
     }
}
 
Example #24
Source File: KuduConfigMonitor.java    From canal with Apache License 2.0 5 votes vote down vote up
public void init(KuduAdapter kuduAdapter, Properties envProperties) {
    this.kuduAdapter = kuduAdapter;
    this.envProperties = envProperties;
    File confDir = Util.getConfDirPath(adapterName);
    try {
        FileAlterationObserver observer = new FileAlterationObserver(confDir,
            FileFilterUtils.and(FileFilterUtils.fileFileFilter(), FileFilterUtils.suffixFileFilter("yml")));
        FileListener listener = new FileListener();
        observer.addListener(listener);
        fileMonitor = new FileAlterationMonitor(3000, observer);
        fileMonitor.start();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}
 
Example #25
Source File: DefaultWatchService.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Nullable
private FileAlterationObserver find(File file) {
   File parent = file.getParentFile();
   if (parent != null && parent != file) {
      FileAlterationObserver result = find(parent);
      if (result != null) {
         return result;
      }
   }

   return watches.get(file);
}
 
Example #26
Source File: Pipeline.java    From wisdom with Apache License 2.0 5 votes vote down vote up
/**
 * Starts the watching.
 *
 * @return the current pipeline.
 */
public Pipeline watch() {
    // Delete all error reports before starting the watcher.
    error = new File(baseDir, "target/pipeline");
    FileUtils.deleteQuietly(error);
    mojo.getLog().debug("Creating the target/pipeline directory : " + error.mkdirs());

    // Start the watching process.
    watcher = new FileAlterationMonitor(Integer.getInteger("watch.period", 2) * 1000);
    watcher.setThreadFactory(new DefensiveThreadFactory("wisdom-pipeline-watcher", mojo));
    FileAlterationObserver srcObserver = new FileAlterationObserver(new File(baseDir, "src"),
            TrueFileFilter.INSTANCE);
    PipelineWatcher listener = new PipelineWatcher(this);
    srcObserver.addListener(listener);
    watcher.addObserver(srcObserver);

    if (pomFileMonitoring) {
        FileAlterationObserver pomObserver = new FileAlterationObserver(baseDir, new FileFilter() {
            @Override
            public boolean accept(File file) {
                return file.equals(new File(baseDir, "pom.xml"));
            }
        });
        pomObserver.addListener(listener);
        watcher.addObserver(pomObserver);
    }

    try {
        mojo.getLog().info("Start watching " + baseDir.getAbsolutePath());
        watcher.start();
    } catch (Exception e) {
        mojo.getLog().error("Cannot start the watcher", e);
    }
    return this;
}
 
Example #27
Source File: FileObserverListener.java    From Hive2Hive with MIT License 4 votes vote down vote up
@Override
public void onStart(FileAlterationObserver observer) {
	// logger.debug("File observer for '{}' has been started.", observer.getDirectory().toPath()));
}
 
Example #28
Source File: FileObserverListener.java    From Hive2Hive with MIT License 4 votes vote down vote up
@Override
public void onStop(FileAlterationObserver observer) {
	// logger.debug("File observer for '{}' has been stopped.", observer.getDirectory().toPath()));
}
 
Example #29
Source File: SiteObserver.java    From opoopress with Apache License 2.0 4 votes vote down vote up
@Override
public void onStart(FileAlterationObserver observer) {
    this.dir = observer.getDirectory();
}
 
Example #30
Source File: TemplateWatcher.java    From roboconf-platform with Apache License 2.0 4 votes vote down vote up
/**
 * Constructor.
 * @param manager the templating manager, to which event handling is delegated.
 * @param templateDir the templates directory to watch.
 * @param pollInterval the poll interval.
 * @throws IOException if there is a problem watching the template directory.
 */
public TemplateWatcher( final TemplatingManager manager, final File templateDir, final long pollInterval ) {
	this.templateDir = templateDir;

	// Register the custom helpers.
	this.handlebars.registerHelper( AllHelper.NAME, new AllHelper());
	this.handlebars.registerHelper( IsKeyHelper.NAME, new IsKeyHelper());

	// Pretty formatting
	this.handlebars.prettyPrint( true );

	// Create the observer, register this object as the event listener.
	FileFilter fileFilter = FileFilterUtils.or(
			FileFilterUtils.and(
					FileFilterUtils.fileFileFilter(),
					FileFilterUtils.suffixFileFilter( ".tpl" ),
					CanReadFileFilter.CAN_READ,
					new TemplateFileFilter(templateDir)),
			FileFilterUtils.and(
					FileFilterUtils.directoryFileFilter(),
					CanReadFileFilter.CAN_READ,
					new TemplateSubDirectoryFileFilter(templateDir))
	);

	FileAlterationObserver observer = new FileAlterationObserver( this.templateDir, fileFilter );
	observer.addListener( this );

	// Create the monitor.
	this.monitor = new FileAlterationMonitor( pollInterval, observer );
	this.monitor.setThreadFactory( THREAD_FACTORY );

	this.manager = manager;
	this.logger.fine( "Template watcher is watching "
			+ this.templateDir
			+ " with an interval of " + pollInterval + " ms." );
}