1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.velocity.internal; |
21 |
|
|
22 |
|
import java.io.Reader; |
23 |
|
import java.io.StringReader; |
24 |
|
import java.io.Writer; |
25 |
|
import java.util.Enumeration; |
26 |
|
import java.util.Map; |
27 |
|
import java.util.Properties; |
28 |
|
import java.util.concurrent.ConcurrentHashMap; |
29 |
|
|
30 |
|
import javax.inject.Inject; |
31 |
|
|
32 |
|
import org.apache.commons.lang3.StringUtils; |
33 |
|
import org.apache.velocity.context.Context; |
34 |
|
import org.apache.velocity.context.InternalContextAdapterImpl; |
35 |
|
import org.apache.velocity.runtime.RuntimeConstants; |
36 |
|
import org.apache.velocity.runtime.RuntimeServices; |
37 |
|
import org.apache.velocity.runtime.directive.Scope; |
38 |
|
import org.apache.velocity.runtime.directive.StopCommand; |
39 |
|
import org.apache.velocity.runtime.parser.node.SimpleNode; |
40 |
|
import org.slf4j.Logger; |
41 |
|
import org.xwiki.component.annotation.Component; |
42 |
|
import org.xwiki.component.annotation.InstantiationStrategy; |
43 |
|
import org.xwiki.component.descriptor.ComponentInstantiationStrategy; |
44 |
|
import org.xwiki.component.manager.ComponentManager; |
45 |
|
import org.xwiki.velocity.VelocityConfiguration; |
46 |
|
import org.xwiki.velocity.VelocityContextFactory; |
47 |
|
import org.xwiki.velocity.VelocityEngine; |
48 |
|
import org.xwiki.velocity.XWikiVelocityException; |
49 |
|
import org.xwiki.velocity.internal.log.AbstractSLF4JLogChute; |
50 |
|
import org.xwiki.velocity.introspection.TryCatchDirective; |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
@link |
55 |
|
|
56 |
|
|
57 |
|
@link@link |
58 |
|
@link |
59 |
|
|
60 |
|
@version |
61 |
|
|
62 |
|
@Component |
63 |
|
@InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP) |
|
|
| 91% |
Uncovered Elements: 12 (134) |
Complexity: 38 |
Complexity Density: 0.46 |
|
64 |
|
public class DefaultVelocityEngine extends AbstractSLF4JLogChute implements VelocityEngine |
65 |
|
{ |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
private static final String TEMPLATE_SCOPE_NAME = "template"; |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
@Inject |
76 |
|
private ComponentManager componentManager; |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
@Inject |
82 |
|
private VelocityConfiguration velocityConfiguration; |
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
@link |
87 |
|
|
88 |
|
@Inject |
89 |
|
private VelocityContextFactory velocityContextFactory; |
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
@Inject |
95 |
|
private Logger logger; |
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
private org.apache.velocity.app.VelocityEngine engine; |
101 |
|
|
102 |
|
|
103 |
|
@link |
104 |
|
|
105 |
|
private RuntimeServices rsvc; |
106 |
|
|
107 |
|
|
108 |
|
private final Map<String, Integer> namespaceUsageCount = new ConcurrentHashMap<String, Integer>(); |
109 |
|
|
|
|
| 87.5% |
Uncovered Elements: 1 (8) |
Complexity: 2 |
Complexity Density: 0.25 |
|
110 |
110 |
@Override... |
111 |
|
public void initialize(Properties overridingProperties) throws XWikiVelocityException |
112 |
|
{ |
113 |
110 |
org.apache.velocity.app.VelocityEngine velocityEngine = new org.apache.velocity.app.VelocityEngine(); |
114 |
|
|
115 |
|
|
116 |
110 |
velocityEngine.setApplicationAttribute(ComponentManager.class.getName(), this.componentManager); |
117 |
|
|
118 |
|
|
119 |
110 |
initializeProperties(velocityEngine, this.velocityConfiguration.getProperties(), overridingProperties); |
120 |
|
|
121 |
|
|
122 |
110 |
velocityEngine.loadDirective(TryCatchDirective.class.getName()); |
123 |
|
|
124 |
110 |
try { |
125 |
110 |
velocityEngine.init(); |
126 |
|
} catch (Exception e) { |
127 |
0 |
throw new XWikiVelocityException("Cannot start the Velocity engine", e); |
128 |
|
} |
129 |
|
|
130 |
110 |
this.engine = velocityEngine; |
131 |
|
} |
132 |
|
|
133 |
|
|
134 |
|
@param |
135 |
|
@param |
136 |
|
@param |
137 |
|
|
138 |
|
|
|
|
| 92% |
Uncovered Elements: 2 (25) |
Complexity: 6 |
Complexity Density: 0.4 |
|
139 |
110 |
private void initializeProperties(org.apache.velocity.app.VelocityEngine velocityEngine,... |
140 |
|
Properties configurationProperties, Properties overridingProperties) |
141 |
|
{ |
142 |
|
|
143 |
|
|
144 |
110 |
velocityEngine.setProperty("velocimacro.library", ""); |
145 |
|
|
146 |
110 |
velocityEngine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, this); |
147 |
|
|
148 |
|
|
149 |
110 |
if (configurationProperties != null) { |
150 |
1042 |
for (Enumeration<?> e = configurationProperties.propertyNames(); e.hasMoreElements();) { |
151 |
932 |
String key = e.nextElement().toString(); |
152 |
|
|
153 |
932 |
if (!overridingProperties.containsKey(key)) { |
154 |
916 |
String value = configurationProperties.getProperty(key); |
155 |
916 |
velocityEngine.setProperty(key, value); |
156 |
916 |
this.logger.debug("Setting property [{}] = [{}]", key, value); |
157 |
|
} |
158 |
|
} |
159 |
|
} |
160 |
|
|
161 |
|
|
162 |
110 |
if (overridingProperties != null) { |
163 |
308 |
for (Enumeration<?> e = overridingProperties.propertyNames(); e.hasMoreElements();) { |
164 |
198 |
String key = e.nextElement().toString(); |
165 |
198 |
String value = overridingProperties.getProperty(key); |
166 |
198 |
velocityEngine.setProperty(key, value); |
167 |
198 |
this.logger.debug("Overriding property [{}] = [{}]", key, value); |
168 |
|
} |
169 |
|
} |
170 |
|
} |
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
@param |
176 |
|
@param |
177 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
178 |
100973 |
private void restoreTemplateScope(InternalContextAdapterImpl ica, Scope currentTemplateScope)... |
179 |
|
{ |
180 |
100964 |
if (currentTemplateScope.getParent() != null) { |
181 |
90433 |
ica.put(TEMPLATE_SCOPE_NAME, currentTemplateScope.getParent()); |
182 |
10533 |
} else if (currentTemplateScope.getReplaced() != null) { |
183 |
174 |
ica.put(TEMPLATE_SCOPE_NAME, currentTemplateScope.getReplaced()); |
184 |
|
} else { |
185 |
10359 |
ica.remove(TEMPLATE_SCOPE_NAME); |
186 |
|
} |
187 |
|
} |
188 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
189 |
167739 |
private String toThreadSafeNamespace(String namespace)... |
190 |
|
{ |
191 |
167739 |
return StringUtils.isNotEmpty(namespace) ? Thread.currentThread().getId() + ":" + namespace : namespace; |
192 |
|
} |
193 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
194 |
11298 |
@Override... |
195 |
|
public boolean evaluate(Context context, Writer out, String templateName, String source) |
196 |
|
throws XWikiVelocityException |
197 |
|
{ |
198 |
11298 |
return evaluate(context, out, templateName, new StringReader(source)); |
199 |
|
} |
200 |
|
|
|
|
| 88.2% |
Uncovered Elements: 2 (17) |
Complexity: 6 |
Complexity Density: 0.55 |
|
201 |
100968 |
@Override... |
202 |
|
public boolean evaluate(Context context, Writer out, String templateName, Reader source) |
203 |
|
throws XWikiVelocityException |
204 |
|
{ |
205 |
|
|
206 |
100967 |
if (this.engine == null) { |
207 |
0 |
throw new XWikiVelocityException("This Velocity Engine has not yet been initialized. " |
208 |
|
+ " You must call its initialize() method before you can use it."); |
209 |
|
} |
210 |
|
|
211 |
|
|
212 |
|
|
213 |
100969 |
String namespace = toThreadSafeNamespace(templateName); |
214 |
|
|
215 |
|
|
216 |
|
|
217 |
100961 |
try { |
218 |
100966 |
if (StringUtils.isNotEmpty(namespace)) { |
219 |
100929 |
startedUsingMacroNamespaceInternal(namespace); |
220 |
|
} |
221 |
|
|
222 |
100975 |
return evaluateInternal(context, out, namespace, source); |
223 |
|
} catch (StopCommand s) { |
224 |
|
|
225 |
4 |
return true; |
226 |
|
} catch (Exception e) { |
227 |
1 |
throw new XWikiVelocityException("Failed to evaluate content with id [" + templateName + "]", e); |
228 |
|
} finally { |
229 |
100962 |
if (StringUtils.isNotEmpty(namespace)) { |
230 |
100931 |
stoppedUsingMacroNamespaceInternal(namespace); |
231 |
|
} |
232 |
|
} |
233 |
|
} |
234 |
|
|
|
|
| 84.4% |
Uncovered Elements: 5 (32) |
Complexity: 8 |
Complexity Density: 0.36 |
|
235 |
100950 |
private boolean evaluateInternal(Context context, Writer out, String namespace, Reader source) throws Exception... |
236 |
|
{ |
237 |
|
|
238 |
|
|
239 |
|
|
240 |
|
|
241 |
|
|
242 |
100955 |
SimpleNode nodeTree = this.rsvc.parse(source, namespace, false); |
243 |
|
|
244 |
100979 |
if (nodeTree != null) { |
245 |
100979 |
InternalContextAdapterImpl ica = |
246 |
100978 |
new InternalContextAdapterImpl(context != null ? context : this.velocityContextFactory.createContext()); |
247 |
100974 |
ica.pushCurrentTemplateName(namespace); |
248 |
100966 |
boolean provideTemplateScope = this.rsvc.getBoolean("template.provide.scope.control", true); |
249 |
100979 |
Object templateScopeMarker = new Object(); |
250 |
100978 |
Scope templateScope = null; |
251 |
100978 |
if (provideTemplateScope) { |
252 |
100979 |
Object previous = ica.get(TEMPLATE_SCOPE_NAME); |
253 |
100977 |
templateScope = new Scope(templateScopeMarker, previous); |
254 |
100976 |
templateScope.put("templateName", namespace); |
255 |
100974 |
ica.put(TEMPLATE_SCOPE_NAME, templateScope); |
256 |
|
} |
257 |
100968 |
try { |
258 |
100968 |
nodeTree.init(ica, this.rsvc); |
259 |
100978 |
nodeTree.render(ica, out); |
260 |
|
} catch (StopCommand stop) { |
261 |
|
|
262 |
|
|
263 |
|
|
264 |
920 |
if (!stop.isFor(templateScopeMarker) && ica.getTemplateNameStack().length > 1) { |
265 |
4 |
throw stop; |
266 |
|
} |
267 |
|
} finally { |
268 |
100956 |
ica.popCurrentTemplateName(); |
269 |
100959 |
if (provideTemplateScope) { |
270 |
100966 |
restoreTemplateScope(ica, templateScope); |
271 |
|
} |
272 |
|
} |
273 |
100959 |
return true; |
274 |
|
} |
275 |
|
|
276 |
0 |
return false; |
277 |
|
} |
278 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
279 |
0 |
@Override... |
280 |
|
public void clearMacroNamespace(String templateName) |
281 |
|
{ |
282 |
0 |
this.rsvc.dumpVMNamespace(toThreadSafeNamespace(templateName)); |
283 |
|
} |
284 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
285 |
33386 |
@Override... |
286 |
|
public void startedUsingMacroNamespace(String namespace) |
287 |
|
{ |
288 |
33387 |
startedUsingMacroNamespaceInternal(toThreadSafeNamespace(namespace)); |
289 |
|
} |
290 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
291 |
134313 |
private void startedUsingMacroNamespaceInternal(String namespace)... |
292 |
|
{ |
293 |
134294 |
Integer count = this.namespaceUsageCount.get(namespace); |
294 |
134314 |
if (count == null) { |
295 |
30866 |
count = Integer.valueOf(0); |
296 |
|
} |
297 |
134310 |
count = count + 1; |
298 |
134317 |
this.namespaceUsageCount.put(namespace, count); |
299 |
|
} |
300 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
301 |
33389 |
@Override... |
302 |
|
public void stoppedUsingMacroNamespace(String namespace) |
303 |
|
{ |
304 |
33389 |
stoppedUsingMacroNamespaceInternal(toThreadSafeNamespace(namespace)); |
305 |
|
} |
306 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
307 |
134325 |
private void stoppedUsingMacroNamespaceInternal(String namespace)... |
308 |
|
{ |
309 |
134319 |
Integer count = this.namespaceUsageCount.get(namespace); |
310 |
134321 |
if (count == null) { |
311 |
|
|
312 |
2 |
this.logger.warn("Wrong usage count for namespace [{}]", namespace); |
313 |
2 |
return; |
314 |
|
} |
315 |
134323 |
count = count - 1; |
316 |
134316 |
if (count <= 0) { |
317 |
30877 |
this.namespaceUsageCount.remove(namespace); |
318 |
30880 |
this.rsvc.dumpVMNamespace(namespace); |
319 |
|
} else { |
320 |
103439 |
this.namespaceUsageCount.put(namespace, count); |
321 |
|
} |
322 |
|
} |
323 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
324 |
110 |
@Override... |
325 |
|
public void init(RuntimeServices runtimeServices) |
326 |
|
{ |
327 |
|
|
328 |
|
|
329 |
|
|
330 |
|
|
331 |
|
|
332 |
110 |
this.rsvc = runtimeServices; |
333 |
|
} |
334 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
335 |
270821 |
@Override... |
336 |
|
public Logger getLogger() |
337 |
|
{ |
338 |
270820 |
return this.logger; |
339 |
|
} |
340 |
|
} |