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.handler; |
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
import java.util.Arrays; |
24 |
|
import java.util.Collections; |
25 |
|
import java.util.HashSet; |
26 |
|
import java.util.List; |
27 |
|
import java.util.Map; |
28 |
|
import java.util.Set; |
29 |
|
|
30 |
|
import javax.inject.Inject; |
31 |
|
import javax.inject.Named; |
32 |
|
import javax.inject.Provider; |
33 |
|
import javax.inject.Singleton; |
34 |
|
|
35 |
|
import org.slf4j.Logger; |
36 |
|
import org.xwiki.component.annotation.Component; |
37 |
|
import org.xwiki.context.Execution; |
38 |
|
import org.xwiki.context.ExecutionContext; |
39 |
|
import org.xwiki.extension.LocalExtension; |
40 |
|
import org.xwiki.extension.job.internal.InstallJob; |
41 |
|
import org.xwiki.extension.job.internal.UninstallJob; |
42 |
|
import org.xwiki.extension.repository.InstalledExtensionRepository; |
43 |
|
import org.xwiki.extension.xar.internal.handler.packager.PackageConfiguration; |
44 |
|
import org.xwiki.extension.xar.internal.handler.packager.Packager; |
45 |
|
import org.xwiki.extension.xar.internal.repository.XarInstalledExtensionRepository; |
46 |
|
import org.xwiki.extension.xar.question.CleanPagesQuestion; |
47 |
|
import org.xwiki.job.Job; |
48 |
|
import org.xwiki.job.Request; |
49 |
|
import org.xwiki.job.event.JobFinishingEvent; |
50 |
|
import org.xwiki.model.reference.DocumentReference; |
51 |
|
import org.xwiki.observation.EventListener; |
52 |
|
import org.xwiki.observation.event.Event; |
53 |
|
import org.xwiki.xar.XarEntry; |
54 |
|
|
55 |
|
import com.xpn.xwiki.XWikiContext; |
56 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
@version |
62 |
|
@since |
63 |
|
|
64 |
|
@Component |
65 |
|
@Singleton |
66 |
|
@Named("XarExtensionJobFinishedListener") |
|
|
| 79.4% |
Uncovered Elements: 21 (102) |
Complexity: 23 |
Complexity Density: 0.31 |
|
67 |
|
public class XarExtensionJobFinishedListener implements EventListener |
68 |
|
{ |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
private static final List<Event> EVENTS = |
73 |
|
Arrays.<Event>asList(new JobFinishingEvent(InstallJob.JOBTYPE), new JobFinishingEvent(UninstallJob.JOBTYPE)); |
74 |
|
|
75 |
|
@Inject |
76 |
|
private Execution execution; |
77 |
|
|
78 |
|
@Inject |
79 |
|
private Provider<Packager> packagerProvider; |
80 |
|
|
81 |
|
@Inject |
82 |
|
private Provider<XWikiContext> xcontextProvider; |
83 |
|
|
84 |
|
@Inject |
85 |
|
private Logger logger; |
86 |
|
|
87 |
|
@Inject |
88 |
|
@Named(XarExtensionHandler.TYPE) |
89 |
|
private InstalledExtensionRepository xarRepository; |
90 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
91 |
104 |
@Override... |
92 |
|
public String getName() |
93 |
|
{ |
94 |
104 |
return "XarExtensionJobFinishedListener"; |
95 |
|
} |
96 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
97 |
26 |
@Override... |
98 |
|
public List<Event> getEvents() |
99 |
|
{ |
100 |
26 |
return EVENTS; |
101 |
|
} |
102 |
|
|
|
|
| 75.9% |
Uncovered Elements: 21 (87) |
Complexity: 19 |
Complexity Density: 0.29 |
|
103 |
60 |
@Override... |
104 |
|
public void onEvent(Event event, Object source, Object data) |
105 |
|
{ |
106 |
60 |
JobFinishingEvent jobFinishingEvent = (JobFinishingEvent) event; |
107 |
|
|
108 |
60 |
if (!jobFinishingEvent.getRequest().isRemote()) { |
109 |
60 |
ExecutionContext context = this.execution.getContext(); |
110 |
|
|
111 |
60 |
if (context != null) { |
112 |
60 |
XarExtensionPlan xarExtensionPlan = |
113 |
|
(XarExtensionPlan) context.getProperty(XarExtensionPlan.CONTEXTKEY_XARINSTALLPLAN); |
114 |
|
|
115 |
60 |
if (xarExtensionPlan != null) { |
116 |
53 |
try { |
117 |
53 |
Map<String, Map<XarEntry, XarExtensionPlanEntry>> previousXAREntries = |
118 |
|
xarExtensionPlan.previousXAREntries; |
119 |
53 |
Map<String, Map<XarEntry, LocalExtension>> nextXAREntries = xarExtensionPlan.nextXAREntries; |
120 |
|
|
121 |
53 |
Map<XarEntry, LocalExtension> rootNextPages = nextXAREntries.get(null); |
122 |
53 |
if (rootNextPages == null) { |
123 |
42 |
rootNextPages = Collections.emptyMap(); |
124 |
|
} |
125 |
|
|
126 |
53 |
XWikiContext xcontext = this.xcontextProvider.get(); |
127 |
|
|
128 |
53 |
Packager packager = this.packagerProvider.get(); |
129 |
|
|
130 |
|
|
131 |
|
|
132 |
53 |
Set<DocumentReference> pagesToDelete = new HashSet<DocumentReference>(); |
133 |
|
|
134 |
53 |
for (Map.Entry<String, Map<XarEntry, XarExtensionPlanEntry>> previousWikiEntry : previousXAREntries |
135 |
|
.entrySet()) { |
136 |
22 |
if (!previousWikiEntry.getValue().isEmpty()) { |
137 |
22 |
try { |
138 |
22 |
List<DocumentReference> references = |
139 |
|
packager.getDocumentReferences(previousWikiEntry.getValue().keySet(), |
140 |
|
createPackageConfiguration(jobFinishingEvent.getRequest(), |
141 |
|
previousWikiEntry.getKey())); |
142 |
|
|
143 |
22 |
for (DocumentReference reference : references) { |
144 |
|
|
145 |
|
|
146 |
146 |
if (((XarInstalledExtensionRepository) this.xarRepository) |
147 |
|
.getXarInstalledExtensions(reference).isEmpty()) { |
148 |
92 |
pagesToDelete.add(reference); |
149 |
|
} |
150 |
|
} |
151 |
|
} catch (Exception e) { |
152 |
0 |
this.logger.warn( |
153 |
|
"Exception when cleaning pages removed since previous xar extension version", |
154 |
|
e); |
155 |
|
} |
156 |
|
} |
157 |
|
} |
158 |
|
|
159 |
|
|
160 |
|
|
161 |
53 |
CleanPagesQuestion question = new CleanPagesQuestion(pagesToDelete); |
162 |
|
|
163 |
53 |
Map<DocumentReference, Boolean> pages = question.getPages(); |
164 |
|
|
165 |
|
|
166 |
53 |
for (DocumentReference previousReference : pagesToDelete) { |
167 |
92 |
if (xarExtensionPlan.containsNewPage(previousReference)) { |
168 |
0 |
pages.remove(previousReference); |
169 |
|
} |
170 |
|
} |
171 |
|
|
172 |
|
|
173 |
|
|
174 |
53 |
for (Map.Entry<DocumentReference, Boolean> entry : pages.entrySet()) { |
175 |
92 |
DocumentReference reference = entry.getKey(); |
176 |
|
|
177 |
|
|
178 |
92 |
XWikiDocument currentDocument; |
179 |
92 |
try { |
180 |
92 |
currentDocument = xcontext.getWiki().getDocument(reference, xcontext); |
181 |
|
} catch (Exception e) { |
182 |
0 |
this.logger.error("Failed to get document [{}]", reference, e); |
183 |
|
|
184 |
0 |
pages.put(reference, false); |
185 |
0 |
continue; |
186 |
|
} |
187 |
92 |
if (currentDocument.isNew()) { |
188 |
|
|
189 |
0 |
pages.put(reference, false); |
190 |
0 |
continue; |
191 |
|
} |
192 |
|
|
193 |
|
|
194 |
92 |
XWikiDocument previousDocument; |
195 |
92 |
try { |
196 |
92 |
previousDocument = xarExtensionPlan.getPreviousXWikiDocument(reference, packager); |
197 |
|
} catch (Exception e) { |
198 |
0 |
this.logger.error("Failed to get previous version of document [{}]", reference, e); |
199 |
|
|
200 |
0 |
pages.put(reference, false); |
201 |
0 |
continue; |
202 |
|
} |
203 |
|
|
204 |
|
|
205 |
92 |
try { |
206 |
92 |
currentDocument.loadAttachmentsContent(xcontext); |
207 |
92 |
if (!currentDocument.equalsData(previousDocument)) { |
208 |
|
|
209 |
22 |
pages.put(reference, false); |
210 |
|
} |
211 |
|
} catch (Exception e) { |
212 |
0 |
this.logger.error("Failed to load attachments", e); |
213 |
|
|
214 |
0 |
pages.put(reference, false); |
215 |
0 |
continue; |
216 |
|
} |
217 |
|
} |
218 |
|
|
219 |
|
|
220 |
53 |
if (!pages.isEmpty() && jobFinishingEvent.getRequest().isInteractive()) { |
221 |
3 |
try { |
222 |
3 |
((Job) source).getStatus().ask(question); |
223 |
|
} catch (InterruptedException e) { |
224 |
0 |
this.logger.warn("The thread has been interrupted", e); |
225 |
|
|
226 |
|
|
227 |
0 |
return; |
228 |
|
} |
229 |
|
} |
230 |
|
|
231 |
|
|
232 |
|
|
233 |
53 |
PackageConfiguration configuration = createPackageConfiguration(jobFinishingEvent.getRequest()); |
234 |
|
|
235 |
53 |
for (Map.Entry<DocumentReference, Boolean> entry : pages.entrySet()) { |
236 |
92 |
if (entry.getValue()) { |
237 |
70 |
packager.deleteDocument(entry.getKey(), configuration); |
238 |
|
} |
239 |
|
} |
240 |
|
} finally { |
241 |
|
|
242 |
53 |
try { |
243 |
53 |
xarExtensionPlan.close(); |
244 |
|
} catch (IOException e) { |
245 |
0 |
this.logger.error("Failed to close XAR extension plan", e); |
246 |
|
} |
247 |
53 |
context.setProperty(XarExtensionPlan.CONTEXTKEY_XARINSTALLPLAN, null); |
248 |
|
} |
249 |
|
} |
250 |
|
} |
251 |
|
} |
252 |
|
} |
253 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
254 |
53 |
private PackageConfiguration createPackageConfiguration(Request request)... |
255 |
|
{ |
256 |
53 |
return createPackageConfiguration(request, null); |
257 |
|
} |
258 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
259 |
75 |
private PackageConfiguration createPackageConfiguration(Request request, String wiki)... |
260 |
|
{ |
261 |
75 |
PackageConfiguration configuration = new PackageConfiguration(); |
262 |
|
|
263 |
75 |
configuration.setInteractive(false); |
264 |
75 |
configuration |
265 |
|
.setUser(XarExtensionHandler.getRequestUserReference(XarExtensionHandler.PROPERTY_USERREFERENCE, request)); |
266 |
75 |
configuration.setWiki(wiki); |
267 |
75 |
configuration.setVerbose(request.isVerbose()); |
268 |
75 |
configuration.setSkipMandatorytDocuments(true); |
269 |
|
|
270 |
75 |
return configuration; |
271 |
|
} |
272 |
|
} |