package org.jvalue.commons.utils;


import java.net.HttpURLConnection;
import java.net.URL;

public final class HttpServiceCheck {

	private static final int DEFAULT_RETRIES = 50;
	private static final int DEFAULT_SLEEP_TIME_BETWEEN_RETRIES = 3000;

	public static boolean check(String url) {
		return doCheck(url, DEFAULT_RETRIES, DEFAULT_SLEEP_TIME_BETWEEN_RETRIES);
	}


	public static boolean check(String url, int retries, int sleepTimeBetweenRetries) {
		return doCheck(url, retries, sleepTimeBetweenRetries);
	}


	private static boolean doCheck(String url, int retries, int sleepTimeBetweenRetries) {
		int retryCounter = retries;

		do {
			if (isReachable(url)) {
				Log.info("[success] Service check for URL " + url);
				return true;
			} else {
				String msg = String.format("[%d/%d] Retry service check for URL %s", retryCounter, retries, url);
				Log.info(msg);
				doSleep(sleepTimeBetweenRetries);
			}
			retryCounter--;
		} while (retryCounter > 0);

		Log.error("[failure] Service check for URL  " + url);
		return false;
	}


	private static boolean isReachable(String url) {
		try {
			URL serviceUrl = new URL(url);
			HttpURLConnection connection = (HttpURLConnection) serviceUrl.openConnection();
			connection.connect();

			int responseCode = connection.getResponseCode();
			return isOk(responseCode);
		} catch (Exception e) {
		}

		return false;
	}


	private static void doSleep(int sleepTime) {
		try {
			Thread.sleep(sleepTime);
		} catch (InterruptedException e) {
			Log.error(e.getMessage());
		}
	}


	private static boolean isOk(int statusCode) {
		return statusCode == 200;
	}

}

@@ -86,16 +86,167 @@
 t.URL;%0A%0A
+/**%0A * Check if a HTTP service is available.%0A * If a HTTP connection cant't be established, it%0A * will wait a and retry to connect to the service.%0A */%0A
 public f
@@ -274,30 +274,29 @@
 eCheck %7B%0A%0A%09p
-rivate
+ublic
  static fina
@@ -325,22 +325,21 @@
 = 50;%0A%09p
-rivate
+ublic
  static 
@@ -397,130 +397,733 @@
 ;%0A%0A%09
-public static boolean check(String url) %7B%0A%09%09return doCheck(url, DEFAULT_RETRIES, DEFAULT_SLEEP_TIME_BETWEEN_RETRIES);%0A%09%7D%0A%0A
+/**%0A%09 * Check if HTTP service is available.%0A%09 * Default amount of retries are %7B@value #DEFAULT_RETRIES%7D.%0A%09 * Default time to wait until next retry is %7B@value #DEFAULT_SLEEP_TIME_BETWEEN_RETRIES%7D.%0A%09 * @param url URL of the HTTP service to check.%0A%09 * @return true if the service is available and false if not.%0A%09 */%0A%09public static boolean check(String url) %7B%0A%09%09return doCheck(url, DEFAULT_RETRIES, DEFAULT_SLEEP_TIME_BETWEEN_RETRIES);%0A%09%7D%0A%0A%09/**%0A%09 * Check if HTTP service is available.%0A%09 * @param url URL of the HTTP service to check%0A%09 * @param retries amount of retries until fail%0A%09 * @param sleepTimeBetweenRetries time to wait until next retry in milliseconds%0A%09 * @return true if the service is available and false if not.%0A%09 */
 %0A%09pu

Add JavaDoc comments to HttpServiceCheck class