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