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.File; |
23 |
|
import java.io.IOException; |
24 |
|
import java.util.Collection; |
25 |
|
import java.util.HashSet; |
26 |
|
import java.util.Map; |
27 |
|
import java.util.Set; |
28 |
|
|
29 |
|
import javax.inject.Inject; |
30 |
|
import javax.inject.Named; |
31 |
|
import javax.inject.Singleton; |
32 |
|
|
33 |
|
import org.xwiki.component.annotation.Component; |
34 |
|
import org.xwiki.component.manager.ComponentLookupException; |
35 |
|
import org.xwiki.component.manager.ComponentManager; |
36 |
|
import org.xwiki.context.Execution; |
37 |
|
import org.xwiki.context.ExecutionContext; |
38 |
|
import org.xwiki.extension.ExtensionException; |
39 |
|
import org.xwiki.extension.InstallException; |
40 |
|
import org.xwiki.extension.InstalledExtension; |
41 |
|
import org.xwiki.extension.LocalExtension; |
42 |
|
import org.xwiki.extension.UninstallException; |
43 |
|
import org.xwiki.extension.handler.internal.AbstractExtensionHandler; |
44 |
|
import org.xwiki.extension.job.internal.AbstractExtensionJob; |
45 |
|
import org.xwiki.extension.job.plan.ExtensionPlan; |
46 |
|
import org.xwiki.extension.repository.InstalledExtensionRepository; |
47 |
|
import org.xwiki.extension.repository.LocalExtensionRepository; |
48 |
|
import org.xwiki.extension.xar.internal.handler.packager.PackageConfiguration; |
49 |
|
import org.xwiki.extension.xar.internal.handler.packager.Packager; |
50 |
|
import org.xwiki.extension.xar.internal.repository.XarInstalledExtension; |
51 |
|
import org.xwiki.job.Job; |
52 |
|
import org.xwiki.job.JobContext; |
53 |
|
import org.xwiki.job.Request; |
54 |
|
import org.xwiki.logging.marker.TranslationMarker; |
55 |
|
import org.xwiki.model.reference.DocumentReference; |
56 |
|
import org.xwiki.xar.XarEntry; |
57 |
|
import org.xwiki.xar.XarException; |
58 |
|
|
59 |
|
|
60 |
|
@version |
61 |
|
@since |
62 |
|
|
63 |
|
@Component |
64 |
|
@Singleton |
65 |
|
@Named(XarExtensionHandler.TYPE) |
|
|
| 79.2% |
Uncovered Elements: 26 (125) |
Complexity: 35 |
Complexity Density: 0.42 |
|
66 |
|
public class XarExtensionHandler extends AbstractExtensionHandler |
67 |
|
{ |
68 |
|
public static final String TYPE = "xar"; |
69 |
|
|
70 |
|
protected static final String PROPERTY_USERREFERENCE = "user.reference"; |
71 |
|
|
72 |
|
protected static final String PROPERTY_CALLERREFERENCE = "caller.reference"; |
73 |
|
|
74 |
|
protected static final String PROPERTY_CHECKRIGHTS = "checkrights"; |
75 |
|
|
76 |
|
private static final TranslationMarker LOG_EXTENSIONPLAN_BEGIN = |
77 |
|
new TranslationMarker("extension.xar.log.extensionplan.begin"); |
78 |
|
|
79 |
|
private static final TranslationMarker LOG_EXTENSIONPLAN_END = |
80 |
|
new TranslationMarker("extension.xar.log.extensionplan.end"); |
81 |
|
|
82 |
|
@Inject |
83 |
|
private Packager packager; |
84 |
|
|
85 |
|
@Inject |
86 |
|
@Named(XarExtensionHandler.TYPE) |
87 |
|
private InstalledExtensionRepository xarRepository; |
88 |
|
|
89 |
|
@Inject |
90 |
|
private ComponentManager componentManager; |
91 |
|
|
92 |
|
@Inject |
93 |
|
private LocalExtensionRepository localRepository; |
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
@Inject |
99 |
|
private Execution execution; |
100 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
101 |
135 |
protected static DocumentReference getRequestUserReference(String property, Request request)... |
102 |
|
{ |
103 |
135 |
Object obj = request.getProperty(property); |
104 |
|
|
105 |
135 |
if (obj instanceof DocumentReference) { |
106 |
132 |
return (DocumentReference) obj; |
107 |
|
} |
108 |
|
|
109 |
3 |
return null; |
110 |
|
} |
111 |
|
|
|
|
| 85% |
Uncovered Elements: 3 (20) |
Complexity: 6 |
Complexity Density: 0.6 |
|
112 |
78 |
private void initializePagesIndex(Request request) throws ExtensionException, XarException, IOException... |
113 |
|
{ |
114 |
78 |
ExecutionContext context = this.execution.getContext(); |
115 |
|
|
116 |
78 |
if (context != null) { |
117 |
78 |
ExtensionPlan plan = (ExtensionPlan) context.getProperty(AbstractExtensionJob.CONTEXTKEY_PLAN); |
118 |
|
|
119 |
78 |
if (plan != null) { |
120 |
75 |
if (context.getProperty(XarExtensionPlan.CONTEXTKEY_XARINSTALLPLAN) == null) { |
121 |
53 |
if (request.isVerbose()) { |
122 |
53 |
this.logger.info(LOG_EXTENSIONPLAN_BEGIN, "Preparing XAR extension plan"); |
123 |
|
} |
124 |
|
|
125 |
53 |
context.setProperty(XarExtensionPlan.CONTEXTKEY_XARINSTALLPLAN, |
126 |
|
new XarExtensionPlan(plan, this.xarRepository, this.localRepository)); |
127 |
|
|
128 |
53 |
if (request.isVerbose()) { |
129 |
53 |
this.logger.info(LOG_EXTENSIONPLAN_END, "XAR extension plan ready"); |
130 |
|
} |
131 |
|
} |
132 |
|
} |
133 |
|
} |
134 |
|
} |
135 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
136 |
60 |
private XarExtensionPlan getXARExtensionPlan()... |
137 |
|
{ |
138 |
60 |
ExecutionContext context = this.execution.getContext(); |
139 |
|
|
140 |
60 |
if (context != null) { |
141 |
60 |
return (XarExtensionPlan) context.getProperty(XarExtensionPlan.CONTEXTKEY_XARINSTALLPLAN); |
142 |
|
} |
143 |
|
|
144 |
0 |
return null; |
145 |
|
} |
146 |
|
|
|
|
| 87.5% |
Uncovered Elements: 1 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
147 |
55 |
@Override... |
148 |
|
public void install(LocalExtension localExtension, String namespace, Request request) throws InstallException |
149 |
|
{ |
150 |
|
|
151 |
|
|
152 |
55 |
if (!request.isRemote()) { |
153 |
55 |
String wiki; |
154 |
55 |
try { |
155 |
55 |
wiki = XarHandlerUtils.getWikiFromNamespace(namespace); |
156 |
|
} catch (UnsupportedNamespaceException e) { |
157 |
1 |
throw new InstallException("Failed to extract wiki id from namespace", e); |
158 |
|
} |
159 |
|
|
160 |
54 |
installInternal(localExtension, wiki, request); |
161 |
|
} |
162 |
|
} |
163 |
|
|
|
|
| 75% |
Uncovered Elements: 2 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
164 |
6 |
@Override... |
165 |
|
public void upgrade(Collection<InstalledExtension> previousLocalExtensions, LocalExtension newLocalExtension, |
166 |
|
String namespace, Request request) throws InstallException |
167 |
|
{ |
168 |
|
|
169 |
|
|
170 |
6 |
if (!request.isRemote()) { |
171 |
6 |
String wiki; |
172 |
6 |
try { |
173 |
6 |
wiki = XarHandlerUtils.getWikiFromNamespace(namespace); |
174 |
|
} catch (UnsupportedNamespaceException e) { |
175 |
0 |
throw new InstallException("Failed to extract wiki id from namespace", e); |
176 |
|
} |
177 |
|
|
178 |
|
|
179 |
6 |
installInternal(newLocalExtension, wiki, request); |
180 |
|
} |
181 |
|
} |
182 |
|
|
|
|
| 71.4% |
Uncovered Elements: 2 (7) |
Complexity: 3 |
Complexity Density: 0.43 |
|
183 |
60 |
private void installInternal(LocalExtension newLocalExtension, String wiki, Request request) throws InstallException... |
184 |
|
{ |
185 |
60 |
try { |
186 |
60 |
initializePagesIndex(request); |
187 |
|
} catch (Exception e) { |
188 |
0 |
throw new InstallException("Failed to initialize extension plan index", e); |
189 |
|
} |
190 |
|
|
191 |
|
|
192 |
60 |
PackageConfiguration configuration = |
193 |
|
createPackageConfiguration(newLocalExtension, request, wiki, getXARExtensionPlan()); |
194 |
60 |
try { |
195 |
60 |
this.packager.importXAR("Install extension [" + newLocalExtension + "]", |
196 |
|
new File(newLocalExtension.getFile().getAbsolutePath()), configuration); |
197 |
|
} catch (Exception e) { |
198 |
0 |
throw new InstallException("Failed to import xar for extension [" + newLocalExtension + "]", e); |
199 |
|
} |
200 |
|
} |
201 |
|
|
|
|
| 39.1% |
Uncovered Elements: 14 (23) |
Complexity: 7 |
Complexity Density: 0.37 |
|
202 |
18 |
@Override... |
203 |
|
public void uninstall(InstalledExtension installedExtension, String namespace, Request request) |
204 |
|
throws UninstallException |
205 |
|
{ |
206 |
18 |
try { |
207 |
18 |
initializePagesIndex(request); |
208 |
|
} catch (Exception e) { |
209 |
0 |
throw new UninstallException("Failed to initialize extension plan index", e); |
210 |
|
} |
211 |
|
|
212 |
|
|
213 |
|
|
214 |
18 |
if (!request.isRemote()) { |
215 |
18 |
Job currentJob; |
216 |
18 |
try { |
217 |
18 |
currentJob = this.componentManager.<JobContext>getInstance(JobContext.class).getCurrentJob(); |
218 |
|
} catch (ComponentLookupException e1) { |
219 |
0 |
currentJob = null; |
220 |
|
} |
221 |
|
|
222 |
18 |
if (currentJob == null) { |
223 |
0 |
String wiki; |
224 |
0 |
try { |
225 |
0 |
wiki = XarHandlerUtils.getWikiFromNamespace(namespace); |
226 |
|
} catch (UnsupportedNamespaceException e) { |
227 |
0 |
throw new UninstallException("Failed to extract wiki id from namespace", e); |
228 |
|
} |
229 |
|
|
230 |
|
|
231 |
|
|
232 |
|
|
233 |
|
|
234 |
|
|
235 |
|
|
236 |
0 |
PackageConfiguration configuration = |
237 |
|
createPackageConfiguration(null, request, wiki, getXARExtensionPlan()); |
238 |
0 |
try { |
239 |
0 |
XarInstalledExtension xarLocalExtension = |
240 |
|
(XarInstalledExtension) this.xarRepository.resolve(installedExtension.getId()); |
241 |
0 |
Collection<XarEntry> pages = xarLocalExtension.getXarPackage().getEntries(); |
242 |
0 |
this.packager.unimportPages(pages, configuration); |
243 |
|
} catch (Exception e) { |
244 |
|
|
245 |
0 |
throw new UninstallException( |
246 |
|
"Failed to get xar extension [" + installedExtension.getId() + "] from xar repository", e); |
247 |
|
} |
248 |
|
} else { |
249 |
|
|
250 |
|
} |
251 |
|
} |
252 |
|
} |
253 |
|
|
|
|
| 94.9% |
Uncovered Elements: 2 (39) |
Complexity: 9 |
Complexity Density: 0.33 |
|
254 |
60 |
private PackageConfiguration createPackageConfiguration(LocalExtension extension, Request request, String wiki,... |
255 |
|
XarExtensionPlan xarExtensionPlan) |
256 |
|
{ |
257 |
60 |
PackageConfiguration configuration = new PackageConfiguration(); |
258 |
|
|
259 |
60 |
configuration.setInteractive(request.isInteractive()); |
260 |
60 |
configuration.setUser(getRequestUserReference(PROPERTY_USERREFERENCE, request)); |
261 |
60 |
configuration.setWiki(wiki); |
262 |
60 |
configuration.setVerbose(request.isVerbose()); |
263 |
60 |
configuration.setSkipMandatorytDocuments(true); |
264 |
60 |
configuration.setXarExtensionPlan(xarExtensionPlan); |
265 |
|
|
266 |
60 |
try { |
267 |
60 |
Job currentJob = this.componentManager.<JobContext>getInstance(JobContext.class).getCurrentJob(); |
268 |
60 |
if (currentJob != null) { |
269 |
57 |
configuration.setJobStatus(currentJob.getStatus()); |
270 |
|
} |
271 |
|
} catch (Exception e) { |
272 |
0 |
this.logger.error("Failed to lookup JobContext, it will be impossible to do interactive install"); |
273 |
|
} |
274 |
|
|
275 |
|
|
276 |
60 |
if (extension != null && xarExtensionPlan != null) { |
277 |
57 |
Map<String, Map<XarEntry, LocalExtension>> nextXAREntries = xarExtensionPlan.nextXAREntries; |
278 |
|
|
279 |
57 |
Set<String> entriesToImport = new HashSet<String>(); |
280 |
|
|
281 |
57 |
Map<XarEntry, LocalExtension> nextXAREntriesOnRoot = nextXAREntries.get(null); |
282 |
57 |
if (nextXAREntriesOnRoot != null) { |
283 |
16 |
for (Map.Entry<XarEntry, LocalExtension> entry : nextXAREntriesOnRoot.entrySet()) { |
284 |
156 |
if (entry.getValue() == extension) { |
285 |
86 |
entriesToImport.add(entry.getKey().getEntryName()); |
286 |
|
} |
287 |
|
} |
288 |
|
} |
289 |
57 |
Map<XarEntry, LocalExtension> nextXAREntriesOnWiki = nextXAREntries.get(wiki); |
290 |
57 |
if (nextXAREntriesOnWiki != null) { |
291 |
57 |
for (Map.Entry<XarEntry, LocalExtension> entry : nextXAREntriesOnWiki.entrySet()) { |
292 |
495 |
if (entry.getValue() == extension) { |
293 |
268 |
entriesToImport.add(entry.getKey().getEntryName()); |
294 |
|
} |
295 |
|
} |
296 |
|
} |
297 |
|
|
298 |
57 |
configuration.setEntriesToImport(entriesToImport); |
299 |
|
} |
300 |
|
|
301 |
60 |
return configuration; |
302 |
|
} |
303 |
|
} |