View Javadoc

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      protected final Scriptable scriptable = new Scriptable();
30  
31      public ScriptTransformer()
32      {
33          super();
34      }
35  
36      public Object transform(Object src, String encoding, UMOEventContext context) throws TransformerException
37      {
38          Bindings bindings = this.getScriptEngine().createBindings();
39          this.populateBindings(bindings, context, src);
40  
41          try
42          {
43              return scriptable.runScript(bindings);
44          }
45          catch (ScriptException e)
46          {
47              throw new TransformerException(this, e);
48          }
49      }
50  
51      protected void populateBindings(Bindings namespace, UMOEventContext context, Object src)
52      {
53          namespace.put("eventContext", context);
54          namespace.put("message", context.getMessage());
55          namespace.put("src", src);
56          namespace.put("transformerNamespace", namespace);
57          namespace.put("log", logger);
58      }
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          super.initialise();
69          scriptable.initialise();
70      }
71  
72      public ScriptEngine getScriptEngine()
73      {
74          return scriptable.getScriptEngine();
75      }
76  
77      public void setScriptEngine(ScriptEngine scriptEngine)
78      {
79          scriptable.setScriptEngine(scriptEngine);
80      }
81  
82      public CompiledScript getCompiledScript()
83      {
84          return scriptable.getCompiledScript();
85      }
86  
87      public void setCompiledScript(CompiledScript compiledScript)
88      {
89          scriptable.setCompiledScript(compiledScript);
90      }
91  
92      public String getScriptText()
93      {
94          return scriptable.getScriptText();
95      }
96  
97      public void setScriptText(String scriptText)
98      {
99          scriptable.setScriptText(scriptText);
100     }
101 
102     public String getScriptFile()
103     {
104         return scriptable.getScriptFile();
105     }
106 
107     public void setScriptFile(String scriptFile)
108     {
109         scriptable.setScriptFile(scriptFile);
110     }
111 
112     public void setScriptEngineName(String scriptEngineName)
113     {
114         scriptable.setScriptEngineName(scriptEngineName);
115     }
116 
117     public String getScriptEngineName()
118     {
119         return scriptable.getScriptEngineName();
120     }
121 
122 }