001: /*
002: * $Id: ScriptTransformer.java 11236 2008-03-06 23:48:23Z 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.transformer;
012:
013: import org.mule.api.MuleMessage;
014: import org.mule.api.lifecycle.InitialisationException;
015: import org.mule.api.lifecycle.LifecycleTransitionResult;
016: import org.mule.api.transformer.TransformerException;
017: import org.mule.module.scripting.component.Scriptable;
018: import org.mule.transformer.AbstractMessageAwareTransformer;
019:
020: import javax.script.Bindings;
021: import javax.script.CompiledScript;
022: import javax.script.ScriptEngine;
023: import javax.script.ScriptException;
024:
025: /**
026: * Runs a script to perform transformation on an object.
027: */
028: public class ScriptTransformer extends AbstractMessageAwareTransformer {
029: protected final Scriptable scriptable = new Scriptable();
030:
031: public ScriptTransformer() {
032: super ();
033: }
034:
035: public Object transform(MuleMessage message, String outputEncoding)
036: throws TransformerException {
037: Bindings bindings = this .getScriptEngine().createBindings();
038: this .populateBindings(bindings, message);
039:
040: try {
041: return scriptable.runScript(bindings);
042: } catch (ScriptException e) {
043: throw new TransformerException(this , e);
044: }
045: }
046:
047: protected void populateBindings(Bindings namespace,
048: MuleMessage message) {
049: namespace.put("message", message);
050: namespace.put("src", message.getPayload());
051: namespace.put("transformerNamespace", namespace);
052: namespace.put("log", logger);
053: }
054:
055: /**
056: * Template method were deriving classes can do any initialisation after the
057: * properties have been set on this transformer
058: *
059: * @throws org.mule.api.lifecycle.InitialisationException
060: */
061: public LifecycleTransitionResult initialise()
062: throws InitialisationException {
063: return LifecycleTransitionResult.initialiseAll(super
064: .initialise(), new LifecycleTransitionResult.Closure() {
065: public LifecycleTransitionResult doContinue()
066: throws InitialisationException {
067: return scriptable.initialise();
068: }
069: });
070: }
071:
072: public ScriptEngine getScriptEngine() {
073: return scriptable.getScriptEngine();
074: }
075:
076: public void setScriptEngine(ScriptEngine scriptEngine) {
077: scriptable.setScriptEngine(scriptEngine);
078: }
079:
080: public CompiledScript getCompiledScript() {
081: return scriptable.getCompiledScript();
082: }
083:
084: public void setCompiledScript(CompiledScript compiledScript) {
085: scriptable.setCompiledScript(compiledScript);
086: }
087:
088: public String getScriptText() {
089: return scriptable.getScriptText();
090: }
091:
092: public void setScriptText(String scriptText) {
093: scriptable.setScriptText(scriptText);
094: }
095:
096: public String getScriptFile() {
097: return scriptable.getScriptFile();
098: }
099:
100: public void setScriptFile(String scriptFile) {
101: scriptable.setScriptFile(scriptFile);
102: }
103:
104: public void setScriptEngineName(String scriptEngineName) {
105: scriptable.setScriptEngineName(scriptEngineName);
106: }
107:
108: public String getScriptEngineName() {
109: return scriptable.getScriptEngineName();
110: }
111:
112: }
|