1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.job.script

File JobScriptService.java

 

Coverage histogram

../../../../img/srcFileCovDistChart3.png
80% of files have more coverage

Code metrics

10
23
6
1
178
85
12
0.52
3.83
6
2

Classes

Class Line # Actions
JobScriptService 51 23 0% 12 28
0.282051328.2%
 

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 org.xwiki.job.script;
21   
22    import java.util.Collection;
23    import java.util.List;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Singleton;
28   
29    import org.xwiki.component.annotation.Component;
30    import org.xwiki.context.Execution;
31    import org.xwiki.job.Job;
32    import org.xwiki.job.JobException;
33    import org.xwiki.job.JobExecutor;
34    import org.xwiki.job.JobGroupPath;
35    import org.xwiki.job.JobStatusStore;
36    import org.xwiki.job.event.status.JobStatus;
37    import org.xwiki.script.internal.safe.ScriptSafeProvider;
38    import org.xwiki.script.service.ScriptService;
39    import org.xwiki.security.authorization.ContextualAuthorizationManager;
40    import org.xwiki.security.authorization.Right;
41   
42    /**
43    * Provides job-specific scripting APIs.
44    *
45    * @version $Id: dd6a59384f62a8080da337398c6b94ae76cf5d51 $
46    * @since 7.2M1
47    */
48    @Component
49    @Named("job")
50    @Singleton
 
51    public class JobScriptService implements ScriptService
52    {
53    /**
54    * The key under which the last encountered error is stored in the current execution context.
55    */
56    public static final String JOB_ERROR_KEY = "scriptservice.job.error";
57   
58    /**
59    * Used to retrieve the job that is currently being executed.
60    */
61    @Inject
62    private JobExecutor jobExecutor;
63   
64    /**
65    * Used to retrieve the job status.
66    */
67    @Inject
68    private JobStatusStore jobStore;
69   
70    /**
71    * Used to check programming rights.
72    */
73    @Inject
74    private ContextualAuthorizationManager authorization;
75   
76    @Inject
77    @SuppressWarnings("rawtypes")
78    private ScriptSafeProvider scriptSafeProvider;
79   
80    /**
81    * Provides access to the current context.
82    */
83    @Inject
84    private Execution execution;
85   
86    /**
87    * @param jobId the job id
88    * @return the status of the specified job, {@code null} if the status cannot be found
89    */
 
90  4069 toggle public JobStatus getJobStatus(List<String> jobId)
91    {
92  4069 JobStatus jobStatus;
93   
94  4069 Job job = this.jobExecutor.getJob(jobId);
95  4069 if (job == null) {
96  4012 jobStatus = this.jobStore.getJobStatus(jobId);
97    } else {
98  57 jobStatus = job.getStatus();
99    }
100   
101  4069 if (jobStatus != null && !this.authorization.hasAccess(Right.PROGRAM)) {
102  0 jobStatus = safe(jobStatus);
103    }
104   
105  4069 return jobStatus;
106    }
107   
108    /**
109    * Get a reference to the currently job executed in the specified job group.
110    *
111    * @param path specifies the job group where to look for a running job
112    * @return currently executing job in the specified job group, or {@code null} if no job is being executed
113    */
 
114  0 toggle public Job getCurrentJob(Collection<String> path)
115    {
116  0 setError(null);
117   
118  0 if (this.authorization.hasAccess(Right.PROGRAM)) {
119  0 return this.jobExecutor.getCurrentJob(new JobGroupPath(path));
120    } else {
121  0 setError(new JobException("You need programming rights to get the current job."));
122  0 return null;
123    }
124    }
125   
126    /**
127    * Get the status of the currently executing job in the specified group job, if any.
128    *
129    * @param path specifies the job group where to look for a running job
130    * @return status of the currently executing job in the specified group, or {@code null} if no job is being executed
131    */
 
132  0 toggle public JobStatus getCurrentJobStatus(Collection<String> path)
133    {
134  0 Job job = this.jobExecutor.getCurrentJob(new JobGroupPath(path));
135   
136  0 JobStatus jobStatus = null;
137  0 if (job != null) {
138  0 jobStatus = job.getStatus();
139  0 if (!this.authorization.hasAccess(Right.PROGRAM)) {
140  0 jobStatus = safe(jobStatus);
141    }
142    }
143   
144  0 return jobStatus;
145    }
146   
147    /**
148    * @param <T> the type of the object
149    * @param unsafe the unsafe object
150    * @return the safe version of the passed object
151    */
 
152  0 toggle @SuppressWarnings("unchecked")
153    protected <T> T safe(T unsafe)
154    {
155  0 return (T) this.scriptSafeProvider.get(unsafe);
156    }
157   
158    /**
159    * Get the error generated while performing the previously called action.
160    *
161    * @return an eventual exception or {@code null} if no exception was thrown
162    */
 
163  0 toggle public Exception getLastError()
164    {
165  0 return (Exception) this.execution.getContext().getProperty(JOB_ERROR_KEY);
166    }
167   
168    /**
169    * Store a caught exception in the context, so that it can be later retrieved using {@link #getLastError()}.
170    *
171    * @param e the exception to store, can be {@code null} to clear the previously stored exception
172    * @see #getLastError()
173    */
 
174  0 toggle private void setError(Exception e)
175    {
176  0 this.execution.getContext().setProperty(JOB_ERROR_KEY, e);
177    }
178    }