1 18 package org.apache.tools.ant.util.optional; 19 20 import org.apache.bsf.BSFException; 21 import org.apache.bsf.BSFManager; 22 import org.apache.bsf.BSFEngine; 23 24 25 import java.util.Iterator ; 26 import java.util.Hashtable ; 27 28 import org.apache.tools.ant.BuildException; 29 import org.apache.tools.ant.Project; 30 31 import org.apache.tools.ant.util.ReflectUtil; 32 import org.apache.tools.ant.util.ScriptRunnerBase; 33 34 38 public class ScriptRunner extends ScriptRunnerBase { 39 static { 42 BSFManager.registerScriptingEngine( 43 "groovy", 44 "org.codehaus.groovy.bsf.GroovyEngine", 45 new String [] {"groovy", "gy"}); 46 } 47 48 private BSFEngine engine; 49 private BSFManager manager; 50 51 55 public String getManagerName() { 56 return "bsf"; 57 } 58 59 63 public boolean supportsLanguage() { 64 Hashtable table = (Hashtable ) ReflectUtil.getField( 65 new BSFManager(), "registeredEngines"); 66 String engineClassName = (String ) table.get(getLanguage()); 67 if (engineClassName == null) { 68 getProject().log( 69 "This is no BSF engine class for language '" 70 + getLanguage() + "'", 71 Project.MSG_VERBOSE); 72 return false; 73 } 74 try { 75 getScriptClassLoader().loadClass(engineClassName); 76 return true; 77 } catch (Throwable ex) { 78 getProject().log( 79 "unable to create BSF engine class for language '" 80 + getLanguage() + "'", 81 ex, 82 Project.MSG_VERBOSE); 83 return false; 84 } 85 } 86 87 95 public void executeScript(String execName) throws BuildException { 96 checkLanguage(); 97 ClassLoader origLoader = replaceContextLoader(); 98 try { 99 BSFManager m = createManager(); 100 declareBeans(m); 101 if (engine == null) { 103 m.exec(getLanguage(), execName, 0, 0, getScript()); 104 } else { 105 engine.exec(execName, 0, 0, getScript()); 106 } 107 } catch (BSFException be) { 108 throwBuildException(be); 109 } finally { 110 restoreContextLoader(origLoader); 111 } 112 } 113 114 122 public Object evaluateScript(String execName) 123 throws BuildException { 124 checkLanguage(); 125 ClassLoader origLoader = replaceContextLoader(); 126 try { 127 BSFManager m = createManager(); 128 declareBeans(m); 129 if (engine == null) { 131 return m.eval(getLanguage(), execName, 0, 0, getScript()); 132 } else { 133 return engine.eval(execName, 0, 0, getScript()); 134 } 135 } catch (BSFException be) { 136 throwBuildException(be); 137 return null; 139 } finally { 140 restoreContextLoader(origLoader); 141 } 142 } 143 144 149 private void throwBuildException(BSFException be) { 150 Throwable t = be; 151 Throwable te = be.getTargetException(); 152 if (te != null) { 153 if (te instanceof BuildException) { 154 throw (BuildException) te; 155 } else { 156 t = te; 157 } 158 } 159 throw new BuildException(t); 160 } 161 162 private void declareBeans(BSFManager m) throws BSFException { 163 for (Iterator i = getBeans().keySet().iterator(); i.hasNext();) { 164 String key = (String ) i.next(); 165 Object value = getBeans().get(key); 166 if (value != null) { 167 m.declareBean(key, value, value.getClass()); 168 } else { 169 m.undeclareBean(key); 174 } 175 } 176 } 177 178 private BSFManager createManager() throws BSFException { 179 if (manager != null) { 180 return manager; 181 } 182 BSFManager m = new BSFManager(); 183 m.setClassLoader(getScriptClassLoader()); 184 if (getKeepEngine()) { 185 BSFEngine e = manager.loadScriptingEngine(getLanguage()); 186 this.manager = m; 187 this.engine = e; 188 } 189 return m; 190 } 191 } 192 | Popular Tags |