1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.plugin.scheduler

File AbstractJob.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

0
15
2
1
90
42
3
0.2
7.5
2
1.5

Classes

Class Line # Actions
AbstractJob 42 15 0% 3 1
0.941176594.1%
 

Contributing tests

No tests hitting this source file were found.

Source view

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.quartz.Job;
23    import org.quartz.JobDataMap;
24    import org.quartz.JobExecutionContext;
25    import org.quartz.JobExecutionException;
26    import org.xwiki.context.Execution;
27    import org.xwiki.context.ExecutionContext;
28    import org.xwiki.context.ExecutionContextException;
29    import org.xwiki.context.ExecutionContextManager;
30   
31    import com.xpn.xwiki.XWikiContext;
32    import com.xpn.xwiki.web.Utils;
33   
34    /**
35    * Base class for any XWiki Quartz Job. This class take care of initializing ExecutionContext properly.
36    * <p>
37    * A class extending {@link AbstractJob} should implements {@link #executeJob(JobExecutionContext)}.
38    *
39    * @since 1.8
40    * @version $Id: 60a9906f513b68b5ecb7c65c15eaa627d2ba3c66 $
41    */
 
42    public abstract class AbstractJob implements Job
43    {
44    private XWikiContext xcontext;
45   
 
46  6 toggle @Override
47    public final void execute(JobExecutionContext jobContext) throws JobExecutionException
48    {
49  6 JobDataMap data = jobContext.getJobDetail().getJobDataMap();
50   
51    // The XWiki context was saved in the Job execution data map. Get it as we'll retrieve
52    // the script to execute from it.
53  6 this.xcontext = (XWikiContext) data.get("context");
54   
55    // Clone the XWikiContex to have a new one for each run
56  6 this.xcontext = this.xcontext.clone();
57   
58    // Init execution context
59  6 Execution execution;
60  6 try {
61  6 ExecutionContextManager ecim = Utils.getComponent(ExecutionContextManager.class);
62  6 execution = Utils.getComponent(Execution.class);
63   
64  6 ExecutionContext context = new ExecutionContext();
65   
66    // Bridge with old XWiki Context, required for old code
67  6 this.xcontext.declareInExecutionContext(context);
68   
69  6 ecim.initialize(context);
70    } catch (ExecutionContextException e) {
71  0 throw new JobExecutionException("Fail to initialize execution context", e);
72    }
73   
74  6 try {
75    // Execute the job
76  6 executeJob(jobContext);
77    } finally {
78    // We must ensure we clean the ThreadLocal variables located in the Execution
79    // component as otherwise we will have a potential memory leak.
80  6 execution.removeContext();
81    }
82    }
83   
 
84  13 toggle protected XWikiContext getXWikiContext()
85    {
86  13 return this.xcontext;
87    }
88   
89    protected abstract void executeJob(JobExecutionContext jobContext) throws JobExecutionException;
90    }