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

File ReplayJob.java

 

Coverage histogram

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

Code metrics

12
37
7
1
148
96
14
0.38
5.29
7
2

Classes

Class Line # Actions
ReplayJob 47 37 0% 14 4
0.928571492.9%
 

Contributing tests

This file is covered by 2 tests. .

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.extension.job.history.internal;
21   
22    import java.util.Collection;
23    import java.util.List;
24   
25    import javax.inject.Named;
26   
27    import org.xwiki.component.annotation.Component;
28    import org.xwiki.component.manager.ComponentLookupException;
29    import org.xwiki.extension.job.history.ExtensionJobHistoryRecord;
30    import org.xwiki.extension.job.history.ReplayJobStatus;
31    import org.xwiki.extension.job.history.ReplayRequest;
32    import org.xwiki.extension.job.internal.AbstractExtensionJob;
33    import org.xwiki.job.AbstractJob;
34    import org.xwiki.job.GroupedJob;
35    import org.xwiki.job.Job;
36    import org.xwiki.job.JobGroupPath;
37    import org.xwiki.job.Request;
38   
39    /**
40    * A job that can replay records from the extension job history.
41    *
42    * @version $Id: 343163a2a6e35bdba752f957c5688f30312fe162 $
43    * @since 7.1RC1
44    */
45    @Component
46    @Named(ReplayJob.JOB_TYPE)
 
47    public class ReplayJob extends AbstractJob<ReplayRequest, ReplayJobStatus> implements GroupedJob
48    {
49    /**
50    * The id of the job.
51    */
52    public static final String JOB_TYPE = "replay";
53   
54    /**
55    * Specifies the group this job is part of. If all the extension history records to be replayed by this job target
56    * the same wiki then this job is part of the group of extension jobs that run on that wiki (it will only block the
57    * extension jobs that run on that wiki). Otherwise, if there is at least one record that targets multiple wikis or
58    * the root namespace then this job is part of the group of extension jobs that run on global (root) namespace.
59    */
60    private JobGroupPath groupPath;
61   
 
62  5 toggle @Override
63    public String getType()
64    {
65  5 return JOB_TYPE;
66    }
67   
 
68  3 toggle @Override
69    public JobGroupPath getGroupPath()
70    {
71  3 return this.groupPath;
72    }
73   
 
74  4 toggle @Override
75    public void initialize(Request request)
76    {
77  4 super.initialize(request);
78   
79    // Build the job group path.
80  4 String targetNamespace = getTargetNamespace();
81  4 if (targetNamespace != null) {
82  1 this.groupPath = new JobGroupPath(targetNamespace, AbstractExtensionJob.ROOT_GROUP);
83    } else {
84  3 this.groupPath = AbstractExtensionJob.ROOT_GROUP;
85    }
86    }
87   
 
88  4 toggle @Override
89    protected ReplayJobStatus createNewStatus(ReplayRequest request)
90    {
91  4 return new ReplayJobStatus(request, this.observationManager, this.loggerManager);
92    }
93   
 
94  1 toggle @Override
95    protected void runInternal() throws Exception
96    {
97  1 List<ExtensionJobHistoryRecord> records = this.request.getRecords();
98  1 if (records == null) {
99  0 return;
100    }
101   
102  1 this.progressManager.pushLevelProgress(records.size(), this);
103   
104  1 try {
105  1 int currentRecordNumber = 0;
106  1 for (ExtensionJobHistoryRecord record : records) {
107  2 this.progressManager.startStep(this);
108  2 this.status.setCurrentRecordNumber(currentRecordNumber++);
109  2 replay(record);
110  2 this.progressManager.endStep(this);
111    }
112    } finally {
113  1 this.progressManager.popLevelProgress(this);
114    }
115    }
116   
 
117  2 toggle private void replay(ExtensionJobHistoryRecord record) throws ComponentLookupException
118    {
119  2 Job job = this.componentManager.getInstance(Job.class, record.getJobType());
120  2 job.initialize(record.getRequest());
121  2 job.run();
122    }
123   
 
124  4 toggle private String getTargetNamespace()
125    {
126  4 List<ExtensionJobHistoryRecord> records = this.request.getRecords();
127  4 if (records == null) {
128  0 return null;
129    }
130   
131  4 String targetNamespace = null;
132  4 for (ExtensionJobHistoryRecord record : records) {
133  7 Collection<String> namespaces = record.getRequest().getNamespaces();
134  7 if (namespaces != null && namespaces.size() == 1) {
135  5 String namespace = namespaces.iterator().next();
136  5 if (targetNamespace == null) {
137  3 targetNamespace = namespace;
138  2 } else if (!targetNamespace.equals(namespace)) {
139  1 return null;
140    }
141    } else {
142  2 return null;
143    }
144    }
145   
146  1 return targetNamespace;
147    }
148    }