001: /*
002: * $Id: Scriptable.java 11195 2008-03-06 04:13:01Z tcarlson $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.module.scripting.component;
012:
013: import org.mule.api.lifecycle.Initialisable;
014: import org.mule.api.lifecycle.InitialisationException;
015: import org.mule.api.lifecycle.LifecycleTransitionResult;
016: import org.mule.config.i18n.CoreMessages;
017: import org.mule.util.IOUtils;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.io.InputStreamReader;
022: import java.io.Reader;
023: import java.io.StringReader;
024:
025: import javax.script.Bindings;
026: import javax.script.Compilable;
027: import javax.script.CompiledScript;
028: import javax.script.ScriptEngine;
029: import javax.script.ScriptEngineManager;
030: import javax.script.ScriptException;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034:
035: /**
036: * A JSR 223 Script service. Allows any JSR 223 compliant script engines such as
037: * javaScript, Groovy or Rhino to be embedded as Mule components.
038: */
039: public class Scriptable implements Initialisable {
040:
041: /**
042: * logger used by this class
043: */
044: protected transient Log logger = LogFactory.getLog(getClass());
045:
046: private String scriptText;
047: private String scriptFile;
048: private Reader script;
049:
050: private CompiledScript compiledScript;
051: private ScriptEngine scriptEngine;
052: private String scriptEngineName;
053:
054: public LifecycleTransitionResult initialise()
055: throws InitialisationException {
056: if (scriptEngine == null) {
057: if (compiledScript == null) {
058: if (scriptEngineName != null) {
059: scriptEngine = createScriptEngineByName(scriptEngineName);
060: } else if (scriptFile != null) {
061: int i = scriptFile.lastIndexOf(".");
062: if (i > -1) {
063: logger
064: .info("Script Engine name not set. Guessing by file extension.");
065: scriptEngine = createScriptEngineByExtension(scriptFile
066: .substring(i + 1));
067: if (scriptEngine != null) {
068: setScriptEngineName(scriptEngine
069: .getFactory().getEngineName());
070: }
071: }
072: }
073: if (scriptEngine == null) {
074: throw new InitialisationException(
075: CoreMessages
076: .propertiesNotSet("scriptEngine, scriptEngineName, compiledScript"),
077: this );
078: }
079: } else {
080: scriptEngine = compiledScript.getEngine();
081: }
082: }
083:
084: if (compiledScript == null) {
085: if (script == null) {
086: if (scriptText == null && scriptFile == null) {
087: throw new InitialisationException(
088: CoreMessages
089: .propertiesNotSet("scriptText, scriptFile"),
090: this );
091: } else if (scriptText != null) {
092: script = new StringReader(scriptText);
093: } else {
094: InputStream is;
095: try {
096: is = IOUtils.getResourceAsStream(scriptFile,
097: getClass());
098: script = new InputStreamReader(is);
099: } catch (IOException e) {
100: throw new InitialisationException(CoreMessages
101: .cannotLoadFromClasspath(scriptFile),
102: e, this );
103: }
104: }
105: }
106: try {
107: compiledScript = compileScript(script);
108: } catch (ScriptException e) {
109: throw new InitialisationException(e, this );
110: }
111: }
112: return LifecycleTransitionResult.OK;
113: }
114:
115: public ScriptEngine getScriptEngine() {
116: return scriptEngine;
117: }
118:
119: public void setScriptEngine(ScriptEngine scriptEngine) {
120: this .scriptEngine = scriptEngine;
121: }
122:
123: public CompiledScript getCompiledScript() {
124: return compiledScript;
125: }
126:
127: public void setCompiledScript(CompiledScript compiledScript) {
128: this .compiledScript = compiledScript;
129: }
130:
131: public String getScriptText() {
132: return scriptText;
133: }
134:
135: public void setScriptText(String scriptText) {
136: this .scriptText = scriptText;
137: }
138:
139: public String getScriptFile() {
140: return scriptFile;
141: }
142:
143: public void setScriptFile(String scriptFile) {
144: this .scriptFile = scriptFile;
145: }
146:
147: public void setScriptEngineName(String scriptEngineName) {
148: this .scriptEngineName = scriptEngineName;
149: }
150:
151: public String getScriptEngineName() {
152: return scriptEngineName;
153: }
154:
155: protected CompiledScript compileScript(Compilable compilable,
156: Reader scriptReader) throws ScriptException {
157: return compilable.compile(scriptReader);
158: }
159:
160: protected CompiledScript compileScript(Reader scriptReader)
161: throws ScriptException {
162: if (scriptEngine instanceof Compilable) {
163: Compilable compilable = (Compilable) scriptEngine;
164: return compileScript(compilable, scriptReader);
165: }
166: return null;
167: }
168:
169: protected CompiledScript compileScript(Compilable compilable)
170: throws ScriptException {
171: return compileScript(compilable, script);
172: }
173:
174: protected Object evaluteScript(Bindings bindings)
175: throws ScriptException {
176: return scriptEngine.eval(scriptText, bindings);
177: }
178:
179: public Object runScript(Bindings bindings) throws ScriptException {
180: Object result;
181: if (compiledScript != null) {
182: result = compiledScript.eval(bindings);
183: } else {
184: result = evaluteScript(bindings);
185: }
186: return result;
187: }
188:
189: public Object runScript(CompiledScript compiledScript,
190: Bindings bindings) throws ScriptException {
191: Object result = null;
192: if (compiledScript != null) {
193: result = compiledScript.eval(bindings);
194: }
195: return result;
196: }
197:
198: protected ScriptEngine createScriptEngineByName(String name) {
199: return new ScriptEngineManager().getEngineByName(name);
200: }
201:
202: protected ScriptEngine createScriptEngineByExtension(String ext) {
203: return new ScriptEngineManager().getEngineByExtension(ext);
204: }
205: }
|