1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Map;
  4. import java.util.HashMap;
  5. import java.util.Arrays;
  6. public class UrlParser {
  7. public static void main(String[] args) {
  8. if (args.length < 3) {
  9. System.err.println("Usage: java UrlParser <url_list_file> <retry_interval> <staging_environment>");
  10. System.exit(1);
  11. }
  12. String urlListFile = args[0];
  13. int retryInterval = Integer.parseInt(args[1]);
  14. String stagingEnvironment = args[2];
  15. List<String> urls = parseUrlsFromFile(urlListFile);
  16. if (urls.isEmpty()) {
  17. System.out.println("No URLs found in the file.");
  18. return;
  19. }
  20. System.out.println("Processing URLs for staging environment: " + stagingEnvironment);
  21. for (String url : urls) {
  22. try {
  23. if (!isUrlValid(url)) {
  24. System.err.println("Invalid URL: " + url);
  25. continue;
  26. }
  27. System.out.println("Checking URL: " + url + " in " + stagingEnvironment);
  28. // Simulate a check with retry
  29. checkUrl(url, stagingEnvironment, retryInterval);
  30. } catch (Exception e) {
  31. System.err.println("Error processing URL " + url + ": " + e.getMessage());
  32. }
  33. }
  34. }
  35. private static List<String> parseUrlsFromFile(String filePath) {
  36. List<String> urls = new ArrayList<>();
  37. try (java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.FileReader(filePath))) {
  38. String line;
  39. while ((line = reader.readLine()) != null) {
  40. urls.add(line.trim()); // Remove leading/trailing whitespace
  41. }
  42. } catch (Exception e) {
  43. System.err.println("Error reading file: " + e.getMessage());
  44. }
  45. return urls;
  46. }
  47. private static boolean isUrlValid(String url) {
  48. //Basic URL validation - can be improved.
  49. if (url == null || url.isEmpty()) return false;
  50. return url.startsWith("http://") || url.startsWith("https://");
  51. }
  52. private static void checkUrl(String url, String stagingEnvironment, int retryInterval) throws Exception {
  53. boolean success = false;
  54. for (int i = 0; i < 3; i++) { // Retry up to 3 times
  55. try {
  56. // Simulate URL check. Replace with actual URL checking logic.
  57. System.out.println("Attempting to check URL: " + url);
  58. // Replace with actual HTTP request logic
  59. if (url.equals("http://example.com")) {
  60. success = true;
  61. } else {
  62. throw new Exception("Simulated error for URL: " + url); // Simulate error
  63. }
  64. if (success) {
  65. System.out.println("URL " + url + " is valid in " + stagingEnvironment);
  66. break; // Exit retry loop on success
  67. } else {
  68. System.out.println("URL " + url + " failed in " + stagingEnvironment + ". Retrying in " + retryInterval + " seconds...");
  69. Thread.sleep(retryInterval * 1000); // Sleep for retry interval
  70. }
  71. } catch (Exception e) {
  72. System.err.println("Error checking URL " + url + ": " + e.getMessage());
  73. if (i == 2) {
  74. throw e; // Re-throw if all retries fail
  75. }
  76. }
  77. }
  78. if (!success) {
  79. throw new Exception("Failed to check URL " + url + " after multiple retries.");
  80. }
  81. }
  82. }

Add your comment