1 20 21 package org.jahia.tools.files; 22 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.util.Observable ; 26 import org.quartz.JobDetail; 27 import org.quartz.Trigger; 28 import org.jahia.registries.ServicesRegistry; 29 import org.quartz.SimpleTrigger; 30 import org.quartz.Scheduler; 31 import org.jahia.exceptions.JahiaException; 32 import org.quartz.JobDataMap; 33 import java.io.Serializable ; 34 35 50 public class FileWatcher extends Observable implements Serializable { 51 52 private static org.apache.log4j.Logger logger = 53 org.apache.log4j.Logger.getLogger(FileWatcher.class); 54 55 56 private String m_FolderPath = ""; 57 58 59 private File m_Folder; 60 61 62 private long m_Interval; 63 64 65 private JobDetail jobDetail; 66 private Trigger trigger; 67 68 69 private boolean m_FileOnly = true; 70 71 72 private boolean m_IsDeamon = true; 73 74 75 public boolean mCheckDate = false; 76 77 78 private long m_LastCheckTime; 79 80 89 public FileWatcher (String fullFolderPath, 90 boolean checkDate, 91 long interval, 92 boolean fileOnly 93 ) 94 throws IOException { 95 96 setFolderPath(fullFolderPath); 97 setCheckDate(checkDate); 98 setInterval(interval); 99 setFileOnly(fileOnly); 100 } 101 102 113 public FileWatcher (String fullFolderPath, 114 boolean checkDate, 115 long interval, 116 boolean fileOnly, 117 boolean isDeamon 118 ) 119 throws IOException { 120 121 setFolderPath(fullFolderPath); 122 setCheckDate(checkDate); 123 setInterval(interval); 124 setFileOnly(fileOnly); 125 setDeamon(isDeamon); 126 127 } 128 129 134 public void start () 135 throws IOException { 136 137 initialize(); 138 139 logger.debug("Time created, Check Interval=" + getInterval() + 140 " (millis) "); 141 142 jobDetail = new JobDetail(m_FolderPath + "_Job", Scheduler.DEFAULT_GROUP, 143 FileWatcherJob.class); 144 JobDataMap jobDataMap = new JobDataMap(); 145 jobDataMap.put("fileWatcher", this); 146 jobDetail.setJobDataMap(jobDataMap); 147 148 trigger = new SimpleTrigger(m_FolderPath + "_Trigger", 149 Scheduler.DEFAULT_GROUP, 150 SimpleTrigger.REPEAT_INDEFINITELY, 151 m_Interval); 152 153 154 try { 155 ServicesRegistry.getInstance().getSchedulerService().unscheduleJob(trigger.getName(), Scheduler.DEFAULT_GROUP); 156 ServicesRegistry.getInstance().getSchedulerService().scheduleJob( 157 jobDetail, trigger); 158 } catch (JahiaException je) { 159 logger.error("Error while scheduling file watch for " + m_FolderPath, je); 160 } 161 162 } 163 164 public void stop () { 165 try { 166 ServicesRegistry.getInstance().getSchedulerService().unscheduleJob( 167 m_FolderPath + "_Trigger", Scheduler.DEFAULT_GROUP); 168 } catch (JahiaException je) { 169 logger.error("Error while scheduling file watch for " + m_FolderPath, je); 170 } 171 } 172 173 178 public long getInterval () { 179 return m_Interval; 180 } 181 182 187 protected void setInterval (long interval) { 188 m_Interval = interval; 189 } 190 191 195 public void externalSetChanged () { 196 setChanged(); 197 } 198 199 204 public String getFolderPath () { 205 return m_FolderPath; 206 } 207 208 213 protected void setFolderPath (String fullFolderPath) { 214 m_FolderPath = fullFolderPath; 215 } 216 217 222 public void setFileOnly (boolean fileOnly) { 223 m_FileOnly = fileOnly; 224 } 225 226 public boolean getFileOnly() { 227 return m_FileOnly; 228 } 229 230 235 public boolean getCheckDate () { 236 return mCheckDate; 237 } 238 239 244 public void setCheckDate (boolean checkDate) { 245 mCheckDate = checkDate; 246 } 247 248 253 public boolean isDeamon () { 254 return m_IsDeamon; 255 } 256 257 262 protected void setDeamon (boolean isDeamon) { 263 m_IsDeamon = isDeamon; 264 } 265 266 public File getFolder() { 267 return m_Folder; 268 } 269 270 public long getLastCheckTime() { 271 return m_LastCheckTime; 272 } 273 274 280 protected void initialize () 281 throws IOException { 282 283 logger.debug("Initializing file watcher"); 284 285 289 m_LastCheckTime = System.currentTimeMillis(); 290 logger.debug("Watching directory=" + getFolderPath()); 291 File tmpFile = new File (getFolderPath()); 292 if (tmpFile != null && tmpFile.isDirectory() && !tmpFile.canWrite()) { 293 logger.debug("No write access to directory " + getFolderPath() + 294 " tmpFile=" + tmpFile.toString()); 295 } else if (!tmpFile.exists()) { 296 logger.debug("Directory " + tmpFile.toString() + 297 " does not exist, creating..."); 298 tmpFile.mkdirs(); 299 logger.debug("Directory " + tmpFile.toString() + 300 " created successfully."); 301 if (tmpFile == null) { 302 throw new IOException ( 303 "FileWatcher::initialize(), cannot create directory " + 304 tmpFile.toString()); 305 } 306 } 307 m_Folder = tmpFile; 308 } 309 310 311 } | Popular Tags |