Coverage Report - org.mule.components.script.jsr223.ScriptMessageBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
ScriptMessageBuilder
49%
23/47
50%
1/2
1.222
 
 1  
 /*
 2  
  * $Id: ScriptMessageBuilder.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.components.script.jsr223;
 12  
 
 13  
 import org.mule.components.builder.AbstractMessageBuilder;
 14  
 import org.mule.components.builder.MessageBuilderException;
 15  
 import org.mule.umo.UMOEventContext;
 16  
 import org.mule.umo.UMOMessage;
 17  
 import org.mule.umo.lifecycle.Initialisable;
 18  
 import org.mule.umo.lifecycle.InitialisationException;
 19  
 
 20  
 import javax.script.Bindings;
 21  
 import javax.script.Compilable;
 22  
 import javax.script.CompiledScript;
 23  
 import javax.script.ScriptEngine;
 24  
 import javax.script.ScriptException;
 25  
 
 26  
 /**
 27  
  * A message builder component that can execute message building as a script.
 28  
  */
 29  
 public class ScriptMessageBuilder extends AbstractMessageBuilder implements Initialisable
 30  
 {
 31  
 
 32  
     /** Delegating script component that actually does the work */
 33  
     protected Scriptable scriptable;
 34  
 
 35  
     public ScriptMessageBuilder()
 36  2
     {
 37  2
         this.scriptable = new Scriptable();
 38  2
     }
 39  
 
 40  
     public Object buildMessage(UMOMessage request, UMOMessage response) throws MessageBuilderException
 41  
     {
 42  4
         Bindings bindings = scriptable.getScriptEngine().createBindings();
 43  4
         populateBindings(bindings, request, response);
 44  4
         Object result = null;
 45  
         try
 46  
         {
 47  4
             result = runScript(bindings);
 48  
         }
 49  0
         catch (ScriptException e)
 50  
         {
 51  0
             throw new MessageBuilderException(response, e);
 52  4
         }
 53  4
         if (result == null)
 54  
         {
 55  0
             throw new IllegalArgumentException("A result payload must be returned from the groovy script");
 56  
         }
 57  4
         return result;
 58  
     }
 59  
 
 60  
     public void initialise() throws InitialisationException {
 61  2
         scriptable.initialise();
 62  2
     }
 63  
 
 64  
     protected void populateBindings(Bindings namespace, UMOMessage request, UMOMessage response)
 65  
     {
 66  4
         namespace.put("request", request);
 67  4
         namespace.put("response", response);
 68  4
         namespace.put("descriptor", descriptor);
 69  4
         namespace.put("componentNamespace", namespace);
 70  4
         namespace.put("log", logger);
 71  4
     }
 72  
 
 73  
     public ScriptEngine getScriptEngine()
 74  
     {
 75  0
         return scriptable.getScriptEngine();
 76  
     }
 77  
 
 78  
     public void setScriptEngine(ScriptEngine scriptEngine)
 79  
     {
 80  0
         scriptable.setScriptEngine(scriptEngine);
 81  0
     }
 82  
 
 83  
     public CompiledScript getCompiledScript()
 84  
     {
 85  0
         return scriptable.getCompiledScript();
 86  
     }
 87  
 
 88  
     public void setCompiledScript(CompiledScript compiledScript)
 89  
     {
 90  0
         scriptable.setCompiledScript(compiledScript);
 91  0
     }
 92  
 
 93  
     public String getScriptText()
 94  
     {
 95  0
         return scriptable.getScriptText();
 96  
     }
 97  
 
 98  
     public void setScriptText(String scriptText)
 99  
     {
 100  4
         scriptable.setScriptText(scriptText);
 101  4
     }
 102  
 
 103  
     public String getScriptFile()
 104  
     {
 105  0
         return scriptable.getScriptFile();
 106  
     }
 107  
 
 108  
     public void setScriptFile(String scriptFile)
 109  
     {
 110  0
         scriptable.setScriptFile(scriptFile);
 111  0
     }
 112  
 
 113  
     public void setScriptEngineName(String scriptEngineName)
 114  
     {
 115  4
         scriptable.setScriptEngineName(scriptEngineName);
 116  4
     }
 117  
 
 118  
     protected void populateBindings(Bindings namespace, UMOEventContext context)
 119  
     {
 120  0
         namespace.put("context", context);
 121  0
         namespace.put("message", context.getMessage());
 122  0
         namespace.put("descriptor", context.getComponentDescriptor());
 123  0
         namespace.put("componentNamespace", namespace);
 124  0
         namespace.put("log", logger);
 125  0
         namespace.put("result", new Object());
 126  0
     }
 127  
 
 128  
     protected void compileScript(Compilable compilable) throws ScriptException
 129  
     {
 130  0
         scriptable.compileScript(compilable);
 131  0
     }
 132  
 
 133  
     protected Object evaluteScript(Bindings namespace) throws ScriptException
 134  
     {
 135  0
         return scriptable.evaluteScript(namespace);
 136  
     }
 137  
 
 138  
     protected Object runScript(Bindings namespace) throws ScriptException
 139  
     {
 140  4
         return scriptable.runScript(namespace);
 141  
     }
 142  
 
 143  
     protected ScriptEngine createScriptEngine()
 144  
     {
 145  0
         return scriptable.createScriptEngine();
 146  
     }
 147  
 
 148  
 }