package com.jaeksoft.searchlib;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URISyntaxException;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;

import com.github.jankroken.commandline.CommandLineParser;
import com.github.jankroken.commandline.OptionStyle;
import com.github.jankroken.commandline.annotations.LongSwitch;
import com.github.jankroken.commandline.annotations.Option;
import com.github.jankroken.commandline.annotations.ShortSwitch;
import com.github.jankroken.commandline.annotations.SingleArgument;
import com.github.jankroken.commandline.annotations.Toggle;
import com.jaeksoft.searchlib.util.FileUtils;
import com.jaeksoft.searchlib.util.ThreadUtils;
import com.jaeksoft.searchlib.web.StartStopListener;

public class Server {

	private final Tomcat tomcat;

	private Server(Arguments arguments) {
		File baseDir = new File(arguments.extractDirectory == null ? "server" : arguments.extractDirectory);
		if (baseDir.exists())
			if (arguments.resetExtract && baseDir.isDirectory())
				FileUtils.deleteDirectoryQuietly(baseDir);
		if (!baseDir.exists())
			baseDir.mkdir();
		tomcat = new Tomcat();
		tomcat.noDefaultWebXmlPath();
		tomcat.setPort(arguments.httpPort == null ? 9090 : arguments.httpPort);
		tomcat.setBaseDir(baseDir.getAbsolutePath());
		tomcat.getHost().setAppBase(baseDir.getAbsolutePath());
		tomcat.getHost().setAutoDeploy(true);
		tomcat.getHost().setDeployOnStartup(true);
		tomcat.getConnector().setURIEncoding(arguments.uriEncoding == null ? "UTF-8" : arguments.uriEncoding);
	}

	public static class Arguments {

		private String extractDirectory = null;
		private Integer httpPort = null;
		private String uriEncoding = null;
		private boolean resetExtract = false;

		@Option
		@LongSwitch("extractDirectory")
		@ShortSwitch("e")
		@SingleArgument
		public void setExtractDirectory(String extractDirectory) {
			this.extractDirectory = extractDirectory;
		}

		@Option
		@LongSwitch("httpPort")
		@ShortSwitch("p")
		@SingleArgument
		public void setHttpPort(String port) {
			this.httpPort = port == null ? null : Integer.parseInt(port);
		}

		@Option
		@LongSwitch("uriEncoding")
		@ShortSwitch("u")
		@SingleArgument
		public void setUriEncoding(String uriEncoding) {
			this.uriEncoding = uriEncoding;
		}

		@Option
		@LongSwitch("resetExtract")
		@ShortSwitch("r")
		@Toggle(true)
		public void setResetExtract(boolean resetExtract) {
			this.resetExtract = resetExtract;
		}

	}

	private void start(boolean await) throws IOException, URISyntaxException {
		final File srcFile = new File(
				Server.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
		if (!srcFile.exists())
			throw new IOException("The jar file or classes directory has not been found: " + srcFile);
		final File webFile;
		if (srcFile.isFile()) {
			webFile = new File(srcFile.getParent(), FilenameUtils.removeExtension(srcFile.getName()) + ".war");
			if (!webFile.exists() || !webFile.isFile())
				throw new IOException("The war file has not been found: " + webFile);
		} else {
			webFile = new File("src/main/webapp");
			if (!webFile.exists() && !webFile.isDirectory())
				throw new IOException("The web content directory has not been found: " + webFile);
		}

		try {
			tomcat.start();
			System.out.println("Tomcat started on " + tomcat.getHost());
		} catch (LifecycleException e) {
			System.err.println("Tomcat could not be started.");
		}
		System.out.println("Loading WebApplication: " + webFile);
		tomcat.addWebapp(tomcat.getHost(), StringUtils.EMPTY, webFile.getAbsolutePath());

		ThreadUtils.waitUntil(120, new StartStopListener.StartedWaitInterface());
		if (await)
			tomcat.getServer().await();
	}

	public static void start(String[] args, boolean await) throws IllegalAccessException, InstantiationException,
			InvocationTargetException, IOException, URISyntaxException {
		if (args == null)
			args = new String[] {};
		Arguments arguments = CommandLineParser.parse(Arguments.class, args, OptionStyle.SIMPLE);
		Server server = new Server(arguments);
		server.start(await);
	}

	public static void main(String[] args) throws IllegalAccessException, InstantiationException,
			InvocationTargetException, IOException, URISyntaxException {
		start(args, true);
	}
}

@@ -37,892 +37,629 @@
 ort 
-java.io.File;%0Aimport java.io.IOException;%0Aimport java.lang.reflect.InvocationTargetException;%0Aimport java.net.URISyntaxException;%0A%0Aimport org.apache.catalina.LifecycleException;%0Aimport org.apache.catalina.startup.Tomcat;%0Aimport org.apache.commons.io.FilenameUtils;%0Aimport org.apache.commons.lang.StringUtils;%0A%0Aimport com.github.jankroken.commandline.CommandLineParser;%0Aimport com.github.jankroken.commandline.OptionStyle;%0Aimport com.github.jankroken.commandline.annotations.LongSwitch;%0Aimport com.github.jankroken.commandline.annotations.Option;%0Aimport com.github.jankroken.commandline.annotations.ShortSwitch;%0Aimport com.github.jankroken.commandline.annotations.SingleArgument;%0Aimport com.github.jankroken.commandline.annotations.Toggle;%0Aimport com.jaeksoft.searchlib.util.FileUtils;%0Aimport com.jaeksoft.searchlib.util.ThreadUtils;%0Aimport com.jaeksoft.searchlib.web.StartStopListener
+com.github.jankroken.commandline.CommandLineParser;%0Aimport com.github.jankroken.commandline.OptionStyle;%0Aimport com.github.jankroken.commandline.annotations.*;%0Aimport com.jaeksoft.searchlib.util.FileUtils;%0Aimport com.jaeksoft.searchlib.util.ThreadUtils;%0Aimport com.jaeksoft.searchlib.web.StartStopListener;%0Aimport org.apache.catalina.LifecycleException;%0Aimport org.apache.catalina.startup.Tomcat;%0Aimport org.apache.commons.io.FilenameUtils;%0Aimport org.apache.commons.lang.StringUtils;%0A%0Aimport java.io.File;%0Aimport java.io.IOException;%0Aimport java.lang.reflect.InvocationTargetException;%0Aimport java.net.URISyntaxException
 ;%0A%0Ap

Move to intellij