1. import java.io.IOException;
  2. import java.nio.file.Files;
  3. import java.nio.file.Path;
  4. import java.nio.file.Paths;
  5. import java.util.concurrent.Executors;
  6. import java.util.concurrent.ScheduledExecutorService;
  7. import java.util.concurrent.TimeUnit;
  8. import java.util.concurrent.atomic.AtomicBoolean;
  9. public class CookieBootstrapper {
  10. private final Path scriptPath;
  11. private final int retryCount;
  12. private final long retryDelayMillis;
  13. private final AtomicBoolean bootstrapped = new AtomicBoolean(false);
  14. public CookieBootstrapper(Path scriptPath, int retryCount, long retryDelayMillis) {
  15. this.scriptPath = scriptPath;
  16. this.retryCount = retryCount;
  17. this.retryDelayMillis = retryDelayMillis;
  18. }
  19. public void bootstrap() throws IOException {
  20. for (int i = 0; i < retryCount; i++) {
  21. try {
  22. executeScript();
  23. bootstrapped.set(true);
  24. return; // Success, exit loop
  25. } catch (IOException e) {
  26. if (i == retryCount - 1) {
  27. throw e; // Re-throw on last retry
  28. }
  29. System.err.println("Bootstrap failed (attempt " + (i + 1) + "): " + e.getMessage());
  30. try {
  31. Thread.sleep(retryDelayMillis);
  32. } catch (InterruptedException e2) {
  33. Thread.currentThread().interrupt();
  34. throw new IOException("Interrupted during retry", e2);
  35. }
  36. }
  37. }
  38. }
  39. private void executeScript() throws IOException {
  40. // Read the script file
  41. String scriptContent = new String(Files.readAllBytes(scriptPath));
  42. // Execute the script (replace with your actual execution logic)
  43. System.out.println("Executing script: " + scriptPath);
  44. // Example: Run the script using a shell command. Important: sanitize input!
  45. // ProcessBuilder pb = new ProcessBuilder("bash", "-c", scriptContent);
  46. // Process p = pb.start();
  47. // p.waitFor();
  48. //Simulate success
  49. System.out.println("Script executed successfully.");
  50. }
  51. public boolean isBootstrapped() {
  52. return bootstrapped.get();
  53. }
  54. public static void main(String[] args) throws IOException {
  55. Path scriptPath = Paths.get("cookie_script.sh"); // Replace with your script path
  56. int retryCount = 3;
  57. long retryDelayMillis = 1000;
  58. // Create a dummy script for testing
  59. String scriptContent = "echo 'Running cookie bootstrap script' > /tmp/cookie_boot_test.txt";
  60. Files.write(scriptPath, scriptContent.getBytes());
  61. CookieBootstrapper bootstrapper = new CookieBootstrapper(scriptPath, retryCount, retryDelayMillis);
  62. try {
  63. bootstrapper.bootstrap();
  64. if (bootstrapper.isBootstrapped()) {
  65. System.out.println("Cookie bootstrap completed successfully.");
  66. } else {
  67. System.err.println("Cookie bootstrap failed after multiple retries.");
  68. }
  69. } catch (IOException e) {
  70. System.err.println("Bootstrap failed permanently: " + e.getMessage());
  71. } finally {
  72. // Clean up the dummy script
  73. try {
  74. Files.deleteIfExists(scriptPath);
  75. } catch (IOException e) {
  76. System.err.println("Error deleting dummy script: " + e.getMessage());
  77. }
  78. }
  79. }
  80. }

Add your comment