KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > components > script > jsr223 > ScriptMessageBuilder


1 /*
2  * $Id: ScriptMessageBuilder.java 4259 2006-12-14 03:12:07Z aperepel $
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 MuleSource MPL
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 javax.script.Compilable;
14 import javax.script.CompiledScript;
15 import javax.script.Namespace;
16 import javax.script.ScriptEngine;
17 import javax.script.ScriptException;
18
19 import org.mule.components.builder.AbstractMessageBuilder;
20 import org.mule.components.builder.MessageBuilderException;
21 import org.mule.umo.UMOEventContext;
22 import org.mule.umo.UMOMessage;
23 import org.mule.umo.lifecycle.Initialisable;
24 import org.mule.umo.lifecycle.InitialisationException;
25 import org.mule.umo.lifecycle.RecoverableException;
26
27 /**
28  * A message builder component that can execute message building as a script.
29  */

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