import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class ApiPayloadBootstrapper {
private final String payloadDirectory;
private final int numThreads;
public ApiPayloadBootstrapper(String payloadDirectory, int numThreads) {
this.payloadDirectory = payloadDirectory;
this.numThreads = numThreads;
}
public void bootstrapPayloads() {
ExecutorService executor = Executors.newFixedThreadPool(numThreads);
try {
Files.walkFileTree(Paths.get(payloadDirectory), new FileTreeEvaluator(executor));
} catch (IOException e) {
System.err.println("Error during file processing: " + e.getMessage());
//Consider more robust error handling, logging, or alerting here.
executor.shutdownNow(); // Attempt to shutdown the executor gracefully.
return;
}
executor.shutdown(); // Shutdown the executor after processing all files.
try {
executor.awaitTermination(60, java.util.concurrent.TimeUnit.SECONDS); //Wait for completion
} catch (InterruptedException e) {
System.err.println("Executor termination interrupted: " + e.getMessage());
}
}
private static class FileTreeEvaluator implements java.io.FileTreeEvaluator {
private final ExecutorService executor;
public FileTreeEvaluator(ExecutorService executor) {
this.executor = executor;
}
@Override
public void evaluate(java.nio.file.Path path, java.nio.file.FileVisitor<java.nio.file.Path> visitor) throws IOException {
if (Files.isDirectory(path)) {
visitor.visitFile(path, null); //Visit the directory itself.
java.nio.file.Files.walk(path, java.nio.file.FileVisitOption.CONCURRENT).forEach(f -> visitor.visitFile(f, null));
} else if (Files.isRegularFile(path) && path.toString().endsWith(".sh") || path.toString().endsWith(".py")) { //Example script extensions.
Future<?> future = executor.submit(() -> {
try {
// Execute the script with graceful failure handling
executeScript(path.toString()); //Pass the absolute path
} catch (Exception e) {
System.err.println("Error executing script " + path.toString() + ": " + e.getMessage());
e.printStackTrace(); //Log detailed error information.
}
});
}
}
}
private void executeScript(String scriptPath) throws Exception {
try {
// Execute the script using a separate process.
Process process = new ProcessBuilder("bash", scriptPath).start();
try {
int exitCode = process.waitFor();
if (exitCode != 0) {
throw new Exception("Script exited with code " + exitCode);
}
System.out.println("Script " + scriptPath + " completed successfully.");
} catch (InterruptedException e) {
throw new Exception("Script execution interrupted: " + e.getMessage());
} finally {
// Ensure the process is closed.
process.destroy();
}
} catch (IOException e) {
throw new Exception("Error executing script: " + e.getMessage());
}
}
public static void main(String[] args) {
String payloadDirectory = "scripts"; //Replace with the actual directory
int numThreads = 4;
ApiPayloadBootstrapper bootstrapper = new ApiPayloadBootstrapper(payloadDirectory, numThreads);
bootstrapper.bootstrapPayloads();
}
}
Add your comment