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.util.Collection; |
23 |
|
|
24 |
|
import javax.inject.Inject; |
25 |
|
import javax.inject.Named; |
26 |
|
import javax.inject.Provider; |
27 |
|
import javax.inject.Singleton; |
28 |
|
|
29 |
|
import org.slf4j.Logger; |
30 |
|
import org.xwiki.bridge.event.WikiCopiedEvent; |
31 |
|
import org.xwiki.bridge.event.WikiCreatedEvent; |
32 |
|
import org.xwiki.bridge.event.WikiDeletedEvent; |
33 |
|
import org.xwiki.component.annotation.Component; |
34 |
|
import org.xwiki.extension.ExtensionDependency; |
35 |
|
import org.xwiki.extension.ExtensionException; |
36 |
|
import org.xwiki.extension.InstallException; |
37 |
|
import org.xwiki.extension.InstalledExtension; |
38 |
|
import org.xwiki.extension.UninstallException; |
39 |
|
import org.xwiki.extension.handler.ExtensionHandler; |
40 |
|
import org.xwiki.extension.handler.ExtensionHandlerManager; |
41 |
|
import org.xwiki.extension.job.InstallRequest; |
42 |
|
import org.xwiki.extension.repository.InstalledExtensionRepository; |
43 |
|
import org.xwiki.model.reference.DocumentReference; |
44 |
|
import org.xwiki.observation.AbstractEventListener; |
45 |
|
import org.xwiki.observation.event.Event; |
46 |
|
|
47 |
|
import com.xpn.xwiki.XWikiContext; |
48 |
|
|
49 |
|
|
50 |
|
@version |
51 |
|
|
52 |
|
@Component |
53 |
|
@Singleton |
54 |
|
@Named("extension.xar.WikiCopiedListener") |
|
|
| 89.4% |
Uncovered Elements: 7 (66) |
Complexity: 18 |
Complexity Density: 0.41 |
|
55 |
|
public class WikiEventListener extends AbstractEventListener |
56 |
|
{ |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
private static final String PROPERTY_USER_REFERENCE = "user.reference"; |
61 |
|
|
62 |
|
@Inject |
63 |
|
private InstalledExtensionRepository installedRepository; |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
@Inject |
69 |
|
private ExtensionHandlerManager extensionHandlerManager; |
70 |
|
|
71 |
|
@Inject |
72 |
|
@Named("xar") |
73 |
|
private Provider<ExtensionHandler> xarHandlerProvider; |
74 |
|
|
75 |
|
@Inject |
76 |
|
private Logger logger; |
77 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
78 |
26 |
public WikiEventListener()... |
79 |
|
{ |
80 |
26 |
super("extension.xar.WikiCopiedListener", new WikiCopiedEvent(), new WikiCreatedEvent(), new WikiDeletedEvent()); |
81 |
|
} |
82 |
|
|
|
|
| 91.7% |
Uncovered Elements: 1 (12) |
Complexity: 4 |
Complexity Density: 0.67 |
|
83 |
11 |
@Override... |
84 |
|
public void onEvent(Event event, Object o, Object context) |
85 |
|
{ |
86 |
11 |
if (event instanceof WikiCopiedEvent) { |
87 |
3 |
onWikiCopied((WikiCopiedEvent) event); |
88 |
8 |
} else if (event instanceof WikiCreatedEvent) { |
89 |
5 |
onWikiCreated((WikiCreatedEvent) event, (XWikiContext) context); |
90 |
3 |
} else if (event instanceof WikiDeletedEvent) { |
91 |
3 |
onWikiDeleted((WikiDeletedEvent) event); |
92 |
|
} |
93 |
|
} |
94 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
95 |
3 |
private void onWikiCopied(WikiCopiedEvent event)... |
96 |
|
{ |
97 |
3 |
String sourceNamespace = "wiki:" + event.getSourceWikiId(); |
98 |
3 |
String targetNamespace = "wiki:" + event.getTargetWikiId(); |
99 |
|
|
100 |
3 |
Collection<InstalledExtension> installedExtensions = |
101 |
|
this.installedRepository.getInstalledExtensions(sourceNamespace); |
102 |
|
|
103 |
3 |
for (InstalledExtension installedExtension : installedExtensions) { |
104 |
7 |
copyInstalledExtension(installedExtension, sourceNamespace, targetNamespace); |
105 |
|
} |
106 |
|
} |
107 |
|
|
|
|
| 92.3% |
Uncovered Elements: 1 (13) |
Complexity: 5 |
Complexity Density: 0.56 |
|
108 |
10 |
private void copyInstalledExtension(InstalledExtension installedExtension, String sourceNamespace,... |
109 |
|
String targetNamespace) |
110 |
|
{ |
111 |
10 |
if (!installedExtension.isInstalled(targetNamespace) && !installedExtension.isInstalled(null)) { |
112 |
|
|
113 |
6 |
for (ExtensionDependency dependency : installedExtension.getDependencies()) { |
114 |
13 |
InstalledExtension installedDependency = |
115 |
|
this.installedRepository.getInstalledExtension(dependency.getId(), sourceNamespace); |
116 |
13 |
if (installedDependency != null) { |
117 |
3 |
copyInstalledExtension(installedDependency, sourceNamespace, targetNamespace); |
118 |
|
} |
119 |
|
} |
120 |
|
|
121 |
|
|
122 |
6 |
try { |
123 |
|
|
124 |
6 |
this.extensionHandlerManager.initialize(installedExtension, targetNamespace); |
125 |
|
|
126 |
|
|
127 |
6 |
this.installedRepository.installExtension(installedExtension, targetNamespace, |
128 |
|
installedExtension.isDependency(sourceNamespace)); |
129 |
|
} catch (ExtensionException e) { |
130 |
0 |
this.logger.error("Failed to copy extension [{}] from namespace [{}] to namespace [{}]", |
131 |
|
installedExtension, sourceNamespace, targetNamespace, e); |
132 |
|
} |
133 |
|
} |
134 |
|
} |
135 |
|
|
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
136 |
3 |
private void onWikiDeleted(WikiDeletedEvent event)... |
137 |
|
{ |
138 |
3 |
String namespace = "wiki:" + event.getWikiId(); |
139 |
|
|
140 |
3 |
Collection<InstalledExtension> installedExtensions = this.installedRepository.getInstalledExtensions(namespace); |
141 |
|
|
142 |
3 |
for (InstalledExtension installedExtension : installedExtensions) { |
143 |
6 |
if (!installedExtension.isInstalled(null)) { |
144 |
6 |
try { |
145 |
6 |
this.installedRepository.uninstallExtension(installedExtension, namespace); |
146 |
|
} catch (UninstallException e) { |
147 |
0 |
this.logger.error("Failed to uninstall extension [{}] from namespace [{}]", installedExtension, |
148 |
|
namespace, e); |
149 |
|
} |
150 |
|
} |
151 |
|
} |
152 |
|
} |
153 |
|
|
|
|
| 85% |
Uncovered Elements: 3 (20) |
Complexity: 4 |
Complexity Density: 0.25 |
|
154 |
5 |
private void onWikiCreated(WikiCreatedEvent event, XWikiContext context)... |
155 |
|
{ |
156 |
5 |
String namespace = "wiki:" + event.getWikiId(); |
157 |
|
|
158 |
5 |
Collection<InstalledExtension> installedExtensions = this.installedRepository.getInstalledExtensions(null); |
159 |
|
|
160 |
5 |
InstallRequest installRequest = new InstallRequest(); |
161 |
5 |
DocumentReference userReference = context.getUserReference(); |
162 |
5 |
if (userReference != null) { |
163 |
5 |
installRequest.setProperty(PROPERTY_USER_REFERENCE, userReference); |
164 |
|
|
165 |
|
|
166 |
|
|
167 |
5 |
installRequest.setExtensionProperty(PROPERTY_USER_REFERENCE, userReference.toString()); |
168 |
|
} |
169 |
5 |
installRequest.setVerbose(false); |
170 |
|
|
171 |
5 |
installRequest.setInteractive(false); |
172 |
|
|
173 |
5 |
ExtensionHandler xarHandler = this.xarHandlerProvider.get(); |
174 |
|
|
175 |
5 |
for (InstalledExtension installedExtension : installedExtensions) { |
176 |
3 |
if (installedExtension.getType().equals(XarExtensionHandler.TYPE)) { |
177 |
3 |
installRequest.addExtension(installedExtension.getId()); |
178 |
|
|
179 |
3 |
try { |
180 |
3 |
xarHandler.install(installedExtension, namespace, installRequest); |
181 |
|
} catch (InstallException e) { |
182 |
0 |
this.logger.error("Failed to import extension [{}] in wiki [{}]", installedExtension, |
183 |
|
event.getWikiId(), e); |
184 |
|
} |
185 |
|
} |
186 |
|
} |
187 |
|
} |
188 |
|
} |