AbstractIOWatchService implements IOWatchService,
                                                        Filter<WatchEvent<?>> { @Override public void addWatchService(final FileSystem fs, final WatchService ws) { fileSystems.add(fs.getName()); watchServices.add(ws); final AsyncWatchService asyncWatchService = new AsyncWatchService() { @Override public void execute(final IOWatchServiceExecutor wsExecutor) { while (!isDisposed) { final WatchKey wk; try { wk = ws.take(); } catch (final Exception ex) { break; } try { wsExecutor.execute(wk, AbstractIOWatchService.this); } catch (final Exception ex) { LOG.error("Unexpected error during WatchService execution", ex); } boolean valid = wk.reset(); if (!valid) { break; } } } @Override public String getDescription() { return AbstractIOWatchService.this.getClass().getName() + "(" + ws.toString() + ")"; } }; if (started) { final IOWatchServiceExecutor watchServiceExecutor = getWatchServiceExecutor(); executorService.execute(new DescriptiveRunnable() { @Override public String getDescription() { return asyncWatchService.getDescription(); } @Override public void run() { asyncWatchService.execute(watchServiceExecutor); } }); } else { watchThreads.add(asyncWatchService); } } AbstractIOWatchService(); @Inject  AbstractIOWatchService(Event<ResourceBatchChangesEvent> resourceBatchChanges,
                                  Event<ResourceUpdatedEvent> resourceUpdatedEvent,
                                  Event<ResourceRenamedEvent> resourceRenamedEvent,
                                  Event<ResourceDeletedEvent> resourceDeletedEvent,
                                  Event<ResourceAddedEvent> resourceAddedEvent,
                                  @Unmanaged ExecutorService executorService); @PostConstruct void initialize(); synchronized void start(); @Override boolean hasWatchService(final FileSystem fs); @Override void addWatchService(final FileSystem fs,
                                final WatchService ws); void configureOnEvent(@Observes ApplicationStarted applicationStartedEvent);  }
@Test public void testAddWatchServiceException() { try { System.setProperty("org.uberfire.watcher.autostart", "false"); AbstractIOWatchService service = new AbstractIOWatchService(null, null, null, null, null, Executors.newCachedThreadPool(new DescriptiveThreadFactory())) { @Override public boolean doFilter(WatchEvent<?> t) { return false; } }; WatchService ws = new WatchService() { @Override public void close() throws IOException { } @Override public WatchKey poll() throws ClosedWatchServiceException { return null; } @Override public WatchKey poll(long timeout, TimeUnit unit) throws ClosedWatchServiceException, InterruptedException { return null; } @Override public WatchKey take() throws ClosedWatchServiceException, InterruptedException { return new WatchKey() { @Override public boolean isValid() { return false; } @Override public List<WatchEvent<?>> pollEvents() { return null; } @Override public boolean reset() { return false; } @Override public void cancel() { } @Override public Watchable watchable() { return null; } }; } @Override public boolean isClose() { return false; } }; service.addWatchService(mock(FileSystem.class), ws); Set<AsyncWatchService> watchThreads = null; try { Field field = AbstractIOWatchService.class.getDeclaredField("watchThreads"); field.setAccessible(true); watchThreads = (Set<AsyncWatchService>) field.get(service); } catch (Exception e) { fail(e.getMessage()); } AsyncWatchService asyncWatchService = watchThreads.iterator().next(); IOWatchServiceExecutor wsExecutor = (watchKey, filter) -> { throw new RuntimeException("dummy"); }; try { asyncWatchService.execute(wsExecutor); assertTrue(true); } catch (Exception e) { fail("Exception is thrown from asyncWatchService.execute()"); } } finally { System.clearProperty("org.uberfire.watcher.autostart"); } }