1   /**
2    * Copyright (c) 2000-2009 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.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.scheduler.SchedulerEngine;
29  import com.liferay.portal.kernel.scheduler.SchedulerException;
30  import com.liferay.portal.kernel.scheduler.messaging.SchedulerRequest;
31  import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
32  import com.liferay.portal.scheduler.job.MessageSenderJob;
33  import com.liferay.portal.service.QuartzLocalService;
34  import com.liferay.portal.util.PropsUtil;
35  import com.liferay.portal.util.PropsValues;
36  
37  import java.text.ParseException;
38  
39  import java.util.ArrayList;
40  import java.util.Date;
41  import java.util.Enumeration;
42  import java.util.List;
43  import java.util.Properties;
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 void afterPropertiesSet() {
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 e2) {
87                  quartzLocalService.checkQuartzTables();
88  
89                  _scheduler = schedulerFactory.getScheduler();
90              }
91          }
92          catch (Exception e1) {
93              _log.error("Unable to initialize engine", e1);
94          }
95      }
96  
97      public List<SchedulerRequest> getScheduledJobs(String groupName)
98          throws SchedulerException {
99  
100         if (!PropsValues.SCHEDULER_ENABLED) {
101             return new ArrayList<SchedulerRequest>();
102         }
103 
104         try {
105             String[] jobNames = _scheduler.getJobNames(groupName);
106 
107             List<SchedulerRequest> requests = new ArrayList<SchedulerRequest>();
108 
109             for (String jobName : jobNames) {
110                 JobDetail jobDetail = _scheduler.getJobDetail(
111                     jobName, groupName);
112 
113                 JobDataMap jobDataMap = jobDetail.getJobDataMap();
114 
115                 String description = jobDataMap.getString(DESCRIPTION);
116                 String messageBody = jobDataMap.getString(MESSAGE_BODY);
117 
118                 CronTrigger cronTrigger = (CronTrigger)_scheduler.getTrigger(
119                     jobName, groupName);
120 
121                 SchedulerRequest schedulerRequest = new SchedulerRequest(
122                     null, jobName, groupName, cronTrigger.getCronExpression(),
123                     cronTrigger.getStartTime(), cronTrigger.getEndTime(),
124                     description, null, messageBody);
125 
126                 requests.add(schedulerRequest);
127             }
128 
129             return requests;
130         }
131         catch (org.quartz.SchedulerException se) {
132             throw new SchedulerException("Unable to retrieve job", se);
133         }
134     }
135 
136     public void schedule(
137             String groupName, String cronText, Date startDate, Date endDate,
138             String description, String destination, String messageBody)
139         throws SchedulerException {
140 
141         if (!PropsValues.SCHEDULER_ENABLED) {
142             return;
143         }
144 
145         try {
146             String jobName = PortalUUIDUtil.generate();
147 
148             CronTrigger cronTrigger = new CronTrigger(
149                 jobName, groupName, cronText);
150 
151             if (startDate != null) {
152                 cronTrigger.setStartTime(startDate);
153             }
154 
155             if (endDate != null) {
156                 cronTrigger.setEndTime(endDate);
157             }
158 
159             JobDetail jobDetail = new JobDetail(
160                 jobName, groupName, MessageSenderJob.class);
161 
162             JobDataMap jobDataMap = jobDetail.getJobDataMap();
163 
164             jobDataMap.put(DESCRIPTION, description);
165             jobDataMap.put(DESTINATION, destination);
166             jobDataMap.put(MESSAGE_BODY, messageBody);
167 
168             _scheduler.scheduleJob(jobDetail, cronTrigger);
169         }
170         catch (ObjectAlreadyExistsException oare) {
171             if (_log.isInfoEnabled()) {
172                 _log.info("Message is already scheduled");
173             }
174         }
175         catch (ParseException pe) {
176             throw new SchedulerException("Unable to parse cron text", pe);
177         }
178         catch (org.quartz.SchedulerException se) {
179             throw new SchedulerException("Unable to scheduled job", se);
180         }
181     }
182 
183     public void shutdown() throws SchedulerException {
184         if (!PropsValues.SCHEDULER_ENABLED) {
185             return;
186         }
187 
188         try {
189             _scheduler.shutdown(false);
190         }
191         catch (org.quartz.SchedulerException se) {
192             throw new SchedulerException("Unable to shutdown scheduler", se);
193         }
194     }
195 
196     public void start() throws SchedulerException {
197         if (!PropsValues.SCHEDULER_ENABLED) {
198             return;
199         }
200 
201         try {
202             _scheduler.start();
203         }
204         catch (org.quartz.SchedulerException se) {
205             throw new SchedulerException("Unable to start scheduler", se);
206         }
207     }
208 
209     public void unschedule(String jobName, String groupName)
210         throws SchedulerException {
211 
212         if (!PropsValues.SCHEDULER_ENABLED) {
213             return;
214         }
215 
216         try {
217             _scheduler.unscheduleJob(jobName, groupName);
218         }
219         catch (org.quartz.SchedulerException se) {
220             throw new SchedulerException(
221                 "Unable to unschedule job {jobName=" + jobName +
222                     ", groupName=" + groupName + "}",
223                 se);
224         }
225     }
226 
227     @BeanReference(name = "com.liferay.portal.service.QuartzLocalService.impl")
228     protected QuartzLocalService quartzLocalService;
229 
230     private Log _log = LogFactoryUtil.getLog(QuartzSchedulerEngineImpl.class);
231 
232     private Scheduler _scheduler;
233 
234 }