1. import java.io.IOException;
  2. import java.nio.file.Files;
  3. import java.nio.file.Paths;
  4. import java.util.concurrent.ExecutorService;
  5. import java.util.concurrent.Executors;
  6. import java.util.concurrent.Future;
  7. public class ApiPayloadBootstrapper {
  8. private final String payloadDirectory;
  9. private final int numThreads;
  10. public ApiPayloadBootstrapper(String payloadDirectory, int numThreads) {
  11. this.payloadDirectory = payloadDirectory;
  12. this.numThreads = numThreads;
  13. }
  14. public void bootstrapPayloads() {
  15. ExecutorService executor = Executors.newFixedThreadPool(numThreads);
  16. try {
  17. Files.walkFileTree(Paths.get(payloadDirectory), new FileTreeEvaluator(executor));
  18. } catch (IOException e) {
  19. System.err.println("Error during file processing: " + e.getMessage());
  20. //Consider more robust error handling, logging, or alerting here.
  21. executor.shutdownNow(); // Attempt to shutdown the executor gracefully.
  22. return;
  23. }
  24. executor.shutdown(); // Shutdown the executor after processing all files.
  25. try {
  26. executor.awaitTermination(60, java.util.concurrent.TimeUnit.SECONDS); //Wait for completion
  27. } catch (InterruptedException e) {
  28. System.err.println("Executor termination interrupted: " + e.getMessage());
  29. }
  30. }
  31. private static class FileTreeEvaluator implements java.io.FileTreeEvaluator {
  32. private final ExecutorService executor;
  33. public FileTreeEvaluator(ExecutorService executor) {
  34. this.executor = executor;
  35. }
  36. @Override
  37. public void evaluate(java.nio.file.Path path, java.nio.file.FileVisitor<java.nio.file.Path> visitor) throws IOException {
  38. if (Files.isDirectory(path)) {
  39. visitor.visitFile(path, null); //Visit the directory itself.
  40. java.nio.file.Files.walk(path, java.nio.file.FileVisitOption.CONCURRENT).forEach(f -> visitor.visitFile(f, null));
  41. } else if (Files.isRegularFile(path) && path.toString().endsWith(".sh") || path.toString().endsWith(".py")) { //Example script extensions.
  42. Future<?> future = executor.submit(() -> {
  43. try {
  44. // Execute the script with graceful failure handling
  45. executeScript(path.toString()); //Pass the absolute path
  46. } catch (Exception e) {
  47. System.err.println("Error executing script " + path.toString() + ": " + e.getMessage());
  48. e.printStackTrace(); //Log detailed error information.
  49. }
  50. });
  51. }
  52. }
  53. }
  54. private void executeScript(String scriptPath) throws Exception {
  55. try {
  56. // Execute the script using a separate process.
  57. Process process = new ProcessBuilder("bash", scriptPath).start();
  58. try {
  59. int exitCode = process.waitFor();
  60. if (exitCode != 0) {
  61. throw new Exception("Script exited with code " + exitCode);
  62. }
  63. System.out.println("Script " + scriptPath + " completed successfully.");
  64. } catch (InterruptedException e) {
  65. throw new Exception("Script execution interrupted: " + e.getMessage());
  66. } finally {
  67. // Ensure the process is closed.
  68. process.destroy();
  69. }
  70. } catch (IOException e) {
  71. throw new Exception("Error executing script: " + e.getMessage());
  72. }
  73. }
  74. public static void main(String[] args) {
  75. String payloadDirectory = "scripts"; //Replace with the actual directory
  76. int numThreads = 4;
  77. ApiPayloadBootstrapper bootstrapper = new ApiPayloadBootstrapper(payloadDirectory, numThreads);
  78. bootstrapper.bootstrapPayloads();
  79. }
  80. }

Add your comment