1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.internal.render.groovy

File ParseGroovyFromString.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

12
33
8
2
147
101
18
0.55
4.12
4
2.25

Classes

Class Line # Actions
ParseGroovyFromString 47 29 0% 14 44
0.00%
ParseGroovyFromString.CachedGroovyClass 125 4 0% 4 9
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Helper used to implement {@link com.xpn.xwiki.XWiki#parseGroovyFromString(String, XWikiContext)}.
41    *
42    * @version $Id: a92e6b94048f392fccda68a34677485a8c92c4d1 $
43    * @since 7.1M1
44    */
45    @Component(roles = ParseGroovyFromString.class)
46    @Singleton
 
47    public class ParseGroovyFromString
48    {
49    @Inject
50    private CacheManager cacheManager;
51   
52    private Cache<CachedGroovyClass> classCache;
53   
 
54  0 toggle public void flushCache()
55    {
56  0 if (this.classCache != null) {
57  0 this.classCache.dispose();
58    }
59   
60  0 this.classCache = null;
61    }
62   
 
63  0 toggle 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   
 
77  0 toggle 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   
 
89  0 toggle 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   
 
99  0 toggle 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   
 
125    private class CachedGroovyClass implements DisposableCacheValue
126    {
127    protected Class<?> cl;
128   
 
129  0 toggle CachedGroovyClass(Class<?> cl)
130    {
131  0 this.cl = cl;
132    }
133   
 
134  0 toggle public Class<?> getGroovyClass()
135    {
136  0 return this.cl;
137    }
138   
 
139  0 toggle @Override
140    public void dispose() throws Exception
141    {
142  0 if (this.cl != null) {
143  0 InvokerHelper.removeClass(this.cl);
144    }
145    }
146    }
147    }