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.annotation.BeanReference;
26  import com.liferay.portal.kernel.scheduler.SchedulerEngine;
27  import com.liferay.portal.kernel.scheduler.SchedulerException;
28  import com.liferay.portal.kernel.scheduler.messaging.SchedulerRequest;
29  import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
30  import com.liferay.portal.scheduler.job.MessageSenderJob;
31  import com.liferay.portal.service.QuartzLocalService;
32  import com.liferay.portal.util.PropsUtil;
33  import com.liferay.portal.util.PropsValues;
34  
35  import java.text.ParseException;
36  
37  import java.util.ArrayList;
38  import java.util.Date;
39  import java.util.Enumeration;
40  import java.util.List;
41  import java.util.Properties;
42  
43  import org.apache.commons.logging.Log;
44  import org.apache.commons.logging.LogFactory;
45  
46  import org.quartz.CronTrigger;
47  import org.quartz.JobDataMap;
48  import org.quartz.JobDetail;
49  import org.quartz.ObjectAlreadyExistsException;
50  import org.quartz.Scheduler;
51  import org.quartz.impl.StdSchedulerFactory;
52  
53  /**
54   * <a href="QuartzSchedulerEngineImpl.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Michael C. Han
57   * @author Bruno Farache
58   *
59   */
60  public class QuartzSchedulerEngineImpl implements SchedulerEngine {
61  
62      public void afterPropertiesSet() {
63          try {
64              if (!PropsValues.SCHEDULER_ENABLED) {
65                  return;
66              }
67  
68              Properties props = new Properties();
69  
70              Enumeration<Object> enu = PropsUtil.getProperties().keys();
71  
72              while (enu.hasMoreElements()) {
73                  String key = (String)enu.nextElement();
74  
75                  if (key.startsWith("org.quartz.")) {
76                      props.setProperty(key, PropsUtil.get(key));
77                  }
78              }
79  
80              StdSchedulerFactory schedulerFactory = new StdSchedulerFactory();
81  
82              schedulerFactory.initialize(props);
83  
84              try {
85                  _scheduler = schedulerFactory.getScheduler();
86              }
87              catch (Exception e1) {
88                  quartzLocalService.checkQuartzTables();
89  
90                  _scheduler = schedulerFactory.getScheduler();
91              }
92          }
93          catch (Exception e2) {
94              _log.error("Unable to initialize engine", e2);
95          }
96      }
97  
98      public List<SchedulerRequest> getScheduledJobs(String groupName)
99          throws SchedulerException {
100 
101         if (!PropsValues.SCHEDULER_ENABLED) {
102             return new ArrayList<SchedulerRequest>();
103         }
104 
105         try {
106             String[] jobNames = _scheduler.getJobNames(groupName);
107 
108             List<SchedulerRequest> requests = new ArrayList<SchedulerRequest>();
109 
110             for (String jobName : jobNames) {
111                 JobDetail jobDetail = _scheduler.getJobDetail(
112                     jobName, groupName);
113 
114                 JobDataMap jobDataMap = jobDetail.getJobDataMap();
115 
116                 String description = jobDataMap.getString(DESCRIPTION);
117                 String messageBody = jobDataMap.getString(MESSAGE_BODY);
118 
119                 CronTrigger cronTrigger = (CronTrigger)_scheduler.getTrigger(
120                     jobName, groupName);
121 
122                 SchedulerRequest schedulerRequest = new SchedulerRequest(
123                     null, jobName, groupName, cronTrigger.getCronExpression(),
124                     cronTrigger.getStartTime(), cronTrigger.getEndTime(),
125                     description, null, messageBody);
126 
127                 requests.add(schedulerRequest);
128             }
129 
130             return requests;
131         }
132         catch (org.quartz.SchedulerException se) {
133             throw new SchedulerException("Unable to retrieve job", se);
134         }
135     }
136 
137     public void schedule(
138             String groupName, String cronText, Date startDate, Date endDate,
139             String description, String destination, String messageBody)
140         throws SchedulerException {
141 
142         if (!PropsValues.SCHEDULER_ENABLED) {
143             return;
144         }
145 
146         try {
147             String jobName = PortalUUIDUtil.generate();
148 
149             CronTrigger cronTrigger = new CronTrigger(
150                 jobName, groupName, cronText);
151 
152             if (startDate != null) {
153                 cronTrigger.setStartTime(startDate);
154             }
155 
156             if (endDate != null) {
157                 cronTrigger.setEndTime(endDate);
158             }
159 
160             JobDetail jobDetail = new JobDetail(
161                 jobName, groupName, MessageSenderJob.class);
162 
163             JobDataMap jobDataMap = jobDetail.getJobDataMap();
164 
165             jobDataMap.put(DESCRIPTION, description);
166             jobDataMap.put(DESTINATION, destination);
167             jobDataMap.put(MESSAGE_BODY, messageBody);
168 
169             _scheduler.scheduleJob(jobDetail, cronTrigger);
170         }
171         catch (ObjectAlreadyExistsException oare) {
172             if (_log.isInfoEnabled()) {
173                 _log.info("Message is already scheduled");
174             }
175         }
176         catch (ParseException pe) {
177             throw new SchedulerException("Unable to parse cron text", pe);
178         }
179         catch (org.quartz.SchedulerException se) {
180             throw new SchedulerException("Unable to scheduled job", se);
181         }
182     }
183 
184     public void shutdown() throws SchedulerException {
185         if (!PropsValues.SCHEDULER_ENABLED) {
186             return;
187         }
188 
189         try {
190             _scheduler.shutdown(false);
191         }
192         catch (org.quartz.SchedulerException se) {
193             throw new SchedulerException("Unable to shutdown scheduler", se);
194         }
195     }
196 
197     public void start() throws SchedulerException {
198         if (!PropsValues.SCHEDULER_ENABLED) {
199             return;
200         }
201 
202         try {
203             _scheduler.start();
204         }
205         catch (org.quartz.SchedulerException se) {
206             throw new SchedulerException("Unable to start scheduler", se);
207         }
208     }
209 
210     public void unschedule(String jobName, String groupName)
211         throws SchedulerException {
212 
213         if (!PropsValues.SCHEDULER_ENABLED) {
214             return;
215         }
216 
217         try {
218             _scheduler.unscheduleJob(jobName, groupName);
219         }
220         catch (org.quartz.SchedulerException se) {
221             throw new SchedulerException(
222                 "Unable to unschedule job {jobName=" + jobName +
223                     ", groupName=" + groupName + "}",
224                 se);
225         }
226     }
227 
228     @BeanReference(name = "com.liferay.portal.service.QuartzLocalService.impl")
229     protected QuartzLocalService quartzLocalService;
230 
231     private Log _log = LogFactory.getLog(QuartzSchedulerEngineImpl.class);
232 
233     private Scheduler _scheduler;
234 
235 }