1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.internal.render.groovy; |
21 |
|
|
22 |
|
import javax.inject.Inject; |
23 |
|
import javax.inject.Singleton; |
24 |
|
|
25 |
|
import org.codehaus.groovy.runtime.InvokerHelper; |
26 |
|
import org.xwiki.cache.Cache; |
27 |
|
import org.xwiki.cache.CacheException; |
28 |
|
import org.xwiki.cache.CacheManager; |
29 |
|
import org.xwiki.cache.DisposableCacheValue; |
30 |
|
import org.xwiki.cache.config.CacheConfiguration; |
31 |
|
import org.xwiki.cache.config.LRUCacheConfiguration; |
32 |
|
import org.xwiki.component.annotation.Component; |
33 |
|
|
34 |
|
import com.xpn.xwiki.XWikiContext; |
35 |
|
import com.xpn.xwiki.XWikiException; |
36 |
|
|
37 |
|
import groovy.lang.GroovyClassLoader; |
38 |
|
|
39 |
|
|
40 |
|
@link |
41 |
|
|
42 |
|
@version |
43 |
|
@since |
44 |
|
|
45 |
|
@Component(roles = ParseGroovyFromString.class) |
46 |
|
@Singleton |
|
|
| 0% |
Uncovered Elements: 44 (44) |
Complexity: 14 |
Complexity Density: 0.48 |
|
47 |
|
public class ParseGroovyFromString |
48 |
|
{ |
49 |
|
@Inject |
50 |
|
private CacheManager cacheManager; |
51 |
|
|
52 |
|
private Cache<CachedGroovyClass> classCache; |
53 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
54 |
0 |
public void flushCache()... |
55 |
|
{ |
56 |
0 |
if (this.classCache != null) { |
57 |
0 |
this.classCache.dispose(); |
58 |
|
} |
59 |
|
|
60 |
0 |
this.classCache = null; |
61 |
|
} |
62 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
63 |
0 |
private void initCache(XWikiContext xcontext) throws XWikiException... |
64 |
|
{ |
65 |
0 |
int classCacheSize = 100; |
66 |
0 |
try { |
67 |
0 |
String capacity = xcontext.getWiki().Param("xwiki.render.groovy.classcache.capacity"); |
68 |
0 |
if (capacity != null) { |
69 |
0 |
classCacheSize = Integer.parseInt(capacity); |
70 |
|
} |
71 |
|
} catch (Exception e) { |
72 |
|
} |
73 |
|
|
74 |
0 |
initCache(classCacheSize, xcontext); |
75 |
|
} |
76 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
77 |
0 |
private void initCache(int iClassCapacity, XWikiContext context) throws XWikiException... |
78 |
|
{ |
79 |
0 |
try { |
80 |
0 |
CacheConfiguration configuration = new LRUCacheConfiguration("xwiki.groovy.class", iClassCapacity); |
81 |
|
|
82 |
0 |
this.classCache = this.cacheManager.createNewLocalCache(configuration); |
83 |
|
} catch (CacheException e) { |
84 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_CACHE, XWikiException.ERROR_CACHE_INITIALIZING, |
85 |
|
"Failed to initilize caches", e); |
86 |
|
} |
87 |
|
} |
88 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
89 |
0 |
private void prepareCache(XWikiContext context)... |
90 |
|
{ |
91 |
0 |
try { |
92 |
0 |
if (this.classCache == null) { |
93 |
0 |
initCache(context); |
94 |
|
} |
95 |
|
} catch (Exception e) { |
96 |
|
} |
97 |
|
} |
98 |
|
|
|
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 4 |
Complexity Density: 0.31 |
|
99 |
0 |
public Object parseGroovyFromString(String script, XWikiContext context) throws XWikiException... |
100 |
|
{ |
101 |
0 |
prepareCache(context); |
102 |
|
|
103 |
0 |
ClassLoader parentClassLoader = (ClassLoader) context.get("parentclassloader"); |
104 |
0 |
try { |
105 |
0 |
CachedGroovyClass cgc = this.classCache.get(script); |
106 |
0 |
Class<?> gc; |
107 |
|
|
108 |
0 |
if (cgc == null) { |
109 |
0 |
GroovyClassLoader gcl = |
110 |
0 |
(parentClassLoader == null) ? new GroovyClassLoader() : new GroovyClassLoader(parentClassLoader); |
111 |
0 |
gc = gcl.parseClass(script); |
112 |
0 |
cgc = new CachedGroovyClass(gc); |
113 |
0 |
this.classCache.set(script, cgc); |
114 |
|
} else { |
115 |
0 |
gc = cgc.getGroovyClass(); |
116 |
|
} |
117 |
|
|
118 |
0 |
return gc.newInstance(); |
119 |
|
} catch (Exception e) { |
120 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_GROOVY, |
121 |
|
XWikiException.ERROR_XWIKI_GROOVY_COMPILE_FAILED, "Failed compiling groovy script", e); |
122 |
|
} |
123 |
|
} |
124 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 4 |
Complexity Density: 1 |
|
125 |
|
private class CachedGroovyClass implements DisposableCacheValue |
126 |
|
{ |
127 |
|
protected Class<?> cl; |
128 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
129 |
0 |
CachedGroovyClass(Class<?> cl)... |
130 |
|
{ |
131 |
0 |
this.cl = cl; |
132 |
|
} |
133 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
134 |
0 |
public Class<?> getGroovyClass()... |
135 |
|
{ |
136 |
0 |
return this.cl; |
137 |
|
} |
138 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
139 |
0 |
@Override... |
140 |
|
public void dispose() throws Exception |
141 |
|
{ |
142 |
0 |
if (this.cl != null) { |
143 |
0 |
InvokerHelper.removeClass(this.cl); |
144 |
|
} |
145 |
|
} |
146 |
|
} |
147 |
|
} |