package com.sysdeo.eclipse.tomcat;

/*
 * (c) Copyright Sysdeo SA 2001, 2002.
 * All Rights Reserved.
 */

import java.io.File;
import java.util.ArrayList;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;


/**
 * See %TOMCAT5_HOME%/bin/catalina.bat
 */
public class Tomcat5Bootstrap extends TomcatBootstrap {

	/**
	 * @see TomcatBootstrap#getClasspath()
	 */
	@Override
	public String[] getClasspath() {
		ArrayList<String> classpath = new ArrayList<>();
		classpath.add(getTomcatDir() + File.separator + "bin" + File.separator + "bootstrap.jar");
		// Add tools.jar JDK file to classpath
		//		String toolsJarLocation = VMLauncherUtility.getVMInstall().getInstallLocation()
		//						+ File.separator + "lib" + File.separator + "tools.jar";
		//		if (new File(toolsJarLocation).exists()) {
		//			classpath.add(toolsJarLocation);
		//		}
		return classpath.toArray(new String[classpath.size()]);
	}

	/**
	 * @see TomcatBootstrap#getMainClass()
	 */
	@Override
	public String getMainClass() {
		return "org.apache.catalina.startup.Bootstrap";
	}

	/**
	 * @see TomcatBootstrap#getStartCommand()
	 */
	@Override
	public String getStartCommand() {
		return "start";
	}

	/**
	 * @see TomcatBootstrap#getStopCommand()
	 */
	@Override
	public String getStopCommand() {
		return "stop";
	}

	/**
	 * @see com.sysdeo.eclipse.tomcat.TomcatBootstrap#getPrgArgs(java.lang.String)
	 */
	@Override
	public String[] getPrgArgs(final String command) {
		String[] prgArgs;
		if (TomcatLauncherPlugin.getConfigMode().equals(TomcatPluginResources.SERVERXML_MODE)) {
			prgArgs = new String[3];
			prgArgs[0] = "-config";
			prgArgs[1] = "\"" + TomcatLauncherPlugin.getConfigFile() + "\"";
			prgArgs[2] = command;
		} else {
			prgArgs = new String[1];
			prgArgs[0] = command;
		}
		return prgArgs;
	}

	/**
	 * @see TomcatBootstrap#getVmArgs()
	 */
	@Override
	public String[] getVmArgs() {
		ArrayList<String> vmArgs = new ArrayList<>();
		vmArgs.add("-Dcatalina.home=\"" + getTomcatDir() + "\"");

		String commonEndorsedDir = getTomcatDir() + File.separator + "common" + File.separator + "endorsed";
		vmArgs.add("-Djava.endorsed.dirs=\"" + commonEndorsedDir + "\"");

		String catalinaBase = getTomcatBase();
		if (catalinaBase.length() == 0) {
			catalinaBase = getTomcatDir();
		}

		vmArgs.add("-Dcatalina.base=\"" + catalinaBase + "\"");
		vmArgs.add("-Djava.io.tmpdir=\"" + catalinaBase + File.separator + "temp\"");

		if (TomcatLauncherPlugin.isSecurityManagerEnabled()) {
			vmArgs.add("-Djava.security.manager");
			String securityPolicyFile = catalinaBase + File.separator + "conf" + File.separator + "catalina.policy";
			vmArgs.add("-Djava.security.policy=\"" + securityPolicyFile + "\"");
		}

		return vmArgs.toArray(new String[vmArgs.size()]);
	}


	/**
	 * @see TomcatBootstrap#getXMLTagAfterContextDefinition()
	 */
	@Override
	public String getXMLTagAfterContextDefinition() {
		return "</Host>";
	}

	/**
	 * @see com.sysdeo.eclipse.tomcat.TomcatBootstrap#getJasperJarPath()
	 */
	@Override
	public IPath getJasperJarPath() {
		return new Path("common").append("lib").append("jasper-runtime.jar");
	}

	/**
	 * @see com.sysdeo.eclipse.tomcat.TomcatBootstrap#getServletJarPath()
	 */
	@Override
	public IPath getServletJarPath() {
		return new Path("common").append("lib").append("servlet-api.jar");
	}

	/**
	 * @see com.sysdeo.eclipse.tomcat.TomcatBootstrap#getJSPJarPath()
	 */
	@Override
	public IPath getJSPJarPath() {
		return new Path("common").append("lib").append("jsp-api.jar");
	}

	/**
	 * @see TomcatBootstrap#getLabel()
	 */
	@Override
	public String getLabel() {
		return "Tomcat 5.x";
	}

	/**
	 * @see com.sysdeo.eclipse.tomcat.TomcatBootstrap#getContextWorkDir(java.lang.String)
	 */
	@Override
	public String getContextWorkDir(final String workFolder) {
		StringBuilder workDir = new StringBuilder("workDir=");
		workDir.append('"');
		workDir.append(workFolder);
		workDir.append('"');
		return workDir.toString();
	}

	/**
	 * @see com.sysdeo.eclipse.tomcat.TomcatBootstrap#getElJarPath()
	 */
	@Override
	public IPath getElJarPath() {
		return null;
	}

	/**
	 * @see com.sysdeo.eclipse.tomcat.TomcatBootstrap#getAnnotationsJarPath()
	 */
	@Override
	public IPath getAnnotationsJarPath() {
		return null;
	}
}
