1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.scheduler.quartz;
24  
25  import com.liferay.portal.kernel.scheduler.SchedulerEngine;
26  import com.liferay.portal.kernel.scheduler.SchedulerException;
27  import com.liferay.portal.kernel.scheduler.messaging.SchedulerRequest;
28  import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
29  import com.liferay.portal.scheduler.job.MessageSenderJob;
30  import com.liferay.portal.tools.sql.DBUtil;
31  import com.liferay.portal.util.PropsUtil;
32  import com.liferay.portal.util.PropsValues;
33  
34  import java.text.ParseException;
35  
36  import java.util.ArrayList;
37  import java.util.Date;
38  import java.util.Enumeration;
39  import java.util.List;
40  import java.util.Properties;
41  
42  import org.apache.commons.logging.Log;
43  import org.apache.commons.logging.LogFactory;
44  
45  import org.quartz.CronTrigger;
46  import org.quartz.JobDataMap;
47  import org.quartz.JobDetail;
48  import org.quartz.ObjectAlreadyExistsException;
49  import org.quartz.Scheduler;
50  import org.quartz.impl.StdSchedulerFactory;
51  
52  /**
53   * <a href="QuartzSchedulerEngineImpl.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Michael C. Han
56   * @author Bruno Farache
57   *
58   */
59  public class QuartzSchedulerEngineImpl implements SchedulerEngine {
60  
61      public QuartzSchedulerEngineImpl() {
62          try {
63              if (!PropsValues.SCHEDULER_ENABLED) {
64                  return;
65              }
66  
67              Properties props = new Properties();
68  
69              Enumeration<Object> enu = PropsUtil.getProperties().keys();
70  
71              while (enu.hasMoreElements()) {
72                  String key = (String)enu.nextElement();
73  
74                  if (key.startsWith("org.quartz.")) {
75                      props.setProperty(key, PropsUtil.get(key));
76                  }
77              }
78  
79              StdSchedulerFactory schedulerFactory = new StdSchedulerFactory();
80  
81              schedulerFactory.initialize(props);
82  
83              try {
84                  _scheduler = schedulerFactory.getScheduler();
85              }
86              catch (Exception e1) {
87                  DBUtil dbUtil = DBUtil.getInstance();
88  
89                  dbUtil.runSQLTemplate("quartz-tables.sql", false);
90  
91                  _scheduler = schedulerFactory.getScheduler();
92              }
93          }
94          catch (Exception e2) {
95              _log.error("Unable to initialize engine", e2);
96          }
97      }
98  
99      public List<SchedulerRequest> getScheduledJobs(String groupName)
100         throws SchedulerException {
101 
102         if (!PropsValues.SCHEDULER_ENABLED) {
103             return new ArrayList<SchedulerRequest>();
104         }
105 
106         try {
107             String[] jobNames = _scheduler.getJobNames(groupName);
108 
109             List<SchedulerRequest> requests = new ArrayList<SchedulerRequest>();
110 
111             for (String jobName : jobNames) {
112                 JobDetail jobDetail = _scheduler.getJobDetail(
113                     jobName, groupName);
114 
115                 JobDataMap jobDataMap = jobDetail.getJobDataMap();
116 
117                 String description = jobDataMap.getString(DESCRIPTION);
118                 String messageBody = jobDataMap.getString(MESSAGE_BODY);
119 
120                 CronTrigger cronTrigger = (CronTrigger)_scheduler.getTrigger(
121                     jobName, groupName);
122 
123                 SchedulerRequest schedulerRequest = new SchedulerRequest(
124                     null, jobName, groupName, cronTrigger.getCronExpression(),
125                     cronTrigger.getStartTime(), cronTrigger.getEndTime(),
126                     description, null, messageBody);
127 
128                 requests.add(schedulerRequest);
129             }
130 
131             return requests;
132         }
133         catch (org.quartz.SchedulerException se) {
134             throw new SchedulerException("Unable to retrieve job", se);
135         }
136     }
137 
138     public void schedule(
139             String groupName, String cronText, Date startDate, Date endDate,
140             String description, String destination, String messageBody)
141         throws SchedulerException {
142 
143         if (!PropsValues.SCHEDULER_ENABLED) {
144             return;
145         }
146 
147         try {
148             String jobName = PortalUUIDUtil.generate();
149 
150             CronTrigger cronTrigger = new CronTrigger(
151                 jobName, groupName, cronText);
152 
153             if (startDate != null) {
154                 cronTrigger.setStartTime(startDate);
155             }
156 
157             if (endDate != null) {
158                 cronTrigger.setEndTime(endDate);
159             }
160 
161             JobDetail jobDetail = new JobDetail(
162                 jobName, groupName, MessageSenderJob.class);
163 
164             JobDataMap jobDataMap = jobDetail.getJobDataMap();
165 
166             jobDataMap.put(DESCRIPTION, description);
167             jobDataMap.put(DESTINATION, destination);
168             jobDataMap.put(MESSAGE_BODY, messageBody);
169 
170             _scheduler.scheduleJob(jobDetail, cronTrigger);
171         }
172         catch (ObjectAlreadyExistsException oare) {
173             if (_log.isInfoEnabled()) {
174                 _log.info("Message is already scheduled");
175             }
176         }
177         catch (ParseException pe) {
178             throw new SchedulerException("Unable to parse cron text", pe);
179         }
180         catch (org.quartz.SchedulerException se) {
181             throw new SchedulerException("Unable to scheduled job", se);
182         }
183     }
184 
185     public void shutdown() throws SchedulerException {
186         if (!PropsValues.SCHEDULER_ENABLED) {
187             return;
188         }
189 
190         try {
191             _scheduler.shutdown(false);
192         }
193         catch (org.quartz.SchedulerException se) {
194             throw new SchedulerException("Unable to shutdown scheduler", se);
195         }
196     }
197 
198     public void start() throws SchedulerException {
199         if (!PropsValues.SCHEDULER_ENABLED) {
200             return;
201         }
202 
203         try {
204             _scheduler.start();
205         }
206         catch (org.quartz.SchedulerException se) {
207             throw new SchedulerException("Unable to start scheduler", se);
208         }
209     }
210 
211     public void unschedule(String jobName, String groupName)
212         throws SchedulerException {
213 
214         if (!PropsValues.SCHEDULER_ENABLED) {
215             return;
216         }
217 
218         try {
219             _scheduler.unscheduleJob(jobName, groupName);
220         }
221         catch (org.quartz.SchedulerException se) {
222             throw new SchedulerException(
223                 "Unable to unschedule job {jobName=" + jobName +
224                     ", groupName=" + groupName + "}",
225                 se);
226         }
227     }
228 
229     private Log _log = LogFactory.getLog(QuartzSchedulerEngineImpl.class);
230 
231     private Scheduler _scheduler;
232 
233 }