Skip to content

Commit

Permalink
Further review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
me4502 committed May 21, 2023
1 parent 838de06 commit a9cca78
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@

package com.sk89q.worldedit.internal.schematic.backends;

import com.sk89q.worldedit.internal.annotation.SchematicPath;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ public void init() {
lock.writeLock().unlock();
}
if (event instanceof RecursiveDirectoryWatcher.FileCreatedEvent) {
LOGGER.info("New Schematic found: " + event.path());
LOGGER.debug("New Schematic found: " + event.path());
} else if (event instanceof RecursiveDirectoryWatcher.FileDeletedEvent) {
LOGGER.info("Schematic deleted: " + event.path());
LOGGER.debug("Schematic deleted: " + event.path());
}
});
}

@Override
public void uninit() {
directoryWatcher.stop();
directoryWatcher.close();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private List<Path> scanFolder(Path root) {
}
}
} catch (IOException e) {
e.printStackTrace();
LOGGER.error(e);
}
return pathList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.common.collect.HashBiMap;
import org.apache.logging.log4j.Logger;

import java.io.Closeable;
import java.io.IOException;
import java.nio.file.ClosedWatchServiceException;
import java.nio.file.Files;
Expand All @@ -40,7 +41,7 @@
* @apiNote File and folder events might be sent multiple times. Users of this class need to employ their own
* deduplication!
*/
public class RecursiveDirectoryWatcher {
public class RecursiveDirectoryWatcher implements Closeable {

/**
* Base interface for all change events.
Expand Down Expand Up @@ -131,7 +132,9 @@ public void start(Consumer<DirEntryChangeEvent> eventConsumer) {

try {
registerFolderWatchAndScanInitially(root);
} catch (IOException e) { e.printStackTrace(); }
} catch (IOException e) {
LOGGER.error(e);
}

try {
WatchKey watchKey;
Expand All @@ -155,7 +158,9 @@ public void start(Consumer<DirEntryChangeEvent> eventConsumer) {
if (Files.isDirectory(path)) { // new subfolder created, create watch for it
try {
registerFolderWatchAndScanInitially(path);
} catch (IOException e) { e.printStackTrace(); }
} catch (IOException e) {
LOGGER.error(e);
}
} else { // new file created
eventConsumer.accept(new FileCreatedEvent(path));
}
Expand Down Expand Up @@ -191,20 +196,25 @@ public void start(Consumer<DirEntryChangeEvent> eventConsumer) {
}

/**
* Stop this RecursiveDirectoryWatcher instance and wait for it to be completely shut down.
* Close this RecursiveDirectoryWatcher instance and wait for it to be completely shut down.
* @apiNote RecursiveDirectoryWatcher is not reusable!
*/
public void stop() {
@Override
public void close() {
try {
watchService.close();
} catch (IOException e) { e.printStackTrace(); }
} catch (IOException e) {
LOGGER.error(e);
}
if (watchThread != null) {
try {
watchThread.join();
} catch (InterruptedException e) { e.printStackTrace(); }
} catch (InterruptedException e) {
LOGGER.error(e);
}
watchThread = null;
}
eventConsumer = null;
}

}
}

0 comments on commit a9cca78

Please sign in to comment.