1 26 package org.infoglue.deliver.jobs; 27 28 import java.io.File ; 29 import java.io.FileNotFoundException ; 30 import java.io.IOException ; 31 import java.net.URL ; 32 import java.util.Date ; 33 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 import org.infoglue.cms.util.CmsPropertyHandler; 37 import org.quartz.JobDetail; 38 import org.quartz.Scheduler; 39 import org.quartz.SchedulerConfigException; 40 import org.quartz.SchedulerException; 41 import org.quartz.SimpleTrigger; 42 import org.quartz.jobs.FileScanJob; 43 import org.quartz.jobs.FileScanListener; 44 import org.quartz.spi.SchedulerPlugin; 45 import org.quartz.xml.JobSchedulingDataProcessor; 46 47 55 public class JobInitializationPlugin implements SchedulerPlugin, FileScanListener { 56 57 64 65 private String name; 66 67 private Scheduler scheduler; 68 69 private boolean overWriteExistingJobs = false; 70 71 private boolean failOnFileNotFound = true; 72 73 private boolean fileFound = false; 74 75 private String fileName = JobSchedulingDataProcessor.QUARTZ_XML_FILE_NAME; 76 77 private String filePath = null; 78 79 private boolean useContextClassLoader = true; 80 81 private boolean validating = true; 82 83 private boolean validatingSchema = true; 84 85 private long scanInterval = 0; 86 87 boolean initializing = true; 88 89 boolean started = false; 90 91 98 99 public JobInitializationPlugin() { 100 } 101 102 109 110 115 public String getFileName() { 116 return fileName; 117 } 118 119 124 public void setFileName(String fileName) { 125 this.fileName = fileName; 126 } 127 128 134 public boolean isOverWriteExistingJobs() { 135 return overWriteExistingJobs; 136 } 137 138 144 public void setOverWriteExistingJobs(boolean overWriteExistingJobs) { 145 this.overWriteExistingJobs = overWriteExistingJobs; 146 } 147 148 155 public long getScanInterval() { 156 return scanInterval / 1000; 157 } 158 159 166 public void setScanInterval(long scanInterval) { 167 this.scanInterval = scanInterval * 1000; 168 } 169 170 176 public boolean isFailOnFileNotFound() { 177 return failOnFileNotFound; 178 } 179 180 186 public void setFailOnFileNotFound(boolean failOnFileNotFound) { 187 this.failOnFileNotFound = failOnFileNotFound; 188 } 189 190 195 public boolean isUseContextClassLoader() { 196 return useContextClassLoader; 197 } 198 199 204 public void setUseContextClassLoader(boolean useContextClassLoader) { 205 this.useContextClassLoader = useContextClassLoader; 206 } 207 208 213 public boolean isValidating() { 214 return validating; 215 } 216 217 222 public void setValidating(boolean validating) { 223 this.validating = validating; 224 } 225 226 231 public boolean isValidatingSchema() { 232 return validatingSchema; 233 } 234 235 240 public void setValidatingSchema(boolean validatingSchema) { 241 this.validatingSchema = validatingSchema; 242 } 243 244 protected static Log getLog() { 245 return LogFactory.getLog(JobInitializationPlugin.class); 246 } 247 248 255 256 265 public void initialize(String name, final Scheduler scheduler) 266 throws SchedulerException { 267 268 initializing = true; 269 try { 270 this.name = name; 271 this.scheduler = scheduler; 272 273 getLog().info("Registering Quartz Job Initialization Plug-in."); 274 275 findFile(); 276 } 277 finally { 278 initializing = false; 279 } 280 } 281 282 private String getFilePath() throws SchedulerException { 283 if(this.filePath == null) { 284 findFile(); 285 } 286 return this.filePath; 287 } 288 289 292 private void findFile() throws SchedulerException { 293 java.io.InputStream f = null; 294 295 File file = new File (getFileName()); if (file == null || !file.exists()) { 297 URL url = Thread.currentThread() 299 .getContextClassLoader() 300 .getResource(getFileName()); 301 if(url != null) { 302 file = new File (url.getPath()); 303 } 304 if(file == null || !file.exists()) 305 { 306 String fileName = CmsPropertyHandler.getContextRootPath() + "WEB-INF" + File.separator + "classes" + File.separator + "jobs.xml"; 307 file = new File (fileName); 308 } 309 } 310 try { 311 f = new java.io.FileInputStream (file); 312 }catch (FileNotFoundException e) { 313 } 315 316 if (f == null && isFailOnFileNotFound()) { 317 throw new SchedulerException("File named '" + getFileName() 318 + "' does not exist."); 319 } else if (f == null) { 320 getLog().warn("File named '" + getFileName() + "' does not exist."); 321 } else { 322 fileFound = true; 323 try { 324 this.filePath = file.getPath(); 325 f.close(); 326 } catch (IOException ioe) { 327 getLog() 328 .warn("Error closing file named '" + getFileName(), ioe); 329 } 330 } 331 } 332 333 public void start() { 334 335 if(scanInterval > 0) { 336 try{ 337 SimpleTrigger trig = new SimpleTrigger( 338 "JobInitializationPlugin_"+name, 339 "JobInitializationPlugin", 340 new Date (), null, 341 SimpleTrigger.REPEAT_INDEFINITELY, scanInterval); 342 trig.setVolatility(true); 343 JobDetail job = new JobDetail( 344 "JobInitializationPlugin_"+name, 345 "JobInitializationPlugin", 346 FileScanJob.class); 347 job.setVolatility(true); 348 job.getJobDataMap().put(FileScanJob.FILE_NAME, getFilePath()); 349 job.getJobDataMap().put(FileScanJob.FILE_SCAN_LISTENER_NAME, "JobInitializationPlugin_"+name); 350 351 scheduler.getContext().put("JobInitializationPlugin_"+name, this); 352 scheduler.scheduleJob(job, trig); 353 } 354 catch(SchedulerException se) { 355 getLog().error("Error starting background-task for watching jobs file.", se); 356 } 357 } 358 359 try { 360 processFile(); 361 } 362 finally { 363 started = true; 364 } 365 } 366 367 374 public void shutdown() { 375 } 377 378 379 public void processFile() { 380 if (!fileFound) return; 381 382 JobSchedulingDataProcessor processor = 383 new JobSchedulingDataProcessor(isUseContextClassLoader(), isValidating(), isValidatingSchema()); 384 385 try { 386 processor.processFileAndScheduleJobs(fileName, scheduler, true); 387 } catch (Exception e) { 388 getLog().error("Error scheduling jobs: " + e.getMessage(), e); 389 } 390 } 391 392 395 public void fileUpdated(String fileName) { 396 if(started) 397 processFile(); 398 } 399 400 } 401 402 | Popular Tags |