1 17 package org.columba.core.scripting.interpreter; 18 19 import groovy.lang.GroovyClassLoader; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.lang.reflect.InvocationTargetException ; 24 import java.lang.reflect.Method ; 25 import java.lang.reflect.Modifier ; 26 import java.util.Map ; 27 28 import org.codehaus.groovy.control.CompilationFailedException; 29 import org.columba.core.scripting.model.ColumbaScript; 30 31 34 public class GroovyInterpreter 35 extends ScriptInterpreter 36 { 37 38 private final static String [] EXTENSIONS = new String []{"groovy"}; 39 40 public String getName() 41 { 42 return "Goovy Interpreter"; 43 } 44 45 public String [] getSupportedExtensions() 46 { 47 return EXTENSIONS; 48 } 49 50 public void execute(ColumbaScript script, Map vars) 51 { 52 56 logger.append("Loading groovy script: " + script.getPath()); 57 58 GroovyClassLoader gcl = new GroovyClassLoader(getClass().getClassLoader()); 59 60 File groovyFile = new File (script.getPath()); 61 62 try 63 { 64 Class clazz = gcl.parseClass(groovyFile); 65 66 74 75 Method mainMethod = null; 76 boolean hasMain = true; 77 try 78 { 79 mainMethod = clazz.getDeclaredMethod("main", java.util.Map .class); 80 } 81 catch (NoSuchMethodException e) 82 { 83 hasMain = false; 84 } 85 86 if (!hasMain || 87 (mainMethod.getModifiers() & Modifier.PUBLIC) == 0 || 88 (mainMethod.getModifiers() & Modifier.STATIC) == 0) 89 { 90 logger.append( "Failed to load script", 91 String.format("Groovy script %s doesn't declare a " + 92 "'public static main(Map)' method. Skipping....", 93 script.getPath())); 94 return; 95 } 96 97 mainMethod.invoke(null, vars); 98 99 } 100 catch (CompilationFailedException e) 101 { 102 logger.append(String.format("Failed compilation of %s", script.getPath()), e); 103 } 104 catch (IOException e) 105 { 106 logger.append(String.format("I/O Exception in %s", script.getPath()), e); 107 } 108 catch (IllegalAccessException e) 109 { 110 logger.append(String.format("IllegalAccessException when calling main in %s", 111 script.getPath()), 112 e); 113 } 114 catch (InvocationTargetException e) 115 { 116 logger.append(String.format("InvocationTargetException when calling main in %s", 117 script.getPath()), 118 e); 119 } 120 121 } 122 123 } 124 | Popular Tags |