KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: Scriptable.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 java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.InputStreamReader JavaDoc;
16 import java.io.Reader JavaDoc;
17 import java.io.StringReader JavaDoc;
18
19 import javax.script.Compilable;
20 import javax.script.CompiledScript;
21 import javax.script.Namespace;
22 import javax.script.ScriptEngine;
23 import javax.script.ScriptEngineManager;
24 import javax.script.ScriptException;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.mule.config.i18n.Message;
29 import org.mule.config.i18n.Messages;
30 import org.mule.umo.lifecycle.Initialisable;
31 import org.mule.umo.lifecycle.InitialisationException;
32 import org.mule.umo.lifecycle.RecoverableException;
33 import org.mule.util.IOUtils;
34
35 /**
36  * A JSR 223 Script component. Allows any JSR 223 compliant script engines such as
37  * javaScript, Groovy or Rhino to be embedded as Mule components.
38  */

39 public class Scriptable implements Initialisable
40 {
41
42     /**
43      * logger used by this class
44      */

45     protected transient Log logger = LogFactory.getLog(getClass());
46
47     private String JavaDoc scriptText;
48     private String JavaDoc scriptFile;
49     private Reader JavaDoc script;
50
51     private CompiledScript compiledScript;
52     private ScriptEngine scriptEngine;
53     private String JavaDoc scriptEngineName;
54
55     public void initialise() throws InitialisationException, RecoverableException
56     {
57
58         if (scriptEngine == null)
59         {
60             if (compiledScript == null)
61             {
62                 if (scriptEngineName != null)
63                 {
64                     scriptEngine = createScriptEngine();
65                 }
66                 else if (scriptFile != null)
67                 {
68                     int i = scriptFile.lastIndexOf(".");
69                     if (i > -1)
70                     {
71                         setScriptEngineName(scriptFile.substring(i + 1));
72                         logger.info("Script Engine name not set. Defaulting to file extension: "
73                                     + getScriptEngineName());
74                         scriptEngine = createScriptEngine();
75                     }
76                 }
77                 if (scriptEngine == null)
78                 {
79                     throw new InitialisationException(new Message(Messages.PROPERTIES_X_NOT_SET,
80                         "scriptEngine, scriptEngineName, compiledScript"), this);
81                 }
82             }
83             else
84             {
85                 scriptEngine = compiledScript.getEngine();
86             }
87         }
88         if (compiledScript == null)
89         {
90             if (script == null)
91             {
92                 if (scriptText == null && scriptFile == null)
93                 {
94                     throw new InitialisationException(new Message(Messages.PROPERTIES_X_NOT_SET,
95                         "scriptText, scriptFile"), this);
96                 }
97                 else if (scriptText != null)
98                 {
99                     script = new StringReader JavaDoc(scriptText);
100                 }
101                 else
102                 {
103                     InputStream JavaDoc is = null;
104                     try
105                     {
106                         is = IOUtils.getResourceAsStream(scriptFile, getClass());
107                         script = new InputStreamReader JavaDoc(is);
108                     }
109                     catch (IOException JavaDoc e)
110                     {
111                         throw new InitialisationException(new Message(
112                             Messages.CANT_LOAD_X_FROM_CLASSPATH_FILE, scriptFile), e, this);
113                     }
114                 }
115             }
116             try
117             {
118                 compiledScript = compileScript(script);
119             }
120             catch (ScriptException e)
121             {
122                 throw new InitialisationException(e, this);
123             }
124         }
125     }
126
127     public ScriptEngine getScriptEngine()
128     {
129         return scriptEngine;
130     }
131
132     public void setScriptEngine(ScriptEngine scriptEngine)
133     {
134         this.scriptEngine = scriptEngine;
135     }
136
137     public CompiledScript getCompiledScript()
138     {
139         return compiledScript;
140     }
141
142     public void setCompiledScript(CompiledScript compiledScript)
143     {
144         this.compiledScript = compiledScript;
145     }
146
147     public String JavaDoc getScriptText()
148     {
149         return scriptText;
150     }
151
152     public void setScriptText(String JavaDoc scriptText)
153     {
154         this.scriptText = scriptText;
155     }
156
157     public String JavaDoc getScriptFile()
158     {
159         return scriptFile;
160     }
161
162     public void setScriptFile(String JavaDoc scriptFile)
163     {
164         this.scriptFile = scriptFile;
165     }
166
167     public void setScriptEngineName(String JavaDoc scriptEngineName)
168     {
169         this.scriptEngineName = scriptEngineName;
170     }
171
172     public String JavaDoc getScriptEngineName()
173     {
174         return scriptEngineName;
175     }
176
177     protected CompiledScript compileScript(Compilable compilable, Reader JavaDoc scriptReader) throws ScriptException
178     {
179         return compilable.compile(scriptReader);
180     }
181
182     protected CompiledScript compileScript(Reader JavaDoc scriptReader) throws ScriptException
183     {
184         if (scriptEngine instanceof Compilable)
185         {
186             Compilable compilable = (Compilable)scriptEngine;
187             return compileScript(compilable, scriptReader);
188         }
189         return null;
190     }
191
192     protected CompiledScript compileScript(Compilable compilable) throws ScriptException
193     {
194         return compileScript(compilable, script);
195     }
196
197     protected Object JavaDoc evaluteScript(Namespace namespace) throws ScriptException
198     {
199         return scriptEngine.eval(scriptText, namespace);
200     }
201
202     public Object JavaDoc runScript(Namespace namespace) throws ScriptException
203     {
204         Object JavaDoc result = null;
205         if (compiledScript != null)
206         {
207             result = compiledScript.eval(namespace);
208         }
209         else
210         {
211             result = evaluteScript(namespace);
212         }
213         return result;
214     }
215
216     public Object JavaDoc runScript(CompiledScript compiledScript, Namespace namespace) throws ScriptException
217     {
218         Object JavaDoc result = null;
219         if (compiledScript != null)
220         {
221             result = compiledScript.eval(namespace);
222         }
223         return result;
224     }
225
226     protected ScriptEngine createScriptEngine()
227     {
228         ScriptEngineManager manager = new ScriptEngineManager();
229         return manager.getEngineByName(scriptEngineName);
230     }
231 }
232
Popular Tags