/** * Creates and registers a file listener with the help of the specified build command. * * @param command The build command. * * @throws Exception If any exception occurs. */ private final void registerFileListener(final BuildCommand command) throws Exception { final DocsProject project = command.getProject(); final FileAlterationObserver observer = new FileAlterationObserver(project.getDirectory()); observer.addListener(new FileAlterationListenerAdaptor() { @Override public final void onDirectoryChange(final File directory) { rebuildIfNeeded(command, directory); } @Override public final void onFileChange(final File file) { rebuildIfNeeded(command, file); } }); monitor.addObserver(observer); monitor.start(); }
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(); } }
@Test public void shouldBuild() { try { final TorrentFileWatcher watcher = new TorrentFileWatcher(new FileAlterationListenerAdaptor(), torrentsPath); watcher.start(); watcher.stop(); } catch (final Throwable e) { fail("Should Not fail"); } }
private void observeConfigChanges() throws Exception { if (watchConfigInterval < 0) { return; } FileAlterationListenerAdaptor listener = new FileAlterationListenerAdaptor() { @Override public void onFileChange(File file) { LOG.info("Config file changed: {}", configPath); try { readConfig(true); } catch (ConfigException e) { exitWithError("Failed to refresh config: " + e.getMessage(), false); return; } } }; String filename = configPath.substring(configPath.lastIndexOf('/') + 1); String directory = configPath.substring(0, configPath.lastIndexOf('/')); FileAlterationObserver observer = new FileAlterationObserver( new File(directory), FileFilterUtils.nameFileFilter(filename) ); observer.addListener(listener); FileAlterationMonitor monitor = new FileAlterationMonitor(watchConfigInterval); monitor.addObserver(observer); monitor.start(); }
@Test public void shouldNotBuildWithNullMonitoredFolder() { assertThatThrownBy(() -> new TorrentFileWatcher(new FileAlterationListenerAdaptor(), null)) .isInstanceOf(NullPointerException.class) .hasMessageContaining("monitoredFolder cannot be null"); }
@Test public void shouldNotBuildWithNonExistingMonitoredFolder() { assertThatThrownBy(() -> new TorrentFileWatcher(new FileAlterationListenerAdaptor(), torrentsPath.resolve("nop"))) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("Folder '" + torrentsPath.resolve("nop").toAbsolutePath() + "' does not exists."); }
@Test public void shouldNotBuildWithNullInterval() { assertThatThrownBy(() -> new TorrentFileWatcher(new FileAlterationListenerAdaptor(), torrentsPath, null)) .isInstanceOf(NullPointerException.class) .hasMessageContaining("interval cannot be null"); }
@Test public void shouldNotBuildWithIntervalLessThan1() { assertThatThrownBy(() -> new TorrentFileWatcher(new FileAlterationListenerAdaptor(), torrentsPath, 0)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("interval cannot be less than 1"); }
/** * コピー元ディレクトリの監視を開始します。 * コピー元ディレクトリ内のファイルが更新されたら、そのファイルをコピー先ディレクトリにコピーします。 * * @param srcDir コピー元ディレクトリ * @param dstDir コピー先ディレクトリ * @return 0 (正常終了) * @thrrows Exception */ public int start(final String srcDir, final String dstDir) throws Exception { FileAlterationObserver observer = new FileAlterationObserver(srcDir); int interval = NumberUtils.toInt(System.getProperty(MONITOR_INTERVAL), 1000); final FileAlterationMonitor monitor = new FileAlterationMonitor(interval); FileAlterationListener listener = new FileAlterationListenerAdaptor() { @Override public void onFileChange(File file) { if (!file.isFile()) { return; } String relativePath = StringUtils.substringAfter(file.getAbsolutePath(), srcDir); File dstFile = new File(dstDir, relativePath); LOG.info("コピーします {} -> {}", file.getAbsolutePath(), dstFile.getAbsolutePath()); try { FileUtils.copyFile(file, dstFile); } catch (IOException e) { LOG.error("ファイルコピーに失敗 ", e); } } }; observer.addListener(listener); monitor.addObserver(observer); monitor.start(); // TODO ファイル監視方式の統一 String continuFilePath = System.getProperty("cc.cgfile", ".cg"); ControlFile continueFile = new ControlFile(continuFilePath, new ControlFile.Listener() { @Override public void onDelete() throws Exception { monitor.stop(); } }); continueFile.watch(); while(continueFile.exists()) { // 制御ファイルが存在し続ける間、処理を継続 } return 0; }