1 18 package org.apache.tools.ant.util; 19 20 import java.io.BufferedReader ; 21 import java.io.File ; 22 import java.io.FileReader ; 23 import java.io.IOException ; 24 25 import org.apache.tools.ant.BuildException; 26 import org.apache.tools.ant.ProjectComponent; 27 import org.apache.tools.ant.Project; 28 29 import java.util.Map ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 33 39 public abstract class ScriptRunnerBase { 40 41 private boolean keepEngine = false; 42 43 44 private String language; 45 46 47 private String script = ""; 48 49 50 private Project project; 51 52 53 private ClassLoader scriptLoader; 54 55 56 private Map beans = new HashMap (); 57 58 64 public void addBeans(Map dictionary) { 65 for (Iterator i = dictionary.keySet().iterator(); i.hasNext();) { 66 String key = (String ) i.next(); 67 try { 68 Object val = dictionary.get(key); 69 addBean(key, val); 70 } catch (BuildException ex) { 71 } 76 } 77 } 78 79 85 public void addBean(String key, Object bean) { 86 boolean isValid = key.length() > 0 87 && Character.isJavaIdentifierStart(key.charAt(0)); 88 89 for (int i = 1; isValid && i < key.length(); i++) { 90 isValid = Character.isJavaIdentifierPart(key.charAt(i)); 91 } 92 93 if (isValid) { 94 beans.put(key, bean); 95 } 96 } 97 98 102 protected Map getBeans() { 103 return beans; 104 } 105 106 111 public abstract void executeScript(String execName); 112 113 119 public abstract Object evaluateScript(String execName); 120 121 127 public abstract boolean supportsLanguage(); 128 129 134 public abstract String getManagerName(); 135 136 140 public void setLanguage(String language) { 141 this.language = language; 142 } 143 144 148 public String getLanguage() { 149 return language; 150 } 151 152 156 public void setScriptClassLoader(ClassLoader classLoader) { 157 this.scriptLoader = classLoader; 158 } 159 160 164 protected ClassLoader getScriptClassLoader() { 165 return scriptLoader; 166 } 167 168 172 public void setKeepEngine(boolean keepEngine) { 173 this.keepEngine = keepEngine; 174 } 175 176 180 public boolean getKeepEngine() { 181 return keepEngine; 182 } 183 184 188 public void setSrc(File file) { 189 if (!file.exists()) { 190 throw new BuildException("file " + file.getPath() + " not found."); 191 } 192 BufferedReader in = null; 193 try { 194 in = new BufferedReader (new FileReader (file)); 195 script += FileUtils.readFully(in); 196 } catch (IOException ex) { 197 throw new BuildException(ex); 198 } finally { 199 FileUtils.close(in); 200 } 201 } 202 203 208 public void addText(String text) { 209 this.script += text; 210 } 211 212 216 public String getScript() { 217 return script; 218 } 219 220 223 public void clearScript() { 224 this.script = ""; 225 } 226 227 231 public void setProject(Project project) { 232 this.project = project; 233 } 234 235 239 public Project getProject() { 240 return project; 241 } 242 243 249 public void bindToComponent(ProjectComponent component) { 250 project = component.getProject(); 251 addBeans(project.getProperties()); 252 addBeans(project.getUserProperties()); 253 addBeans(project.getTargets()); 254 addBeans(project.getReferences()); 255 addBean("project", project); 256 addBean("self", component); 257 } 258 259 264 public void bindToComponentMinimum(ProjectComponent component) { 265 project = component.getProject(); 266 addBean("project", project); 267 addBean("self", component); 268 } 269 270 274 protected void checkLanguage() { 275 if (language == null) { 276 throw new BuildException( 277 "script language must be specified"); 278 } 279 } 280 281 286 protected ClassLoader replaceContextLoader() { 287 ClassLoader origContextClassLoader = 288 Thread.currentThread().getContextClassLoader(); 289 if (getScriptClassLoader() == null) { 290 setScriptClassLoader(getClass().getClassLoader()); 291 } 292 Thread.currentThread().setContextClassLoader(getScriptClassLoader()); 293 return origContextClassLoader; 294 } 295 296 302 protected void restoreContextLoader(ClassLoader origLoader) { 303 Thread.currentThread().setContextClassLoader( 304 origLoader); 305 } 306 307 } 308 | Popular Tags |