1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.plugin.scheduler; |
21 |
|
|
22 |
|
import java.net.URL; |
23 |
|
import java.util.ArrayList; |
24 |
|
import java.util.Arrays; |
25 |
|
import java.util.Date; |
26 |
|
import java.util.List; |
27 |
|
|
28 |
|
import javax.inject.Provider; |
29 |
|
|
30 |
|
import org.apache.commons.lang3.exception.ExceptionUtils; |
31 |
|
import org.quartz.CronScheduleBuilder; |
32 |
|
import org.quartz.Job; |
33 |
|
import org.quartz.JobBuilder; |
34 |
|
import org.quartz.JobDataMap; |
35 |
|
import org.quartz.JobKey; |
36 |
|
import org.quartz.Scheduler; |
37 |
|
import org.quartz.SchedulerException; |
38 |
|
import org.quartz.Trigger; |
39 |
|
import org.quartz.Trigger.TriggerState; |
40 |
|
import org.quartz.TriggerBuilder; |
41 |
|
import org.quartz.TriggerKey; |
42 |
|
import org.quartz.impl.StdSchedulerFactory; |
43 |
|
import org.slf4j.Logger; |
44 |
|
import org.slf4j.LoggerFactory; |
45 |
|
import org.xwiki.bridge.event.DocumentCreatedEvent; |
46 |
|
import org.xwiki.bridge.event.DocumentDeletedEvent; |
47 |
|
import org.xwiki.bridge.event.DocumentUpdatedEvent; |
48 |
|
import org.xwiki.context.concurrent.ExecutionContextRunnable; |
49 |
|
import org.xwiki.model.reference.DocumentReference; |
50 |
|
import org.xwiki.model.reference.EntityReference; |
51 |
|
import org.xwiki.observation.EventListener; |
52 |
|
import org.xwiki.observation.ObservationManager; |
53 |
|
import org.xwiki.observation.event.Event; |
54 |
|
import org.xwiki.script.service.ScriptServiceManager; |
55 |
|
|
56 |
|
import com.xpn.xwiki.XWikiContext; |
57 |
|
import com.xpn.xwiki.XWikiException; |
58 |
|
import com.xpn.xwiki.api.Api; |
59 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
60 |
|
import com.xpn.xwiki.objects.BaseObject; |
61 |
|
import com.xpn.xwiki.plugin.XWikiDefaultPlugin; |
62 |
|
import com.xpn.xwiki.plugin.XWikiPluginInterface; |
63 |
|
import com.xpn.xwiki.plugin.scheduler.internal.SchedulerJobClassDocumentInitializer; |
64 |
|
import com.xpn.xwiki.plugin.scheduler.internal.StatusListener; |
65 |
|
import com.xpn.xwiki.web.Utils; |
66 |
|
import com.xpn.xwiki.web.XWikiResponse; |
67 |
|
import com.xpn.xwiki.web.XWikiServletRequest; |
68 |
|
import com.xpn.xwiki.web.XWikiServletRequestStub; |
69 |
|
import com.xpn.xwiki.web.XWikiServletResponseStub; |
70 |
|
|
71 |
|
|
72 |
|
@link |
73 |
|
|
74 |
|
@version |
75 |
|
|
|
|
| 81.1% |
Uncovered Elements: 53 (280) |
Complexity: 74 |
Complexity Density: 0.34 |
|
76 |
|
public class SchedulerPlugin extends XWikiDefaultPlugin implements EventListener |
77 |
|
{ |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(SchedulerPlugin.class); |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
@deprecated@link |
87 |
|
|
88 |
|
@Deprecated |
89 |
|
public static final String XWIKI_JOB_CLASS = "XWiki.SchedulerJobClass"; |
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
public static final EntityReference XWIKI_JOB_CLASSREFERENCE = |
95 |
|
SchedulerJobClassDocumentInitializer.XWIKI_JOB_CLASSREFERENCE; |
96 |
|
|
97 |
|
private static final List<Event> EVENTS = Arrays.<Event>asList(new DocumentCreatedEvent(), |
98 |
|
new DocumentDeletedEvent(), new DocumentUpdatedEvent()); |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
private Scheduler scheduler; |
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
@see |
109 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
110 |
2 |
public SchedulerPlugin(String name, String className, XWikiContext context)... |
111 |
|
{ |
112 |
2 |
super(name, className, context); |
113 |
|
} |
114 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
115 |
2 |
@Override... |
116 |
|
public void init(XWikiContext context) |
117 |
|
{ |
118 |
2 |
Thread thread = new Thread(new ExecutionContextRunnable(new Runnable() |
119 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
120 |
2 |
@Override... |
121 |
|
public void run() |
122 |
|
{ |
123 |
2 |
initAsync(); |
124 |
|
} |
125 |
|
}, Utils.getComponentManager())); |
126 |
2 |
thread.setName("XWiki Scheduler initialization"); |
127 |
2 |
thread.setDaemon(true); |
128 |
|
|
129 |
2 |
thread.start(); |
130 |
|
|
131 |
|
|
132 |
2 |
Utils.getComponent(ObservationManager.class).addListener(this); |
133 |
|
} |
134 |
|
|
|
|
| 81% |
Uncovered Elements: 4 (21) |
Complexity: 5 |
Complexity Density: 0.26 |
|
135 |
2 |
private void initAsync()... |
136 |
|
{ |
137 |
2 |
XWikiContext xcontext = Utils.<Provider<XWikiContext>>getComponent(XWikiContext.TYPE_PROVIDER).get(); |
138 |
|
|
139 |
2 |
try { |
140 |
2 |
String initialDb = !xcontext.getWikiId().equals("") ? xcontext.getWikiId() : xcontext.getMainXWiki(); |
141 |
|
|
142 |
2 |
List<String> wikiServers = new ArrayList<String>(); |
143 |
2 |
try { |
144 |
2 |
wikiServers = xcontext.getWiki().getVirtualWikisDatabaseNames(xcontext); |
145 |
|
} catch (Exception e) { |
146 |
0 |
LOGGER.error("error getting list of wiki servers!", e); |
147 |
|
} |
148 |
|
|
149 |
|
|
150 |
|
|
151 |
2 |
System.setProperty("org.quartz.scheduler.makeSchedulerThreadDaemon", "true"); |
152 |
2 |
System.setProperty("org.quartz.threadPool.makeThreadsDaemons", "true"); |
153 |
|
|
154 |
2 |
setScheduler(getDefaultSchedulerInstance()); |
155 |
2 |
setStatusListener(); |
156 |
2 |
getScheduler().start(); |
157 |
|
|
158 |
|
|
159 |
|
|
160 |
2 |
try { |
161 |
|
|
162 |
2 |
for (String wikiName : wikiServers) { |
163 |
2 |
xcontext.setWikiId(wikiName); |
164 |
2 |
restoreExistingJobs(xcontext); |
165 |
|
} |
166 |
|
} finally { |
167 |
2 |
xcontext.setWikiId(initialDb); |
168 |
|
} |
169 |
|
} catch (SchedulerException e) { |
170 |
0 |
LOGGER.error("Failed to start the scheduler", e); |
171 |
|
} catch (SchedulerPluginException e) { |
172 |
0 |
LOGGER.error("Failed to initialize the scheduler", e); |
173 |
|
} |
174 |
|
} |
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
@param |
183 |
|
@param |
184 |
|
@return |
185 |
|
|
|
|
| 92.2% |
Uncovered Elements: 5 (64) |
Complexity: 11 |
Complexity Density: 0.21 |
|
186 |
4 |
private XWikiContext prepareJobStubContext(BaseObject job, XWikiContext context) throws SchedulerPluginException... |
187 |
|
{ |
188 |
4 |
boolean jobNeedsUpdate = false; |
189 |
4 |
String cUser = job.getStringValue("contextUser"); |
190 |
4 |
if (cUser.equals("")) { |
191 |
|
|
192 |
|
|
193 |
1 |
cUser = context.getUser(); |
194 |
1 |
job.setStringValue("contextUser", cUser); |
195 |
1 |
jobNeedsUpdate = true; |
196 |
|
} |
197 |
4 |
String cLang = job.getStringValue("contextLang"); |
198 |
4 |
if (cLang.equals("")) { |
199 |
1 |
cLang = context.getLanguage(); |
200 |
1 |
job.setStringValue("contextLang", cLang); |
201 |
1 |
jobNeedsUpdate = true; |
202 |
|
} |
203 |
4 |
String iDb = context.getWikiId(); |
204 |
4 |
String cDb = job.getStringValue("contextDatabase"); |
205 |
4 |
if (cDb.equals("") || !cDb.equals(iDb)) { |
206 |
1 |
cDb = context.getWikiId(); |
207 |
1 |
job.setStringValue("contextDatabase", cDb); |
208 |
1 |
jobNeedsUpdate = true; |
209 |
|
} |
210 |
|
|
211 |
4 |
if (jobNeedsUpdate) { |
212 |
1 |
try { |
213 |
1 |
context.setWikiId(cDb); |
214 |
1 |
XWikiDocument jobHolder = context.getWiki().getDocument(job.getName(), context); |
215 |
1 |
jobHolder.setMinorEdit(true); |
216 |
1 |
context.getWiki().saveDocument(jobHolder, context); |
217 |
|
} catch (XWikiException e) { |
218 |
0 |
throw new SchedulerPluginException( |
219 |
|
SchedulerPluginException.ERROR_SCHEDULERPLUGIN_UNABLE_TO_PREPARE_JOB_CONTEXT, |
220 |
|
"Failed to prepare context for job with job name " + job.getStringValue("jobName"), e); |
221 |
|
} finally { |
222 |
1 |
context.setWikiId(iDb); |
223 |
|
} |
224 |
|
} |
225 |
|
|
226 |
|
|
227 |
4 |
XWikiContext scontext = context.clone(); |
228 |
4 |
scontext.setWiki(context.getWiki()); |
229 |
4 |
context.getWiki().getStore().cleanUp(context); |
230 |
|
|
231 |
|
|
232 |
|
|
233 |
4 |
XWikiServletRequestStub dummy = new XWikiServletRequestStub(); |
234 |
4 |
dummy.setHost(context.getRequest().getHeader("x-forwarded-host")); |
235 |
4 |
dummy.setScheme(context.getRequest().getScheme()); |
236 |
4 |
dummy.setContextPath(context.getRequest().getContextPath()); |
237 |
4 |
XWikiServletRequest request = new XWikiServletRequest(dummy); |
238 |
4 |
scontext.setRequest(request); |
239 |
|
|
240 |
|
|
241 |
|
|
242 |
|
|
243 |
4 |
XWikiResponse stub = new XWikiServletResponseStub(); |
244 |
4 |
scontext.setResponse(stub); |
245 |
|
|
246 |
|
|
247 |
4 |
scontext.setUser(cUser); |
248 |
4 |
scontext.setLanguage(cLang); |
249 |
4 |
scontext.setWikiId(cDb); |
250 |
4 |
scontext.setMainXWiki(context.getMainXWiki()); |
251 |
4 |
if (scontext.getURL() == null) { |
252 |
0 |
try { |
253 |
0 |
scontext.setURL(new URL("http://www.mystuburl.com/")); |
254 |
|
} catch (Exception e) { |
255 |
|
|
256 |
|
} |
257 |
|
} |
258 |
|
|
259 |
4 |
com.xpn.xwiki.web.XWikiURLFactory xurf = context.getURLFactory(); |
260 |
4 |
if (xurf == null) { |
261 |
3 |
xurf = context.getWiki().getURLFactoryService().createURLFactory(context.getMode(), context); |
262 |
|
} |
263 |
4 |
scontext.setURLFactory(xurf); |
264 |
|
|
265 |
4 |
try { |
266 |
4 |
XWikiDocument cDoc = context.getWiki().getDocument(job.getDocumentReference(), context); |
267 |
4 |
scontext.setDoc(cDoc); |
268 |
|
} catch (Exception e) { |
269 |
0 |
throw new SchedulerPluginException( |
270 |
|
SchedulerPluginException.ERROR_SCHEDULERPLUGIN_UNABLE_TO_PREPARE_JOB_CONTEXT, |
271 |
|
"Failed to prepare context for job with job name " + job.getStringValue("jobName"), e); |
272 |
|
} |
273 |
|
|
274 |
4 |
return scontext; |
275 |
|
} |
276 |
|
|
277 |
|
|
278 |
|
|
279 |
|
|
280 |
|
|
281 |
|
|
282 |
|
|
283 |
|
@param |
284 |
|
|
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 3 |
Complexity Density: 0.33 |
|
285 |
2 |
private void restoreExistingJobs(XWikiContext context)... |
286 |
|
{ |
287 |
2 |
String hql = ", BaseObject as obj where obj.name=doc.fullName and obj.className='XWiki.SchedulerJobClass'"; |
288 |
2 |
try { |
289 |
2 |
List<DocumentReference> jobDocReferences = |
290 |
|
context.getWiki().getStore().searchDocumentReferences(hql, context); |
291 |
2 |
for (DocumentReference docReference : jobDocReferences) { |
292 |
5 |
try { |
293 |
5 |
XWikiDocument jobDoc = context.getWiki().getDocument(docReference, context); |
294 |
|
|
295 |
5 |
register(jobDoc, context); |
296 |
|
} catch (Exception e) { |
297 |
0 |
LOGGER.error("Failed to restore job with in document [{}] and wiki [{}]", docReference, |
298 |
|
context.getWikiId(), e); |
299 |
|
} |
300 |
|
} |
301 |
|
} catch (Exception e) { |
302 |
0 |
LOGGER.error("Failed to restore existing scheduler jobs in wiki [{}]", context.getWikiId(), e); |
303 |
|
} |
304 |
|
} |
305 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
306 |
5 |
private void register(XWikiDocument jobDoc, XWikiContext context) throws SchedulerPluginException... |
307 |
|
{ |
308 |
5 |
BaseObject jobObj = jobDoc.getXObject(XWIKI_JOB_CLASSREFERENCE); |
309 |
|
|
310 |
5 |
register(jobObj, context); |
311 |
|
} |
312 |
|
|
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 4 |
Complexity Density: 0.8 |
|
313 |
7 |
private void register(BaseObject jobObj, XWikiContext context) throws SchedulerPluginException... |
314 |
|
{ |
315 |
7 |
String status = jobObj.getStringValue("status"); |
316 |
7 |
if (status.equals(JobState.STATE_NORMAL) || status.equals(JobState.STATE_PAUSED)) { |
317 |
3 |
scheduleJob(jobObj, context); |
318 |
|
} |
319 |
7 |
if (status.equals(JobState.STATE_PAUSED)) { |
320 |
0 |
pauseJob(jobObj, context); |
321 |
|
} |
322 |
|
} |
323 |
|
|
|
|
| 55.6% |
Uncovered Elements: 4 (9) |
Complexity: 4 |
Complexity Density: 0.8 |
|
324 |
1 |
private void unregister(BaseObject jobObj, XWikiContext context) throws SchedulerPluginException... |
325 |
|
{ |
326 |
1 |
String status = jobObj.getStringValue("status"); |
327 |
1 |
if (status.equals(JobState.STATE_NORMAL) || status.equals(JobState.STATE_PAUSED)) { |
328 |
0 |
scheduleJob(jobObj, context); |
329 |
|
} |
330 |
1 |
if (status.equals(JobState.STATE_PAUSED)) { |
331 |
0 |
pauseJob(jobObj, context); |
332 |
|
} |
333 |
|
} |
334 |
|
|
335 |
|
|
336 |
|
@link |
337 |
|
|
338 |
|
|
339 |
|
|
340 |
|
@param |
341 |
|
@return@link |
342 |
|
|
343 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
344 |
30 |
public JobState getJobStatus(BaseObject object, XWikiContext context) throws SchedulerException... |
345 |
|
{ |
346 |
30 |
TriggerState state = getScheduler().getTriggerState(new TriggerKey(getObjectUniqueId(object, context))); |
347 |
30 |
return new JobState(state); |
348 |
|
} |
349 |
|
|
|
|
| 63% |
Uncovered Elements: 20 (54) |
Complexity: 9 |
Complexity Density: 0.18 |
|
350 |
4 |
public boolean scheduleJob(BaseObject object, XWikiContext context) throws SchedulerPluginException... |
351 |
|
{ |
352 |
4 |
boolean scheduled = true; |
353 |
4 |
try { |
354 |
|
|
355 |
4 |
String xjob = getObjectUniqueId(object, context); |
356 |
|
|
357 |
|
|
358 |
|
|
359 |
|
|
360 |
4 |
ClassLoader currentThreadClassLoader = Thread.currentThread().getContextClassLoader(); |
361 |
4 |
String jobClassName = object.getStringValue("jobClass"); |
362 |
4 |
Class<Job> jobClass = (Class<Job>) Class.forName(jobClassName, true, currentThreadClassLoader); |
363 |
|
|
364 |
|
|
365 |
4 |
JobBuilder jobBuilder = JobBuilder.newJob(jobClass); |
366 |
|
|
367 |
4 |
jobBuilder.withIdentity(xjob); |
368 |
4 |
jobBuilder.storeDurably(); |
369 |
|
|
370 |
4 |
JobDataMap data = new JobDataMap(); |
371 |
|
|
372 |
|
|
373 |
4 |
XWikiContext stubContext = prepareJobStubContext(object, context); |
374 |
4 |
data.put("context", stubContext); |
375 |
4 |
data.put("xcontext", stubContext); |
376 |
4 |
data.put("xwiki", new com.xpn.xwiki.api.XWiki(context.getWiki(), stubContext)); |
377 |
4 |
data.put("xjob", object); |
378 |
4 |
data.put("services", Utils.getComponent(ScriptServiceManager.class)); |
379 |
|
|
380 |
4 |
jobBuilder.setJobData(data); |
381 |
|
|
382 |
4 |
getScheduler().addJob(jobBuilder.build(), true); |
383 |
|
|
384 |
4 |
TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger(); |
385 |
|
|
386 |
4 |
triggerBuilder.withIdentity(xjob); |
387 |
4 |
triggerBuilder.forJob(xjob); |
388 |
|
|
389 |
4 |
triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(object.getStringValue("cron"))); |
390 |
|
|
391 |
4 |
Trigger trigger = triggerBuilder.build(); |
392 |
|
|
393 |
4 |
JobState status = getJobStatus(object, context); |
394 |
|
|
395 |
4 |
switch (status.getQuartzState()) { |
396 |
0 |
case PAUSED: |
397 |
|
|
398 |
0 |
break; |
399 |
0 |
case NORMAL: |
400 |
0 |
if (getTrigger(object, context).compareTo(trigger) != 0) { |
401 |
0 |
LOGGER.debug("Reschedule Job: [{}]", object.getStringValue("jobName")); |
402 |
|
} |
403 |
0 |
getScheduler().rescheduleJob(trigger.getKey(), trigger); |
404 |
0 |
break; |
405 |
4 |
case NONE: |
406 |
4 |
LOGGER.debug("Schedule Job: [{}]", object.getStringValue("jobName")); |
407 |
4 |
getScheduler().scheduleJob(trigger); |
408 |
4 |
LOGGER.info("XWiki Job Status: [{}]", object.getStringValue("status")); |
409 |
4 |
if (object.getStringValue("status").equals("Paused")) { |
410 |
0 |
getScheduler().pauseJob(new JobKey(xjob)); |
411 |
0 |
saveStatus("Paused", object, context); |
412 |
|
} else { |
413 |
4 |
saveStatus("Normal", object, context); |
414 |
|
} |
415 |
4 |
break; |
416 |
0 |
default: |
417 |
0 |
LOGGER.debug("Schedule Job: [{}]", object.getStringValue("jobName")); |
418 |
0 |
getScheduler().scheduleJob(trigger); |
419 |
0 |
saveStatus("Normal", object, context); |
420 |
0 |
break; |
421 |
|
} |
422 |
|
} catch (SchedulerException e) { |
423 |
0 |
throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_SCHEDULE_JOB, |
424 |
|
"Error while scheduling job " + object.getStringValue("jobName"), e); |
425 |
|
} catch (ClassNotFoundException e) { |
426 |
0 |
throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_JOB_XCLASS_NOT_FOUND, |
427 |
|
"Error while loading job class for job : " + object.getStringValue("jobName"), e); |
428 |
|
} catch (XWikiException e) { |
429 |
0 |
throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_JOB_XCLASS_NOT_FOUND, |
430 |
|
"Error while saving job status for job : " + object.getStringValue("jobName"), e); |
431 |
|
} |
432 |
|
|
433 |
4 |
return scheduled; |
434 |
|
} |
435 |
|
|
436 |
|
|
437 |
|
|
438 |
|
|
439 |
|
@param |
440 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 3 |
Complexity Density: 0.5 |
|
441 |
1 |
public void pauseJob(BaseObject object, XWikiContext context) throws SchedulerPluginException... |
442 |
|
{ |
443 |
1 |
String job = getObjectUniqueId(object, context); |
444 |
1 |
try { |
445 |
1 |
getScheduler().pauseJob(new JobKey(job)); |
446 |
1 |
saveStatus("Paused", object, context); |
447 |
|
} catch (SchedulerException e) { |
448 |
0 |
throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_PAUSE_JOB, |
449 |
|
"Error occured while trying to pause job " + object.getStringValue("jobName"), e); |
450 |
|
} catch (XWikiException e) { |
451 |
0 |
throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_PAUSE_JOB, |
452 |
|
"Error occured while trying to save status of job " + object.getStringValue("jobName"), e); |
453 |
|
} |
454 |
|
} |
455 |
|
|
456 |
|
|
457 |
|
|
458 |
|
|
459 |
|
@param |
460 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 3 |
Complexity Density: 0.5 |
|
461 |
1 |
public void resumeJob(BaseObject object, XWikiContext context) throws SchedulerPluginException... |
462 |
|
{ |
463 |
1 |
String job = getObjectUniqueId(object, context); |
464 |
1 |
try { |
465 |
1 |
getScheduler().resumeJob(new JobKey(job)); |
466 |
1 |
saveStatus("Normal", object, context); |
467 |
|
} catch (SchedulerException e) { |
468 |
0 |
throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_RESUME_JOB, |
469 |
|
"Error occured while trying to resume job " + object.getStringValue("jobName"), e); |
470 |
|
} catch (XWikiException e) { |
471 |
0 |
throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_RESUME_JOB, |
472 |
|
"Error occured while trying to save status of job " + object.getStringValue("jobName"), e); |
473 |
|
} |
474 |
|
} |
475 |
|
|
476 |
|
|
477 |
|
|
478 |
|
|
479 |
|
@param |
480 |
|
@param |
481 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
482 |
2 |
public void triggerJob(BaseObject object, XWikiContext context) throws SchedulerPluginException... |
483 |
|
{ |
484 |
2 |
String job = getObjectUniqueId(object, context); |
485 |
2 |
try { |
486 |
2 |
getScheduler().triggerJob(new JobKey(job)); |
487 |
|
} catch (SchedulerException e) { |
488 |
0 |
throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_TRIGGER_JOB, |
489 |
|
"Error occured while trying to trigger job " + object.getStringValue("jobName"), e); |
490 |
|
} |
491 |
|
} |
492 |
|
|
493 |
|
|
494 |
|
|
495 |
|
|
496 |
|
@param |
497 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 3 |
Complexity Density: 0.5 |
|
498 |
1 |
public void unscheduleJob(BaseObject object, XWikiContext context) throws SchedulerPluginException... |
499 |
|
{ |
500 |
1 |
String job = getObjectUniqueId(object, context); |
501 |
1 |
try { |
502 |
1 |
getScheduler().deleteJob(new JobKey(job)); |
503 |
1 |
saveStatus("None", object, context); |
504 |
|
} catch (SchedulerException e) { |
505 |
0 |
throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_JOB_XCLASS_NOT_FOUND, |
506 |
|
"Error while unscheduling job " + object.getStringValue("jobName"), e); |
507 |
|
} catch (XWikiException e) { |
508 |
0 |
throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_JOB_XCLASS_NOT_FOUND, |
509 |
|
"Error while saving status of job " + object.getStringValue("jobName"), e); |
510 |
|
} |
511 |
|
} |
512 |
|
|
513 |
|
|
514 |
|
|
515 |
|
|
516 |
|
@param |
517 |
|
@param |
518 |
|
@return |
519 |
|
|
|
|
| 70% |
Uncovered Elements: 3 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
|
520 |
16 |
private Trigger getTrigger(BaseObject object, XWikiContext context) throws SchedulerPluginException... |
521 |
|
{ |
522 |
16 |
String job = getObjectUniqueId(object, context); |
523 |
16 |
Trigger trigger; |
524 |
16 |
try { |
525 |
16 |
trigger = getScheduler().getTrigger(new TriggerKey(job)); |
526 |
|
} catch (SchedulerException e) { |
527 |
0 |
throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_JOB_XCLASS_NOT_FOUND, |
528 |
|
"Error while getting trigger for job " + job, e); |
529 |
|
} |
530 |
16 |
if (trigger == null) { |
531 |
0 |
throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_JOB_DOES_NOT_EXITS, |
532 |
|
"Job does not exists"); |
533 |
|
} |
534 |
|
|
535 |
16 |
return trigger; |
536 |
|
} |
537 |
|
|
538 |
|
|
539 |
|
@link |
540 |
|
|
541 |
|
|
542 |
|
|
543 |
|
@param |
544 |
|
@param |
545 |
|
@return |
546 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
547 |
0 |
public Date getPreviousFireTime(BaseObject object, XWikiContext context) throws SchedulerPluginException... |
548 |
|
{ |
549 |
0 |
return getTrigger(object, context).getPreviousFireTime(); |
550 |
|
} |
551 |
|
|
552 |
|
|
553 |
|
|
554 |
|
|
555 |
|
@param |
556 |
|
@return |
557 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
558 |
16 |
public Date getNextFireTime(BaseObject object, XWikiContext context) throws SchedulerPluginException... |
559 |
|
{ |
560 |
16 |
return getTrigger(object, context).getNextFireTime(); |
561 |
|
} |
562 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
563 |
25 |
@Override... |
564 |
|
public Api getPluginApi(XWikiPluginInterface plugin, XWikiContext context) |
565 |
|
{ |
566 |
25 |
return new SchedulerPluginApi((SchedulerPlugin) plugin, context); |
567 |
|
} |
568 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
569 |
20 |
@Override... |
570 |
|
public String getName() |
571 |
|
{ |
572 |
20 |
return "scheduler"; |
573 |
|
} |
574 |
|
|
575 |
|
|
576 |
|
@param |
577 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
578 |
2 |
public void setScheduler(Scheduler scheduler)... |
579 |
|
{ |
580 |
2 |
this.scheduler = scheduler; |
581 |
|
} |
582 |
|
|
583 |
|
|
584 |
|
@return |
585 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
586 |
65 |
public Scheduler getScheduler()... |
587 |
|
{ |
588 |
65 |
return this.scheduler; |
589 |
|
} |
590 |
|
|
591 |
|
|
592 |
|
@return |
593 |
|
@throws |
594 |
|
|
595 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
596 |
2 |
private synchronized Scheduler getDefaultSchedulerInstance() throws SchedulerPluginException... |
597 |
|
{ |
598 |
2 |
Scheduler scheduler; |
599 |
2 |
try { |
600 |
2 |
scheduler = StdSchedulerFactory.getDefaultScheduler(); |
601 |
|
} catch (SchedulerException e) { |
602 |
0 |
throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_GET_SCHEDULER, |
603 |
|
"Error getting default Scheduler instance", e); |
604 |
|
} |
605 |
2 |
return scheduler; |
606 |
|
} |
607 |
|
|
608 |
|
|
609 |
|
|
610 |
|
|
611 |
|
@throws |
612 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
613 |
2 |
private void setStatusListener() throws SchedulerPluginException... |
614 |
|
{ |
615 |
2 |
StatusListener listener = new StatusListener(); |
616 |
2 |
try { |
617 |
2 |
getScheduler().getListenerManager().addSchedulerListener(listener); |
618 |
2 |
getScheduler().getListenerManager().addJobListener(listener); |
619 |
|
} catch (SchedulerException e) { |
620 |
0 |
throw new SchedulerPluginException( |
621 |
|
SchedulerPluginException.ERROR_SCHEDULERPLUGIN_INITIALIZE_STATUS_LISTENER, |
622 |
|
"Error while initializing the status listener", e); |
623 |
|
} |
624 |
|
} |
625 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
626 |
7 |
private void saveStatus(String status, BaseObject object, XWikiContext context) throws XWikiException... |
627 |
|
{ |
628 |
7 |
XWikiDocument jobHolder = context.getWiki().getDocument(object.getDocumentReference(), context); |
629 |
|
|
630 |
|
|
631 |
|
|
632 |
|
|
633 |
7 |
BaseObject job = jobHolder.getXObject(XWIKI_JOB_CLASSREFERENCE); |
634 |
7 |
job.setStringValue("status", status); |
635 |
7 |
jobHolder.setMinorEdit(true); |
636 |
7 |
context.getWiki().saveDocument(jobHolder, context); |
637 |
|
} |
638 |
|
|
639 |
|
|
640 |
|
@link |
641 |
|
|
642 |
|
|
643 |
|
|
644 |
|
|
645 |
|
@return |
646 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
647 |
55 |
private String getObjectUniqueId(BaseObject object, XWikiContext context)... |
648 |
|
{ |
649 |
55 |
return context.getWikiId() + ":" + object.getName() + "_" + object.getNumber(); |
650 |
|
} |
651 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
652 |
2 |
@Override... |
653 |
|
public List<Event> getEvents() |
654 |
|
{ |
655 |
2 |
return EVENTS; |
656 |
|
} |
657 |
|
|
|
|
| 90% |
Uncovered Elements: 2 (20) |
Complexity: 6 |
Complexity Density: 0.43 |
|
658 |
72 |
@Override... |
659 |
|
public void onEvent(Event event, Object source, Object data) |
660 |
|
{ |
661 |
72 |
XWikiContext xcontext = (XWikiContext) data; |
662 |
72 |
XWikiDocument document = (XWikiDocument) source; |
663 |
72 |
XWikiDocument originalDocument = document.getOriginalDocument(); |
664 |
|
|
665 |
72 |
BaseObject jobObj = document.getXObject(XWIKI_JOB_CLASSREFERENCE); |
666 |
72 |
BaseObject originalJobObj = originalDocument.getXObject(XWIKI_JOB_CLASSREFERENCE); |
667 |
|
|
668 |
72 |
if (jobObj == null) { |
669 |
60 |
if (originalJobObj != null) { |
670 |
|
|
671 |
1 |
try { |
672 |
1 |
unregister(originalJobObj, xcontext); |
673 |
|
} catch (SchedulerPluginException e) { |
674 |
0 |
LOGGER.warn("Failed to register job in document [{}]: {}", document.getDocumentReference(), |
675 |
|
ExceptionUtils.getRootCauseMessage(e)); |
676 |
|
} |
677 |
|
} |
678 |
|
} else { |
679 |
12 |
if (originalJobObj == null) { |
680 |
|
|
681 |
2 |
try { |
682 |
2 |
register(jobObj, xcontext); |
683 |
|
} catch (SchedulerPluginException e) { |
684 |
0 |
LOGGER.warn("Failed to register job in document [{}]: {}", document.getDocumentReference(), |
685 |
|
ExceptionUtils.getRootCauseMessage(e)); |
686 |
|
} |
687 |
|
} |
688 |
|
} |
689 |
|
} |
690 |
|
} |