Coverage Report - org.mule.transformers.script.ScriptTransformer
 
Classes in this File Line Coverage Branch Coverage Complexity
ScriptTransformer
94%
30/32
N/A
1.214
 
 1  
 /*
 2  
  * $Id: ScriptTransformer.java 7963 2007-08-21 08:53:15Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.transformers.script;
 12  
 
 13  
 import org.mule.components.script.jsr223.Scriptable;
 14  
 import org.mule.transformers.AbstractEventAwareTransformer;
 15  
 import org.mule.umo.UMOEventContext;
 16  
 import org.mule.umo.lifecycle.InitialisationException;
 17  
 import org.mule.umo.transformer.TransformerException;
 18  
 
 19  
 import javax.script.Bindings;
 20  
 import javax.script.CompiledScript;
 21  
 import javax.script.ScriptEngine;
 22  
 import javax.script.ScriptException;
 23  
 
 24  
 /**
 25  
  * Runs a script to perform transformation on an object.
 26  
  */
 27  
 public class ScriptTransformer extends AbstractEventAwareTransformer
 28  
 {
 29  22
     protected final Scriptable scriptable = new Scriptable();
 30  
 
 31  
     public ScriptTransformer()
 32  
     {
 33  22
         super();
 34  22
     }
 35  
 
 36  
     public Object transform(Object src, String encoding, UMOEventContext context) throws TransformerException
 37  
     {
 38  12
         Bindings bindings = this.getScriptEngine().createBindings();
 39  12
         this.populateBindings(bindings, context, src);
 40  
 
 41  
         try
 42  
         {
 43  12
             return scriptable.runScript(bindings);
 44  
         }
 45  0
         catch (ScriptException e)
 46  
         {
 47  0
             throw new TransformerException(this, e);
 48  
         }
 49  
     }
 50  
 
 51  
     protected void populateBindings(Bindings namespace, UMOEventContext context, Object src)
 52  
     {
 53  12
         namespace.put("eventContext", context);
 54  12
         namespace.put("message", context.getMessage());
 55  12
         namespace.put("src", src);
 56  12
         namespace.put("transformerNamespace", namespace);
 57  12
         namespace.put("log", logger);
 58  12
     }
 59  
 
 60  
     /**
 61  
      * Template method were deriving classes can do any initialisation after the
 62  
      * properties have been set on this transformer
 63  
      * 
 64  
      * @throws org.mule.umo.lifecycle.InitialisationException
 65  
      */
 66  
     public void initialise() throws InitialisationException
 67  
     {
 68  20
         super.initialise();
 69  20
         scriptable.initialise();
 70  20
     }
 71  
 
 72  
     public ScriptEngine getScriptEngine()
 73  
     {
 74  14
         return scriptable.getScriptEngine();
 75  
     }
 76  
 
 77  
     public void setScriptEngine(ScriptEngine scriptEngine)
 78  
     {
 79  2
         scriptable.setScriptEngine(scriptEngine);
 80  2
     }
 81  
 
 82  
     public CompiledScript getCompiledScript()
 83  
     {
 84  2
         return scriptable.getCompiledScript();
 85  
     }
 86  
 
 87  
     public void setCompiledScript(CompiledScript compiledScript)
 88  
     {
 89  2
         scriptable.setCompiledScript(compiledScript);
 90  2
     }
 91  
 
 92  
     public String getScriptText()
 93  
     {
 94  2
         return scriptable.getScriptText();
 95  
     }
 96  
 
 97  
     public void setScriptText(String scriptText)
 98  
     {
 99  2
         scriptable.setScriptText(scriptText);
 100  2
     }
 101  
 
 102  
     public String getScriptFile()
 103  
     {
 104  2
         return scriptable.getScriptFile();
 105  
     }
 106  
 
 107  
     public void setScriptFile(String scriptFile)
 108  
     {
 109  22
         scriptable.setScriptFile(scriptFile);
 110  22
     }
 111  
 
 112  
     public void setScriptEngineName(String scriptEngineName)
 113  
     {
 114  10
         scriptable.setScriptEngineName(scriptEngineName);
 115  10
     }
 116  
 
 117  
     public String getScriptEngineName()
 118  
     {
 119  2
         return scriptable.getScriptEngineName();
 120  
     }
 121  
 
 122  
 }