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.repository; |
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
import java.util.ArrayList; |
24 |
|
import java.util.Collection; |
25 |
|
import java.util.LinkedHashSet; |
26 |
|
import java.util.List; |
27 |
|
import java.util.Locale; |
28 |
|
import java.util.Map; |
29 |
|
import java.util.Set; |
30 |
|
import java.util.concurrent.ConcurrentHashMap; |
31 |
|
|
32 |
|
import javax.inject.Inject; |
33 |
|
import javax.inject.Named; |
34 |
|
import javax.inject.Singleton; |
35 |
|
|
36 |
|
import org.slf4j.Logger; |
37 |
|
import org.xwiki.component.annotation.Component; |
38 |
|
import org.xwiki.component.phase.Initializable; |
39 |
|
import org.xwiki.component.phase.InitializationException; |
40 |
|
import org.xwiki.extension.ExtensionId; |
41 |
|
import org.xwiki.extension.InstallException; |
42 |
|
import org.xwiki.extension.InstalledExtension; |
43 |
|
import org.xwiki.extension.LocalExtension; |
44 |
|
import org.xwiki.extension.ResolveException; |
45 |
|
import org.xwiki.extension.UninstallException; |
46 |
|
import org.xwiki.extension.repository.DefaultExtensionRepositoryDescriptor; |
47 |
|
import org.xwiki.extension.repository.InstalledExtensionRepository; |
48 |
|
import org.xwiki.extension.repository.internal.installed.AbstractInstalledExtensionRepository; |
49 |
|
import org.xwiki.extension.xar.internal.handler.UnsupportedNamespaceException; |
50 |
|
import org.xwiki.extension.xar.internal.handler.XarExtensionHandler; |
51 |
|
import org.xwiki.extension.xar.internal.handler.XarHandlerUtils; |
52 |
|
import org.xwiki.model.reference.DocumentReference; |
53 |
|
import org.xwiki.model.reference.LocalDocumentReference; |
54 |
|
import org.xwiki.model.reference.WikiReference; |
55 |
|
import org.xwiki.xar.XarEntry; |
56 |
|
import org.xwiki.xar.XarException; |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
@version |
62 |
|
@since |
63 |
|
|
64 |
|
@Component |
65 |
|
@Singleton |
66 |
|
@Named(XarExtensionHandler.TYPE) |
|
|
| 79.4% |
Uncovered Elements: 26 (126) |
Complexity: 41 |
Complexity Density: 0.59 |
|
67 |
|
public class XarInstalledExtensionRepository extends AbstractInstalledExtensionRepository<XarInstalledExtension> |
68 |
|
implements InstalledExtensionRepository, Initializable |
69 |
|
{ |
70 |
|
@Inject |
71 |
|
private transient InstalledExtensionRepository installedRepository; |
72 |
|
|
73 |
|
@Inject |
74 |
|
private Logger logger; |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
private Map<DocumentReference, Collection<XarInstalledExtension>> documents = new ConcurrentHashMap<>(); |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
private Map<LocalDocumentReference, Collection<XarInstalledExtension>> rootDocuments = new ConcurrentHashMap<>(); |
85 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
86 |
27 |
@Override... |
87 |
|
public void initialize() throws InitializationException |
88 |
|
{ |
89 |
27 |
setDescriptor(new DefaultExtensionRepositoryDescriptor(XarExtensionHandler.TYPE, XarExtensionHandler.TYPE, |
90 |
|
this.installedRepository.getDescriptor().getURI())); |
91 |
|
|
92 |
27 |
loadExtensions(); |
93 |
|
} |
94 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
95 |
25 |
void pagesRemoved(ExtensionId extensionId, String namespace) throws UnsupportedNamespaceException... |
96 |
|
{ |
97 |
25 |
pagesUpdated(extensionId, namespace, false); |
98 |
|
} |
99 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
100 |
63 |
void pagesAdded(ExtensionId extensionId, String namespace) throws UnsupportedNamespaceException... |
101 |
|
{ |
102 |
63 |
pagesUpdated(extensionId, namespace, true); |
103 |
|
} |
104 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
105 |
88 |
private void pagesUpdated(ExtensionId extensionId, String namespace, boolean add)... |
106 |
|
throws UnsupportedNamespaceException |
107 |
|
{ |
108 |
88 |
XarInstalledExtension installedExtension = (XarInstalledExtension) getInstalledExtension(extensionId); |
109 |
|
|
110 |
88 |
pagesUpdated(installedExtension, namespace, add); |
111 |
|
} |
112 |
|
|
|
|
| 94.4% |
Uncovered Elements: 2 (36) |
Complexity: 11 |
Complexity Density: 0.55 |
|
113 |
113 |
private void pagesUpdated(XarInstalledExtension installedExtension, String namespace, boolean add)... |
114 |
|
throws UnsupportedNamespaceException |
115 |
|
{ |
116 |
113 |
if (installedExtension != null) { |
117 |
111 |
for (XarEntry xarEntry : installedExtension.getXarPackage().getEntries()) { |
118 |
457 |
if (namespace != null) { |
119 |
298 |
DocumentReference reference = new DocumentReference(xarEntry, |
120 |
|
new WikiReference(XarHandlerUtils.getWikiFromNamespace(namespace))); |
121 |
|
|
122 |
298 |
synchronized (this.documents) { |
123 |
298 |
Collection<XarInstalledExtension> referenceExtensions = this.documents.get(reference); |
124 |
298 |
if (referenceExtensions != null || add) { |
125 |
298 |
Set<XarInstalledExtension> newSet = referenceExtensions != null |
126 |
|
? new LinkedHashSet<>(referenceExtensions) : new LinkedHashSet<>(); |
127 |
|
|
128 |
298 |
if (add) { |
129 |
206 |
newSet.add(installedExtension); |
130 |
|
} else { |
131 |
92 |
newSet.remove(installedExtension); |
132 |
|
} |
133 |
|
|
134 |
298 |
this.documents.put(reference, newSet); |
135 |
|
} |
136 |
|
} |
137 |
|
} else { |
138 |
159 |
synchronized (this.rootDocuments) { |
139 |
159 |
Collection<XarInstalledExtension> referenceExtensions = this.rootDocuments.get(xarEntry); |
140 |
159 |
if (referenceExtensions != null || add) { |
141 |
159 |
Set<XarInstalledExtension> newSet = referenceExtensions != null |
142 |
|
? new LinkedHashSet<>(referenceExtensions) : new LinkedHashSet<>(); |
143 |
|
|
144 |
159 |
if (add) { |
145 |
131 |
newSet.add(installedExtension); |
146 |
|
} else { |
147 |
28 |
newSet.remove(installedExtension); |
148 |
|
} |
149 |
|
|
150 |
159 |
this.rootDocuments.put(xarEntry, newSet); |
151 |
|
} |
152 |
|
} |
153 |
|
} |
154 |
|
} |
155 |
|
} |
156 |
|
} |
157 |
|
|
|
|
| 81.8% |
Uncovered Elements: 2 (11) |
Complexity: 5 |
Complexity Density: 0.71 |
|
158 |
88 |
void updateCachedXarExtension(ExtensionId extensionId)... |
159 |
|
{ |
160 |
88 |
InstalledExtension installedExtension = this.installedRepository.getInstalledExtension(extensionId); |
161 |
|
|
162 |
88 |
if (installedExtension != null && installedExtension.getType().equals(XarExtensionHandler.TYPE)) { |
163 |
62 |
if (getInstalledExtension(installedExtension.getId()) == null) { |
164 |
62 |
try { |
165 |
62 |
addCacheXarExtension(installedExtension); |
166 |
|
} catch (Exception e) { |
167 |
0 |
this.logger.error("Failed to parse extension [{}]", installedExtension.getId(), e); |
168 |
|
} |
169 |
|
} |
170 |
|
} else { |
171 |
26 |
removeCachedXarExtension(extensionId); |
172 |
|
} |
173 |
|
} |
174 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
175 |
87 |
private XarInstalledExtension addCacheXarExtension(InstalledExtension installedExtension)... |
176 |
|
throws IOException, XarException |
177 |
|
{ |
178 |
87 |
XarInstalledExtension xarExtension = new XarInstalledExtension(installedExtension, this); |
179 |
|
|
180 |
87 |
addCachedExtension(xarExtension); |
181 |
|
|
182 |
87 |
return xarExtension; |
183 |
|
} |
184 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
185 |
26 |
protected void removeCachedXarExtension(ExtensionId extensionId)... |
186 |
|
{ |
187 |
26 |
XarInstalledExtension extension = (XarInstalledExtension) getInstalledExtension(extensionId); |
188 |
|
|
189 |
26 |
if (extension != null) { |
190 |
24 |
super.removeCachedExtension(extension); |
191 |
|
} |
192 |
|
} |
193 |
|
|
|
|
| 57.1% |
Uncovered Elements: 6 (14) |
Complexity: 4 |
Complexity Density: 0.4 |
|
194 |
27 |
private void loadExtensions()... |
195 |
|
{ |
196 |
27 |
for (InstalledExtension localExtension : this.installedRepository.getInstalledExtensions()) { |
197 |
25 |
if (localExtension.getType().equalsIgnoreCase(XarExtensionHandler.TYPE)) { |
198 |
25 |
try { |
199 |
|
|
200 |
25 |
XarInstalledExtension xarInstalledExtension = addCacheXarExtension(localExtension); |
201 |
|
|
202 |
|
|
203 |
25 |
if (xarInstalledExtension.getNamespaces() == null) { |
204 |
25 |
pagesUpdated(xarInstalledExtension, null, true); |
205 |
|
} else { |
206 |
0 |
for (String namespace : localExtension.getNamespaces()) { |
207 |
0 |
pagesUpdated(xarInstalledExtension, namespace, true); |
208 |
|
} |
209 |
|
} |
210 |
|
} catch (Exception e) { |
211 |
0 |
this.logger.error("Failed to parse extension [{}]", localExtension.getId(), e); |
212 |
|
|
213 |
0 |
continue; |
214 |
|
} |
215 |
|
} |
216 |
|
} |
217 |
|
} |
218 |
|
|
219 |
|
|
220 |
|
@param |
221 |
|
@return |
222 |
|
@since |
223 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 5 |
Complexity Density: 0.62 |
|
224 |
157 |
public Collection<XarInstalledExtension> getXarInstalledExtensions(DocumentReference reference)... |
225 |
|
{ |
226 |
157 |
Collection<XarInstalledExtension> wikiExtensions = this.documents |
227 |
157 |
.get(reference.getLocale() == null ? new DocumentReference(reference, Locale.ROOT) : reference); |
228 |
157 |
Collection<XarInstalledExtension> rootExtensions = |
229 |
157 |
this.rootDocuments.get(reference.getLocaleDocumentReference().getLocale() == null |
230 |
|
? new LocalDocumentReference(reference.getLocaleDocumentReference(), Locale.ROOT) |
231 |
|
: reference.getLocaleDocumentReference()); |
232 |
|
|
233 |
157 |
List<XarInstalledExtension> allExtensions = new ArrayList<>(); |
234 |
|
|
235 |
157 |
if (wikiExtensions != null) { |
236 |
95 |
allExtensions.addAll(wikiExtensions); |
237 |
|
} |
238 |
|
|
239 |
157 |
if (rootExtensions != null) { |
240 |
81 |
allExtensions.addAll(rootExtensions); |
241 |
|
} |
242 |
|
|
243 |
157 |
return allExtensions; |
244 |
|
} |
245 |
|
|
246 |
|
|
247 |
|
|
|
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
248 |
2 |
@Override... |
249 |
|
public InstalledExtension getInstalledExtension(String id, String namespace) |
250 |
|
{ |
251 |
2 |
InstalledExtension extension = this.installedRepository.getInstalledExtension(id, namespace); |
252 |
|
|
253 |
2 |
if (extension != null) { |
254 |
1 |
if (extension.getType().equals(XarExtensionHandler.TYPE)) { |
255 |
1 |
extension = this.extensions.get(extension.getId()); |
256 |
|
} else { |
257 |
0 |
extension = null; |
258 |
|
} |
259 |
|
} |
260 |
|
|
261 |
2 |
return extension; |
262 |
|
} |
263 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
264 |
0 |
@Override... |
265 |
|
public InstalledExtension installExtension(LocalExtension extension, String namespace, boolean dependency, |
266 |
|
Map<String, Object> properties) throws InstallException |
267 |
|
{ |
268 |
0 |
throw new UnsupportedOperationException("Not implemented"); |
269 |
|
} |
270 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
271 |
0 |
@Override... |
272 |
|
public void uninstallExtension(InstalledExtension extension, String namespace) throws UninstallException |
273 |
|
{ |
274 |
0 |
throw new UnsupportedOperationException("Not implemented"); |
275 |
|
} |
276 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
277 |
0 |
@Override... |
278 |
|
public Collection<InstalledExtension> getBackwardDependencies(String id, String namespace) throws ResolveException |
279 |
|
{ |
280 |
0 |
InstalledExtension extension = this.installedRepository.getInstalledExtension(id, namespace); |
281 |
|
|
282 |
0 |
return extension.getType().equals(XarExtensionHandler.TYPE) |
283 |
|
? this.installedRepository.getBackwardDependencies(id, namespace) : null; |
284 |
|
} |
285 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
286 |
0 |
@Override... |
287 |
|
public Map<String, Collection<InstalledExtension>> getBackwardDependencies(ExtensionId extensionId) |
288 |
|
throws ResolveException |
289 |
|
{ |
290 |
0 |
InstalledExtension extension = this.installedRepository.resolve(extensionId); |
291 |
|
|
292 |
0 |
return extension.getType().equals(XarExtensionHandler.TYPE) |
293 |
|
? this.installedRepository.getBackwardDependencies(extensionId) : null; |
294 |
|
} |
295 |
|
} |