Class | Line # | Actions | |||||
---|---|---|---|---|---|---|---|
GroovyJob | 43 | 13 | 0% | 4 | 4 |
1 | /* | |
2 | * See the NOTICE file distributed with this work for additional | |
3 | * information regarding copyright ownership. | |
4 | * | |
5 | * This is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU Lesser General Public License as | |
7 | * published by the Free Software Foundation; either version 2.1 of | |
8 | * the License, or (at your option) any later version. | |
9 | * | |
10 | * This software is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * Lesser General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU Lesser General Public | |
16 | * License along with this software; if not, write to the Free | |
17 | * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
18 | * 02110-1301 USA, or see the FSF site: http://www.fsf.org. | |
19 | */ | |
20 | package com.xpn.xwiki.plugin.scheduler; | |
21 | ||
22 | import org.codehaus.groovy.control.CompilationFailedException; | |
23 | import org.quartz.JobDataMap; | |
24 | import org.quartz.JobExecutionContext; | |
25 | import org.quartz.JobExecutionException; | |
26 | ||
27 | import com.xpn.xwiki.doc.XWikiDocument; | |
28 | import com.xpn.xwiki.objects.BaseObject; | |
29 | ||
30 | import groovy.lang.Binding; | |
31 | import groovy.lang.GroovyShell; | |
32 | ||
33 | /** | |
34 | * The task that will get executed by the Scheduler when the Job is triggered. This task in turn calls a Groovy script | |
35 | * to perform the execution. | |
36 | * <p> | |
37 | * <b>Important:</b>: Note that the script will execute in the XWiki Context that was set at the time the Job was | |
38 | * scheduled for execution. For example calling <code>context.getDoc()</code> will return the current document that was | |
39 | * set at that time and not the current document that is set when the Groovy script executes... | |
40 | * | |
41 | * @version $Id: 2b715d960c5dfd71fc326117fac7a775e90c6c6b $ | |
42 | */ | |
43 | public class GroovyJob extends AbstractJob | |
44 | { | |
45 | /** | |
46 | * Executes the Groovy script passed in the <code>script</code> property of the | |
47 | * {@link com.xpn.xwiki.plugin.scheduler.SchedulerPlugin#XWIKI_JOB_CLASS} object extracted from the XWiki context | |
48 | * passed in the Quartz's Job execution context. The XWiki Task object is looked for in the current document that | |
49 | * was set in the context at the time the Job was scheduled. | |
50 | * | |
51 | * @param jobContext the Quartz execution context containing the XWiki context from which the script to execute is | |
52 | * retrieved | |
53 | * @throws JobExecutionException if the script fails to execute or if the user didn't have programming rights when | |
54 | * the Job was scheduled | |
55 | * @see org.quartz.Job#execute(org.quartz.JobExecutionContext) | |
56 | */ | |
57 | 1 | ![]() |
58 | protected void executeJob(JobExecutionContext jobContext) throws JobExecutionException | |
59 | { | |
60 | 1 | try { |
61 | 1 | JobDataMap data = jobContext.getJobDetail().getJobDataMap(); |
62 | ||
63 | // Get the job XObject to be executed | |
64 | 1 | BaseObject object = (BaseObject) data.get("xjob"); |
65 | ||
66 | // Force context document | |
67 | 1 | XWikiDocument jobDocument = getXWikiContext().getWiki().getDocument(object.getName(), getXWikiContext()); |
68 | 1 | getXWikiContext().setDoc(jobDocument); |
69 | 1 | getXWikiContext().put("sdoc", jobDocument); |
70 | ||
71 | 1 | if (getXWikiContext().getWiki().getRightService().hasProgrammingRights(getXWikiContext())) { |
72 | ||
73 | // Make the Job execution data available to the Groovy script | |
74 | 1 | Binding binding = new Binding(data.getWrappedMap()); |
75 | ||
76 | // Execute the Groovy script | |
77 | 1 | GroovyShell shell = new GroovyShell(Thread.currentThread().getContextClassLoader(), binding); |
78 | 1 | shell.evaluate(object.getLargeStringValue("script")); |
79 | } else { | |
80 | 0 | throw new JobExecutionException("The user [" + getXWikiContext().getUser() + "] didn't have " |
81 | + "programming rights when the job [" + jobContext.getJobDetail().getKey() + "] was scheduled."); | |
82 | } | |
83 | } catch (CompilationFailedException e) { | |
84 | 0 | throw new JobExecutionException( |
85 | "Failed to execute script for job [" + jobContext.getJobDetail().getKey() + "]", e, true); | |
86 | } catch (Exception e) { | |
87 | 0 | e.printStackTrace(); |
88 | } | |
89 | } | |
90 | } |