//----------------------------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.script;

import java.io.File;
import java.util.Enumeration;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.huihoo.workflow.server.WorkflowConfig;
import org.huihoo.workflow.server.WorkflowContext;
import org.huihoo.workflow.util.StringManager;

public class EmbeddedScriptOptions implements Options
{

	// Logger
	private static Log log = LogFactory.getLog(EmbeddedScriptOptions.class);

	private Properties settings = new Properties();

	/**
	 * Do you want to keep the generated Java files around?
	 */
	private boolean keepGenerated = true;

	/**
	 * Do we want to include debugging information in the class file?
	 */
	private boolean classDebugInfo = true;

	/**
	 * Background compile thread check interval in seconds.
	 */
	private int checkInterval = 300;

	/**
	 * Is Rules being used in development mode?
	 */
	private boolean development = true;

	/**
	 * WFS reloading check ?
	 */
	private boolean reloading = true;

	/**
	 * What classpath should I use while compiling generated scripts?
	 */
	private String classpath = null;

	/**
	 * Compiler to use.
	 */
	private String javaCompiler = null;
	
	/**
	 * I want to see my generated servlets. Which directory are they
	 * in?
	 */
	private File scratchDir;

	/**
	 * What is my script file dir? TemplateContext path,
	 * must be start with '/'
	 * 
	 * e.g. /WEB-INF/script
	 */
	private String scriptDir;

	/**
	 * Java platform encoding to generate the rule script.
	 */
	private String javaEncoding = "UTF8";

	/**
	 * The string manager for this package.
	 */
	protected static StringManager sm = StringManager.getManager(Constants.RESOURCE_BUNDLE_BASE);

	public String getProperty(String name)
	{
		return settings.getProperty(name);
	}

	public void setProperty(String name, String value)
	{
		if (name != null && value != null)
		{
			settings.setProperty(name, value);
		}
	}

	/**
	 * Are we keeping generated code around?
	 */
	public boolean getKeepGenerated()
	{
		return keepGenerated;
	}

	/**
	 * Should class files be compiled with debug information?
	 */
	public boolean getClassDebugInfo()
	{
		return classDebugInfo;
	}

	/**
	 * Background RULE compile thread check intervall
	 */
	public int getCheckInterval()
	{
		return checkInterval;
	}

	/**
	 * Is RULE being used in development mode?
	 */
	public boolean getDevelopment()
	{
		return development;
	}

	/**
	 * RULE reloading check ?
	 */
	public boolean getReloading()
	{
		return reloading;
	}

	/**
	 * What is my scratch dir?
	 */
	public File getScratchDir()
	{
		return scratchDir;
	}

	/**
	 * What is my scratch dir?
	 */
	public String getScriptDir()
	{
		return scriptDir;
	}

	/**
	 * What classpath should I use while compiling the servlets
	 * generated from JAVA files?
	 */
	public String getClassPath()
	{
		return classpath;
	}

	/**
	 * Compiler to use.
	 */
	public String getJavaCompiler()
	{
		return javaCompiler;
	}
	
	public String getJavaEncoding()
	{
		return javaEncoding;
	}

	/**
	 * Create an EmbeddedServletOptions object using data available from
	 * WorkflowConfig and WorkflowContext. 
	 */
	public EmbeddedScriptOptions(WorkflowConfig config, WorkflowContext context)
	{

		Enumeration enum = config.getInitParameterNames();
		while (enum != null && enum.hasMoreElements())
		{
			String k = (String) enum.nextElement();
			String v = config.getInitParameter(k);
			setProperty(k, v);
		}

		//		enum = context.getScriptParameterNames();
		//		while (enum != null && enum.hasMoreElements())
		//		{
		//			String k = (String) enum.nextElement();
		//			String v = context(k);
		//			setProperty(k, v);
		//		}

		// quick hack        
		String keepgen = config.getInitParameter("keepgenerated");
		if (keepgen != null)
		{
			if (keepgen.equalsIgnoreCase("true"))
			{
				this.keepGenerated = true;
			}
			else if (keepgen.equalsIgnoreCase("false"))
			{
				this.keepGenerated = false;
			}
			else
			{
				log.warn(sm.getString("script.warning.keepgen"));
			}
		}

		String debugInfo = config.getInitParameter("classdebuginfo");
		if (debugInfo != null)
		{
			if (debugInfo.equalsIgnoreCase("true"))
			{
				this.classDebugInfo = true;
			}
			else if (debugInfo.equalsIgnoreCase("false"))
			{
				this.classDebugInfo = false;
			}
			else
			{
				log.warn(sm.getString("script.warning.classDebugInfo"));
			}
		}

		String checkInterval = config.getInitParameter("checkInterval");
		if (checkInterval != null)
		{
			try
			{
				this.checkInterval = Integer.parseInt(checkInterval);
				if (this.checkInterval == 0)
				{
					this.checkInterval = 300;
					log.warn(sm.getString("script.warning.checkInterval"));
				}
			}
			catch (NumberFormatException ex)
			{
				log.warn(sm.getString("script.warning.checkInterval"));
			}
		}

		String development = config.getInitParameter("development");
		if (development != null)
		{
			if (development.equalsIgnoreCase("true"))
			{
				this.development = true;
			}
			else if (development.equalsIgnoreCase("false"))
			{
				this.development = false;
			}
			else
			{
				if (log.isWarnEnabled())
				{
					log.warn(sm.getString("script.warning.development"));
				}
			}
		}
		String reloading = config.getInitParameter("reloading");
		if (reloading != null)
		{
			if (reloading.equalsIgnoreCase("true"))
			{
				this.reloading = true;
			}
			else if (reloading.equalsIgnoreCase("false"))
			{
				this.reloading = false;
			}
			else
			{
				log.warn(sm.getString("script.warning.reloading"));
			}
		}

		String classpath = config.getInitParameter("classpath");
		if (classpath != null && !"".equals(classpath.trim()))
		{
			this.classpath = classpath;
		}

		this.javaCompiler = config.getInitParameter("javaCompiler");
		
		String scratchdir = config.getInitParameter("scratchdir");
		if (scratchdir != null && !"".equals(scratchdir.trim()))
		{
			scratchDir = new File(scratchdir);
		}
		else
		{
			scratchDir = (File) context.getAttribute(Constants.SCRIPT_SCRATCH_DIR);
			if (scratchDir == null)
			{
				scratchdir = System.getProperty("java.io.tmpdir");
				if (scratchdir != null)
				{
					scratchDir = new File(scratchdir);
				}
			}
		}

		if (this.scratchDir == null)
		{
			log.fatal(sm.getString("script.error.no.scratch.dir"));
		}
		else
		{
			if (!(scratchDir.exists() && scratchDir.canRead() && scratchDir.canWrite() && scratchDir.isDirectory()))
			{
				log.fatal(sm.getString("script.error.bad.scratch.dir", scratchDir.getAbsolutePath()));
			}
		}

		/*
		 * scriptdir
		 */
		String script_dir = config.getInitParameter("scriptdir");
		if (script_dir != null && !"".equals(script_dir.trim()))
		{
			this.scriptDir = script_dir;
		}

		if (this.scriptDir == null)
		{
			this.scriptDir = context.getRealPath("/WEB-INF/script/");
		}

		if (this.scriptDir == null)
		{
			this.scriptDir = "/WEB-INF/script/";
		}

		String javaEncoding = config.getInitParameter("javaEncoding");
		if (javaEncoding != null && !"".equals(javaEncoding.trim()))
		{
			this.javaEncoding = javaEncoding;
		}
	}

}