import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLDecoder;
import java.util.StringTokenizer;

public class ConfigRefresher {

	private static String[] apps = { "Backend", "AppProps", "NDM" };
	
	private static final int TOTAL_APPS = 3;

	public static void main(String[] args) {

		try {

			System.out

					.println("This application refreshes the config file(s) within an application by invoking the ReloadProperties\n"

							+ "servlet on all the JVMs running on the given hostname. If a JVM is not up on a certain port (by design or\n"

							+ "otherwise), it logs a connection exception and moves on \n");

			System.out

					.println("Enter the application # to refresh - 0 [Backend(backend props)] 1 [AppProperties (application props)] 2 [NDMInput(ndm-libraries)] \n");

			byte[] buffer = new byte[100];

			int number = -1;

			int len = System.in.read(buffer);

			try {

				number = Integer.parseInt(new String(buffer, 0, len).trim());

			} catch (NumberFormatException nfe) {

				System.out.println("Invalid application # - " + buffer);
				System.exit(-1);

			}

			if (number < -1 || number > TOTAL_APPS) {

				System.out.println("Invalid application # - "

				+ new String(buffer, 0, len));

				System.exit(-1);

			}

			String app_name = apps[number];

			System.out.println("Enter the Environment name in UpperCase like DEV, SIT, PITESIT, UAT, PITEUAT, PREPROD, PROD \n");

			len = System.in.read(buffer);

			StringTokenizer stk = new StringTokenizer(
					new String(buffer, 0, len).trim(), ",");

			while (stk.hasMoreTokens()) {

				String env = stk.nextToken().trim();

				new ConfigRefresher().refreshProperties(app_name,
						env);
			}

		} catch (Exception e) {

			System.out.println("Got exception - " + e.getMessage());
			e.printStackTrace();

		}

	}

	private void refreshProperties(String app_name, String envname) {

		String[] ports = { "8080" };	
		String[] DEV = { "dev-vgentcs03"};
		String[] SIT = { "sit-vgentcs03"};
		String[] PITESIT = { "sit-vgentcs04"};
		String[] UAT = { "uat-vgentcs01","uat-vgentcs02"};
		String[] PITEUAT = { "uat-vgentcs03", "uat-vgentcs04"};
		String[] PREPROD = { "tpp-vgentcs01","tpp-vgentcs02","tpp-vgentcs03","tpp-vgentcs04","tpp-vgentcs05","tpp-vgentcs06","tpp-vgentcs10","tpp-vgentcs11","tpp-vgentcs12"};
		String[] PROD = { "prd-vgentcs01","prd-vgentcs02","prd-vgentcs03", "prd-vgentcs04","prd-vgentcs05","prd-vgentcs06","drs-vgentcs01","drs-vgentcs02","drs-vgentcs03","drs-vgentcs04","drs-vgentcs05","drs-vgentcs06"};

		for (int i = 0; i < ports.length; i++) {
			if (envname.equalsIgnoreCase("DEV")){
				if (app_name.equals("Backend")) {		
				invokeURL("http://" + DEV[0] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");
				}  			
				else if (app_name.equals("AppProps")) {

				invokeURL("http://" + DEV[0] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
				}			
				else {
				invokeURL("http://" + DEV[0] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
				}
			}
			if (envname.equalsIgnoreCase("SIT")){
				if (app_name.equals("Backend")) {				

					invokeURL("http://" + SIT[0] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");					

				}  			
				else if (app_name.equals("AppProps")) {

					invokeURL("http://" + SIT[0] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");

				}			
				else {
					invokeURL("http://" + SIT[0] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					
				}
			}
			if (envname.equalsIgnoreCase("PITESIT")){
				if (app_name.equals("Backend")) {				

					invokeURL("http://" + PITESIT[0] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");					

				}  			
				else if (app_name.equals("AppProps")) {

					invokeURL("http://" + PITESIT[0] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");

				}			
				else {
					invokeURL("http://" + PITESIT[0] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					
				}
			}
			if (envname.equalsIgnoreCase("UAT")){
				if (app_name.equals("Backend")) {				

					invokeURL("http://" + UAT[0] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");
					invokeURL("http://" + UAT[1] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");
				}  			
				else if (app_name.equals("AppProps")) {

					invokeURL("http://" + UAT[0] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + UAT[1] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
				}			
				else {
					invokeURL("http://" + UAT[0] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + UAT[1] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");
					
				}
			}
			if (envname.equalsIgnoreCase("PITEUAT")){
				if (app_name.equals("Backend")) {				

					invokeURL("http://" + PITEUAT[0] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");
					invokeURL("http://" + PITEUAT[1] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");
				}  			
				else if (app_name.equals("AppProps")) {

					invokeURL("http://" + PITEUAT[0] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PITEUAT[1] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
				}			
				else {
					invokeURL("http://" + PITEUAT[0] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PITEUAT[1] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");
					
				}
			}
			if (envname.equalsIgnoreCase("PREPROD")){
				if (app_name.equals("Backend")) {				

					invokeURL("http://" + PREPROD[0] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");
					invokeURL("http://" + PREPROD[1] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");
					invokeURL("http://" + PREPROD[2] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");
					invokeURL("http://" + PREPROD[3] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");
					invokeURL("http://" + PREPROD[4] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");
					invokeURL("http://" + PREPROD[5] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");
					invokeURL("http://" + PREPROD[6] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");
					invokeURL("http://" + PREPROD[7] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");
					invokeURL("http://" + PREPROD[8] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");
				}  			
				else if (app_name.equals("AppProps")) {

					invokeURL("http://" + PREPROD[0] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PREPROD[1] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PREPROD[2] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PREPROD[3] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PREPROD[4] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PREPROD[5] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PREPROD[6] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PREPROD[7] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PREPROD[8] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
				}			
				else {
					invokeURL("http://" + PREPROD[0] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PREPROD[1] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");
					invokeURL("http://" + PREPROD[2] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PREPROD[3] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PREPROD[4] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PREPROD[5] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PREPROD[6] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PREPROD[7] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PREPROD[8] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					
				}
			}
			if (envname.equalsIgnoreCase("PROD")){
				if (app_name.equals("Backend")) {				

					invokeURL("http://" + PROD[0] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");
					invokeURL("http://" + PROD[1] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");
					invokeURL("http://" + PROD[2] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");
					invokeURL("http://" + PROD[3] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");
					invokeURL("http://" + PROD[4] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");
					invokeURL("http://" + PROD[5] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");
					invokeURL("http://" + PROD[6] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");
					invokeURL("http://" + PROD[7] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");
					invokeURL("http://" + PROD[8] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");
					invokeURL("http://" + PROD[9] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");
					invokeURL("http://" + PROD[10] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");
					invokeURL("http://" + PROD[11] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");
					
				}  			
				else if (app_name.equals("AppProps")) {

					invokeURL("http://" + PROD[0] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PROD[1] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PROD[2] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PROD[3] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PROD[4] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PROD[5] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PROD[6] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PROD[7] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PROD[8] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PROD[9] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PROD[10] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PROD[11] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
				}			
				else {
					invokeURL("http://" + PROD[0] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PROD[1] + ":" + ports[i] + "/TruistMain/src/ReloadBackendProps.jsp?action=reload");
					invokeURL("http://" + PROD[2] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PROD[3] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PROD[4] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PROD[5] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PROD[6] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PROD[7] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PROD[8] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PROD[9] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PROD[10] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					invokeURL("http://" + PROD[11] + ":" + ports[i] + "/TruistMain/src/ReloadAppProps.jsp?action=reload");
					
				}
			}
			}

	}

	private boolean invokeURL(String url) {

		try {

			url = URLDecoder.decode(url, "UTF-8");

			System.out.println("Invoking url - " + url);

			BufferedReader br = new BufferedReader(new InputStreamReader(

			new URL(url).openConnection().getInputStream()));

			String line = null;

			while ((line = br.readLine()) != null)



			System.out.println("DONE....\n\n");
			return true;

		} catch (Exception e) {

			System.out.println("While invoking URL " + url
			+ " , got exception -" + e.toString() + " Message - " + e.getMessage() + "\n\n");
			return false;

		}

	}

	

}