Skip to content

Commit 83ffeee

Browse files
committed
Use MetafactureLogger (#702)
- improve doc - test removing a file
1 parent fd4423d commit 83ffeee

2 files changed

Lines changed: 32 additions & 7 deletions

File tree

metafacture-io/src/main/java/org/metafacture/io/DirectoryListener.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.metafacture.io;
1818

1919
import org.metafacture.framework.FluxCommand;
20+
import org.metafacture.framework.MetafactureLogger;
2021
import org.metafacture.framework.ObjectReceiver;
2122
import org.metafacture.framework.annotations.Description;
2223
import org.metafacture.framework.annotations.In;
@@ -46,7 +47,7 @@
4647
*
4748
* @author Pascal Christoph (dr0i)
4849
*/
49-
@Description("Listens to a directory and passes occurring filenames to the receiver. " +
50+
@Description("Listens to a directory and passes filenames of occurring or modified files to the receiver." +
5051
"If a file named 'shutdownEtlNow' appears the process " +
5152
"is closed." +
5253
"Keep bug https://bugs.openjdk.org/browse/JDK-8202759 " +
@@ -59,6 +60,7 @@ public final class DirectoryListener extends DefaultObjectPipe<String, ObjectRec
5960
/* This special filename triggers the end of listing and closes the module */
6061
public static final String TRIGGER_SHUTDOWN_FILENAME = "shutdownEtlNow";
6162
private static final WatchService WATCHER;
63+
private static final MetafactureLogger LOG = new MetafactureLogger(DirectoryListener.class);
6264

6365
static {
6466
try {
@@ -104,7 +106,7 @@ private void start(final String directory) {
104106
*/
105107
private void register(final Path dir) throws IOException {
106108
final WatchKey key = dir.register(WATCHER, java.nio.file.StandardWatchEventKinds.ENTRY_CREATE, java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY);
107-
System.out.println("Add directory to watch: " + dir);
109+
LOG.combinedInfo("Add directory to watch: " + dir.toString());
108110
KEYS.put(key, dir);
109111
}
110112

@@ -148,7 +150,7 @@ public void run() {
148150
}
149151
final Path dir = KEYS.get(key);
150152
if (dir == null) {
151-
System.err.println("WatchKey not recognized!");
153+
LOG.warn("WatchKey not recognized!");
152154
continue;
153155
}
154156

@@ -157,20 +159,22 @@ public void run() {
157159
if (event.kind() == java.nio.file.StandardWatchEventKinds.OVERFLOW) {
158160
throw new OpenFailed("Overflow event occurred on directory " + directory);
159161
}
160-
System.out.println("Event kind:" + event.kind() + ". File affected: " + event.context() + ".");
162+
LOG.info("Event kind {} on file: '{}'", event.kind() , event.context());
161163

162164
@SuppressWarnings("unchecked")
163165
final Path fileName = ((WatchEvent<Path>) event).context();
164166
final Path absolutePath = dir.resolve(fileName);
165-
166167
processFile(fileName, absolutePath);
167168
}
168169
// reset key and remove from set if directory no longer accessible
169170
final boolean valid = key.reset();
170171
if (!valid) {
171172
KEYS.remove(key);
173+
LOG.info("Directory no longer accessible: " + key.toString());
172174
// all directories are inaccessible
173175
if (KEYS.isEmpty()) {
176+
LOG.combinedWarn("Root directory {} is not accessible anymore. Closing ...", directory);
177+
closeStream();
174178
break;
175179
}
176180
}
@@ -188,10 +192,12 @@ private void processFile(final Path fileName, final Path absolutePath) {
188192
}
189193
else {
190194
if (fileName.toString().equals(TRIGGER_SHUTDOWN_FILENAME)) {
195+
LOG.combinedInfo("Shutdown triggered. Going down ...");
191196
closeStream();
192197
Thread.currentThread().interrupt();
193198
}
194199
else {
200+
LOG.combinedDebug("processing '{}'", absolutePath.toString());
195201
getReceiver().process(absolutePath.toString());
196202
}
197203
}

metafacture-io/src/test/java/org/metafacture/io/DirectoryListenerTest.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import org.mockito.junit.MockitoJUnit;
2929
import org.mockito.junit.MockitoRule;
3030

31+
import static org.mockito.Mockito.times;
32+
3133
import java.io.File;
3234
import java.io.IOException;
3335
import java.nio.file.Files;
@@ -84,11 +86,23 @@ public void testFileOccursInSubdirectory() throws InterruptedException {
8486
Mockito.verify(receiver, org.mockito.Mockito.timeout(MAX_MILLISECONDS_WAITING_OF_THREAD)).process(pathToTestfile);
8587
}
8688

89+
@Test
90+
public void testFileOccursAndIsIgnoredWhenDeleted() throws InterruptedException {
91+
final String pathToTestfile = pathToDirectory + FILE_NAME + "1";
92+
createFile(pathToTestfile);
93+
Mockito.verify(receiver, org.mockito.Mockito.timeout(MAX_MILLISECONDS_WAITING_OF_THREAD)).process(pathToTestfile);
94+
Mockito.verify(receiver,times(1)).process(pathToTestfile);
95+
removeFile(pathToTestfile);
96+
Thread.sleep(100); // because of https://bugs.openjdk.org/browse/JDK-8202759
97+
Mockito.verify(receiver,times(1)).process(pathToTestfile);
98+
99+
}
100+
87101
@Test
88102
public void testDontProcessDirectoryWithoutFiles() throws InterruptedException {
89-
final String pathToTestfile = pathToDirectory + SUBDIRECTORY_NAME;
90-
Thread.sleep(400); // because of https://bugs.openjdk.org/browse/JDK-8202759
103+
final String pathToTestfile = pathToSubdirectory;
91104
createFile(pathToTestfile);
105+
Thread.sleep(100); // because of https://bugs.openjdk.org/browse/JDK-8202759
92106
Mockito.verify(receiver, org.mockito.Mockito.timeout(MAX_MILLISECONDS_WAITING_OF_THREAD).times(0)).process(pathToTestfile);
93107
}
94108

@@ -120,4 +134,9 @@ private void createDirectory(final String dir) {
120134
throw new RuntimeException(e);
121135
}
122136
}
137+
138+
private void removeFile(final String path) {
139+
final File testFile = new File(path);
140+
testFile.delete();
141+
}
123142
}

0 commit comments

Comments
 (0)