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

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.huihoo.willow.Globals;
import org.huihoo.willow.startup.Willow;

/**
 * @author reic
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
/*  
 <context-param>    
		<param-name>WILLOW_HOME</param-name>
		<param-value>/usr/local/reic</param-value>
	</context-param> 
		
	<context-param>    
		<param-name>WILLOW_CONFIG</param-name>
		<param-value>willow-server.xml</param-value>
	</context-param> 
	
	<context-param>    
		<param-name>WILLOW_STORE</param-name>
		<param-value>willow-store.properties</param-value>
	</context-param> 

	<context-param>    
		<param-name>WILLOW_DATABASE</param-name>
		<param-value>willow-database.xml</param-value>
	</context-param>
					   
	<servlet>
		<servlet-name>WillowServlet</servlet-name>
		<servlet-class>org.huihoo.willow.deploy.WillowServlet</servlet-class>
		<init-param>
			<param-name>WILLOW_HOME</param-name>
			<param-value>/usr/local/reic</param-value>
		</init-param>		
		<init-param>
			<param-name>WILLOW_CONFIG</param-name>
			<param-value>willow-server.xml</param-value>
		</init-param>
		<init-param>
			<param-name>WILLOW_STORE</param-name>
			<param-value>willow-store.properties</param-value>
		</init-param>
		<init-param>
			<param-name>WILLOW_DATABASE</param-name>
			<param-value>willow-database.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	The servlet init-parameter values will override those of the
	context-parameters.
*/

public class WillowServlet extends HttpServlet
{
	public void init() throws ServletException
	{
		String configFilename    = null;
		String storeFilename     = null;
		String databaseFilename  = null;
		String willowHome        = null;
		
		ClassLoader servletClassLoader = Thread.currentThread().getContextClassLoader();

		try
		{
			/*Make sure that we got a useful classloader; if we can not
			even load ourselves, then at least use the classloader that
			loaded us.*/
			servletClassLoader.loadClass(this.getClass().getName());
		}
		catch (ClassNotFoundException e)
		{
			servletClassLoader = getClass().getClassLoader();
		}

		if (servletClassLoader == null)
		{
			/*This is needed because some containers use hashtable to store
			servlet attributes and therefore they can't be null.
			If Class.forName is passed in null as the classloader, then it
			seems to default to the system class loader, so this should be ok.*/
			servletClassLoader = ClassLoader.getSystemClassLoader();
		}
		ServletConfig servletConfig = getServletConfig();
		ServletContext servletContext = servletConfig.getServletContext();

		String tempStr = null;
		
		//----------------------------------------------------------------
		// WILLOW_HOME Property Setting (mandatory)
		//----------------------------------------------------------------
		tempStr=servletConfig.getInitParameter(Constants.WILLOW_HOME);
		if (tempStr == null)
		{
			tempStr = servletContext.getInitParameter(Constants.WILLOW_HOME);
		}
		
		if(tempStr!=null)
		{
			willowHome=tempStr;
			File tempFile=new File(tempStr);
			if(!tempFile.isAbsolute())
			{
				willowHome=new File(servletContext.getRealPath("/"),tempStr).getAbsolutePath();
			}
		}
		else
		{
			willowHome=servletContext.getRealPath(Constants.DEFAULT_WILLOW_HOME);
		}
		
		System.setProperty(Globals.PROPS_WILLOW_HOME,willowHome);
		
		//----------------------------------------------------------------
		//  WILLOW_CONFIG Property Setting (mandatory)
		//----------------------------------------------------------------
		tempStr=servletConfig.getInitParameter(Constants.WILLOW_CONFIG);
		if (tempStr == null)
		{
			tempStr = servletContext.getInitParameter(Constants.WILLOW_CONFIG);
		}
		
		if(tempStr!=null)
		{
			configFilename=tempStr;
			File tempFile=new File(tempStr);
			if(!tempFile.isAbsolute())
			{
				configFilename=new File(servletContext.getRealPath("/"),tempStr).getAbsolutePath();
			}
		}
		else
		{
			configFilename=servletContext.getRealPath(Constants.DEFAULT_WILLOW_CONFIG);
		}
		
		File configFile=new File(configFilename);
		if(!configFile.exists() || !configFile.isFile())
		{
			throw new ServletException("can not find the willow config file : "+configFile);
		}
		//----------------------------------------------------------------
		//  WILLOW_STORE Property Setting (optional,may be use workflow.xml)
		//----------------------------------------------------------------
		tempStr=servletConfig.getInitParameter(Constants.WILLOW_STORE);
		if (tempStr == null)
		{
			tempStr = servletContext.getInitParameter(Constants.WILLOW_STORE);
		}
		
		if(tempStr!=null)
		{
			storeFilename=tempStr;
			File tempFile=new File(tempStr);
			if(!tempFile.isAbsolute())
			{
				storeFilename=new File(servletContext.getRealPath("/"),tempStr).getAbsolutePath();
			}
		}
		else
		{
			storeFilename=servletContext.getRealPath(Constants.DEFAULT_WILLOW_STORE);
		}
		
		File storeFile=new File(storeFilename);
		if(storeFile.exists()  && configFile.isFile())
		{
			System.setProperty(Constants.PROPS_WILLOW_STORE,storeFile.getAbsolutePath());
		}

		//----------------------------------------------------------------
		//  WILLOW_STORE Property Setting (optional)
		//----------------------------------------------------------------
		tempStr=servletConfig.getInitParameter(Constants.WILLOW_DATABASE);
		if (tempStr == null)
		{
			tempStr = servletContext.getInitParameter(Constants.WILLOW_DATABASE);
		}
		
		if(tempStr!=null)
		{
			databaseFilename=tempStr;
			File tempFile=new File(tempStr);
			if(!tempFile.isAbsolute())
			{
				databaseFilename=new File(servletContext.getRealPath("/"),tempStr).getAbsolutePath();
			}
		}
		else
		{
			databaseFilename=servletContext.getRealPath(Constants.DEFAULT_WILLOW_DATABASE);
		}
		
		File databaseFile=new File(databaseFilename);
		if(databaseFile.exists() && databaseFile.isFile())
		{
			System.setProperty(Constants.PROPS_WILLOW_DATABASE,databaseFile.getAbsolutePath());
		}

		//----------------------------------------------------------------
		//  Startup willow workflow engine
		//----------------------------------------------------------------
		String[] args={"-config",configFilename,"start"};	
		Willow.main(args);	
	}
	
  public void doGet (HttpServletRequest req, HttpServletResponse res)
  throws ServletException, IOException 
  {
    res.setContentType("text/html");
  
    PrintWriter out = res.getWriter ();
  
    out.println("<html><head><title>WILLOW WorkflowService Engine Initializer</title></head>");
    out.println ("<body><h1>WILLOW WorkflowService Engine Initializer</h1>");
    out.println ("<p>Sorry, I don't speak via HTTP GET/POST </p>");
    out.println ("</body></html>");
  }
  
  public void doPost (HttpServletRequest req, HttpServletResponse res)
  throws ServletException, IOException 
  {
    doGet(req,res);
  }
}
