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.job;
24  
25  import com.liferay.portal.kernel.job.IntervalJob;
26  import com.liferay.portal.kernel.job.JobScheduler;
27  import com.liferay.portal.kernel.util.ServerDetector;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.Time;
30  
31  import java.util.Date;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  import org.quartz.JobDetail;
37  import org.quartz.Scheduler;
38  import org.quartz.SimpleTrigger;
39  import org.quartz.Trigger;
40  
41  /**
42   * <a href="JobSchedulerImpl.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class JobSchedulerImpl implements JobScheduler {
48  
49      public void schedule(IntervalJob intervalJob) {
50          if (intervalJob == null) {
51              return;
52          }
53  
54          try {
55              if (_scheduler.isShutdown()) {
56                  return;
57              }
58          }
59          catch (Exception e) {
60              _log.error(e, e);
61          }
62  
63          String jobName =
64              intervalJob.getClass().getName() + StringPool.AT +
65                  intervalJob.hashCode();
66  
67          JobClassUtil.put(jobName, (Class<IntervalJob>)intervalJob.getClass());
68  
69          Date startTime = null;
70  
71          try {
72              if (ServerDetector.getServerId().equals(ServerDetector.TOMCAT_ID)) {
73                  startTime = new Date(System.currentTimeMillis() + Time.MINUTE);
74              }
75          }
76          catch (RuntimeException re) {
77  
78              // ServerDetector will throw an exception when JobSchedulerImpl is
79              // initialized in a test environment
80  
81          }
82  
83          if (startTime == null) {
84              startTime = new Date(System.currentTimeMillis() + Time.MINUTE * 3);
85          }
86  
87          Date endTime = null;
88  
89          JobDetail jobDetail = new JobDetail(
90              jobName, Scheduler.DEFAULT_GROUP, JobWrapper.class);
91  
92          Trigger trigger = new SimpleTrigger(
93              jobName, Scheduler.DEFAULT_GROUP, startTime, endTime,
94              SimpleTrigger.REPEAT_INDEFINITELY, intervalJob.getInterval());
95  
96          try {
97              _scheduler.scheduleJob(jobDetail, trigger);
98          }
99          catch (Exception e) {
100             _log.error(e, e);
101         }
102     }
103 
104     public void setScheduler(Scheduler scheduler) {
105         _scheduler = scheduler;
106     }
107 
108     public void shutdown() {
109         try {
110             if (!_scheduler.isShutdown()) {
111                 _scheduler.shutdown();
112             }
113         }
114         catch (Exception e) {
115             _log.error(e, e);
116         }
117     }
118 
119     public void unschedule(IntervalJob intervalJob) {
120         if (intervalJob == null) {
121             return;
122         }
123 
124         try {
125             if (_scheduler.isShutdown()) {
126                 return;
127             }
128         }
129         catch (Exception e) {
130             _log.error(e, e);
131         }
132 
133         try {
134             String jobName =
135                 intervalJob.getClass().getName() + StringPool.AT +
136                     intervalJob.hashCode();
137 
138             _scheduler.unscheduleJob(jobName, Scheduler.DEFAULT_GROUP);
139         }
140         catch (Exception e) {
141             _log.error(e, e);
142         }
143     }
144 
145     private static Log _log = LogFactory.getLog(JobScheduler.class);
146 
147     private Scheduler _scheduler;
148 
149 }