KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > transformers > script > ScriptTransformer


1 /*
2  * $Id: ScriptTransformer.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.transformers.script;
12
13 import javax.script.CompiledScript;
14 import javax.script.Namespace;
15 import javax.script.ScriptEngine;
16 import javax.script.ScriptException;
17
18 import org.mule.components.script.jsr223.Scriptable;
19 import org.mule.transformers.AbstractEventAwareTransformer;
20 import org.mule.umo.UMOEventContext;
21 import org.mule.umo.lifecycle.InitialisationException;
22 import org.mule.umo.transformer.TransformerException;
23
24 /**
25  * Runs a script to perform transformation on an object.
26  */

27 public class ScriptTransformer extends AbstractEventAwareTransformer
28 {
29     /**
30      * Serial version
31      */

32     private static final long serialVersionUID = -2384663903730064892L;
33
34     protected Scriptable scriptable;
35
36     public ScriptTransformer()
37     {
38         scriptable = new Scriptable();
39     }
40
41     public Object JavaDoc transform(Object JavaDoc src, String JavaDoc encoding, UMOEventContext context) throws TransformerException
42     {
43         Namespace ns = getScriptEngine().createNamespace();
44         populateNamespace(ns, context, src);
45
46         try
47         {
48             return scriptable.runScript(ns);
49         }
50         catch (ScriptException e)
51         {
52             throw new TransformerException(this, e);
53         }
54     }
55
56     protected void populateNamespace(Namespace namespace, UMOEventContext context, Object JavaDoc src)
57     {
58         namespace.put("context", context);
59         namespace.put("message", context.getMessage());
60         namespace.put("src", src);
61         namespace.put("transformertNamespace", namespace);
62         namespace.put("log", logger);
63     }
64
65     /**
66      * Template method were deriving classes can do any initialisation after the
67      * properties have been set on this transformer
68      *
69      * @throws org.mule.umo.lifecycle.InitialisationException
70      */

71     public void initialise() throws InitialisationException
72     {
73         super.initialise();
74         scriptable.initialise();
75     }
76
77     public ScriptEngine getScriptEngine()
78     {
79         return scriptable.getScriptEngine();
80     }
81
82     public void setScriptEngine(ScriptEngine scriptEngine)
83     {
84         scriptable.setScriptEngine(scriptEngine);
85     }
86
87     public CompiledScript getCompiledScript()
88     {
89         return scriptable.getCompiledScript();
90     }
91
92     public void setCompiledScript(CompiledScript compiledScript)
93     {
94         scriptable.setCompiledScript(compiledScript);
95     }
96
97     public String JavaDoc getScriptText()
98     {
99         return scriptable.getScriptText();
100     }
101
102     public void setScriptText(String JavaDoc scriptText)
103     {
104         scriptable.setScriptText(scriptText);
105     }
106
107     public String JavaDoc getScriptFile()
108     {
109         return scriptable.getScriptFile();
110     }
111
112     public void setScriptFile(String JavaDoc scriptFile)
113     {
114         scriptable.setScriptFile(scriptFile);
115     }
116
117     public void setScriptEngineName(String JavaDoc scriptEngineName)
118     {
119         scriptable.setScriptEngineName(scriptEngineName);
120     }
121
122     public String JavaDoc getScriptEngineName()
123     {
124         return scriptable.getScriptEngineName();
125     }
126
127     Scriptable getScriptable()
128     {
129         return scriptable;
130     }
131
132     void setScriptable(Scriptable scriptable)
133     {
134         this.scriptable = scriptable;
135     }
136
137     /*
138      * (non-Javadoc)
139      *
140      * @see java.lang.Object#clone()
141      */

142     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc
143     {
144         ScriptTransformer trans = (ScriptTransformer)super.clone();
145         trans.setScriptable(scriptable);
146         return trans;
147     }
148
149 }
150
Popular Tags