1 17 package org.oddjob.script; 18 19 import java.io.ByteArrayOutputStream ; 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 27 import org.oddjob.arooa.ArooaContext; 28 import org.oddjob.arooa.ArooaConstants; 29 import org.oddjob.arooa.ObjectFactory; 30 import org.oddjob.framework.SimpleJob; 31 import org.oddjob.util.IO; 32 import org.oddjob.util.OddjobConfigException; 33 34 93 public class ScriptJob extends SimpleJob { 94 95 101 private transient String language; 102 103 108 private transient String text; 109 110 115 private transient File file; 116 117 124 private transient Map beans; 125 126 131 private transient InputStream input; 132 133 139 private transient ObjectFactory componentFactory; 140 141 147 private transient ObjectFactory valueFactory; 148 149 155 private int result; 156 157 161 public boolean setContext(ArooaContext context) { 162 this.componentFactory = (ObjectFactory) context.get(ArooaConstants.COMPONENT_FACTORY); 163 this.valueFactory = (ObjectFactory) context.get(ArooaConstants.VALUE_FACTORY); 164 return super.setContext(context); 165 } 166 167 171 protected int execute() throws IOException { 172 ScriptRunner runner = new ScriptRunner(); 173 if (language != null) { 174 runner.setLanguage(language); 175 } 176 177 if (text != null) { 178 runner.setScript(text); 179 } 180 else if (file != null) { 181 input = new FileInputStream (file); 182 } 183 else if (input == null) { 184 throw new OddjobConfigException("No script provided!"); 185 } 186 187 if (input != null) { 188 ByteArrayOutputStream script = new ByteArrayOutputStream (); 189 IO.copy(input, script); 190 runner.setScript(script.toString()); 191 } 192 193 if (beans != null) { 194 runner.addBeans(beans); 195 } 196 197 runner.executeScript("ODDJOB"); 198 199 return result; 200 } 201 202 207 public void setLanguage(String language) { 208 this.language = language; 209 } 210 211 216 public String getLanguage() { 217 return language; 218 } 219 220 226 public Object getBeans(String name) { 227 if (beans == null) { 228 return null; 229 } 230 return beans.get(name); 231 } 232 233 239 public void setBeans(String name, Object value) { 240 if (beans == null) { 241 beans = new HashMap (); 242 } 243 logger().debug("Adding bean (" + name 244 + ", [" + value + "]"); 245 beans.put(name, value); 246 } 247 248 253 public File getFile() { 254 return file; 255 } 256 257 262 public void setFile(File file) { 263 this.file = file; 264 } 265 266 271 public InputStream getInput() { 272 return input; 273 } 274 275 280 public void setInput(InputStream input) { 281 this.input = input; 282 } 283 284 289 public String getText() { 290 return text; 291 } 292 293 298 public void setText(String text) { 299 this.text = text; 300 } 301 302 307 public ObjectFactory getComponentFactory() { 308 return componentFactory; 309 } 310 311 316 public int getResult() { 317 return result; 318 } 319 320 325 public void setResult(int result) { 326 this.result = result; 327 } 328 329 334 public ObjectFactory getValueFactory() { 335 return valueFactory; 336 } 337 } 338 | Popular Tags |