KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > jobs > FileScanJob


1 /*
2  * Copyright 2004-2005 OpenSymphony
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  *
16  */

17
18 /*
19  * Previously Copyright (c) 2001-2004 James House
20  */

21 package org.quartz.jobs;
22
23 import java.io.File JavaDoc;
24
25 import org.quartz.JobDataMap;
26 import org.quartz.JobExecutionContext;
27 import org.quartz.JobExecutionException;
28 import org.quartz.SchedulerContext;
29 import org.quartz.SchedulerException;
30 import org.quartz.StatefulJob;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35 /**
36  * Inspects a file and compares whether it's "last modified date" has changed
37  * since the last time it was inspected. If the file has been updated, the
38  * job invokes a "call-back" method on an identified
39  * <code>FileScanListener</code> that can be found in the
40  * <code>SchedulerContext</code>.
41  *
42  * @author jhouse
43  * @see org.quartz.jobs.FileScanListener
44  */

45 public class FileScanJob implements StatefulJob {
46
47     public static String JavaDoc FILE_NAME = "FILE_NAME";
48     public static String JavaDoc FILE_SCAN_LISTENER_NAME = "FILE_SCAN_LISTENER_NAME";
49     private static String JavaDoc LAST_MODIFIED_TIME = "LAST_MODIFIED_TIME";
50     
51     private final Log log = LogFactory.getLog(getClass());
52
53     public FileScanJob() {
54     }
55
56     /**
57      * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
58      */

59     public void execute(JobExecutionContext context) throws JobExecutionException {
60         JobDataMap mergedJobDataMap = context.getMergedJobDataMap();
61         SchedulerContext schedCtxt = null;
62         try {
63             schedCtxt = context.getScheduler().getContext();
64         } catch (SchedulerException e) {
65             throw new JobExecutionException("Error obtaining scheduler context.", e, false);
66         }
67         
68         String JavaDoc fileName = mergedJobDataMap.getString(FILE_NAME);
69         String JavaDoc listenerName = mergedJobDataMap.getString(FILE_SCAN_LISTENER_NAME);
70         
71         if(fileName == null) {
72             throw new JobExecutionException("Required parameter '" +
73                     FILE_NAME + "' not found in merged JobDataMap");
74         }
75         if(listenerName == null) {
76             throw new JobExecutionException("Required parameter '" +
77                     FILE_SCAN_LISTENER_NAME + "' not found in merged JobDataMap");
78         }
79
80         FileScanListener listener = (FileScanListener)schedCtxt.get(listenerName);
81         
82         if(listener == null) {
83             throw new JobExecutionException("FileScanListener named '" +
84                     listenerName + "' not found in SchedulerContext");
85         }
86         
87         long lastDate = -1;
88         if(mergedJobDataMap.containsKey(LAST_MODIFIED_TIME)) {
89             lastDate = mergedJobDataMap.getLong(LAST_MODIFIED_TIME);
90         }
91         
92         long newDate = getLastModifiedDate(fileName);
93
94         if(newDate < 0) {
95             log.warn("File '"+fileName+"' does not exist.");
96             return;
97         }
98         
99         if(lastDate > 0 && (newDate != lastDate)) {
100             // notify call back...
101
log.info("File '"+fileName+"' updated, notifying listener.");
102             listener.fileUpdated(fileName);
103         } else if (log.isDebugEnabled()) {
104             log.debug("File '"+fileName+"' unchanged.");
105         }
106         
107         // It is the JobDataMap on the JobDetail which is actually stateful
108
context.getJobDetail().getJobDataMap().put(LAST_MODIFIED_TIME, newDate);
109     }
110     
111     protected long getLastModifiedDate(String JavaDoc fileName) {
112         
113         File JavaDoc file = new File JavaDoc(fileName);
114         
115         if(!file.exists()) {
116             return -1;
117         } else {
118             return file.lastModified();
119         }
120     }
121 }
122
Popular Tags