1717package org .metafacture .io ;
1818
1919import org .metafacture .framework .FluxCommand ;
20+ import org .metafacture .framework .MetafactureLogger ;
2021import org .metafacture .framework .ObjectReceiver ;
2122import org .metafacture .framework .annotations .Description ;
2223import org .metafacture .framework .annotations .In ;
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 }
0 commit comments