1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.extension.xar.internal.job; |
21 |
|
|
22 |
|
import java.io.File; |
23 |
|
import java.io.IOException; |
24 |
|
import java.util.Collection; |
25 |
|
import java.util.HashSet; |
26 |
|
import java.util.Set; |
27 |
|
|
28 |
|
import javax.inject.Inject; |
29 |
|
import javax.inject.Named; |
30 |
|
import javax.inject.Provider; |
31 |
|
|
32 |
|
import org.xwiki.component.annotation.Component; |
33 |
|
import org.xwiki.extension.ExtensionDependency; |
34 |
|
import org.xwiki.extension.ExtensionId; |
35 |
|
import org.xwiki.extension.InstalledExtension; |
36 |
|
import org.xwiki.extension.job.InstallRequest; |
37 |
|
import org.xwiki.extension.job.internal.AbstractExtensionJob; |
38 |
|
import org.xwiki.extension.xar.internal.handler.UnsupportedNamespaceException; |
39 |
|
import org.xwiki.extension.xar.internal.handler.XarExtensionHandler; |
40 |
|
import org.xwiki.extension.xar.internal.handler.XarHandlerUtils; |
41 |
|
import org.xwiki.extension.xar.internal.handler.packager.Packager; |
42 |
|
import org.xwiki.extension.xar.internal.job.diff.DocumentUnifiedDiffBuilder; |
43 |
|
import org.xwiki.extension.xar.job.diff.DiffXarJobStatus; |
44 |
|
import org.xwiki.extension.xar.job.diff.DocumentUnifiedDiff; |
45 |
|
import org.xwiki.extension.xar.job.diff.DocumentVersionReference; |
46 |
|
import org.xwiki.model.reference.WikiReference; |
47 |
|
import org.xwiki.xar.XarEntry; |
48 |
|
import org.xwiki.xar.XarException; |
49 |
|
import org.xwiki.xar.XarFile; |
50 |
|
|
51 |
|
import com.xpn.xwiki.XWikiContext; |
52 |
|
import com.xpn.xwiki.XWikiException; |
53 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
@version |
59 |
|
@since |
60 |
|
|
61 |
|
@Component |
62 |
|
@Named(DiffXarJob.JOB_TYPE) |
|
|
| 0% |
Uncovered Elements: 110 (110) |
Complexity: 33 |
Complexity Density: 0.45 |
|
63 |
|
public class DiffXarJob extends AbstractExtensionJob<InstallRequest, DiffXarJobStatus> |
64 |
|
{ |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
public static final String JOB_TYPE = "diffXar"; |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
@Inject |
74 |
|
private Packager packager; |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
@Inject |
80 |
|
private Provider<XWikiContext> xcontextProvider; |
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
@Inject |
86 |
|
private DocumentUnifiedDiffBuilder documentDiffBuilder; |
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
private Set<String> comparedFeatures = new HashSet<>(); |
93 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
94 |
0 |
@Override... |
95 |
|
public String getType() |
96 |
|
{ |
97 |
0 |
return JOB_TYPE; |
98 |
|
} |
99 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
100 |
0 |
@Override... |
101 |
|
protected DiffXarJobStatus createNewStatus(InstallRequest request) |
102 |
|
{ |
103 |
0 |
return new DiffXarJobStatus(request, this.observationManager, this.loggerManager); |
104 |
|
} |
105 |
|
|
|
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 5 |
Complexity Density: 0.38 |
|
106 |
0 |
@Override... |
107 |
|
protected void runInternal() throws Exception |
108 |
|
{ |
109 |
0 |
InstallRequest request = getRequest(); |
110 |
|
|
111 |
0 |
if (!request.hasNamespaces() || request.getNamespaces().size() != 1) { |
112 |
0 |
return; |
113 |
|
} |
114 |
|
|
115 |
0 |
String namespace = request.getNamespaces().iterator().next(); |
116 |
|
|
117 |
0 |
Collection<ExtensionId> extensionIds = request.getExtensions(); |
118 |
0 |
this.progressManager.pushLevelProgress(extensionIds.size(), this); |
119 |
0 |
try { |
120 |
0 |
for (ExtensionId extensionId : extensionIds) { |
121 |
0 |
this.progressManager.startStep(this); |
122 |
|
|
123 |
0 |
InstalledExtension installedExtension = getInstalledExtension(extensionId, namespace); |
124 |
|
|
125 |
0 |
if (installedExtension != null && installedExtension.isInstalled(namespace)) { |
126 |
0 |
diff(extensionId.getId(), namespace); |
127 |
|
} |
128 |
|
} |
129 |
|
} finally { |
130 |
0 |
this.progressManager.popLevelProgress(this); |
131 |
|
} |
132 |
|
} |
133 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
134 |
0 |
private InstalledExtension getInstalledExtension(ExtensionId extensionId, String namespace)... |
135 |
|
{ |
136 |
0 |
if (extensionId.getVersion() != null) { |
137 |
0 |
return this.installedExtensionRepository.getInstalledExtension(extensionId); |
138 |
|
} else { |
139 |
0 |
return this.installedExtensionRepository.getInstalledExtension(extensionId.getId(), namespace); |
140 |
|
} |
141 |
|
} |
142 |
|
|
|
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 3 |
Complexity Density: 0.23 |
|
143 |
0 |
private void diff(String feature, String namespace)... |
144 |
|
{ |
145 |
0 |
if (this.comparedFeatures.contains(feature)) { |
146 |
|
|
147 |
0 |
return; |
148 |
|
} |
149 |
0 |
this.comparedFeatures.add(feature); |
150 |
|
|
151 |
0 |
InstalledExtension installedExtension = |
152 |
|
this.installedExtensionRepository.getInstalledExtension(feature, namespace); |
153 |
0 |
if (installedExtension != null) { |
154 |
0 |
diff(installedExtension, namespace); |
155 |
|
|
156 |
0 |
Collection<? extends ExtensionDependency> dependencies = installedExtension.getDependencies(); |
157 |
|
|
158 |
0 |
this.progressManager.pushLevelProgress(dependencies.size(), this); |
159 |
0 |
try { |
160 |
0 |
for (ExtensionDependency dependency : dependencies) { |
161 |
0 |
this.progressManager.startStep(this); |
162 |
|
|
163 |
0 |
diff(dependency.getId(), namespace); |
164 |
|
} |
165 |
|
} finally { |
166 |
0 |
this.progressManager.popLevelProgress(this); |
167 |
|
} |
168 |
|
} |
169 |
|
} |
170 |
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 8 |
Complexity Density: 0.8 |
|
171 |
0 |
private void diff(InstalledExtension installedExtension, String namespace)... |
172 |
|
{ |
173 |
0 |
Collection<ExtensionId> excludedExtensions = getRequest().getExcludedExtensions(); |
174 |
0 |
if (XarExtensionHandler.TYPE.equals(installedExtension.getType()) |
175 |
|
&& (excludedExtensions == null || !excludedExtensions.contains(installedExtension.getId()))) { |
176 |
0 |
if (getRequest().isVerbose()) { |
177 |
0 |
this.logger.info("Computing differences for [{}] on namespace [{}]", installedExtension.getId(), |
178 |
|
namespace); |
179 |
|
} |
180 |
0 |
try { |
181 |
0 |
WikiReference wikiReference = new WikiReference(XarHandlerUtils.getWikiFromNamespace(namespace)); |
182 |
0 |
diff(new XarFile(new File(installedExtension.getFile().getAbsolutePath())), wikiReference, |
183 |
|
installedExtension.getId()); |
184 |
|
} catch (UnsupportedNamespaceException e) { |
185 |
0 |
this.logger.error("Failed to extract the wiki id from the namespace [{}].", namespace, e); |
186 |
|
} catch (IOException e) { |
187 |
0 |
this.logger.error("Failed to read the XAR file of the extension [{}].", installedExtension.getId(), e); |
188 |
|
} catch (XarException e) { |
189 |
0 |
this.logger.error("Failed to parse the XAR file of the extension [{}].", installedExtension.getId(), e); |
190 |
|
} |
191 |
|
} |
192 |
|
} |
193 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 3 |
Complexity Density: 0.27 |
|
194 |
0 |
private void diff(XarFile xarFile, WikiReference wikiReference, ExtensionId extensionId)... |
195 |
|
{ |
196 |
0 |
Collection<XarEntry> xarEntries = xarFile.getEntries(); |
197 |
0 |
this.progressManager.pushLevelProgress(xarEntries.size(), this); |
198 |
0 |
try { |
199 |
0 |
for (XarEntry xarEntry : xarEntries) { |
200 |
0 |
this.progressManager.startStep(this); |
201 |
|
|
202 |
0 |
try { |
203 |
0 |
diff(this.packager.getXWikiDocument(xarFile.getInputStream(xarEntry), wikiReference), extensionId); |
204 |
|
} catch (Exception e) { |
205 |
|
|
206 |
0 |
this.logger.error("Failed to parse document [{}] from XAR.", xarEntry.getDocumentName(), e); |
207 |
|
} |
208 |
|
} |
209 |
|
} finally { |
210 |
0 |
try { |
211 |
0 |
xarFile.close(); |
212 |
|
} catch (IOException e) { |
213 |
|
|
214 |
|
} |
215 |
0 |
this.progressManager.popLevelProgress(this); |
216 |
|
} |
217 |
|
} |
218 |
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 4 |
Complexity Density: 0.4 |
|
219 |
0 |
private void diff(XWikiDocument document, ExtensionId extensionId)... |
220 |
|
{ |
221 |
0 |
if (getRequest().isVerbose()) { |
222 |
0 |
this.logger.info("Computing differences for document [{}]", document.getDocumentReferenceWithLocale()); |
223 |
|
} |
224 |
|
|
225 |
0 |
XWikiDocument previousDocument = |
226 |
|
document.duplicate(new DocumentVersionReference(document.getDocumentReference(), extensionId)); |
227 |
0 |
XWikiContext xcontext = this.xcontextProvider.get(); |
228 |
0 |
try { |
229 |
0 |
XWikiDocument nextDocument = |
230 |
|
xcontext.getWiki().getDocument(document.getDocumentReferenceWithLocale(), xcontext); |
231 |
0 |
if (nextDocument.isNew()) { |
232 |
0 |
nextDocument = null; |
233 |
|
} |
234 |
0 |
maybeAddDocumentDiff(this.documentDiffBuilder.diff(previousDocument, nextDocument)); |
235 |
|
} catch (XWikiException e) { |
236 |
0 |
this.logger.error("Failed to get document [{}] from the database.", document.getDocumentReference(), e); |
237 |
|
} |
238 |
|
} |
239 |
|
|
|
|
| 0% |
Uncovered Elements: 21 (21) |
Complexity: 6 |
Complexity Density: 0.55 |
|
240 |
0 |
private void maybeAddDocumentDiff(DocumentUnifiedDiff documentDiff)... |
241 |
|
{ |
242 |
0 |
int differencesCount = |
243 |
|
documentDiff.size() + documentDiff.getAttachmentDiffs().size() + documentDiff.getObjectDiffs().size() |
244 |
|
+ documentDiff.getClassPropertyDiffs().size(); |
245 |
0 |
if (getRequest().isVerbose()) { |
246 |
0 |
if (documentDiff.getNextReference() == null) { |
247 |
0 |
this.logger.info("The document [{}] has been deleted", documentDiff.getPreviousReference()); |
248 |
0 |
} else if (documentDiff.getPreviousReference() == null) { |
249 |
0 |
this.logger.info("The document [{}] has been added", documentDiff.getNextReference()); |
250 |
0 |
} else if (differencesCount > 0) { |
251 |
0 |
this.logger.info("The document [{}] has [{}] changes", documentDiff.getPreviousReference(), |
252 |
|
differencesCount); |
253 |
|
} else { |
254 |
0 |
this.logger.info("The document [{}] has no changes", documentDiff.getPreviousReference()); |
255 |
|
} |
256 |
|
} |
257 |
0 |
if (differencesCount > 0) { |
258 |
0 |
getStatus().getDocumentDiffs().add(documentDiff); |
259 |
|
} |
260 |
|
} |
261 |
|
} |