1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.classloader.internal; |
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
import java.net.URI; |
24 |
|
import java.util.Map; |
25 |
|
import java.util.concurrent.ConcurrentHashMap; |
26 |
|
|
27 |
|
import javax.inject.Inject; |
28 |
|
import javax.inject.Singleton; |
29 |
|
|
30 |
|
import org.slf4j.Logger; |
31 |
|
import org.xwiki.classloader.ClassLoaderManager; |
32 |
|
import org.xwiki.classloader.NamespaceURLClassLoader; |
33 |
|
import org.xwiki.component.annotation.Component; |
34 |
|
import org.xwiki.component.manager.ComponentLifecycleException; |
35 |
|
import org.xwiki.component.phase.Disposable; |
36 |
|
import org.xwiki.component.phase.Initializable; |
37 |
|
import org.xwiki.component.phase.InitializationException; |
38 |
|
|
39 |
|
|
40 |
|
@link |
41 |
|
|
42 |
|
@version |
43 |
|
@since |
44 |
|
|
45 |
|
@Component |
46 |
|
@Singleton |
|
|
| 90.6% |
Uncovered Elements: 5 (53) |
Complexity: 16 |
Complexity Density: 0.52 |
|
47 |
|
public class DefaultClassLoaderManager implements ClassLoaderManager, Initializable, Disposable |
48 |
|
{ |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
protected ClassLoader containerClassLoader; |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
private volatile NamespaceURLClassLoader rootClassLoader; |
58 |
|
|
59 |
|
@Inject |
60 |
|
private Logger logger; |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
private Map<String, NamespaceURLClassLoader> wikiClassLoaderMap = |
66 |
|
new ConcurrentHashMap<String, NamespaceURLClassLoader>(); |
67 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
68 |
63 |
@Override... |
69 |
|
public void initialize() throws InitializationException |
70 |
|
{ |
71 |
63 |
this.containerClassLoader = Thread.currentThread().getContextClassLoader(); |
72 |
|
} |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
@return |
78 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
79 |
93 |
protected ClassLoader getContainerClassLoader()... |
80 |
|
{ |
81 |
93 |
return this.containerClassLoader; |
82 |
|
} |
83 |
|
|
|
|
| 95.7% |
Uncovered Elements: 1 (23) |
Complexity: 6 |
Complexity Density: 0.46 |
|
84 |
4177 |
@Override... |
85 |
|
public NamespaceURLClassLoader getURLClassLoader(String namespace, boolean create) |
86 |
|
{ |
87 |
|
|
88 |
4169 |
if (this.rootClassLoader == null) { |
89 |
93 |
synchronized (this) { |
90 |
93 |
if (this.rootClassLoader == null) { |
91 |
93 |
this.rootClassLoader = new NamespaceURLClassLoader(new URI[] {}, getContainerClassLoader(), null); |
92 |
|
} |
93 |
|
} |
94 |
|
} |
95 |
|
|
96 |
|
|
97 |
4172 |
NamespaceURLClassLoader wikiClassLoader = this.rootClassLoader; |
98 |
|
|
99 |
|
|
100 |
4164 |
if (namespace != null) { |
101 |
4026 |
wikiClassLoader = this.wikiClassLoaderMap.get(namespace); |
102 |
|
|
103 |
4020 |
if (wikiClassLoader == null) { |
104 |
590 |
if (create) { |
105 |
|
|
106 |
93 |
wikiClassLoader = new NamespaceURLClassLoader(new URI[] {}, this.rootClassLoader, namespace); |
107 |
|
|
108 |
|
|
109 |
93 |
this.wikiClassLoaderMap.put(namespace, wikiClassLoader); |
110 |
|
} else { |
111 |
497 |
wikiClassLoader = this.rootClassLoader; |
112 |
|
} |
113 |
|
} |
114 |
|
} |
115 |
|
|
116 |
4174 |
return wikiClassLoader; |
117 |
|
} |
118 |
|
|
|
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
|
119 |
95 |
@Override... |
120 |
|
public void dropURLClassLoaders() |
121 |
|
{ |
122 |
95 |
if (this.rootClassLoader != null) { |
123 |
93 |
for (String namespace : this.wikiClassLoaderMap.keySet()) { |
124 |
69 |
dropURLClassLoader(namespace); |
125 |
|
} |
126 |
|
|
127 |
|
|
128 |
93 |
NamespaceURLClassLoader classloader = this.rootClassLoader; |
129 |
93 |
this.rootClassLoader = null; |
130 |
93 |
try { |
131 |
93 |
classloader.close(); |
132 |
|
} catch (IOException e) { |
133 |
0 |
this.logger.warn("Failed to close root Classloader", e); |
134 |
|
} |
135 |
|
} |
136 |
|
} |
137 |
|
|
|
|
| 72.7% |
Uncovered Elements: 3 (11) |
Complexity: 4 |
Complexity Density: 0.57 |
|
138 |
90 |
@Override... |
139 |
|
public void dropURLClassLoader(String namespace) |
140 |
|
{ |
141 |
90 |
if (namespace == null) { |
142 |
|
|
143 |
0 |
dropURLClassLoaders(); |
144 |
|
} else { |
145 |
|
|
146 |
90 |
NamespaceURLClassLoader classloader = this.wikiClassLoaderMap.remove(namespace); |
147 |
|
|
148 |
90 |
if (classloader != null) { |
149 |
|
|
150 |
89 |
try { |
151 |
89 |
classloader.close(); |
152 |
|
} catch (IOException e) { |
153 |
0 |
this.logger.warn("Failed to close Classloader", e); |
154 |
|
} |
155 |
|
} |
156 |
|
} |
157 |
|
} |
158 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
159 |
86 |
@Override... |
160 |
|
public void dispose() throws ComponentLifecycleException |
161 |
|
{ |
162 |
86 |
dropURLClassLoaders(); |
163 |
|
} |
164 |
|
} |