1 10 11 package org.mule.transformers.script; 12 13 import javax.script.CompiledScript; 14 import javax.script.Namespace; 15 import javax.script.ScriptEngine; 16 import javax.script.ScriptException; 17 18 import org.mule.components.script.jsr223.Scriptable; 19 import org.mule.transformers.AbstractEventAwareTransformer; 20 import org.mule.umo.UMOEventContext; 21 import org.mule.umo.lifecycle.InitialisationException; 22 import org.mule.umo.transformer.TransformerException; 23 24 27 public class ScriptTransformer extends AbstractEventAwareTransformer 28 { 29 32 private static final long serialVersionUID = -2384663903730064892L; 33 34 protected Scriptable scriptable; 35 36 public ScriptTransformer() 37 { 38 scriptable = new Scriptable(); 39 } 40 41 public Object transform(Object src, String encoding, UMOEventContext context) throws TransformerException 42 { 43 Namespace ns = getScriptEngine().createNamespace(); 44 populateNamespace(ns, context, src); 45 46 try 47 { 48 return scriptable.runScript(ns); 49 } 50 catch (ScriptException e) 51 { 52 throw new TransformerException(this, e); 53 } 54 } 55 56 protected void populateNamespace(Namespace namespace, UMOEventContext context, Object src) 57 { 58 namespace.put("context", context); 59 namespace.put("message", context.getMessage()); 60 namespace.put("src", src); 61 namespace.put("transformertNamespace", namespace); 62 namespace.put("log", logger); 63 } 64 65 71 public void initialise() throws InitialisationException 72 { 73 super.initialise(); 74 scriptable.initialise(); 75 } 76 77 public ScriptEngine getScriptEngine() 78 { 79 return scriptable.getScriptEngine(); 80 } 81 82 public void setScriptEngine(ScriptEngine scriptEngine) 83 { 84 scriptable.setScriptEngine(scriptEngine); 85 } 86 87 public CompiledScript getCompiledScript() 88 { 89 return scriptable.getCompiledScript(); 90 } 91 92 public void setCompiledScript(CompiledScript compiledScript) 93 { 94 scriptable.setCompiledScript(compiledScript); 95 } 96 97 public String getScriptText() 98 { 99 return scriptable.getScriptText(); 100 } 101 102 public void setScriptText(String scriptText) 103 { 104 scriptable.setScriptText(scriptText); 105 } 106 107 public String getScriptFile() 108 { 109 return scriptable.getScriptFile(); 110 } 111 112 public void setScriptFile(String scriptFile) 113 { 114 scriptable.setScriptFile(scriptFile); 115 } 116 117 public void setScriptEngineName(String scriptEngineName) 118 { 119 scriptable.setScriptEngineName(scriptEngineName); 120 } 121 122 public String getScriptEngineName() 123 { 124 return scriptable.getScriptEngineName(); 125 } 126 127 Scriptable getScriptable() 128 { 129 return scriptable; 130 } 131 132 void setScriptable(Scriptable scriptable) 133 { 134 this.scriptable = scriptable; 135 } 136 137 142 public Object clone() throws CloneNotSupportedException 143 { 144 ScriptTransformer trans = (ScriptTransformer)super.clone(); 145 trans.setScriptable(scriptable); 146 return trans; 147 } 148 149 } 150 | Popular Tags |