use of java.nio.file.WatchEvent in project logging-log4j2 by apache.
the class RollingAppenderTempCompressedFilePatternTest method testAppender.
@Test
public void testAppender() throws Exception {
final File dirTmp = new File(DIR_TMP);
dirTmp.mkdirs();
try (final WatchService watcher = FileSystems.getDefault().newWatchService()) {
WatchKey key = dirTmp.toPath().register(watcher, StandardWatchEventKinds.ENTRY_CREATE);
final List<String> messages = new ArrayList<>();
for (int i = 0; i < 500; ++i) {
final String message = "This is test message number " + i;
messages.add(message);
logger.debug(message);
if (i % 100 == 0) {
Thread.sleep(500);
}
}
if (!loggerContextRule.getLoggerContext().stop(30, TimeUnit.SECONDS)) {
System.err.println("Could not stop cleanly " + loggerContextRule + " for " + this);
}
final File dir = new File(DIR);
assertTrue("Directory not created", dir.exists());
final File[] files = dir.listFiles();
assertNotNull(files);
int gzippedFiles = 0;
for (final File file : files) {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream in = null;
final FileExtension ext = FileExtension.lookupForFile(file.getName());
try {
try (FileInputStream fis = new FileInputStream(file)) {
if (ext != null) {
gzippedFiles++;
try {
in = new CompressorStreamFactory().createCompressorInputStream(ext.name().toLowerCase(), fis);
} catch (final CompressorException ce) {
ce.printStackTrace();
fail("Error creating intput stream from " + file.toString() + ": " + ce.getMessage());
}
} else {
in = new FileInputStream(file);
}
assertNotNull("No input stream for " + file.getName(), in);
try {
IOUtils.copy(in, baos);
} catch (final Exception ex) {
ex.printStackTrace();
fail("Unable to decompress " + file.getAbsolutePath());
}
}
} finally {
Closer.close(in);
}
final String text = new String(baos.toByteArray(), Charset.defaultCharset());
final String[] lines = text.split("[\\r\\n]+");
for (final String line : lines) {
messages.remove(line);
}
}
assertTrue("Log messages lost : " + messages.size(), messages.isEmpty());
assertTrue("Files not rolled : " + files.length, files.length > 2);
assertTrue("Files gzipped not rolled : " + gzippedFiles, gzippedFiles > 0);
int temporaryFilesCreated = 0;
key = watcher.take();
for (final WatchEvent<?> event : key.pollEvents()) {
final WatchEvent<Path> ev = (WatchEvent<Path>) event;
final Path filename = ev.context();
if (filename.toString().endsWith(".tmp")) {
temporaryFilesCreated++;
}
}
assertTrue("No temporary file created during compression", temporaryFilesCreated > 0);
assertEquals("Temporarys file created not equals to compressed files " + temporaryFilesCreated + "/" + gzippedFiles, gzippedFiles, temporaryFilesCreated);
}
}
use of java.nio.file.WatchEvent in project logging-log4j2 by apache.
the class RollingAppenderDirectWriteTempCompressedFilePatternTest method testAppender.
@Test
public void testAppender() throws Exception {
final File dir = new File(DIR);
dir.mkdirs();
try (final WatchService watcher = FileSystems.getDefault().newWatchService()) {
WatchKey key = dir.toPath().register(watcher, StandardWatchEventKinds.ENTRY_CREATE);
for (int i = 0; i < 100; ++i) {
logger.debug("This is test message number " + i);
}
Thread.sleep(50);
assertTrue("Directory not created", dir.exists() && dir.listFiles().length > 0);
final File[] files = dir.listFiles();
assertNotNull(files);
assertThat(files, hasItemInArray(that(hasName(that(endsWith(".gz"))))));
int temporaryFilesCreated = 0;
int compressedFiles = 0;
key = watcher.take();
for (final WatchEvent<?> event : key.pollEvents()) {
final WatchEvent<Path> ev = (WatchEvent<Path>) event;
final Path filename = ev.context();
if (filename.toString().endsWith(".tmp")) {
temporaryFilesCreated++;
}
if (filename.toString().endsWith(".gz")) {
compressedFiles++;
}
}
assertTrue("No temporary file created during compression", temporaryFilesCreated > 0);
assertEquals("Temporarys file created not equals to compressed files", compressedFiles, temporaryFilesCreated);
}
}
use of java.nio.file.WatchEvent in project grails-core by grails.
the class WatchServiceDirectoryWatcher method run.
@Override
public void run() {
while (active) {
try {
WatchKey watchKey = watchService.poll(sleepTime, TimeUnit.MILLISECONDS);
if (watchKey != null) {
List<WatchEvent<?>> watchEvents = watchKey.pollEvents();
for (WatchEvent<?> watchEvent : watchEvents) {
WatchEvent.Kind<?> kind = watchEvent.kind();
if (kind == StandardWatchEventKinds.OVERFLOW) {
// TODO how is this supposed to be handled? I think the best thing to do is ignore it, but I'm not positive
LOG.warn("WatchService Overflow occurred");
continue;
}
WatchEvent<Path> pathWatchEvent = cast(watchEvent);
Path name = pathWatchEvent.context();
Path dir = (Path) watchKey.watchable();
Path child = dir.resolve(name).toAbsolutePath();
File childFile = child.toFile();
if (individualWatchedFiles.contains(child) || individualWatchedFiles.contains(child.normalize())) {
if (kind == StandardWatchEventKinds.ENTRY_CREATE) {
fireOnNew(childFile);
} else if (kind == StandardWatchEventKinds.ENTRY_MODIFY) {
fireOnChange(childFile);
} else if (kind == StandardWatchEventKinds.ENTRY_DELETE) {
// do nothing... there's no way to communicate deletions
}
} else {
List<String> fileExtensions = watchKeyToExtensionsMap.get(watchKey);
if (fileExtensions == null) {
// this event didn't match a file in individualWatchedFiles so it's a not an individual file we're interested in
// this event also didn't match a directory that we're interested in (if it did, fileExtentions wouldn't be null)
// so it must be event for a file we're not interested in. An example of how this can happen is:
// there's a directory with files in it like this:
// /images/a.png
// /images/b.png
// by using the addWatchFile method, /images/a.png is watched.
// Now, /images/b.png is changed. Because java.nio.file.WatchService watches directories, it gets a WatchEvent
// for /images/b.png. But we aren't interested in that.
LOG.debug("WatchService received an event for a file/directory that it's not interested in.");
} else {
if (kind == StandardWatchEventKinds.ENTRY_CREATE) {
// new directory created, so watch its contents
addWatchDirectory(child, fileExtensions);
if (childFile.isDirectory() && childFile.exists()) {
final File[] files = childFile.listFiles();
if (files != null) {
for (File newFile : files) {
if (isValidFileToMonitor(newFile, fileExtensions)) {
fireOnNew(newFile);
}
}
}
}
}
if (isValidFileToMonitor(childFile, fileExtensions)) {
if (kind == StandardWatchEventKinds.ENTRY_CREATE) {
fireOnNew(childFile);
} else if (kind == StandardWatchEventKinds.ENTRY_MODIFY) {
fireOnChange(childFile);
} else if (kind == StandardWatchEventKinds.ENTRY_DELETE) {
// do nothing... there's no way to communicate deletions
}
}
}
}
}
watchKey.reset();
}
} catch (InterruptedException e) {
// ignore
}
}
try {
watchService.close();
} catch (IOException e) {
LOG.debug("Exception while closing watchService", e);
}
}
use of java.nio.file.WatchEvent in project neo4j by neo4j.
the class DefaultFileSystemWatcher method startWatching.
@Override
public void startWatching() throws InterruptedException {
watch = true;
while (watch) {
WatchKey key = watchService.take();
if (key != null) {
List<WatchEvent<?>> watchEvents = key.pollEvents();
for (WatchEvent<?> watchEvent : watchEvents) {
WatchEvent.Kind<?> kind = watchEvent.kind();
if (StandardWatchEventKinds.ENTRY_MODIFY == kind) {
notifyAboutModification(key, watchEvent);
}
if (StandardWatchEventKinds.ENTRY_DELETE == kind) {
notifyAboutDeletion(key, watchEvent);
}
}
key.reset();
}
}
}
use of java.nio.file.WatchEvent in project ninja by ninjaframework.
the class WatchAndRestartMachine method run.
@Override
public void run() {
for (; ; ) {
WatchKey watchKey;
try {
watchKey = watchService.take();
takeCount.incrementAndGet();
} catch (InterruptedException e) {
if (!shutdown) {
log.error("Unexpectedly interrupted while waiting for take()", e);
}
return;
}
Path path = mapOfWatchKeysToPaths.get(watchKey);
if (path == null) {
log.error("WatchKey not recognized!!");
continue;
}
for (WatchEvent<?> watchEvent : watchKey.pollEvents()) {
WatchEvent.Kind watchEventKind = watchEvent.kind();
// TBD - provide example of how OVERFLOW watchEvent is handled
if (watchEventKind == OVERFLOW) {
continue;
}
// Context for directory entry watchEvent is the file name of entry
WatchEvent<Path> ev = (WatchEvent<Path>) watchEvent;
Path name = ev.context();
Path child = path.resolve(name);
if (watchEventKind == ENTRY_MODIFY) {
// we are not interested in events from parent directories...
if (!child.toFile().isDirectory()) {
handleNewOrModifiedFile("Modified", child);
}
}
// if directory is created, then register it and its sub-directories recursively
if (watchEventKind == ENTRY_CREATE) {
if (!child.toFile().isDirectory()) {
handleNewOrModifiedFile("New", child);
}
try {
if (Files.isDirectory(child, NOFOLLOW_LINKS)) {
registerAll(child);
}
} catch (IOException e) {
log.error("Something fishy happened. Unable to register new dir for watching", e);
}
}
}
// reset watchKey and remove from set if directory no longer accessible
boolean valid = watchKey.reset();
if (!valid) {
mapOfWatchKeysToPaths.remove(watchKey);
// all directories are inaccessible
if (mapOfWatchKeysToPaths.isEmpty()) {
break;
}
}
}
}
Aggregations