Currently I am working on some kind of mechanism which will be able to monitor directory and when file with extension .pdf
appears I need to mock some kind of processing on this file. Let's say, renaming and creating log file with the information of this processing.
And as I wrote it is some kind of mock. So information which will be added to log file will be mocked/hard coded.
So what I was able to do is this:
public class FileUtils {
private FileUtils() {}
private static final EnumMap<ImportOption, Consumer<String>> runOnOption = new EnumMap<> (ImportOption.class);
static {
runOnOption.put(ImportOption.SUCCEEDED, (filePath) -> processSucceededPdfFile(filePath));
runOnOption.put(ImportOption.FAILED, (filePath) -> processFailFile(filePath));
runOnOption.put(ImportOption.WARNING, (filePath) -> processWarningPdfFile(filePath));
runOnOption.put(ImportOption.ERROR_ON_MISSING_RIGHTS, (filePath) -> processErrorPdfFile(filePath));
}
private final static String FILE_PATH_TO_DIR = "D:\\dirTest";
private static final String SUCCEEDED_EXTENSION = ".1.SUCCEEDED";
private static final String UPDATING_EXTENSION = ".1.UPDATING";
private static final String log_EXTENSION = ".1.log";
public static void monitorDirectory(String dirLocation, ImportOption option) {
String dirPath = FILE_PATH_TO_DIR + File.separator + dirPath;
try {
WatchService watchService = FileSystems.getDefault().newWatchService();
Path path = Paths.get(dirPath);
path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);
System.out.println("Starting monitoring: ");
WatchKey key = watchService.take();
for (WatchEvent<?> event : key.pollEvents()) {
String fileName = event.context().toString();
if (ispdfFile(fileName)) {
runOnOption.get(option).accept(fileName);
break;
}
}
}
catch (IOException | InterruptedException e) {
}
}
private static boolean ispdfFile(String fileName) {
return fileName.substring(fileName.lastIndexOf('.'), fileName.length()).equals(".pdf");
}
private static void processFailFile(String filePath){
Path pdfFilePath = Paths.get(filePath);
Path updatingPath = Paths.get(filePath + UPDATING_EXTENSION);
Path failedPath = Paths.get(filePath + SUCCEEDED_EXTENSION);
try {
Files.move(pdfFilePath, updatingPath);
Files.move(updatingPath, failedPath);
}
catch (IOException e) {
}
}
private static void processSucceededpdfFile(String filePath){
Path pdfFilePath = Paths.get(filePath);
Path updatingPath = Paths.get(filePath + UPDATING_EXTENSION);
Path logPath = Paths.get(filePath + LOG_EXTENSION);
Path succeededPath = Paths.get(filePath + SUCCEEDED_EXTENSION);
try {
Files.move(pdfFilePath, updatingPath);
Files.move(updatingPath, succeededPath);
Files.createFile(logPath);
Files.write(logPath, LogFileContent.SUCCEEDED_LOG_CONTENT.getBytes());
}
catch (IOException e) {
}
}
private static void processErrorpdfFile(String filePath){
Path pdfFilePath = Paths.get(filePath);
Path updatingGPath = Paths.get(filePath + UPDATING_EXTENSION);
Path logPath = Paths.get(filePath + LOG_EXTENSION);
Path succeededPath = Paths.get(filePath + SUCCEEDED_EXTENSION);
try {
Files.move(pdfFilePath, updatingPath);
Files.move(updatingPath, succeededPath);
Files.createFile(logPath);
Files.write(logPath, LogFileContent.ERROR_RIGHT_LOG_CONTENT.getBytes());
}
catch (Exception e) {
}
}
private static void processWarningpdfFile(String filePath) {
Path pdfFilePath = Paths.get(filePath);
Path updatingPath = Paths.get(filePath + UPDATING_EXTENSION);
Path logPath = Paths.get(filePath + LOG_EXTENSION);
Path succeededPath = Paths.get(filePath + SUCCEEDED_EXTENSION);
try {
Files.move(pdfFilePath, updatingPath);
Files.move(updatingGPath, succeededPath);
Files.createFile(logPath);
Files.write(logPath, LogFileContent.WARNING_LOG_CONTENT.getBytes());
}
catch (Exception e) {
}
}
}
So this code will be used like this:
FileUtils.monitorDirectory(dirLocation, importOption/*Enum*/);
So what I dont like? Mostly every method witch process
in name. They seems to be realy similar to each others. And because it is utils/static class I am not sure how to reduce this duplicate code.
ImportOption enum in some time could contain much more options. But processing will not change much, except maybe content of log file.
If you have any other points, please go ahead with them.