View Javadoc

1   /*
2    * Copyright (c) 2003, Henri Yandell
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or 
6    * without modification, are permitted provided that the 
7    * following conditions are met:
8    * 
9    * + Redistributions of source code must retain the above copyright notice, 
10   *   this list of conditions and the following disclaimer.
11   * 
12   * + Redistributions in binary form must reproduce the above copyright notice, 
13   *   this list of conditions and the following disclaimer in the documentation 
14   *   and/or other materials provided with the distribution.
15   * 
16   * + Neither the name of OSJava nor the names of its contributors 
17   *   may be used to endorse or promote products derived from this software 
18   *   without specific prior written permission.
19   * 
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
23   * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
30   * POSSIBILITY OF SUCH DAMAGE.
31   */
32  package org.osjava.oscube.container;
33  
34  import org.apache.commons.lang.StringUtils;
35  
36  import java.util.Date;
37  
38  import org.quartz.impl.StdSchedulerFactory;
39  import org.quartz.JobDetail;
40  import org.quartz.JobDataMap;
41  import org.quartz.Job;
42  //import org.quartz.Scheduler;
43  import org.quartz.SchedulerException;
44  import org.quartz.Trigger;
45  import org.quartz.CronTrigger;
46  import org.quartz.SimpleTrigger;
47  
48  import java.text.ParseException;
49  
50  import org.apache.log4j.Logger;
51  
52  import com.generationjava.config.Config;
53  
54  /// BEWARE: Name clash. Scheduler and org.quartz.Scheduler
55  public class QuartzScheduler implements Scheduler {
56  
57      private static Logger logger = Logger.getLogger(QuartzScheduler.class);
58  
59      private org.quartz.Scheduler quartz;
60  
61      public QuartzScheduler() {
62          try {
63              quartz = new StdSchedulerFactory().getScheduler();
64              quartz.start();
65          } catch(SchedulerException se) {
66              throw new RuntimeException("Failed to schedule job. " + se);
67          }
68      }
69  
70      public void schedule(Config cfg, Session session, Runner runner) {
71          // TODO: Should this clone, or the driver of this?
72          cfg = cfg.cloneConfig();
73          String schedule = cfg.getString("schedule");
74  
75          // set default
76          if(StringUtils.isEmpty(schedule)) {
77              schedule = "startup";
78          } 
79  
80          logger.debug("SCHEDULE: "+schedule);
81  
82          // this should be one of the following:
83          // a cron-like line or a 'startup' option 
84          if("startup".equalsIgnoreCase(schedule)) { 
85              Scheduler sch = new SimpleScheduler();
86              sch.schedule(cfg, session, runner);
87          } else {
88              // assume it's a cron statement
89              try {
90                  String ctxt = cfg.getContext();
91                  String jobName = ctxt+"job";
92                  // TODO: Make this 'Scraper' or some such. Maybe 
93                  // a name that links to the current engine's instance
94                  // Engine: +engine.toString()
95                  String jobgroup = ctxt+"group"; 
96                  JobDetail detail = new JobDetail(jobName, jobgroup, QuartzJob.class);
97                  JobDataMap map = detail.getJobDataMap();
98                  map.put("cfg", cfg);
99                  map.put("session", session);
100                 map.put("runner", runner);
101 
102                 Trigger trigger = null;
103                 if("cron".equalsIgnoreCase(schedule)) {
104                     String cronTxt = cfg.getString("cron");
105                     logger.debug("Creating cron trigger: "+cronTxt+" for "+ctxt);
106                     CronTrigger cron = new CronTrigger(ctxt+"crontrigger", jobgroup, jobName, jobgroup, cronTxt);
107                     trigger = cron;
108                 } else {
109                     // TODO: ie) "simple" in this case. need to make this explicit
110 //                    Date start = cfg.getDate("simple.start");
111 //                    Date end = cfg.getDate("simple.end");
112                     int repeat = cfg.getInt("simple.repeat");
113                     if(repeat == -1) {
114                         repeat = SimpleTrigger.REPEAT_INDEFINITELY;
115                     }
116                     int interval = cfg.getInt("simple.interval");
117                     logger.debug("Creating simple trigger: "+interval+":"+repeat+" for "+ctxt);
118                     SimpleTrigger simp = new SimpleTrigger(ctxt+"simpletrigger", jobgroup, repeat, interval);
119                     trigger = simp;
120                 }
121 
122                 quartz.scheduleJob(detail, trigger);
123             } catch(ParseException pe) {
124                 throw new RuntimeException("Failed to parse cron. " + pe);
125             } catch(SchedulerException se) {
126                 throw new RuntimeException("Failed to schedule job. " + se);
127             }
128         }
129     }
130 
131 }