1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.extension.jar.internal.handler; |
21 |
|
|
22 |
|
import java.io.File; |
23 |
|
import java.io.IOException; |
24 |
|
import java.io.InputStream; |
25 |
|
import java.net.MalformedURLException; |
26 |
|
import java.net.URL; |
27 |
|
import java.util.List; |
28 |
|
|
29 |
|
import javax.inject.Inject; |
30 |
|
import javax.inject.Singleton; |
31 |
|
|
32 |
|
import org.xwiki.classloader.ClassLoaderManager; |
33 |
|
import org.xwiki.classloader.NamespaceURLClassLoader; |
34 |
|
import org.xwiki.component.annotation.Component; |
35 |
|
import org.xwiki.component.annotation.ComponentAnnotationLoader; |
36 |
|
import org.xwiki.component.annotation.ComponentDeclaration; |
37 |
|
import org.xwiki.component.internal.StackingComponentEventManager; |
38 |
|
import org.xwiki.component.internal.multi.ComponentManagerManager; |
39 |
|
import org.xwiki.component.manager.ComponentEventManager; |
40 |
|
import org.xwiki.component.manager.ComponentManager; |
41 |
|
import org.xwiki.component.phase.Initializable; |
42 |
|
import org.xwiki.component.phase.InitializationException; |
43 |
|
import org.xwiki.extension.Extension; |
44 |
|
import org.xwiki.extension.ExtensionException; |
45 |
|
import org.xwiki.extension.InstallException; |
46 |
|
import org.xwiki.extension.InstalledExtension; |
47 |
|
import org.xwiki.extension.LocalExtension; |
48 |
|
import org.xwiki.extension.LocalExtensionFile; |
49 |
|
import org.xwiki.extension.UninstallException; |
50 |
|
import org.xwiki.extension.handler.internal.AbstractExtensionHandler; |
51 |
|
import org.xwiki.job.Request; |
52 |
|
import org.xwiki.observation.ObservationManager; |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
@version |
58 |
|
@since |
59 |
|
|
60 |
|
@Component(hints = {JarExtensionHandler.JAR, JarExtensionHandler.WEBJAR}) |
61 |
|
@Singleton |
|
|
| 84.1% |
Uncovered Elements: 14 (88) |
Complexity: 26 |
Complexity Density: 0.47 |
|
62 |
|
public class JarExtensionHandler extends AbstractExtensionHandler implements Initializable |
63 |
|
{ |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
@since |
68 |
|
|
69 |
|
public static final String JAR = "jar"; |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
@since |
75 |
|
|
76 |
|
public static final String WEBJAR = "webjar"; |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
@since |
82 |
|
|
83 |
|
public static final String PROPERTY_TYPE = "xwiki.extension.jar.type"; |
84 |
|
|
85 |
|
@Inject |
86 |
|
private ComponentManagerManager componentManagerManager; |
87 |
|
|
88 |
|
@Inject |
89 |
|
private ClassLoaderManager jarExtensionClassLoader; |
90 |
|
|
91 |
|
private ComponentAnnotationLoader jarLoader; |
92 |
|
|
93 |
|
|
94 |
|
@param |
95 |
|
@return |
96 |
|
@since |
97 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
98 |
148 |
public static boolean isSupported(String type)... |
99 |
|
{ |
100 |
148 |
return type != null && (type.equals(JarExtensionHandler.JAR) || type.equals(JarExtensionHandler.WEBJAR)); |
101 |
|
} |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
@param |
107 |
|
@return |
108 |
|
@since |
109 |
|
|
|
|
| 69.2% |
Uncovered Elements: 4 (13) |
Complexity: 4 |
Complexity Density: 0.57 |
|
110 |
133 |
public static boolean isWebjar(Extension extension)... |
111 |
|
{ |
112 |
|
|
113 |
133 |
if (extension.getType().equals(WEBJAR)) { |
114 |
0 |
return true; |
115 |
|
} |
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
133 |
if (extension.getId().getId().startsWith("org.webjars:")) { |
122 |
1 |
return true; |
123 |
|
} |
124 |
|
|
125 |
|
|
126 |
132 |
if (JarExtensionHandler.WEBJAR.equals(extension.getProperty(JarExtensionHandler.PROPERTY_TYPE))) { |
127 |
0 |
return true; |
128 |
|
} |
129 |
|
|
130 |
132 |
return false; |
131 |
|
} |
132 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
133 |
26 |
@Override... |
134 |
|
public void initialize() throws InitializationException |
135 |
|
{ |
136 |
26 |
this.jarLoader = new ComponentAnnotationLoader(); |
137 |
|
} |
138 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
139 |
79 |
@Override... |
140 |
|
public void initialize(LocalExtension localExtension, String namespace) throws ExtensionException |
141 |
|
{ |
142 |
79 |
install(localExtension, namespace, null); |
143 |
|
} |
144 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
145 |
135 |
private static URL getExtensionURL(LocalExtension localExtension) throws MalformedURLException... |
146 |
|
{ |
147 |
135 |
return new File(localExtension.getFile().getAbsolutePath()).toURI().toURL(); |
148 |
|
} |
149 |
|
|
|
|
| 87.5% |
Uncovered Elements: 1 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
150 |
135 |
@Override... |
151 |
|
public void install(LocalExtension localExtension, String namespace, Request request) throws InstallException |
152 |
|
{ |
153 |
135 |
NamespaceURLClassLoader classLoader = this.jarExtensionClassLoader.getURLClassLoader(namespace, true); |
154 |
|
|
155 |
|
|
156 |
135 |
try { |
157 |
135 |
classLoader.addURL(getExtensionURL(localExtension)); |
158 |
|
} catch (MalformedURLException e) { |
159 |
0 |
throw new InstallException("Failed to load jar file", e); |
160 |
|
} |
161 |
|
|
162 |
|
|
163 |
135 |
if (containsComponents(localExtension)) { |
164 |
132 |
loadComponents(localExtension.getFile(), classLoader, namespace); |
165 |
|
} |
166 |
|
} |
167 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
168 |
135 |
private boolean containsComponents(Extension extension)... |
169 |
|
{ |
170 |
135 |
return extension != null && extension.getType().equals(JAR) && !isWebjar(extension); |
171 |
|
} |
172 |
|
|
|
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 5 |
Complexity Density: 0.83 |
|
173 |
89 |
@Override... |
174 |
|
public void uninstall(InstalledExtension installedExtension, String namespace, Request request) |
175 |
|
throws UninstallException |
176 |
|
{ |
177 |
89 |
if (installedExtension.isValid(namespace)) { |
178 |
70 |
NamespaceURLClassLoader classLoader = this.jarExtensionClassLoader.getURLClassLoader(namespace, false); |
179 |
|
|
180 |
70 |
if (namespace == null || classLoader.getNamespace().equals(namespace)) { |
181 |
|
|
182 |
70 |
try { |
183 |
70 |
unloadComponents(installedExtension.getFile(), classLoader, namespace); |
184 |
|
} catch (Throwable e) { |
185 |
|
|
186 |
|
|
187 |
|
|
188 |
0 |
this.logger.warn("Failed to unregister some components of the JAR extension [{}]", |
189 |
|
installedExtension.getId(), e); |
190 |
|
} |
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
} |
195 |
|
} |
196 |
|
} |
197 |
|
|
|
|
| 76.7% |
Uncovered Elements: 7 (30) |
Complexity: 6 |
Complexity Density: 0.27 |
|
198 |
132 |
private void loadComponents(LocalExtensionFile jarFile, NamespaceURLClassLoader classLoader, String namespace)... |
199 |
|
throws InstallException |
200 |
|
{ |
201 |
132 |
try { |
202 |
132 |
List<ComponentDeclaration> componentDeclarations = getDeclaredComponents(jarFile); |
203 |
|
|
204 |
132 |
if (componentDeclarations == null) { |
205 |
2 |
this.logger.debug("[{}] does not contain any components to load", jarFile.getName()); |
206 |
2 |
return; |
207 |
|
} |
208 |
|
|
209 |
130 |
ComponentManager componentManager = this.componentManagerManager.getComponentManager(namespace, true); |
210 |
|
|
211 |
130 |
ComponentEventManager componentEventManager = componentManager.getComponentEventManager(); |
212 |
|
|
213 |
|
|
214 |
130 |
StackingComponentEventManager stackingComponentEventManager = null; |
215 |
130 |
try { |
216 |
130 |
if (componentEventManager instanceof StackingComponentEventManager) { |
217 |
130 |
stackingComponentEventManager = (StackingComponentEventManager) componentEventManager; |
218 |
|
} else { |
219 |
0 |
stackingComponentEventManager = new StackingComponentEventManager(); |
220 |
0 |
componentManager.setComponentEventManager(stackingComponentEventManager); |
221 |
|
} |
222 |
130 |
stackingComponentEventManager.shouldStack(true); |
223 |
|
|
224 |
130 |
this.jarLoader.initialize(componentManager, classLoader, componentDeclarations); |
225 |
|
} finally { |
226 |
130 |
if (stackingComponentEventManager != null) { |
227 |
130 |
if (componentEventManager != stackingComponentEventManager) { |
228 |
0 |
componentManager.setComponentEventManager(componentEventManager); |
229 |
|
} |
230 |
|
|
231 |
130 |
stackingComponentEventManager.setObservationManager( |
232 |
|
componentManager.<ObservationManager>getInstance(ObservationManager.class)); |
233 |
130 |
stackingComponentEventManager.shouldStack(false); |
234 |
130 |
stackingComponentEventManager.flushEvents(); |
235 |
|
} |
236 |
|
} |
237 |
|
} catch (Exception e) { |
238 |
0 |
throw new InstallException("Failed to load jar file components", e); |
239 |
|
} |
240 |
|
} |
241 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
242 |
202 |
private List<ComponentDeclaration> getDeclaredComponents(LocalExtensionFile jarFile) throws IOException... |
243 |
|
{ |
244 |
202 |
InputStream is = jarFile.openStream(); |
245 |
|
|
246 |
202 |
try { |
247 |
202 |
return this.jarLoader.getDeclaredComponentsFromJAR(is); |
248 |
|
} finally { |
249 |
202 |
is.close(); |
250 |
|
} |
251 |
|
} |
252 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
253 |
70 |
private void unloadComponents(LocalExtensionFile jarFile, NamespaceURLClassLoader classLoader, String namespace)... |
254 |
|
throws IOException |
255 |
|
{ |
256 |
70 |
List<ComponentDeclaration> componentDeclarations = getDeclaredComponents(jarFile); |
257 |
|
|
258 |
70 |
if (componentDeclarations == null) { |
259 |
2 |
this.logger.debug("[{}] does not contain any component", jarFile.getName()); |
260 |
2 |
return; |
261 |
|
} |
262 |
|
|
263 |
68 |
this.jarLoader.unregister(this.componentManagerManager.getComponentManager(namespace, false), classLoader, |
264 |
|
componentDeclarations); |
265 |
|
} |
266 |
|
} |