1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.util.ServerDetector;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.kernel.util.Time;
32  
33  import java.util.Date;
34  
35  import org.quartz.JobDetail;
36  import org.quartz.Scheduler;
37  import org.quartz.SimpleTrigger;
38  import org.quartz.Trigger;
39  
40  /**
41   * <a href="JobSchedulerImpl.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   */
45  public class JobSchedulerImpl implements JobScheduler {
46  
47      public void schedule(IntervalJob intervalJob) {
48          if (intervalJob == null) {
49              return;
50          }
51  
52          try {
53              if (_scheduler.isShutdown()) {
54                  return;
55              }
56          }
57          catch (Exception e) {
58              _log.error(e, e);
59          }
60  
61          String jobName =
62              intervalJob.getClass().getName() + StringPool.AT +
63                  intervalJob.hashCode();
64  
65          JobClassUtil.put(jobName, (Class<IntervalJob>)intervalJob.getClass());
66  
67          Date startTime = null;
68  
69          try {
70              if (ServerDetector.getServerId().equals(ServerDetector.TOMCAT_ID)) {
71                  startTime = new Date(System.currentTimeMillis() + Time.MINUTE);
72              }
73          }
74          catch (RuntimeException re) {
75  
76              // ServerDetector will throw an exception when JobSchedulerImpl is
77              // initialized in a test environment
78  
79          }
80  
81          if (startTime == null) {
82              startTime = new Date(System.currentTimeMillis() + Time.MINUTE * 3);
83          }
84  
85          Date endTime = null;
86  
87          JobDetail jobDetail = new JobDetail(
88              jobName, Scheduler.DEFAULT_GROUP, JobWrapper.class);
89  
90          Trigger trigger = new SimpleTrigger(
91              jobName, Scheduler.DEFAULT_GROUP, startTime, endTime,
92              SimpleTrigger.REPEAT_INDEFINITELY, intervalJob.getInterval());
93  
94          try {
95              _scheduler.scheduleJob(jobDetail, trigger);
96          }
97          catch (Exception e) {
98              _log.error(e, e);
99          }
100     }
101 
102     public void setScheduler(Scheduler scheduler) {
103         _scheduler = scheduler;
104     }
105 
106     public void shutdown() {
107         try {
108             if (!_scheduler.isShutdown()) {
109                 _scheduler.shutdown();
110             }
111         }
112         catch (Exception e) {
113             _log.error(e, e);
114         }
115     }
116 
117     public void unschedule(IntervalJob intervalJob) {
118         if (intervalJob == null) {
119             return;
120         }
121 
122         try {
123             if (_scheduler.isShutdown()) {
124                 return;
125             }
126         }
127         catch (Exception e) {
128             _log.error(e, e);
129         }
130 
131         try {
132             String jobName =
133                 intervalJob.getClass().getName() + StringPool.AT +
134                     intervalJob.hashCode();
135 
136             _scheduler.unscheduleJob(jobName, Scheduler.DEFAULT_GROUP);
137         }
138         catch (Exception e) {
139             _log.error(e, e);
140         }
141     }
142 
143     private static Log _log = LogFactoryUtil.getLog(JobScheduler.class);
144 
145     private Scheduler _scheduler;
146 
147 }