Java Code Examples for java.nio.file.StandardWatchEventKinds#ENTRY_DELETE

The following examples show how to use java.nio.file.StandardWatchEventKinds#ENTRY_DELETE . 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: WindowsWatchService.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private WatchEvent.Kind<?> translateActionToEvent(int action) {
    switch (action) {
        case FILE_ACTION_MODIFIED :
            return StandardWatchEventKinds.ENTRY_MODIFY;

        case FILE_ACTION_ADDED :
        case FILE_ACTION_RENAMED_NEW_NAME :
            return StandardWatchEventKinds.ENTRY_CREATE;

        case FILE_ACTION_REMOVED :
        case FILE_ACTION_RENAMED_OLD_NAME :
            return StandardWatchEventKinds.ENTRY_DELETE;

        default :
            return null;  // action not recognized
    }
}
 
Example 2
Source File: DirectoryWatcher.java    From word with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    DirectoryWatcher dictionaryWatcher = new DirectoryWatcher(new WatcherCallback(){
        private long lastExecute = System.currentTimeMillis();
        @Override
        public void execute(WatchEvent.Kind<?> kind, String path) {
            if(System.currentTimeMillis() - lastExecute > 1000){                  
                lastExecute = System.currentTimeMillis();
                //刷新词典
                System.out.println("事件:"+kind.name()+" ,路径:"+path);
            }
        }
    }, StandardWatchEventKinds.ENTRY_CREATE,
                StandardWatchEventKinds.ENTRY_MODIFY,
                StandardWatchEventKinds.ENTRY_DELETE);
    //监控DIC目录及其所有子目录的子目录...递归
    dictionaryWatcher.watchDirectoryTree("d:/DIC");
    //只监控DIC2目录
    dictionaryWatcher.watchDirectory("d:/DIC2");
}
 
Example 3
Source File: Watcher.java    From packagedrone with Eclipse Public License 1.0 6 votes vote down vote up
@SuppressWarnings ( "unchecked" )
private void handleEvent ( final WatchEvent<?> event )
{
    final Kind<?> kind = event.kind ();

    if ( kind == StandardWatchEventKinds.OVERFLOW )
    {
        // FIXME: full rescan
        return;
    }

    final Path path = this.path.resolve ( ( (WatchEvent<Path>)event ).context () );

    if ( kind == StandardWatchEventKinds.ENTRY_CREATE )
    {
        this.listener.event ( path, Event.ADDED );
    }
    else if ( kind == StandardWatchEventKinds.ENTRY_DELETE )
    {
        this.listener.event ( path, Event.REMOVED );
    }
    else if ( kind == StandardWatchEventKinds.ENTRY_MODIFY )
    {
        this.listener.event ( path, Event.MODIFIED );
    }
}
 
Example 4
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
public void updateTable(WatchEvent.Kind<?> kind, Path child) {
  if (kind == StandardWatchEventKinds.ENTRY_CREATE) {
    model.addPath(child);
  } else if (kind == StandardWatchEventKinds.ENTRY_DELETE) {
    for (int i = 0; i < model.getRowCount(); i++) {
      Object value = model.getValueAt(i, 2);
      String path = Objects.toString(value, "");
      if (path.equals(child.toString())) {
        deleteRowSet.add(i);
        // model.removeRow(i);
        break;
      }
    }
    sorter.setRowFilter(new RowFilter<TableModel, Integer>() {
      @Override public boolean include(Entry<? extends TableModel, ? extends Integer> entry) {
        return !isDeleteRow(entry.getIdentifier());
      }
    });
  }
}
 
Example 5
Source File: WindowsWatchService.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private WatchEvent.Kind<?> translateActionToEvent(int action) {
    switch (action) {
        case FILE_ACTION_MODIFIED :
            return StandardWatchEventKinds.ENTRY_MODIFY;

        case FILE_ACTION_ADDED :
        case FILE_ACTION_RENAMED_NEW_NAME :
            return StandardWatchEventKinds.ENTRY_CREATE;

        case FILE_ACTION_REMOVED :
        case FILE_ACTION_RENAMED_OLD_NAME :
            return StandardWatchEventKinds.ENTRY_DELETE;

        default :
            return null;  // action not recognized
    }
}
 
Example 6
Source File: WindowsWatchService.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private WatchEvent.Kind<?> translateActionToEvent(int action) {
    switch (action) {
        case FILE_ACTION_MODIFIED :
            return StandardWatchEventKinds.ENTRY_MODIFY;

        case FILE_ACTION_ADDED :
        case FILE_ACTION_RENAMED_NEW_NAME :
            return StandardWatchEventKinds.ENTRY_CREATE;

        case FILE_ACTION_REMOVED :
        case FILE_ACTION_RENAMED_OLD_NAME :
            return StandardWatchEventKinds.ENTRY_DELETE;

        default :
            return null;  // action not recognized
    }
}
 
Example 7
Source File: WatchDir.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Register the given directory with the WatchService
 *
 * @param aDir
 *        Directory to be watched. May not be <code>null</code>.
 */
private void _registerDir (@Nonnull final Path aDir) throws IOException
{
  if (LOGGER.isDebugEnabled ())
    LOGGER.debug ("Register directory " +
                  aDir +
                  (m_bRecursive && !m_bRegisterRecursiveManually ? " (recursively)" : ""));

  final WatchEvent.Kind <?> [] aKinds = new WatchEvent.Kind <?> [] { StandardWatchEventKinds.ENTRY_CREATE,
                                                                     StandardWatchEventKinds.ENTRY_DELETE,
                                                                     StandardWatchEventKinds.ENTRY_MODIFY };

  // throws exception when using with modifiers even if null
  final WatchKey aKey = m_aModifiers != null ? aDir.register (m_aWatcher, aKinds, m_aModifiers)
                                             : aDir.register (m_aWatcher, aKinds);
  m_aKeys.put (aKey, aDir);
}
 
Example 8
Source File: WindowsWatchService.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private WatchEvent.Kind<?> translateActionToEvent(int action) {
    switch (action) {
        case FILE_ACTION_MODIFIED :
            return StandardWatchEventKinds.ENTRY_MODIFY;

        case FILE_ACTION_ADDED :
        case FILE_ACTION_RENAMED_NEW_NAME :
            return StandardWatchEventKinds.ENTRY_CREATE;

        case FILE_ACTION_REMOVED :
        case FILE_ACTION_RENAMED_OLD_NAME :
            return StandardWatchEventKinds.ENTRY_DELETE;

        default :
            return null;  // action not recognized
    }
}
 
Example 9
Source File: FSEventWatchService.java    From cyberduck with GNU General Public License v3.0 6 votes vote down vote up
public MacOSXWatchKey(final Watchable file, final FSEventWatchService service, final WatchEvent.Kind<?>[] events) {
    super(service);
    this.file = file;

    boolean reportCreateEvents = false;
    boolean reportModifyEvents = false;
    boolean reportDeleteEvents = false;

    for(WatchEvent.Kind<?> event : events) {
        if(event == StandardWatchEventKinds.ENTRY_CREATE) {
            reportCreateEvents = true;
        }
        else if(event == StandardWatchEventKinds.ENTRY_MODIFY) {
            reportModifyEvents = true;
        }
        else if(event == StandardWatchEventKinds.ENTRY_DELETE) {
            reportDeleteEvents = true;
        }
    }
    this.reportCreateEvents = reportCreateEvents;
    this.reportDeleteEvents = reportDeleteEvents;
    this.reportModifyEvents = reportModifyEvents;
}
 
Example 10
Source File: WindowsWatchService.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private WatchEvent.Kind<?> translateActionToEvent(int action) {
    switch (action) {
        case FILE_ACTION_MODIFIED :
            return StandardWatchEventKinds.ENTRY_MODIFY;

        case FILE_ACTION_ADDED :
        case FILE_ACTION_RENAMED_NEW_NAME :
            return StandardWatchEventKinds.ENTRY_CREATE;

        case FILE_ACTION_REMOVED :
        case FILE_ACTION_RENAMED_OLD_NAME :
            return StandardWatchEventKinds.ENTRY_DELETE;

        default :
            return null;  // action not recognized
    }
}
 
Example 11
Source File: WindowsWatchService.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private WatchEvent.Kind<?> translateActionToEvent(int action) {
    switch (action) {
        case FILE_ACTION_MODIFIED :
            return StandardWatchEventKinds.ENTRY_MODIFY;

        case FILE_ACTION_ADDED :
        case FILE_ACTION_RENAMED_NEW_NAME :
            return StandardWatchEventKinds.ENTRY_CREATE;

        case FILE_ACTION_REMOVED :
        case FILE_ACTION_RENAMED_OLD_NAME :
            return StandardWatchEventKinds.ENTRY_DELETE;

        default :
            return null;  // action not recognized
    }
}
 
Example 12
Source File: WindowsWatchService.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private WatchEvent.Kind<?> translateActionToEvent(int action) {
    switch (action) {
        case FILE_ACTION_MODIFIED :
            return StandardWatchEventKinds.ENTRY_MODIFY;

        case FILE_ACTION_ADDED :
        case FILE_ACTION_RENAMED_NEW_NAME :
            return StandardWatchEventKinds.ENTRY_CREATE;

        case FILE_ACTION_REMOVED :
        case FILE_ACTION_RENAMED_OLD_NAME :
            return StandardWatchEventKinds.ENTRY_DELETE;

        default :
            return null;  // action not recognized
    }
}
 
Example 13
Source File: BaseWatcher.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
private void checkBaseEvents ( final WatchKey key, final List<WatchEvent<?>> events )
{
    for ( final WatchEvent<?> event : events )
    {
        if ( ! ( event.context () instanceof Path ) )
        {
            continue;
        }

        final Path path = this.base.resolve ( (Path)event.context () );

        logger.debug ( "Event for {}, {} : {} -> {}", new Object[] { event.context (), key.watchable (), event.kind (), path } );

        if ( event.kind () == StandardWatchEventKinds.ENTRY_DELETE )
        {
            // check delete
            checkDeleteStorage ( path );
        }
        else
        {
            try
            {
                checkAddStorage ( path );
            }
            catch ( final IOException e )
            {
                logger.warn ( "Failed to check for storage", e );
            }
        }
    }
}
 
Example 14
Source File: BaseWatcher.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
public void handleEvent ( final Path watchable, final WatchEvent<?> event ) throws IOException
{
    final Path path = (Path)event.context ();
    logger.debug ( "Change {} for base: {} on {}", new Object[] { event.kind (), watchable, path } );

    if ( watchable.endsWith ( "native" ) && path.toString ().equals ( "settings.xml" ) )
    {
        if ( event.kind () != StandardWatchEventKinds.ENTRY_DELETE )
        {
            check ();
        }
        else
        {
            storageRemoved ();
        }
    }
    else
    {
        if ( path.toString ().equals ( "settings.xml" ) )
        {
            if ( event.kind () == StandardWatchEventKinds.ENTRY_CREATE )
            {
                this.nativeKey = new File ( watchable.toFile (), "native" ).toPath ().register ( this.watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE );
            }
        }
        else if ( path.toString ().endsWith ( ".hds" ) )
        {
            if ( this.id != null )
            {
                this.storageManager.fileChanged ( this.path.toFile (), this.id, path.toFile () );
            }
        }
    }
}
 
Example 15
Source File: FileLock.java    From scheduling with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void run() {
    while (running) {
        WatchKey key;
        try {
            key = watcher.take();
        } catch (InterruptedException ex) {
            return;
        }

        for (WatchEvent<?> event : key.pollEvents()) {
            WatchEvent.Kind<?> kind = event.kind();

            @SuppressWarnings("unchecked")
            WatchEvent<Path> ev = (WatchEvent<Path>) event;
            Path fileName = ev.context();

            if (kind == StandardWatchEventKinds.OVERFLOW) {
                continue;
            } else if (kind == StandardWatchEventKinds.ENTRY_DELETE) {
                if (fileName.equals(this.lockFile.getFileName())) {
                    running = false;
                }
            }
        }

        boolean valid = key.reset();
        if (!valid) {
            break;
        }
    }

    try {
        watcher.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 16
Source File: PollingWatchService.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Register the given file with this watch service
 */
@Override
WatchKey register(final Path path,
                  WatchEvent.Kind<?>[] events,
                  WatchEvent.Modifier... modifiers)
     throws IOException
{
    // check events - CCE will be thrown if there are invalid elements
    final Set<WatchEvent.Kind<?>> eventSet = new HashSet<>(events.length);
    for (WatchEvent.Kind<?> event: events) {
        // standard events
        if (event == StandardWatchEventKinds.ENTRY_CREATE ||
            event == StandardWatchEventKinds.ENTRY_MODIFY ||
            event == StandardWatchEventKinds.ENTRY_DELETE)
        {
            eventSet.add(event);
            continue;
        }

        // OVERFLOW is ignored
        if (event == StandardWatchEventKinds.OVERFLOW) {
            continue;
        }

        // null/unsupported
        if (event == null)
            throw new NullPointerException("An element in event set is 'null'");
        throw new UnsupportedOperationException(event.name());
    }
    if (eventSet.isEmpty())
        throw new IllegalArgumentException("No events to register");

    // Extended modifiers may be used to specify the sensitivity level
    int sensitivity = 10;
    if (modifiers.length > 0) {
        for (WatchEvent.Modifier modifier: modifiers) {
            if (modifier == null)
                throw new NullPointerException();

            if (ExtendedOptions.SENSITIVITY_HIGH.matches(modifier)) {
                sensitivity = ExtendedOptions.SENSITIVITY_HIGH.parameter();
            } else if (ExtendedOptions.SENSITIVITY_MEDIUM.matches(modifier)) {
                sensitivity = ExtendedOptions.SENSITIVITY_MEDIUM.parameter();
            } else if (ExtendedOptions.SENSITIVITY_LOW.matches(modifier)) {
                sensitivity = ExtendedOptions.SENSITIVITY_LOW.parameter();
            } else {
                throw new UnsupportedOperationException("Modifier not supported");
            }
        }
    }

    // check if watch service is closed
    if (!isOpen())
        throw new ClosedWatchServiceException();

    // registration is done in privileged block as it requires the
    // attributes of the entries in the directory.
    try {
        int value = sensitivity;
        return AccessController.doPrivileged(
            new PrivilegedExceptionAction<PollingWatchKey>() {
                @Override
                public PollingWatchKey run() throws IOException {
                    return doPrivilegedRegister(path, eventSet, value);
                }
            });
    } catch (PrivilegedActionException pae) {
        Throwable cause = pae.getCause();
        if (cause != null && cause instanceof IOException)
            throw (IOException)cause;
        throw new AssertionError(pae);
    }
}
 
Example 17
Source File: PollingWatchService.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Register the given file with this watch service
 */
@Override
WatchKey register(final Path path,
                  WatchEvent.Kind<?>[] events,
                  WatchEvent.Modifier... modifiers)
     throws IOException
{
    // check events - CCE will be thrown if there are invalid elements
    final Set<WatchEvent.Kind<?>> eventSet = new HashSet<>(events.length);
    for (WatchEvent.Kind<?> event: events) {
        // standard events
        if (event == StandardWatchEventKinds.ENTRY_CREATE ||
            event == StandardWatchEventKinds.ENTRY_MODIFY ||
            event == StandardWatchEventKinds.ENTRY_DELETE)
        {
            eventSet.add(event);
            continue;
        }

        // OVERFLOW is ignored
        if (event == StandardWatchEventKinds.OVERFLOW) {
            continue;
        }

        // null/unsupported
        if (event == null)
            throw new NullPointerException("An element in event set is 'null'");
        throw new UnsupportedOperationException(event.name());
    }
    if (eventSet.isEmpty())
        throw new IllegalArgumentException("No events to register");

    // Extended modifiers may be used to specify the sensitivity level
    int sensitivity = 10;
    if (modifiers.length > 0) {
        for (WatchEvent.Modifier modifier: modifiers) {
            if (modifier == null)
                throw new NullPointerException();

            if (ExtendedOptions.SENSITIVITY_HIGH.matches(modifier)) {
                sensitivity = ExtendedOptions.SENSITIVITY_HIGH.parameter();
            } else if (ExtendedOptions.SENSITIVITY_MEDIUM.matches(modifier)) {
                sensitivity = ExtendedOptions.SENSITIVITY_MEDIUM.parameter();
            } else if (ExtendedOptions.SENSITIVITY_LOW.matches(modifier)) {
                sensitivity = ExtendedOptions.SENSITIVITY_LOW.parameter();
            } else {
                throw new UnsupportedOperationException("Modifier not supported");
            }
        }
    }

    // check if watch service is closed
    if (!isOpen())
        throw new ClosedWatchServiceException();

    // registration is done in privileged block as it requires the
    // attributes of the entries in the directory.
    try {
        int value = sensitivity;
        return AccessController.doPrivileged(
            new PrivilegedExceptionAction<PollingWatchKey>() {
                @Override
                public PollingWatchKey run() throws IOException {
                    return doPrivilegedRegister(path, eventSet, value);
                }
            });
    } catch (PrivilegedActionException pae) {
        Throwable cause = pae.getCause();
        if (cause != null && cause instanceof IOException)
            throw (IOException)cause;
        throw new AssertionError(pae);
    }
}
 
Example 18
Source File: FileMonitorJdkImpl.java    From util4j with Apache License 2.0 4 votes vote down vote up
public void watchRNDir(Path path) throws IOException, InterruptedException {  
    try (WatchService watchService = FileSystems.getDefault().newWatchService()) {  
        //给path路径加上文件观察服务  
        path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,  
                StandardWatchEventKinds.ENTRY_MODIFY,  
                StandardWatchEventKinds.ENTRY_DELETE);  
        // start an infinite loop  
        while (true) {  
            // retrieve and remove the next watch key  
            final WatchKey key = watchService.take();  
            // get list of pending events for the watch key  
            for (WatchEvent<?> watchEvent : key.pollEvents()) {  
                // get the kind of event (create, modify, delete)  
                final Kind<?> kind = watchEvent.kind();  
                // handle OVERFLOW event  
                if (kind == StandardWatchEventKinds.OVERFLOW) {  
                    continue;  
                }  
                //创建事件  
                if(kind == StandardWatchEventKinds.ENTRY_CREATE){  
                      
                }  
                //修改事件  
                if(kind == StandardWatchEventKinds.ENTRY_MODIFY){  
                      
                }  
                //删除事件  
                if(kind == StandardWatchEventKinds.ENTRY_DELETE){  
                      
                }  
                // get the filename for the event  
                @SuppressWarnings("unchecked")
	final WatchEvent<Path> watchEventPath = (WatchEvent<Path>) watchEvent;  
                final Path filename = watchEventPath.context();  
                // print it out  
                System.out.println(kind + " -> " + filename);  
  
            }  
            // reset the keyf  
            boolean valid = key.reset();  
            // exit loop if the key is not valid (if the directory was  
            // deleted, for  
            if (!valid) {  
                break;  
            }  
        }  
    }  
}
 
Example 19
Source File: DirWatcher.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public Void call() {
  registerAll(FS.toPath(Settings.USER_DIR.get()));
  while(!isCancelled()) {
    // wait for key to be signalled
    WatchKey key;
    try {
      key = watcher.take();
    } catch (InterruptedException e) {
      break;
    }
    // get path
    Path dir = keys.get(key);
    // ignore unregistered keys
    if (dir == null) {
      continue;
    }
    // get events
    for (WatchEvent<?> event : key.pollEvents()) {
      WatchEvent.Kind kind = event.kind();
      // ignore overflow events
      if (kind == StandardWatchEventKinds.OVERFLOW) {
        continue;
      }
      @SuppressWarnings("unchecked")
      File file = dir.resolve(((WatchEvent<Path>) event).context()).toFile();
      // handle event
      if (kind == StandardWatchEventKinds.ENTRY_CREATE && file.isDirectory()) {
        pcs.firePropertyChange("create_dir", "create", file);
        // register new keys
        registerAll(file.toPath());
      } else if (kind == StandardWatchEventKinds.ENTRY_CREATE) {
        pcs.firePropertyChange("create_file", "create", file);
      } else if (kind == StandardWatchEventKinds.ENTRY_DELETE && file.isDirectory()) {
        pcs.firePropertyChange("delete_dir", "delete", file);
      } else if (kind == StandardWatchEventKinds.ENTRY_DELETE) {
        pcs.firePropertyChange("delete_file", "delete", file);
      } else if (kind == StandardWatchEventKinds.ENTRY_MODIFY && file.isDirectory()) {
        pcs.firePropertyChange("modify_dir", "modify", file);
      } else if (kind == StandardWatchEventKinds.ENTRY_MODIFY) {
        pcs.firePropertyChange("modify_file", "modify", file);
      }
    }
    boolean valid = key.reset();
    if (!valid) {
      keys.remove(key);
      if (keys.isEmpty()) {
        break;
      }
    }
  }
  return null;
}
 
Example 20
Source File: PathsWatcher.java    From cssfx with Apache License 2.0 4 votes vote down vote up
public void watch() {
    watcherThread = new Thread(new Runnable() {
        @Override
        public void run() {
            logger(PathsWatcher.class).info("starting to monitor physical files");
            while (true) {
                WatchKey key;
                try {
                    key = watchService.take();
                } catch (InterruptedException ex) {
                    return;
                }
                Path directory = ((Path) key.watchable()).toAbsolutePath().normalize();

                for (WatchEvent<?> event : key.pollEvents()) {
                    WatchEvent.Kind<?> kind = event.kind();
                    logger(PathsWatcher.class).debug("'%s' change detected in directory %s", kind, directory);

                    if (kind == StandardWatchEventKinds.ENTRY_MODIFY || kind == StandardWatchEventKinds.ENTRY_CREATE || kind == StandardWatchEventKinds.ENTRY_DELETE) {
                        // it is a modification
                        @SuppressWarnings("unchecked")
                        WatchEvent<Path> ev = (WatchEvent<Path>) event;
                        Path modifiedFile = directory.resolve(ev.context()).toAbsolutePath().normalize();

                        if (filesActions.containsKey(directory.toString())) {
                            logger(PathsWatcher.class).debug("file: %s was modified", modifiedFile.getFileName());
                            Map<String, List<Runnable>> filesAction = filesActions.get(directory.toString());
                            if (filesAction.containsKey(modifiedFile.toString())) {
                                logger(PathsWatcher.class).debug("file is monitored");
                                List<Runnable> actions = filesAction.get(modifiedFile.toString());
                                logger(PathsWatcher.class).debug("%d CSS modification will be performed ", actions.size());
                                
                                for (Runnable action : actions) {
                                    action.run();
                                }
                            } else {
                                logger(PathsWatcher.class).debug("file is not monitored");
                            }
                        }
                    }
                }

                boolean valid = key.reset();
                if (!valid) {
                    break;
                }
            }
        }
    }, "CSSFX-file-monitor");
    watcherThread.setDaemon(true);
    watcherThread.start();
}