|
|
@@ -1,17 +1,5 @@
|
|
|
package info.fetter.logstashforwarder;
|
|
|
|
|
|
-import java.io.File;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-
|
|
|
-import org.apache.commons.io.FilenameUtils;
|
|
|
-import org.apache.commons.io.filefilter.FileFilterUtils;
|
|
|
-import org.apache.commons.io.filefilter.IOFileFilter;
|
|
|
-import org.apache.commons.io.filefilter.WildcardFileFilter;
|
|
|
-import org.apache.commons.io.monitor.FileAlterationListener;
|
|
|
-import org.apache.commons.io.monitor.FileAlterationObserver;
|
|
|
-import org.apache.log4j.Logger;
|
|
|
-
|
|
|
/*
|
|
|
* Copyright 2015 Didier Fetter
|
|
|
*
|
|
|
@@ -29,16 +17,35 @@ import org.apache.log4j.Logger;
|
|
|
*
|
|
|
*/
|
|
|
|
|
|
+import info.fetter.logstashforwarder.util.LastModifiedFileFilter;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
+import org.apache.commons.io.FilenameUtils;
|
|
|
+import org.apache.commons.io.filefilter.FileFilterUtils;
|
|
|
+import org.apache.commons.io.filefilter.IOFileFilter;
|
|
|
+import org.apache.commons.io.filefilter.WildcardFileFilter;
|
|
|
+import org.apache.commons.io.monitor.FileAlterationListener;
|
|
|
+import org.apache.commons.io.monitor.FileAlterationObserver;
|
|
|
+import org.apache.log4j.Logger;
|
|
|
+
|
|
|
public class FileWatcher implements FileAlterationListener {
|
|
|
private static final Logger logger = Logger.getLogger(FileWatcher.class);
|
|
|
private List<FileAlterationObserver> observerList = new ArrayList<FileAlterationObserver>();
|
|
|
- static final int ONE_DAY = 24 * 3600 * 1000;
|
|
|
- private long cutoff;
|
|
|
-
|
|
|
+ private static final int ONE_DAY = 24 * 3600 * 1000;
|
|
|
+ private long deadTime;
|
|
|
+ private Map<File,FileState> watchMap = new HashMap<File,FileState>();
|
|
|
+
|
|
|
public FileWatcher(long deadTime) {
|
|
|
- cutoff = System.currentTimeMillis() - deadTime;
|
|
|
+ this.deadTime = deadTime;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public FileWatcher() {
|
|
|
this(ONE_DAY);
|
|
|
}
|
|
|
@@ -58,38 +65,54 @@ public class FileWatcher implements FileAlterationListener {
|
|
|
}
|
|
|
|
|
|
private void watchFile(String fileToWatch) throws Exception {
|
|
|
- logger.info("Watching file : " + new File(fileToWatch).getAbsolutePath());
|
|
|
+ logger.info("Watching file : " + new File(fileToWatch).getCanonicalPath());
|
|
|
String directory = FilenameUtils.getFullPath(fileToWatch);
|
|
|
String fileName = FilenameUtils.getName(fileToWatch);
|
|
|
IOFileFilter fileFilter = FileFilterUtils.and(
|
|
|
FileFilterUtils.fileFileFilter(),
|
|
|
FileFilterUtils.nameFileFilter(fileName),
|
|
|
- FileFilterUtils.ageFileFilter(cutoff, false));
|
|
|
- FileAlterationObserver observer = new FileAlterationObserver(new File(directory), fileFilter);
|
|
|
- observer.addListener(this);
|
|
|
- observerList.add(observer);
|
|
|
- observer.initialize();
|
|
|
+ new LastModifiedFileFilter(deadTime));
|
|
|
+ updateWatchMap(new File(directory), fileFilter);
|
|
|
}
|
|
|
|
|
|
private void watchWildCardFiles(String filesToWatch) throws Exception {
|
|
|
logger.info("Watching wildcard files : " + filesToWatch);
|
|
|
String directory = FilenameUtils.getFullPath(filesToWatch);
|
|
|
String wildcard = FilenameUtils.getName(filesToWatch);
|
|
|
- logger.trace("Directory : " + new File(directory).getAbsolutePath() + ", wildcard : " + wildcard);
|
|
|
+ logger.trace("Directory : " + new File(directory).getCanonicalPath() + ", wildcard : " + wildcard);
|
|
|
IOFileFilter fileFilter = FileFilterUtils.and(
|
|
|
FileFilterUtils.fileFileFilter(),
|
|
|
new WildcardFileFilter(wildcard),
|
|
|
- FileFilterUtils.ageFileFilter(cutoff, false));
|
|
|
- FileAlterationObserver observer = new FileAlterationObserver(new File(directory), fileFilter);
|
|
|
+ new LastModifiedFileFilter(deadTime));
|
|
|
+ updateWatchMap(new File(directory), fileFilter);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void watchStdIn() {
|
|
|
+ logger.info("Watching stdin : not implemented yet");
|
|
|
+ }
|
|
|
+
|
|
|
+ private void updateWatchMap(File directory, IOFileFilter fileFilter) throws Exception {
|
|
|
+ FileAlterationObserver observer = new FileAlterationObserver(directory, fileFilter);
|
|
|
observer.addListener(this);
|
|
|
observerList.add(observer);
|
|
|
observer.initialize();
|
|
|
+ for(File file : FileUtils.listFiles(directory, fileFilter, null)) {
|
|
|
+ addFileToWatchMap(file);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ private void addFileToWatchMap(File file) {
|
|
|
+ try {
|
|
|
+ FileState state = new FileState(file);
|
|
|
+ state.refresh();
|
|
|
+ watchMap.put(file, state);
|
|
|
+ } catch(IOException e) {
|
|
|
+ logger.error("Caught IOException : " + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
-
|
|
|
- private void watchStdIn() {
|
|
|
- logger.info("Watching stdin : not implemented yet");
|
|
|
+ private void removeFileFromWatchMap(File file) {
|
|
|
+ watchMap.remove(file);
|
|
|
}
|
|
|
|
|
|
public void onDirectoryChange(File directory) {
|
|
|
@@ -109,21 +132,21 @@ public class FileWatcher implements FileAlterationListener {
|
|
|
}
|
|
|
|
|
|
public void onFileCreate(File file) {
|
|
|
- logger.debug("Create detected on file : " + file.getAbsolutePath());
|
|
|
+ logger.debug("Create detected on file : " + file.getAbsolutePath());
|
|
|
+ addFileToWatchMap(file);
|
|
|
}
|
|
|
|
|
|
public void onFileDelete(File file) {
|
|
|
logger.debug("Delete detected on file : " + file.getAbsolutePath());
|
|
|
+ removeFileFromWatchMap(file);
|
|
|
}
|
|
|
|
|
|
public void onStart(FileAlterationObserver observer) {
|
|
|
// TODO Auto-generated method stub
|
|
|
-
|
|
|
}
|
|
|
|
|
|
public void onStop(FileAlterationObserver observer) {
|
|
|
// TODO Auto-generated method stub
|
|
|
-
|
|
|
}
|
|
|
|
|
|
public void checkFiles() {
|