Coverage Report - org.webslinger.bsf.groovy.GroovyEngine
 
Classes in this File Line Coverage Branch Coverage Complexity
GroovyEngine
86%
18/21
100%
3/3
0
GroovyEngine$1
100%
2/2
N/A
0
GroovyEngine$2
50%
3/6
N/A
0
 
 1  
 package org.webslinger.bsf.groovy;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.concurrent.Callable;
 5  
 
 6  
 import org.webslinger.bsf.CompilingLanguageEngineImpl;
 7  
 import org.webslinger.bsf.LanguageEngineInfo;
 8  
 
 9  
 import groovy.lang.Closure;
 10  
 import org.codehaus.groovy.runtime.GroovyCategorySupport;
 11  
 
 12  
 public class GroovyEngine extends CompilingLanguageEngineImpl<Object, Class<? extends Object>> {
 13  3
     public GroovyEngine() {
 14  3
         compiler = new GroovyCompiler(this);
 15  3
     }
 16  
 
 17  
     public LanguageEngineInfo getLanguageEngineInfo() {
 18  0
         return GroovyInfo.INSTANCE;
 19  
     }
 20  
 
 21  
     public Object apply(Object body, final Object compiled, final Object[] args) throws Exception {
 22  3
         Callable callable = new Callable() {
 23  
             public Object call() throws Exception {
 24  3
                 return ((GroovyApply) compiled).apply(getManager(), args);
 25  
             }
 26  
         };
 27  3
         return run(body, callable);
 28  
     }
 29  
 
 30  
     protected Object run(Object body, final Callable callable) throws Exception {
 31  3
         String categoryNames = (String) getVFSDelegate().getAttribute(body, "groovy-categories");
 32  3
         if (categoryNames == null) return callable.call();
 33  2
         String[] names = categoryNames.split("[, ]");
 34  2
         ArrayList<Class> categories = new ArrayList<Class>(names.length);
 35  2
         ClassLoader loader = getManager().getClassLoader();
 36  4
         for (String name : names) {
 37  2
             categories.add(loader.loadClass(name));
 38  
         }
 39  2
         final Exception[] thrownException = new Exception[] { null };
 40  2
         final Object[] result = new Object[] { null };
 41  2
         Closure closure = new Closure(this) {
 42  
             public Object call() {
 43  
                 try {
 44  2
                     result[0] = callable.call();
 45  2
                     return null;
 46  0
                 } catch (Exception e) {
 47  0
                     thrownException[0] = e;
 48  0
                     return null;
 49  
                 }
 50  
             }
 51  
         };
 52  
         try {
 53  2
             GroovyCategorySupport.use(categories, closure);
 54  2
             return result[0];
 55  
         } finally {
 56  2
             if (thrownException[0] != null) throw thrownException[0];
 57  
         }
 58  
     }
 59  
 
 60  
     public Object eval(Object body, Object compiled) throws Exception {
 61  0
         throw new UnsupportedOperationException();
 62  
         //return ((GroovyCompiler.Eval) compiled).eval();
 63  
     }
 64  
 
 65  
     public void exec(Object body, Object compiled) throws Exception {
 66  0
         throw new UnsupportedOperationException();
 67  
         //((GroovyCompiler.Exec) compiled).exec();
 68  
     }
 69  
 }