Coverage Report - org.mule.transformers.script.ScriptTransformer
 
Classes in this File Line Coverage Branch Coverage Complexity
ScriptTransformer
0%
0/32
N/A
1.214
 
 1  
 /*
 2  
  * $Id: ScriptTransformer.java 7976 2007-08-21 14:26:13Z 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  0
     protected final Scriptable scriptable = new Scriptable();
 30  
 
 31  
     public ScriptTransformer()
 32  
     {
 33  0
         super();
 34  0
     }
 35  
 
 36  
     public Object transform(Object src, String encoding, UMOEventContext context) throws TransformerException
 37  
     {
 38  0
         Bindings bindings = this.getScriptEngine().createBindings();
 39  0
         this.populateBindings(bindings, context, src);
 40  
 
 41  
         try
 42  
         {
 43  0
             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  0
         namespace.put("eventContext", context);
 54  0
         namespace.put("message", context.getMessage());
 55  0
         namespace.put("src", src);
 56  0
         namespace.put("transformerNamespace", namespace);
 57  0
         namespace.put("log", logger);
 58  0
     }
 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  0
         super.initialise();
 69  0
         scriptable.initialise();
 70  0
     }
 71  
 
 72  
     public ScriptEngine getScriptEngine()
 73  
     {
 74  0
         return scriptable.getScriptEngine();
 75  
     }
 76  
 
 77  
     public void setScriptEngine(ScriptEngine scriptEngine)
 78  
     {
 79  0
         scriptable.setScriptEngine(scriptEngine);
 80  0
     }
 81  
 
 82  
     public CompiledScript getCompiledScript()
 83  
     {
 84  0
         return scriptable.getCompiledScript();
 85  
     }
 86  
 
 87  
     public void setCompiledScript(CompiledScript compiledScript)
 88  
     {
 89  0
         scriptable.setCompiledScript(compiledScript);
 90  0
     }
 91  
 
 92  
     public String getScriptText()
 93  
     {
 94  0
         return scriptable.getScriptText();
 95  
     }
 96  
 
 97  
     public void setScriptText(String scriptText)
 98  
     {
 99  0
         scriptable.setScriptText(scriptText);
 100  0
     }
 101  
 
 102  
     public String getScriptFile()
 103  
     {
 104  0
         return scriptable.getScriptFile();
 105  
     }
 106  
 
 107  
     public void setScriptFile(String scriptFile)
 108  
     {
 109  0
         scriptable.setScriptFile(scriptFile);
 110  0
     }
 111  
 
 112  
     public void setScriptEngineName(String scriptEngineName)
 113  
     {
 114  0
         scriptable.setScriptEngineName(scriptEngineName);
 115  0
     }
 116  
 
 117  
     public String getScriptEngineName()
 118  
     {
 119  0
         return scriptable.getScriptEngineName();
 120  
     }
 121  
 
 122  
 }