//Source file: H:\\temp\\generated\\modTransf\\rules\\core\\ScriptAction.java
package modTransf.rules.core;
import modTransf.engine.RuleContext;
import modTransf.engine.TransformationException;
import javax.script.CompiledScript;
import modTransf.script.CompiledScriptFactory;
import javax.script.ScriptException;
import javax.script.ScriptContext;
import modTransf.engine.EngineException;
/**
* An action executing a script.
*/
public class ScriptAction implements Action, Guard, ActionClause, GuardClause
{
CompiledScript compiledScript;
/**
* Constructor to be used by subclasses.
* Subclasses is responsible to set the script and language.
* @param script
*/
protected ScriptAction()
{
}
/**
* @param script
*/
public ScriptAction(CompiledScript script)
{
this.compiledScript = script;
}
/**
* @param script
*/
public ScriptAction(String script)
throws ScriptException
{
compiledScript = CompiledScriptFactory.compileScript(script);
}
/**
* @param script
* @param language
*/
public ScriptAction(String script, String language)
throws ScriptException
{
compiledScript = CompiledScriptFactory.compileScript(script, language);
}
/**
* Setup the compiled script from the script and language.
* This method is to bee used by subclasses.
* @param script String
* @param language String
* @throws ScriptException
*/
protected void setUpCompiledScript(String script, String language)
throws ScriptException
{
compiledScript = CompiledScriptFactory.compileScript(script, language);
}
/**
* @param request
* @return Object
* @throws ispuml.mdaTransformation.TransformationException
*/
public Object execute(Object parentBean, RuleContext request)
throws TransformationException
{
ScriptContext scriptContext = new RuleContextWrapperForScriptContext(request);
try
{
//System.out.println("ScriptAction.execute()");
return compiledScript.eval(scriptContext);
}
catch(ScriptException ex)
{
throw new TransformationException(ex);
}
}
/**
* isAllowed
*
* @param bean Object
* @param request RuleContext
* @return boolean
*/
public boolean isAllowed(Object bean, RuleContext request)
throws TransformationException
{
//System.out.println("call guard '" + compiledScript +"'.");
ScriptContext scriptContext = new RuleContextWrapperForScriptContext(request);
try
{
Object res = compiledScript.eval(scriptContext);
//System.out.println("guard.isAllowed():'" + res +"'.");
return ((Boolean)res).booleanValue();
}
catch(NullPointerException ex)
{
throw new TransformationException("Expression value must return a boolean (found 'null' for '"
+ compiledScript + "')." );
}
catch(ClassCastException ex)
{
throw new TransformationException("Expression value must return a boolean ("
+ compiledScript + ")." );
}
catch(ScriptException ex)
{
throw new TransformationException(ex);
}
}
/**
* @param context
*/
public void engineStart(RuleContext context)
throws EngineException
{
}
/**
* @param context
*/
public void engineFinish(RuleContext context)
throws EngineException
{
}
/**
* setUpLocalVariables
*
* @param object Object
* @param context RuleContext
*/
public void setUpLocalVariables(Object object, RuleContext context)
{
}
}