//----------------------------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.FileNotFoundException;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.huihoo.workflow.rules.*;
import org.huihoo.workflow.rules.Script;
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.Options;
import org.huihoo.workflow.rules.config.ScriptCompilationContext;
import org.huihoo.workflow.runtime.WorkflowWork;
import org.huihoo.workflow.xpdl.WorkflowTransition;
/**
* @author reic
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class ScriptWrapper
{
// Logger
private static Log log = LogFactory.getLog(ScriptWrapper.class);
private Script theScript;
private Class scriptClass;
private ScriptContext scriptContext;
private WorkflowTransition workflowTransition;
private ScriptCompilationContext ctxt;
private Options options;
private boolean firstTime = true;
private boolean reload = true;
private int tripCount;
/*
* ScriptScriptWrapper for WFS scripts.
*/
public ScriptWrapper(
ScriptContext scriptContext,
WorkflowTransition workflowTransition,
Options options,
ScriptRuntimeContext rctxt)
throws ScriptException
{
this.scriptContext = scriptContext;
this.workflowTransition = workflowTransition;
this.options = options;
ctxt =
new ScriptCompilationContext(workflowTransition, options, scriptContext, this,rctxt);
}
public ScriptCompilationContext getServletEngineContext()
{
return ctxt;
}
public void setReload(boolean reload)
{
this.reload = reload;
}
public Script getServlet() throws ScriptException, IOException, FileNotFoundException
{
if (reload)
{
synchronized (this)
{
// Synchronizing on jsw enables simultaneous loading
// of different pages, but not the same script.
if (reload)
{
// This is to maintain the original protocol.
destroy();
try
{
scriptClass = ctxt.load();
theScript = (Script) scriptClass.newInstance();
}
catch (Exception ex1)
{
throw new ScriptException(ex1);
}
theScript.init(scriptContext, workflowTransition);
firstTime = false;
reload = false;
}
}
}
return theScript;
}
public ScriptContext getServletContext()
{
return this.scriptContext;
}
public int incTripCount()
{
return tripCount++;
}
public int decTripCount()
{
return tripCount--;
}
public boolean evalute(WorkflowWork workflowWork, WorkflowTransition workflowTransition,RuntimeContext paramContext)
throws ScriptException
{
try
{
if (ctxt.isRemoved())
{
throw new ScriptException("condition not found");
}
if (firstTime)
{
synchronized (this)
{
ctxt.compile();
}
}
if (reload)
{
getServlet();
}
return theScript.evalute(workflowWork,paramContext);
}
catch (Exception ex)
{
ScriptException newEx=new ScriptException(ex);
throw newEx;
}
}
public void destroy()
{
if (theScript != null)
{
theScript.destroy();
}
}
}