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