package com.cronos.admin.tomcat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class WindowsServices {

	public static String GetTomcatServiceName() {
		String os = System.getProperty("os.name").toLowerCase();
		String result = "", candidate1 = "", candidate2 = "";
		if (os.startsWith("windows")) {
			try {
				Process p = Runtime.getRuntime().exec("net start");
				BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
				String text;

				while ((text = in.readLine()) != null) {
					if (text.toLowerCase().contains("tomcat7")) {
						result = text;
						break;
					} else if (text.toLowerCase().contains("tomcat"))
						if (text.toLowerCase().contains("7"))
							candidate1 = text;
						else
							candidate2 = text;

				}

				if (result == "")
					if (candidate1.isEmpty())
						result = candidate2;
					else
						result = candidate1;

				in.close();

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		return result;
	}

}
