1 10 11 package org.mule.components.script.jsr223; 12 13 import javax.script.Namespace; 14 15 import org.mule.MuleManager; 16 import org.mule.umo.UMOEventContext; 17 import org.mule.umo.lifecycle.Callable; 18 import org.mule.umo.lifecycle.InitialisationException; 19 import org.mule.umo.lifecycle.RecoverableException; 20 import org.mule.util.MuleLogger; 21 22 26 public class ScriptComponent extends Scriptable implements Callable 27 { 28 private Namespace namespace; 29 30 public void initialise() throws InitialisationException, RecoverableException 31 { 32 super.initialise(); 33 namespace = getScriptEngine().createNamespace(); 34 } 35 36 public Object onCall(UMOEventContext eventContext) throws Exception 37 { 38 populateNamespace(namespace, eventContext); 39 Object result = runScript(namespace); 40 if (result == null) 41 { 42 result = namespace.get("result"); 43 } 44 return result; 45 } 46 47 protected void populateNamespace(Namespace namespace, UMOEventContext context) 48 { 49 namespace.put("eventContext", context); 50 namespace.put("managementContext", MuleManager.getInstance()); 51 namespace.put("message", context.getMessage()); 52 namespace.put("descriptor", context.getComponentDescriptor()); 53 namespace.put("componentNamespace", this.namespace); 54 namespace.put("log", new MuleLogger(logger)); 55 namespace.put("result", new Object ()); 56 } 57 58 public Namespace getNamespace() 59 { 60 return namespace; 61 } 62 63 } 64 | Popular Tags |