View Javadoc

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      {
37          this.scriptable = new Scriptable();
38      }
39  
40      public Object buildMessage(UMOMessage request, UMOMessage response) throws MessageBuilderException
41      {
42          Bindings bindings = scriptable.getScriptEngine().createBindings();
43          populateBindings(bindings, request, response);
44          Object result = null;
45          try
46          {
47              result = runScript(bindings);
48          }
49          catch (ScriptException e)
50          {
51              throw new MessageBuilderException(response, e);
52          }
53          if (result == null)
54          {
55              throw new IllegalArgumentException("A result payload must be returned from the groovy script");
56          }
57          return result;
58      }
59  
60      public void initialise() throws InitialisationException {
61          scriptable.initialise();
62      }
63  
64      protected void populateBindings(Bindings namespace, UMOMessage request, UMOMessage response)
65      {
66          namespace.put("request", request);
67          namespace.put("response", response);
68          namespace.put("descriptor", descriptor);
69          namespace.put("componentNamespace", namespace);
70          namespace.put("log", logger);
71      }
72  
73      public ScriptEngine getScriptEngine()
74      {
75          return scriptable.getScriptEngine();
76      }
77  
78      public void setScriptEngine(ScriptEngine scriptEngine)
79      {
80          scriptable.setScriptEngine(scriptEngine);
81      }
82  
83      public CompiledScript getCompiledScript()
84      {
85          return scriptable.getCompiledScript();
86      }
87  
88      public void setCompiledScript(CompiledScript compiledScript)
89      {
90          scriptable.setCompiledScript(compiledScript);
91      }
92  
93      public String getScriptText()
94      {
95          return scriptable.getScriptText();
96      }
97  
98      public void setScriptText(String scriptText)
99      {
100         scriptable.setScriptText(scriptText);
101     }
102 
103     public String getScriptFile()
104     {
105         return scriptable.getScriptFile();
106     }
107 
108     public void setScriptFile(String scriptFile)
109     {
110         scriptable.setScriptFile(scriptFile);
111     }
112 
113     public void setScriptEngineName(String scriptEngineName)
114     {
115         scriptable.setScriptEngineName(scriptEngineName);
116     }
117 
118     protected void populateBindings(Bindings namespace, UMOEventContext context)
119     {
120         namespace.put("context", context);
121         namespace.put("message", context.getMessage());
122         namespace.put("descriptor", context.getComponentDescriptor());
123         namespace.put("componentNamespace", namespace);
124         namespace.put("log", logger);
125         namespace.put("result", new Object());
126     }
127 
128     protected void compileScript(Compilable compilable) throws ScriptException
129     {
130         scriptable.compileScript(compilable);
131     }
132 
133     protected Object evaluteScript(Bindings namespace) throws ScriptException
134     {
135         return scriptable.evaluteScript(namespace);
136     }
137 
138     protected Object runScript(Bindings namespace) throws ScriptException
139     {
140         return scriptable.runScript(namespace);
141     }
142 
143     protected ScriptEngine createScriptEngine()
144     {
145         return scriptable.createScriptEngine();
146     }
147 
148 }