Skip to content

Commit

Permalink
- add Files.isReadable() check
Browse files Browse the repository at this point in the history
 - log the watching file path
  • Loading branch information
poljudov committed May 31, 2020
1 parent bae63bf commit 0bc13fc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import java.util.concurrent.Executor;

import static com.google.common.base.Strings.isNullOrEmpty;
import static java.nio.file.Files.exists;
import static java.nio.file.Files.isDirectory;
import static java.nio.file.Files.isReadable;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
import static java.util.Objects.requireNonNullElse;
import static java.util.concurrent.Executors.newSingleThreadExecutor;
Expand All @@ -37,7 +37,7 @@ class ChoicesAutoReloadConfiguration {

@Bean(CHOICES_HOT_RELOAD_EXECUTOR)
Executor choicesHotReloadExecutor() {
return newSingleThreadExecutor(new CustomizableThreadFactory("hot-reload"));
return newSingleThreadExecutor(new CustomizableThreadFactory("hot-reload-"));
}

@Bean
Expand All @@ -52,8 +52,8 @@ FileWatcherProvider fileWatcherProvider(ChoicesAutoReloadProperties choiceProper
try {
Path choicesPropertiesPath = Paths.get(choicesPropertiesLocation); //check exception

if (!exists(choicesPropertiesPath)) {
log.warn("File '{}' does not exist", choicesPropertiesLocation);
if (!isReadable(choicesPropertiesPath)) {
log.warn("File '{}' does not exist or there is no read access to this file", choicesPropertiesLocation);
return null;
}
if (isDirectory(choicesPropertiesPath)) {
Expand All @@ -74,22 +74,26 @@ static class FileWatcherProvider {

FileWatcherProvider(Path choicesPropertiesPath) throws IOException {
this.choicesPropertiesPath = choicesPropertiesPath;
this.watchDir = getParent(choicesPropertiesPath);
this.watchDir = fetchParent(choicesPropertiesPath);
this.watchService = FileSystems.getDefault().newWatchService();

watchDir.register(watchService, ENTRY_MODIFY);
}

public WatchService getWatchService() {
return watchService;
}

public Path getChoicesPropertiesPath() {
return choicesPropertiesPath;
}

public boolean isChoicesPropertiesFileEvent(Path eventPath) {
Path eventResolvedPath = watchDir.resolve(eventPath);
return Objects.equals(choicesPropertiesPath, eventResolvedPath);
}

public WatchService getWatchService() {
return watchService;
}

private Path getParent(Path path) {
private Path fetchParent(Path path) {
return requireNonNullElse(path.getParent(), path);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void watchFile() {
return;
}
try {
log.info("Start watching for the choices properties file");
log.info("Start watching for the choices properties file: '{}'", fileWatcherProvider.getChoicesPropertiesPath().toAbsolutePath());
WatchService watchService = fileWatcherProvider.getWatchService();
WatchKey key;
while ((key = watchService.take()) != null) {
Expand Down

0 comments on commit 0bc13fc

Please sign in to comment.