//----------------------------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.willow.core;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Hashtable;
import org.huihoo.willow.Context;
import org.huihoo.willow.ContextDeployer;
import org.huihoo.willow.util.StringManager;
import org.huihoo.workflow.WorkflowException;
import org.huihoo.workflow.factory.XPDLParserFactory;
import org.huihoo.workflow.impl.monitor.EventMonitorThread;
import org.huihoo.workflow.xpdl.WorkflowPackage;
import org.huihoo.workflow.xpdl.parser.XPDLParser;
import org.huihoo.workflow.xpdl.parser.XPDLParserTools;
/**
* @author reic
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class StandardContextDeployer implements ContextDeployer
{
private static org.apache.commons.logging.Log log =
org.apache.commons.logging.LogFactory.getLog(StandardContextDeployer.class);
/**
* The names of applications that we have auto-deployed (to avoid
* double deployment attempts).
*/
private HashMap deployed = new HashMap();
//----------------------------------------------------------- Constructors
public StandardContextDeployer()
{
}
/**
* Create a new StandardContextDeployer associated with the specified
* StandardContext.
*
* @param engine The StandardContext we are associated with
*/
public StandardContextDeployer(StandardContext context)
{
super();
this.context = context;
}
/**
* The <code>StandardContext</code> instance we are associated with.
*/
protected StandardContext context = null;
/**
* The string manager for this package.
*/
protected static StringManager sm = StringManager.getManager(Constants.PACKAGE);
public Context getContext()
{
return context;
}
public void setContext(Context context)
{
this.context = (StandardContext) context;
}
/**
* Return the name of the Container with which this EngineDeployer is associated.
*/
public String getName()
{
return (context.getName());
}
public void install(String xpdlFileName, URL xpdlFileURL) throws WorkflowException
{
log.info(sm.getString("standardContext.installing", xpdlFileName, xpdlFileURL.toString()));
Hashtable env = new Hashtable();
String xpdlFactoryClass = context.findFactoryClass(org.huihoo.workflow.xpdl.parser.Constants.WORKFLOW_FACTORY_XPDL);
if (xpdlFactoryClass != null)
{
env.put(org.huihoo.workflow.xpdl.parser.Constants.WORKFLOW_FACTORY_XPDL, xpdlFactoryClass);
}
XPDLParser xpdlParser = XPDLParserFactory.newInstance(env).newXPDLParser();
env.clear();
String xpdlFileBase = xpdlFileURL.toString();
if (xpdlFileBase.startsWith("file://"))
{
xpdlFileBase = xpdlFileBase.substring(7);
}
else if (xpdlFileBase.startsWith("file:"))
{
xpdlFileBase = xpdlFileBase.substring(5);
}
WorkflowPackage workflowPackage;
try
{
workflowPackage = xpdlParser.parse(new File(xpdlFileBase));
}
catch (IOException e)
{
throw new WorkflowException(e);
}
//------------------------------------------------------------
// VERY IMPORTANT CODE SECTION BY ZOSATAPO 2004-10-30
//
// TO Initialize WorkflowProcess and WorkflowActivity Listeners
//------------------------------------------------------------
XPDLParserTools.externalFormat(workflowPackage, context.getLoader().getClassLoader());
String threadName = "EventMonitorThread[" + toString() + " - "+workflowPackage.getName()+"]";
EventMonitorThread eventMonitorThread = new EventMonitorThread(threadName);
eventMonitorThread.setDaemon(true);
context.putEventMonitorThread(workflowPackage,eventMonitorThread);
context.addWorkflowPackage(workflowPackage);
deployed.put(xpdlFileName, workflowPackage);
}
public void remove(String xpdlFileName) throws WorkflowException
{
if (deployed.containsKey(xpdlFileName))
{
WorkflowPackage workflowPackage = findDeployedPackage(xpdlFileName);
EventMonitorThread eventMonitorThread = context.getEventMonitorThread(workflowPackage);
eventMonitorThread.interrupt();
try
{
eventMonitorThread.join();
}
catch (InterruptedException e)
{
;
}
context.removeEventMonitorThread(workflowPackage);
eventMonitorThread = null;
context.reomveWorkflowPackage(workflowPackage.getUUID());
deployed.remove(xpdlFileName);
}
}
public WorkflowPackage findDeployedPackage(String xpdlFileName)
{
if (deployed.containsKey(xpdlFileName))
{
return (WorkflowPackage) deployed.get(xpdlFileName);
}
return null;
}
public String[] findDeployedPackages()
{
synchronized (deployed)
{
String results[] = new String[deployed.size()];
return ((String[]) deployed.keySet().toArray(results));
}
}
}