//----------------------------BEGIN LICENSE----------------------------
/*
* Willow : the Open Source WorkFlow Project
* Distributable under GNU LGPL license by gun.org
*
* Copyright (C) 2004-2010 huihoo.org
* Copyright (C) 2004-2010 ZosaTapo <dertyang@hotmail.com>
*
* ====================================================================
* Project Homepage : http://www.huihoo.org/willow
* Source Forge : http://sourceforge.net/projects/huihoo
* Mailing list : willow@lists.sourceforge.net
*/
//----------------------------END LICENSE-----------------------------
package org.huihoo.workflow.rules.interpretor;
import java.io.File;
import java.net.URL;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.huihoo.workflow.rules.*;
import org.huihoo.workflow.rules.Constants;
import org.huihoo.workflow.rules.ScriptContext;
import org.huihoo.workflow.rules.ScriptException;
import org.huihoo.workflow.rules.compiler.ScriptRuntimeContext;
import org.huihoo.workflow.rules.config.EmbeddedScriptOptions;
import org.huihoo.workflow.rules.config.Options;
import org.huihoo.workflow.rules.loader.ScriptClassLoader;
import org.huihoo.workflow.rules.util.StringManager;
import org.huihoo.workflow.runtime.WorkflowWork;
import org.huihoo.workflow.xpdl.WorkflowCondition;
import org.huihoo.workflow.xpdl.WorkflowTransition;
import org.huihoo.workflow.xpdl.condition.ScriptCondition;
import org.huihoo.workflow.xpdl.condition.XCDATACondition;
/**
* @author zosatapo
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class JavaInterpretor implements ScriptInterpretor
{
private static Log log = LogFactory.getLog(JavaInterpretor.class);
protected static StringManager sm = StringManager.getManager(Constants.RESOURCE_BUNDLE_BASE);
private ScriptContext scriptContext;
private Options options;
private ScriptRuntimeContext rctxt;
private ScriptClassLoader wfsClassLoader;
public void initialize(ScriptContext scriptContext)
{
this.scriptContext = scriptContext;
}
public ScriptContext getScriptContext()
{
return this.scriptContext;
}
public boolean evalute(
WorkflowWork workflowWork,
WorkflowTransition workflowTransition,
RuntimeContext paramContext)
throws ScriptException
{
return evaluteScriptFile(workflowWork, workflowTransition, paramContext);
}
public void start() throws ScriptException
{
this.options = new EmbeddedScriptOptions(this.scriptContext);
this.rctxt = new ScriptRuntimeContext(this.scriptContext, this.options);
try
{
URL baseUrl = options.getScratchDir().toURL();
this.wfsClassLoader = new ScriptClassLoader(new URL[] { baseUrl }, rctxt.getParentClassLoader());
}
catch (Exception ex)
{
ex.printStackTrace();
wfsClassLoader = null;
}
log.debug(
sm.getString("script.message.scratch.dir.is", options.getScratchDir().toString()));
log.debug(sm.getString("script.message.dont.modify.servlets"));
}
public void stop() throws ScriptException
{
log.debug("JavaInterpretor.stop()");
rctxt.destroy();
}
// -------------------------------------------------------- Private Methods
private boolean evaluteScriptFile(
WorkflowWork workflowWork,
WorkflowTransition workflowTransition,
RuntimeContext paramContext)
throws ScriptException
{
WorkflowCondition condition = workflowTransition.getCondition();
if (condition==null || condition.getContent()==null ||condition.getContent().trim().length()==0)
{
//transition unconditionally
return true;
}
ScriptWrapper wrapper = (ScriptWrapper) rctxt.getScriptWrapper(workflowTransition);
if (wrapper == null)
{
if(condition instanceof ScriptCondition)
{
// First check if the requested script exists, to avoid
// creating unnecessary directories and files.
String sciptURI=condition.getContent();
File scriptFile = new File(options.getScriptDir(),sciptURI);
if (!scriptFile.exists() || !scriptFile.isFile())
{
throw new ScriptException("script not found : "+sciptURI);
}
}
else if(condition instanceof XCDATACondition)
{
}
else
{
throw new ScriptException("unknown condition : "+condition);
}
synchronized (this)
{
wrapper = (ScriptWrapper) rctxt.getScriptWrapper(workflowTransition);
if (wrapper == null)
{
wrapper = new ScriptWrapper(scriptContext,workflowTransition, options, rctxt);
rctxt.addScriptWrapper(workflowTransition, wrapper);
}
}
}
return wrapper.evalute(workflowWork,workflowTransition, paramContext);
}
}