1 46 package org.codehaus.groovy.bsf; 47 48 import groovy.lang.Closure; 49 import groovy.lang.GroovyShell; 50 import org.apache.bsf.BSFDeclaredBean; 51 import org.apache.bsf.BSFException; 52 import org.apache.bsf.BSFManager; 53 import org.apache.bsf.util.BSFEngineImpl; 54 import org.apache.bsf.util.BSFFunctions; 55 import org.codehaus.groovy.runtime.InvokerHelper; 56 57 import java.util.Vector ; 58 59 67 public class GroovyEngine extends BSFEngineImpl { 68 private static final String [] EMPTY_ARGS = { 69 }; 70 71 protected GroovyShell shell; 72 73 81 private String convertToValidJavaClassname(String inName) { 82 if (inName == null || inName.equals("")) { 83 return "_"; 84 } 85 StringBuffer output = new StringBuffer (inName.length()); 86 boolean firstChar = true; 87 for (int i = 0; i < inName.length(); ++i) { 88 char ch = inName.charAt(i); 89 if (firstChar && !Character.isJavaIdentifierStart(ch)) { 90 ch = '_'; 91 } else if (!firstChar 92 && !(Character.isJavaIdentifierPart(ch) || ch == '.')) { 93 ch = '_'; 94 } 95 firstChar = (ch == '.'); 96 output.append(ch); 97 } 98 return output.toString(); 99 } 100 101 104 public Object apply(java.lang.String source, 105 int lineNo, 106 int columnNo, 107 Object funcBody, 108 Vector paramNames, 109 Vector arguments) 110 throws BSFException { 111 112 Object object = eval(source, lineNo, columnNo, funcBody); 113 if (object instanceof Closure) { 114 116 117 Closure closure = (Closure) object; 118 return closure.call(arguments.toArray()); 119 } 120 return object; 121 } 122 123 126 public Object call(Object object, String method, Object [] args) throws BSFException { 127 return InvokerHelper.invokeMethod(object, method, args); 128 } 129 130 133 public void declareBean(BSFDeclaredBean bean) throws BSFException { 134 shell.setVariable(bean.name, bean.bean); 136 } 137 138 141 public Object eval(String source, int lineNo, int columnNo, Object script) throws BSFException { 142 try { 143 source = convertToValidJavaClassname(source); 144 Object result = getEvalShell().evaluate(script.toString(), source); 145 return result; 146 } catch (Exception e) { 147 throw new BSFException(BSFException.REASON_EXECUTION_ERROR, "exception from Groovy: " + e, e); 148 } 149 } 150 151 154 public void exec(String source, int lineNo, int columnNo, Object script) throws BSFException { 155 try { 156 source = convertToValidJavaClassname(source); 158 getEvalShell().evaluate(script.toString(), source); 159 } catch (Exception e) { 161 throw new BSFException(BSFException.REASON_EXECUTION_ERROR, "exception from Groovy: " + e, e); 162 } 163 } 164 165 168 public void initialize(BSFManager mgr, String lang, Vector declaredBeans) throws BSFException { 169 super.initialize(mgr, lang, declaredBeans); 170 171 shell = new GroovyShell(mgr.getClassLoader()); 173 174 shell.setVariable("bsf", new BSFFunctions(mgr, this)); 176 177 int size = declaredBeans.size(); 178 for (int i = 0; i < size; i++) { 179 declareBean((BSFDeclaredBean) declaredBeans.elementAt(i)); 180 } 181 } 182 183 186 public void undeclareBean(BSFDeclaredBean bean) throws BSFException { 187 shell.setVariable(bean.name, null); 188 } 189 190 193 protected GroovyShell getEvalShell() { 194 return new GroovyShell(shell); 195 } 196 } 197 | Popular Tags |