List of usage examples for org.apache.commons.io.monitor FileAlterationObserver removeListener
public void removeListener(final FileAlterationListener listener)
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 ww . ja v a 2 s .co 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; }
From source file:org.jboss.forge.addon.resource.monitor.FileMonitor.java
void destroy(@Observes @Local PreShutdown preShutdown) throws Exception { for (FileAlterationObserver observer : alterationMonitor.getObservers()) { for (FileAlterationListener listener : observer.getListeners()) { observer.removeListener(listener); }/*from w ww .ja v a 2 s . co m*/ alterationMonitor.removeObserver(observer); } alterationMonitor.stop(); }
From source file:org.jboss.pressgang.ccms.contentspec.client.commands.EditCommand.java
protected void editFile(final File file, final Integer id) { // Add a listener for any changes to the file content final FileFilter fileFilter = FileFilterUtils.and(FileFilterUtils.fileFileFilter(), FileFilterUtils.nameFileFilter(file.getName())); final FileAlterationObserver fileObserver = new FileAlterationObserver(file.getParentFile(), fileFilter); final FileAlterationMonitor monitor = new FileAlterationMonitor(FILE_CHECK_INTERVAL); monitor.addObserver(fileObserver);/* w w w.j a va 2s . c o m*/ // Create the listener, where on changes (ie saves), the content is saved to PressGang final String[] currentContent = { null }; final FileAlterationListener listener = new FileAlterationListenerAdaptor() { @Override public void onFileChange(final File file) { final String content = FileUtilities.readFileContents(file); final String prevContent = getCurrentContent(); setCurrentContent(content); if (prevContent == null || !content.trim().equals(prevContent.trim())) { // If we are already saving something then wait until it's finished while (saving.get()) { try { Thread.sleep(1000); } catch (InterruptedException e) { // Do nothing } } // Make sure this content is still the latest (ie another save hasn't been done) final String currentContent = getCurrentContent(); if (content.trim().equals(currentContent.trim())) { saveChanges(id, content); } } } protected synchronized void setCurrentContent(final String content) { currentContent[0] = content; } protected synchronized String getCurrentContent() { return currentContent[0]; } }; // Add the listener and start the monitor fileObserver.addListener(listener); try { monitor.start(); } catch (Exception e) { e.printStackTrace(); } // Open the file in the editor JCommander.getConsole().println(ClientUtilities.getMessage("OPENING_FILE_MSG", file.getAbsoluteFile())); try { final Process process = ClientUtilities.runCommand(getCommand(file.getAbsolutePath()), null, null); final long startTime = System.currentTimeMillis(); // Add a stream reader to clear anything that might stop the process from finishing final BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream())); String errorMsg; while ((errorMsg = reader.readLine()) != null) { printError(errorMsg, false); } // Wait for the editor to close try { process.waitFor(); // If the time between the start and the end is small (ie 1 second) then it means the program probably forked a child process // and the parent has ended. So wait instead for the user to type "exit". final long endTime = System.currentTimeMillis(); if (endTime - startTime < MIN_START_INTERVAL) { final Scanner sc = new Scanner(System.in); printWarn(ClientUtilities.getMessage("WARN_EDITOR_FORKED_MSG")); String answer = sc.nextLine(); while (!(answer.equalsIgnoreCase("exit") || answer.equalsIgnoreCase("quit") || answer.equalsIgnoreCase("q"))) { answer = sc.nextLine(); } } // Wait a little to allow for changes to be picked up Thread.sleep(FILE_CHECK_INTERVAL); } catch (InterruptedException e) { } } catch (IOException e) { printError(e.getMessage(), false); } // Clean up try { monitor.stop(); } catch (Exception e) { e.printStackTrace(); } fileObserver.removeListener(listener); // Wait for any saving to finish if (saving.get()) { JCommander.getConsole().println(ClientUtilities.getMessage("WAITING_FOR_SAVE_TO_COMPLETE")); while (saving.get()) { try { Thread.sleep(500); } catch (InterruptedException e) { // Do nothing } } } }