|
| 1 | +package com.sandwich.util.io; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.Vector; |
| 9 | + |
| 10 | +public class FileMonitor { |
| 11 | + |
| 12 | + private final List<FileListener> listeners = new Vector<FileListener>(); |
| 13 | + private final DataFileHelper<Map<String, Long>> fileHashesHelper; |
| 14 | + |
| 15 | + private final Map<String, Long> fileHashesByDirectory; |
| 16 | + |
| 17 | + final File fileSystemPath; |
| 18 | + |
| 19 | + public FileMonitor(File fileSystemPath, File dataFile) { |
| 20 | + this.fileSystemPath = fileSystemPath; |
| 21 | + if(!this.fileSystemPath.exists()){ |
| 22 | + throw new IllegalArgumentException(fileSystemPath+ " cannot be found."); |
| 23 | + } |
| 24 | + fileHashesHelper = new DataFileHelper<Map<String,Long>>( |
| 25 | + dataFile, new HashMap<String,Long>()); |
| 26 | + fileHashesByDirectory = fileHashesHelper.readLastModifiedTimes(); |
| 27 | + } |
| 28 | + |
| 29 | + public String getFilesystemPath() { |
| 30 | + return fileSystemPath.getAbsolutePath(); |
| 31 | + } |
| 32 | + |
| 33 | + public void close(){ |
| 34 | + listeners.clear(); |
| 35 | + } |
| 36 | + |
| 37 | + public void addFileSavedListener(FileListener listener){ |
| 38 | + listeners.add(listener); |
| 39 | + } |
| 40 | + |
| 41 | + public void removeFileSavedListener(FileListener listener){ |
| 42 | + listeners.remove(listener); |
| 43 | + } |
| 44 | + |
| 45 | + synchronized void notifyListeners(){ |
| 46 | + try { |
| 47 | + Map<String, Long> currentHashes = getFilesystemHashes(); |
| 48 | + for(String fileName : currentHashes.keySet()){ |
| 49 | + Long currentHash = currentHashes.get(fileName); |
| 50 | + Long previousHash = fileHashesByDirectory.get(fileName); |
| 51 | + if(previousHash != null && !currentHash.equals(previousHash)){ |
| 52 | + fileHashesByDirectory.put(fileName, currentHash); |
| 53 | + File file = new File(fileName); |
| 54 | + for(FileListener listener : listeners){ |
| 55 | + listener.fileSaved(file); |
| 56 | + } |
| 57 | + break; |
| 58 | + } |
| 59 | + } |
| 60 | + } catch (IOException e) { |
| 61 | + throw new RuntimeException(e); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + Map<String, Long> getFilesystemHashes() throws IOException { |
| 66 | + final HashMap<String,Long> fileHashes = new HashMap<String,Long>(); |
| 67 | + forEachFile(fileSystemPath, fileSystemPath, new FileAction(){ |
| 68 | + public File makeDestination(File dest, String fileInDirectory) { |
| 69 | + return new File(dest, fileInDirectory); |
| 70 | + } |
| 71 | + public void sourceToDestination(File src, File dest) throws IOException { |
| 72 | + fileHashes.put(src.getAbsolutePath(), src.lastModified()); |
| 73 | + } |
| 74 | + }); |
| 75 | + return fileHashes; |
| 76 | + } |
| 77 | + |
| 78 | + public boolean isFileModifiedSinceLastPoll(String filePath, Long lastModified) { |
| 79 | + Long previousHash = fileHashesByDirectory.get(filePath); |
| 80 | + return !lastModified.equals(previousHash); |
| 81 | + } |
| 82 | + |
| 83 | + public void updateFileSaveTime(File file) { |
| 84 | + fileHashesByDirectory.put(file.getAbsolutePath(), file.lastModified()); |
| 85 | + } |
| 86 | + |
| 87 | + public static void forEachFile(File src, File dest, FileAction fileAction) throws IOException { |
| 88 | + if((src == null || !src.exists()) && dest == null){ |
| 89 | + throw new IllegalArgumentException("Both path's must actually exist"); |
| 90 | + } |
| 91 | + if (src.isDirectory()) { |
| 92 | + if (!dest.exists()) { |
| 93 | + dest.mkdir(); |
| 94 | + } |
| 95 | + String files[] = src.list(); |
| 96 | + for (int i = 0; i < files.length; i++) { |
| 97 | + forEachFile(new File(src, files[i]), fileAction.makeDestination(dest, files[i]), fileAction); |
| 98 | + } |
| 99 | + } else { |
| 100 | + if (!src.exists()) { |
| 101 | + throw new IOException("File or directory does not exist: "+src); |
| 102 | + } else { |
| 103 | + fileAction.sourceToDestination(src, dest); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments