@Override public boolean doFilter(WatchEvent<?> object) { final WatchContext context = (WatchContext) object.context(); if (object.kind().equals(StandardWatchEventKind.ENTRY_MODIFY)) { if (shouldFilter(context.getOldPath())) { return true; } } else if (object.kind().equals(StandardWatchEventKind.ENTRY_CREATE)) { if (shouldFilter(context.getPath())) { return true; } } else if (object.kind().equals(StandardWatchEventKind.ENTRY_RENAME)) { if (shouldFilter(context.getOldPath())) { return true; } } else if (object.kind().equals(StandardWatchEventKind.ENTRY_DELETE)) { if (shouldFilter(context.getOldPath())) { return true; } } return false; }
@Test public void simpleWatcherTest() { final Path init = ioService.get(URI.create("git://amend-repo-test/dora1.txt")); final WatchService ws = init.getFileSystem().newWatchService(); ioService.write(init, "init!"); { List<WatchEvent<?>> events = ws.poll().pollEvents(); WatchEvent.Kind<?> kind = events.get(0).kind(); assertEquals(kind.name(), StandardWatchEventKind.ENTRY_CREATE.name()); assertEquals(1, events.size()); } ioService.write(init, "init 2!"); { List<WatchEvent<?>> events = ws.poll().pollEvents(); WatchEvent.Kind<?> kind = events.get(0).kind(); assertEquals(kind.name(), StandardWatchEventKind.ENTRY_MODIFY.name()); assertEquals(1, events.size()); } } }
@Test public void simpleWatcherTest() { final Path init = ioService.get(URI.create("git://amend-repo-test/dora1.txt")); final WatchService ws = init.getFileSystem().newWatchService(); ioService.write(init, "init!"); { List<WatchEvent<?>> events = ws.poll().pollEvents(); WatchEvent.Kind<?> kind = events.get(0).kind(); assertEquals(kind.name(), StandardWatchEventKind.ENTRY_CREATE.name()); assertEquals(1, events.size()); } ioService.write(init, "init 2!"); { List<WatchEvent<?>> events = ws.poll().pollEvents(); WatchEvent.Kind<?> kind = events.get(0).kind(); assertEquals(kind.name(), StandardWatchEventKind.ENTRY_MODIFY.name()); assertEquals(1, events.size()); } } }
final WatchContext context = (WatchContext) event.context(); if (event.kind().equals(StandardWatchEventKind.ENTRY_MODIFY)) { if (context.getOldPath().getFileName().toString().equals(LAST_MODIFIED_MARKER_FILE)) { markerFileModified = true; break; } else if (event.kind().equals(StandardWatchEventKind.ENTRY_CREATE)) { if (context.getPath().getFileName().toString().equals(LAST_MODIFIED_MARKER_FILE)) { markerFileModified = true; break; } else if (event.kind().equals(StandardWatchEventKind.ENTRY_RENAME)) { if (context.getOldPath().getFileName().toString().equals(LAST_MODIFIED_MARKER_FILE)) { markerFileModified = true; break; } else if (event.kind().equals(StandardWatchEventKind.ENTRY_DELETE)) { if (context.getOldPath().getFileName().toString().equals(LAST_MODIFIED_MARKER_FILE)) { markerFileModified = true;
final WatchContext context = (WatchContext) event.context(); if (event.kind().equals(StandardWatchEventKind.ENTRY_MODIFY)) { if (context.getOldPath().getFileName().toString().equals(LAST_MODIFIED_MARKER_FILE)) { markerFileModified = true; break; } else if (event.kind().equals(StandardWatchEventKind.ENTRY_CREATE)) { if (context.getPath().getFileName().toString().equals(LAST_MODIFIED_MARKER_FILE)) { markerFileModified = true; break; } else if (event.kind().equals(StandardWatchEventKind.ENTRY_RENAME)) { if (context.getOldPath().getFileName().toString().equals(LAST_MODIFIED_MARKER_FILE)) { markerFileModified = true; break; } else if (event.kind().equals(StandardWatchEventKind.ENTRY_DELETE)) { if (context.getOldPath().getFileName().toString().equals(LAST_MODIFIED_MARKER_FILE)) { markerFileModified = true;
if (!filter.doFilter(event)) { if (firstContext == null) { firstContext = (WatchContext) event.context(); final WatchEvent<?> event = events.get(0); if (!filter.doFilter(event)) { if (event.kind().equals(StandardWatchEventKind.ENTRY_MODIFY)) { resourceUpdatedEvent.fire(buildEvent(ResourceUpdatedEvent.class, event).getK2()); } else if (event.kind().equals(StandardWatchEventKind.ENTRY_CREATE)) { resourceAddedEvent.fire(buildEvent(ResourceAddedEvent.class, event).getK2()); } else if (event.kind().equals(StandardWatchEventKind.ENTRY_RENAME)) { resourceRenamedEvent.fire(buildEvent(ResourceRenamedEvent.class, event).getK2()); } else if (event.kind().equals(StandardWatchEventKind.ENTRY_DELETE)) { resourceDeletedEvent.fire(buildEvent(ResourceDeletedEvent.class, event).getK2());
private Pair<Path, ResourceChange> buildChange(final WatchEvent<?> event) { final WatchContext context = (WatchContext) event.context(); final Path _affectedPath; final ResourceChange result; if (event.kind().equals(StandardWatchEventKind.ENTRY_MODIFY)) { _affectedPath = convert(context.getOldPath()); result = new ResourceUpdated(context.getMessage()); } else if (event.kind().equals(StandardWatchEventKind.ENTRY_CREATE)) { _affectedPath = convert(context.getPath()); result = new ResourceAdded(context.getMessage()); } else if (event.kind().equals(StandardWatchEventKind.ENTRY_RENAME)) { _affectedPath = convert(context.getOldPath()); result = new ResourceRenamed(convert(context.getPath()), context.getMessage()); } else if (event.kind().equals(StandardWatchEventKind.ENTRY_DELETE)) { _affectedPath = convert(context.getOldPath()); result = new ResourceDeleted(context.getMessage()); } else { _affectedPath = null; result = null; } if (_affectedPath == null) { return null; } return Pair.newPair(_affectedPath, result); }
if (object.kind() == StandardWatchEventKind.ENTRY_MODIFY) { System.out.println("Modify: " + object.context().toString()); if (object.kind() == StandardWatchEventKind.ENTRY_RENAME) { System.out.println("Rename: " + object.context().toString()); if (object.kind() == StandardWatchEventKind.ENTRY_DELETE) { System.out.println("Delete: " + object.context().toString()); if (object.kind() == StandardWatchEventKind.ENTRY_CREATE) { System.out.println("Created: " + object.context().toString());
if (object.kind() == StandardWatchEventKind.ENTRY_MODIFY) { System.out.println("Modify: " + object.context().toString()); if (object.kind() == StandardWatchEventKind.ENTRY_RENAME) { System.out.println("Rename: " + object.context().toString()); if (object.kind() == StandardWatchEventKind.ENTRY_DELETE) { System.out.println("Delete: " + object.context().toString()); if (object.kind() == StandardWatchEventKind.ENTRY_CREATE) { System.out.println("Created: " + object.context().toString());
@SuppressWarnings("unchecked") private WatchEvent<?> mockWatchEvent(WatchEvent.Kind kind, String originalPathURI, String newPathURI, String sessionId, String userId, String commitMessage) throws URISyntaxException { WatchEvent<?> event = mock(WatchEvent.class); when(event.kind()).thenReturn(kind); FileSystem fileSystem = mock(FileSystem.class); Set<String> supportedViews = new HashSet<>(); when(fileSystem.supportedFileAttributeViews()).thenReturn(supportedViews); WatchContext context = mock(WatchContext.class); when(context.getSessionId()).thenReturn(sessionId); when(context.getUser()).thenReturn(userId); when(context.getMessage()).thenReturn(commitMessage); Path oldPath = mock(Path.class); URI oldPathUri = new URI(originalPathURI); when(oldPath.toUri()).thenReturn(oldPathUri); when(oldPath.getFileSystem()).thenReturn(fileSystem); Path path = mock(Path.class); URI pathUri = new URI(newPathURI); when(path.toUri()).thenReturn(pathUri); when(path.getFileSystem()).thenReturn(fileSystem); when(context.getOldPath()).thenReturn(oldPath); when(context.getPath()).thenReturn(path); when(event.context()).thenReturn(context); return event; }
@SuppressWarnings("unchecked") private WatchEvent<?> mockWatchEvent(WatchEvent.Kind kind, String originalPathURI, String newPathURI, String sessionId, String userId, String commitMessage) throws URISyntaxException { WatchEvent<?> event = mock(WatchEvent.class); when(event.kind()).thenReturn(kind); FileSystem fileSystem = mock(FileSystem.class); Set<String> supportedViews = new HashSet<>(); when(fileSystem.supportedFileAttributeViews()).thenReturn(supportedViews); WatchContext context = mock(WatchContext.class); when(context.getSessionId()).thenReturn(sessionId); when(context.getUser()).thenReturn(userId); when(context.getMessage()).thenReturn(commitMessage); Path oldPath = mock(Path.class); URI oldPathUri = new URI(originalPathURI); when(oldPath.toUri()).thenReturn(oldPathUri); when(oldPath.getFileSystem()).thenReturn(fileSystem); Path path = mock(Path.class); URI pathUri = new URI(newPathURI); when(path.toUri()).thenReturn(pathUri); when(path.getFileSystem()).thenReturn(fileSystem); when(context.getOldPath()).thenReturn(oldPath); when(context.getPath()).thenReturn(path); when(event.context()).thenReturn(context); return event; }
private <T extends ResourceEvent> Pair<Path, T> buildEvent(final Class<T> clazz, final WatchEvent<?> event) { final WatchContext context = (WatchContext) event.context(); if (event.kind().equals(StandardWatchEventKind.ENTRY_MODIFY)) { _affectedPath = convert(context.getOldPath()); result = (T) new ResourceUpdatedEvent(_affectedPath, context.getMessage(), sessionInfo(context)); } else if (event.kind().equals(StandardWatchEventKind.ENTRY_CREATE)) { _affectedPath = convert(context.getPath()); result = (T) new ResourceAddedEvent(_affectedPath, context.getMessage(), sessionInfo(context)); } else if (event.kind().equals(StandardWatchEventKind.ENTRY_RENAME)) { _affectedPath = convert(context.getOldPath()); result = (T) new ResourceRenamedEvent(_affectedPath, context.getMessage(), sessionInfo(context)); } else if (event.kind().equals(StandardWatchEventKind.ENTRY_DELETE)) { _affectedPath = convert(context.getOldPath()); result = (T) new ResourceDeletedEvent(_affectedPath,