##### Coverage Report - org.apache.turbine.services.schedule.JobEntryQuartz

Classes in this File | Line Coverage | Branch Coverage | Complexity
JobEntryQuartz |
56% | 25/44
50% | 2/4
| 1.263157894736842;1,263

1 |  |
     package org.apache.turbine.services.schedule;
2 |  |
3 |  |
     /*
4 |  |
      * Licensed to the Apache Software Foundation (ASF) under one
5 |  |
      * or more contributor license agreements.  See the NOTICE file
6 |  |
      * distributed with this work for additional information
7 |  |
      * regarding copyright ownership.  The ASF licenses this file
8 |  |
      * to you under the Apache License, Version 2.0 (the
9 |  |
      * "License"); you may not use this file except in compliance
10 |  |
      * with the License.  You may obtain a copy of the License at
11 |  |
      *
12 |  |
      *   http://www.apache.org/licenses/LICENSE-2.0
13 |  |
      *
14 |  |
      * Unless required by applicable law or agreed to in writing,
15 |  |
      * software distributed under the License is distributed on an
16 |  |
      * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 |  |
      * KIND, either express or implied.  See the License for the
18 |  |
      * specific language governing permissions and limitations
19 |  |
      * under the License.
20 |  |
      */
21 |  |
22 |  |
     import java.util.Date;
23 |  |
     import java.util.concurrent.atomic.AtomicBoolean;
24 |  |
25 |  |
     import org.apache.commons.lang.StringUtils;
26 |  |
     import org.apache.turbine.modules.ScheduledJobLoader;
27 |  |
     import org.apache.turbine.util.TurbineException;
28 |  |
     import org.quartz.Job;
29 |  |
     import org.quartz.JobBuilder;
30 |  |
     import org.quartz.JobDetail;
31 |  |
     import org.quartz.JobExecutionContext;
32 |  |
     import org.quartz.JobExecutionException;
33 |  |
     import org.quartz.Trigger;
34 |  |
     import org.quartz.core.QuartzScheduler;
35 |  |
36 |  |
     /**
37 |  |
      * This implements a Turbine scheduled job model for the {@link QuartzScheduler}.
38 |  |
      *
39 |  |
      * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
40 |  |
      */
41 | 0 |
     public class JobEntryQuartz implements JobEntry, Job
42 |  |
     {
43 |  |
         private int jobId;
44 |  |
         private Trigger jobTrigger;
45 |  |
         private JobDetail jobDetail;
46 |  |
         private String task;
47 | 50 |
         private boolean isnew = true;
48 | 50 |
         private AtomicBoolean active = new AtomicBoolean(false);
49 |  |
50 |  |
         /**
51 |  |
          * the default Quartz schedule group name for Turbine jobs
52 |  |
          */
53 |  |
         public static final String DEFAULT_JOB_GROUP_NAME = "TURBINE";
54 |  |
55 |  |
         /**
56 |  |
          * Default constructor
57 |  |
          */
58 |  |
         public JobEntryQuartz()
59 |  |
         {
60 | 50 |
             super();
61 | 50 |
         }
62 |  |
63 |  |
         /**
64 |  |
          * Constructor
65 |  |
          *
66 |  |
          * @param jobTrigger Job time table
67 |  |
          */
68 |  |
         public JobEntryQuartz(Trigger jobTrigger)
69 |  |
         {
70 | 0 |
             this(jobTrigger, JobBuilder
71 |  |
                     .newJob(JobEntryQuartz.class)
72 |  |
                     .withIdentity(jobTrigger.getJobKey().getName(), DEFAULT_JOB_GROUP_NAME).build());
73 | 0 |
         }
74 |  |
75 |  |
         /**
76 |  |
          * Constructor
77 |  |
          *
78 |  |
          * @param jobTrigger Job time table
79 |  |
          * @param jobDetail job details
80 |  |
          */
81 |  |
         public JobEntryQuartz(Trigger jobTrigger, JobDetail jobDetail)
82 |  |
         {
83 | 6 |
             this();
84 | 6 |
             setTask(jobTrigger.getJobKey().getName());
85 | 6 |
             this.jobTrigger = jobTrigger;
86 | 6 |
             this.jobDetail = jobDetail;
87 | 6 |
         }
88 |  |
89 |  |
         /**
90 |  |
          * Return true, if the entry is not yet persisted
91 |  |
          */
92 |  |
         @Override
93 |  |
         public boolean isNew()
94 |  |
         {
95 | 0 |
             boolean _isnew = isnew;
96 | 0 |
             isnew = false;
97 | 0 |
             return _isnew;
98 |  |
         }
99 |  |
100 |  |
         /**
101 |  |
          * Get the value of jobId.
102 |  |
          *
103 |  |
          * @return int
104 |  |
          */
105 |  |
         @Override
106 |  |
         public int getJobId()
107 |  |
         {
108 | 1 |
             return jobId;
109 |  |
         }
110 |  |
111 |  |
         /**
112 |  |
          * Set the value of jobId.
113 |  |
          *
114 |  |
          * @param v new value
115 |  |
          */
116 |  |
         @Override
117 |  |
         public void setJobId(int v)
118 |  |
         {
119 | 6 |
             this.jobId = v;
120 | 6 |
         }
121 |  |
122 |  |
         /**
123 |  |
          * Get the value of task.
124 |  |
          *
125 |  |
          * @return String
126 |  |
          */
127 |  |
         @Override
128 |  |
         public String getTask()
129 |  |
         {
130 | 45 |
             return task;
131 |  |
         }
132 |  |
133 |  |
         /**
134 |  |
          * Set the value of task.
135 |  |
          *
136 |  |
          * @param v new value
137 |  |
          */
138 |  |
         @Override
139 |  |
         public void setTask(String v)
140 |  |
         {
141 | 6 |
             this.task = v;
142 | 6 |
         }
143 |  |
144 |  |
         /**
145 |  |
          * @return the jobTrigger
146 |  |
          */
147 |  |
         public Trigger getJobTrigger()
148 |  |
         {
149 | 3 |
             return jobTrigger;
150 |  |
         }
151 |  |
152 |  |
         /**
153 |  |
          * @param jobTrigger the jobTrigger to set
154 |  |
          */
155 |  |
         public void setJobTrigger(Trigger jobTrigger)
156 |  |
         {
157 | 0 |
             this.jobTrigger = jobTrigger;
158 | 0 |
         }
159 |  |
160 |  |
         /**
161 |  |
          * @return the jobDetail
162 |  |
          */
163 |  |
         public JobDetail getJobDetail()
164 |  |
         {
165 | 1 |
             return jobDetail;
166 |  |
         }
167 |  |
168 |  |
         /**
169 |  |
          * @see java.lang.Comparable#compareTo(java.lang.Object)
170 |  |
          */
171 |  |
         @Override
172 |  |
         public int compareTo(JobEntry o)
173 |  |
         {
174 | 0 |
             return jobTrigger.compareTo(((JobEntryQuartz)o).getJobTrigger());
175 |  |
         }
176 |  |
177 |  |
         /**
178 |  |
          * @see org.apache.turbine.services.schedule.JobEntry#setActive(boolean)
179 |  |
          */
180 |  |
         @Override
181 |  |
         public void setActive(boolean isActive)
182 |  |
         {
183 | 0 |
             this.active.set(isActive);
184 | 0 |
         }
185 |  |
186 |  |
         /**
187 |  |
          * @see org.apache.turbine.services.schedule.JobEntry#isActive()
188 |  |
          */
189 |  |
         @Override
190 |  |
         public boolean isActive()
191 |  |
         {
192 | 0 |
             return active.get();
193 |  |
         }
194 |  |
195 |  |
         /**
196 |  |
          * @see org.apache.turbine.services.schedule.JobEntry#getNextRuntime()
197 |  |
          */
198 |  |
         @Override
199 |  |
         public long getNextRuntime()
200 |  |
         {
201 | 0 |
             return getNextRunDate().getTime();
202 |  |
         }
203 |  |
204 |  |
         /**
205 |  |
          * @see org.apache.turbine.services.schedule.JobEntry#getNextRunDate()
206 |  |
          */
207 |  |
         @Override
208 |  |
         public Date getNextRunDate()
209 |  |
         {
210 | 0 |
             return jobTrigger.getNextFireTime();
211 |  |
         }
212 |  |
213 |  |
         /**
214 |  |
          * @see org.apache.turbine.services.schedule.JobEntry#getNextRunAsString()
215 |  |
          */
216 |  |
         @Override
217 |  |
         public String getNextRunAsString()
218 |  |
         {
219 | 0 |
             return getNextRunDate().toString();
220 |  |
         }
221 |  |
222 |  |
         /**
223 |  |
          * @see org.apache.turbine.services.schedule.JobEntry#calcRunTime()
224 |  |
          */
225 |  |
         @Override
226 |  |
         public void calcRunTime() throws TurbineException
227 |  |
         {
228 |  |
             // do nothing
229 | 0 |
         }
230 |  |
231 |  |
         /**
232 |  |
          * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
233 |  |
          */
234 |  |
         @Override
235 |  |
         public void execute(JobExecutionContext context) throws JobExecutionException
236 |  |
         {
237 | 44 |
             if (active.compareAndSet(false, true) == false)
238 |  |
             {
239 | 0 |
                 return;
240 |  |
             }
241 |  |
242 |  |
             try
243 |  |
             {
244 | 44 |
                 String task = getTask();
245 | 44 |
                 if (StringUtils.isEmpty(task))
246 |  |
                 {
247 |  |
                     // This happens when the job is configured in the Quartz configuration file
248 | 44 |
                     task = context.getJobDetail().getKey().getName();
249 |  |
                 }
250 | 44 |
                 ScheduledJobLoader.getInstance().exec(this, task);
251 |  |
             }
252 | 0 |
             catch (Exception e)
253 |  |
             {
254 | 0 |
                 throw new JobExecutionException("Error executing scheduled job #" +
255 |  |
                         getJobId() + ", task: " + getTask(), e);
256 |  |
             }
257 |  |
             finally
258 |  |
             {
259 | 44 |
                 active.compareAndSet(true, false);
260 | 43 |
             }
261 | 43 |
         }
262 |  |
     }