/*
 * Created on 2004-8-29
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package org.huihoo.willow.core;

import java.io.File;
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 workflow.WorkflowException;
import workflow.store.factory.XPDLParserFactory;
import workflow.wfmc.xpdl.WorkflowPackage;
import workflow.wfmc.xpdl.parser.XPDLParser;

/**
 * @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(workflow.store.factory.Constants.WORKFLOW_FACTORY_XPDL);
		if (xpdlFactoryClass != null)
		{
			env.put(workflow.store.factory.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 = xpdlParser.parse(new File(xpdlFileBase));
		
		context.addWorkflowPackage(workflowPackage);			
		deployed.put(xpdlFileName,workflowPackage);	
	}

	public void remove(String xpdlFileName) throws WorkflowException
	{
		if(deployed.containsKey(xpdlFileName))
		{
			WorkflowPackage workflowPackage=findDeployedPackage(xpdlFileName);
			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));
		}
	}
}
