KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > tools > files > FileWatcherJob


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 JavaDoc;
8 import java.io.File JavaDoc;
9
10 public class FileWatcherJob implements StatefulJob {
11     public FileWatcherJob () {
12     }
13
14     public void execute (JobExecutionContext context)
15         throws JobExecutionException {
16
17         //logger.debug("Checking files in directory " + m_Folder.toString() + "...");
18
JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
19         FileWatcher fileWatcher = (FileWatcher) jobDataMap.get("fileWatcher");
20
21         /** The Vector of new files **/
22         Vector JavaDoc newFiles = checkFiles(fileWatcher.getFolder(),
23                                      fileWatcher.getFileOnly(),
24                                      fileWatcher.getCheckDate(),
25                                      fileWatcher.getLastCheckTime());
26
27         // Notify Observers if number of files > 0
28
if (newFiles.size() > 0) {
29             fileWatcher.externalSetChanged(); // Alert the Observable Object That there are change in the folder
30
fileWatcher.notifyObservers(newFiles);
31         }
32     }
33
34     /**
35      * Checks new files and builds the Vector of files
36      * to pass to Observers
37      *
38      */

39     protected Vector JavaDoc checkFiles (File JavaDoc folder, boolean fileOnly,
40                                  boolean checkDate, long lastCheckTime) {
41
42         Vector JavaDoc newFiles = new Vector JavaDoc();
43         if (folder.isDirectory()) {
44
45             if (!checkDate) {
46
47                 File JavaDoc[] 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                     // logger.debug("FileWatcher found new file " + files[i].getName() );
55
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 JavaDoc[] 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