package 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 workflow.script.core.ScriptContext;
import workflow.script.util.StringManager;


/*
 * Created on 2004-6-13
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */

/**
 * @author reic
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
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 = 20;

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

	/**
	 * I want to see my generated servlets. Which directory are they
	 * in?
	 */
	private File scratchDir;

	/**
	 * What is my script file dir? scriptContext path,
	 * must be start with '/' and end with '/'
	 * 
	 * e.g. /WEB-INF/script/
	 */
	private String scriptDir;
	
	/**
	 * What classpath should I use while compiling generated servlets?
	 */
	private String classpath = null;

	/**
	 * Compiler to use.
	 */
	private String compiler = null;

	/**
	 * Java platform encoding to generate the WFS servlet.
	 */
	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 JSP compile thread check intervall
	 */
	public int getCheckInterval()
	{
		return checkInterval;
	}

	/**
	 * WFS 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 JSP files?
	 */
	public String getClassPath()
	{
		return classpath;
	}

	/**
	 * Compiler to use.
	 */
	public String getCompiler()
	{
		return compiler;
	}

	public String getJavaEncoding()
	{
		return javaEncoding;
	}


	/**
	 * Create an EmbeddedServletOptions object using data available from
	 * ServletConfig and ScriptContext. 
	 */
	public EmbeddedScriptOptions(ScriptContext context)
	{

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

		enum = context.getInitParameterNames();
		while (enum!=null&& enum.hasMoreElements())
		{
			String k = (String) enum.nextElement();
			String v = context.getInitParameter(k);
			setProperty(k, v);
		}
		
		// quick hack        
		String keepgen = context.getScriptConfig().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 = context.getScriptConfig().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 = context.getScriptConfig().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 reloading = context.getScriptConfig().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 = context.getScriptConfig().getInitParameter("classpath");
		if (classpath != null && !"".equals(classpath.trim()))
		{
			this.classpath = classpath;
		}
		
		/*
		 * scratchdir
		 */
		String dir = context.getScriptConfig().getInitParameter("scratchdir");
		if (dir != null && !"".equals(dir.trim()))
		{
			scratchDir = new File(dir);
		}
		else
		{
			scratchDir = (File) context.getAttribute(Constants.SERVLET_SCRATCH_DIR);
			if (scratchDir == null)
			{
				dir = System.getProperty("java.io.tmpdir");
				if (dir != null)
				{
					scratchDir = new File(dir);
				}
			}
		}
		if (this.scratchDir == null)
		{
			log.fatal(sm.getString("script.error.no.scratch.dir"));
			return;
		}

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

		/*
		 * scriptdir
		 */
		String scriptdir = context.getScriptConfig().getInitParameter("scriptdir");
		if (scriptdir != null && !"".equals(scriptdir.trim()))
		{
			this.scriptDir = scriptdir;
		}
		else
		{
			this.scriptDir = (String) context.getAttribute(Constants.SCRIPT_FILE_DIR);
		}
		if (this.scriptDir == null)
		{
			this.scriptDir="/WEB-INF/script/";
		}
		
		this.compiler = context.getScriptConfig().getInitParameter("compiler");

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

}