1 package org.jahia.tools.files; 2 3 import org.quartz.StatefulJob; 4 import org.quartz.JobExecutionContext; 5 import org.quartz.JobExecutionException; 6 import org.quartz.JobDataMap; 7 import java.util.Vector ; 8 import java.io.File ; 9 10 public class FileWatcherJob implements StatefulJob { 11 public FileWatcherJob () { 12 } 13 14 public void execute (JobExecutionContext context) 15 throws JobExecutionException { 16 17 JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); 19 FileWatcher fileWatcher = (FileWatcher) jobDataMap.get("fileWatcher"); 20 21 22 Vector newFiles = checkFiles(fileWatcher.getFolder(), 23 fileWatcher.getFileOnly(), 24 fileWatcher.getCheckDate(), 25 fileWatcher.getLastCheckTime()); 26 27 if (newFiles.size() > 0) { 29 fileWatcher.externalSetChanged(); fileWatcher.notifyObservers(newFiles); 31 } 32 } 33 34 39 protected Vector checkFiles (File folder, boolean fileOnly, 40 boolean checkDate, long lastCheckTime) { 41 42 Vector newFiles = new Vector (); 43 if (folder.isDirectory()) { 44 45 if (!checkDate) { 46 47 File [] files = folder.listFiles(); 48 if (files == null) { 49 return newFiles; 50 } 51 int size = files.length; 52 53 for (int i = 0; i < size; i++) { 54 if (files[i].canWrite()) { 56 if (!fileOnly) { 57 newFiles.add(files[i]); 58 } else if (files[i].isFile()) { 59 newFiles.add(files[i]); 60 } 61 } 62 } 63 64 } else { 65 66 File [] files = folder.listFiles(); 67 if (files == null) { 68 return newFiles; 69 } 70 int size = files.length; 71 72 for (int i = 0; i < size; i++) { 73 74 if (files[i].lastModified() > lastCheckTime) { 75 newFiles.add(files[i]); 76 } 77 } 78 79 } 80 81 } 82 return newFiles; 83 84 } 85 } 86 | Popular Tags |