@@ -85,29 +85,19 @@ public void process(final String directory) {
8585 }
8686 System .out .println ("Directory is:" + directory );
8787
88- /* try {
89- dir.register(
90- watcher,
91- ENTRY_CREATE);
92- }
93- catch (IOException e) {
94- throw new RuntimeException(e);
95- }*/
96-
9788 new Thread ("DirectoryListener" ) {
89+ WatchKey key ;
9890 public void run () {
9991
100-
10192 while (true ) {
102- WatchKey key ;
10393 try {
10494 key = watcher .take ();
10595 } catch (InterruptedException x ) {
10696 return ;
10797 }
10898 Path dir = keys .get (key );
10999 if (dir == null ) {
110- System .err .println ("WatchKey not recognized!! " );
100+ System .err .println ("WatchKey not recognized!" );
111101 continue ;
112102 }
113103
@@ -116,41 +106,41 @@ public void run() {
116106 if (event .kind () == OVERFLOW ) {
117107 throw new OpenFailed ("Overflow event occurred on directory " + directory );
118108 }
119- System .out .println (
120- "Event kind:" + event .kind ()
109+ System .out .println ("Event kind:" + event .kind ()
121110 + ". File affected: " + event .context () + "." );
122111
123112 @ SuppressWarnings ("unchecked" )
124- Path path = ((WatchEvent <Path >) event ).context ();
125- String pathName = dir .resolve (path ). toAbsolutePath (). toString ( );
126-
127- try {
128- if ( Files . isDirectory ( Path . of ( pathName ), LinkOption . NOFOLLOW_LINKS )) {
129- registerAll (Path . of ( pathName ) );
113+ Path fileName = ((WatchEvent <Path >) event ).context ();
114+ Path absolutePath = dir .resolve (fileName );
115+ System . out . println ( "PATH: " + absolutePath . toString ());
116+ if ( Files . isDirectory ( absolutePath , LinkOption . NOFOLLOW_LINKS )) {
117+ try {
118+ registerAll (absolutePath );
130119 }
120+ catch (IOException e ) {
121+ throw new OpenFailed ("IOException event occurred on directory " + directory , e );
122+ }
123+ } else {
124+ if (fileName .toString ().equals (TRIGGER_SHUTDOWN_FILENAME )) {
125+ //getReceiver().closeStream();
126+ System .out .print (".... closing" );
127+ closeStream ();
128+ System .out .print (".... ret" );
129+ return ;
130+ }
131+ System .out .println ("Pathname ist:" + absolutePath .toString () +" going to process super!" );
132+ getReceiver ().process (absolutePath .toString ());
131133 }
132- catch (IOException e ) {
133- // ignore to keep sample readable
134- }
135-
136- System .out .println ("1=" + pathName );
137- /* pathName = dir.relativize(path).getFileName().toString();
138- System.out.println("2="+pathName);*/
139- System .out .println ("3=" + pathName );
140- pathName = dir .resolve (path ).toAbsolutePath ().toString ();
141- System .out .println ("4=" + pathName );
142-
143- if (pathName .equals (TRIGGER_SHUTDOWN_FILENAME )) {
144- //getReceiver().closeStream();
145- System .out .print (".... closing" );
146- closeStream ();
147- System .out .print (".... ret" );
148- return ;
134+ }
135+ // reset key and remove from set if directory no longer accessible
136+ boolean valid = key .reset ();
137+ if (!valid ) {
138+ keys .remove (key );
139+ // all directories are inaccessible
140+ if (keys .isEmpty ()) {
141+ break ;
149142 }
150- System .out .println ("Pathname ist:" + pathName );
151- getReceiver ().process (pathName );
152143 }
153- key .reset ();
154144 }
155145 }
156146 }.start ();
@@ -192,68 +182,4 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
192182 });
193183 }
194184
195-
196-
197- /**
198- * Process all events for keys queued to the watcher
199- */
200- void processEvents () {
201- for (;;) {
202-
203- // wait for key to be signalled
204- WatchKey key ;
205- try {
206- key = watcher .take ();
207- } catch (InterruptedException x ) {
208- return ;
209- }
210-
211- Path dir = keys .get (key );
212- if (dir == null ) {
213- System .err .println ("WatchKey not recognized!!" );
214- continue ;
215- }
216-
217- for (WatchEvent <?> event : key .pollEvents ()) {
218- WatchEvent .Kind kind = event .kind ();
219-
220- // TBD - provide example of how OVERFLOW event is handled
221- if (kind == OVERFLOW ) {
222- continue ;
223- }
224-
225- // Context for directory entry event is the file name of entry
226- WatchEvent <Path > ev = cast (event );
227- Path name = ev .context ();
228- Path child = dir .resolve (name );
229-
230- // print out event
231- System .out .format ("%s: %s\n " , event .kind ().name (), child );
232-
233- // if directory is created, and watching recursively, then
234- // register it and its sub-directories
235- if (kind == ENTRY_CREATE ) {
236- try {
237- if (Files .isDirectory (child , LinkOption .NOFOLLOW_LINKS )) {
238- registerAll (child );
239- }
240- } catch (IOException x ) {
241- // ignore to keep sample readable
242- }
243- }
244- }
245-
246- // reset key and remove from set if directory no longer accessible
247- boolean valid = key .reset ();
248- if (!valid ) {
249- keys .remove (key );
250-
251- // all directories are inaccessible
252- if (keys .isEmpty ()) {
253- break ;
254- }
255- }
256- }
257- }
258-
259185}
0 commit comments