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