commit
a10b5be973
@ -0,0 +1,2 @@
|
||||
/org.eclipse.jdt.core.prefs
|
||||
/org.eclipse.m2e.core.prefs
|
||||
@ -0,0 +1,73 @@
|
||||
///usr/bin/env jbang "$0" "$@" ; exit $?
|
||||
|
||||
//DEPS com.fasterxml.jackson.core:jackson-databind:2.18.4
|
||||
//DEPS org.apache.commons:commons-exec:1.4.0
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.exec.CommandLine;
|
||||
import org.apache.commons.exec.DefaultExecutor;
|
||||
import org.apache.commons.exec.PumpStreamHandler;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class Splicer {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(Splicer.class.getName());
|
||||
|
||||
public static void main(String[] args) throws InterruptedException, JsonProcessingException {
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(args.length);
|
||||
|
||||
List<Callable<ExecutionResult>> callables = List.of(args).stream()
|
||||
.map(command -> new Callable<ExecutionResult>() {
|
||||
@Override
|
||||
public ExecutionResult call() throws Exception {
|
||||
return executeShell(command);
|
||||
}
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
List<ExecutionResult> results = executorService.invokeAll(callables).parallelStream().map(f -> {
|
||||
try {
|
||||
return f.get();
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
logger.log(Level.SEVERE, "Error found!", e);
|
||||
}
|
||||
return null;
|
||||
}).filter(Objects::nonNull).collect(Collectors.toList());
|
||||
|
||||
executorService.shutdown();
|
||||
|
||||
System.out.println(
|
||||
new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(Map.of("results", results)));
|
||||
}
|
||||
|
||||
public static ExecutionResult executeShell(String command) {
|
||||
ByteArrayOutputStream tempOut = new ByteArrayOutputStream();
|
||||
try {
|
||||
CommandLine cmdLine = CommandLine.parse(command.trim());
|
||||
DefaultExecutor executor = DefaultExecutor.builder().get();
|
||||
PumpStreamHandler streamHandler = new PumpStreamHandler(tempOut);
|
||||
executor.setStreamHandler(streamHandler);
|
||||
int exitValue = executor.execute(cmdLine);
|
||||
return new ExecutionResult(command, exitValue, new String(tempOut.toByteArray()));
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.SEVERE, "Execute cmd failed: " + command.trim(), e);
|
||||
}
|
||||
return new ExecutionResult(command, -1, new String(tempOut.toByteArray()));
|
||||
}
|
||||
|
||||
public static record ExecutionResult(String command, Integer exitValue, String output) {
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,74 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>cyou.vidconnect</groupId>
|
||||
<artifactId>splicer</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<name>splicer</name>
|
||||
<description>Run multiple shell commands concurrently</description>
|
||||
<properties>
|
||||
<jackson.version>2.18.4</jackson.version>
|
||||
<commons-exec.version>1.4.0</commons-exec.version>
|
||||
</properties>
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson</groupId>
|
||||
<artifactId>jackson-bom</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
<scope>import</scope>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-exec</artifactId>
|
||||
<version>${commons-exec.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.14.0</version>
|
||||
<configuration>
|
||||
<source>17</source>
|
||||
<target>17</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.5.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<transformers>
|
||||
<transformer
|
||||
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<mainClass>cyou.vidconnect.splicer.Main</mainClass>
|
||||
</transformer>
|
||||
</transformers>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@ -0,0 +1,70 @@
|
||||
package cyou.vidconnect.splicer;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.exec.CommandLine;
|
||||
import org.apache.commons.exec.DefaultExecutor;
|
||||
import org.apache.commons.exec.PumpStreamHandler;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class Main {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(Main.class.getName());
|
||||
|
||||
public static void main(String[] args) throws InterruptedException, JsonProcessingException {
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(args.length);
|
||||
|
||||
List<Callable<ExecutionResult>> callables = List.of(args).stream()
|
||||
.map(command -> new Callable<ExecutionResult>() {
|
||||
@Override
|
||||
public ExecutionResult call() throws Exception {
|
||||
return executeShell(command);
|
||||
}
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
List<ExecutionResult> results = executorService.invokeAll(callables).parallelStream().map(f -> {
|
||||
try {
|
||||
return f.get();
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
logger.log(Level.SEVERE, "Error found!", e);
|
||||
}
|
||||
return null;
|
||||
}).filter(Objects::nonNull).collect(Collectors.toList());
|
||||
|
||||
executorService.shutdown();
|
||||
|
||||
System.out.println(
|
||||
new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(Map.of("results", results)));
|
||||
}
|
||||
|
||||
public static ExecutionResult executeShell(String command) {
|
||||
ByteArrayOutputStream tempOut = new ByteArrayOutputStream();
|
||||
try {
|
||||
CommandLine cmdLine = CommandLine.parse(command.trim());
|
||||
DefaultExecutor executor = DefaultExecutor.builder().get();
|
||||
PumpStreamHandler streamHandler = new PumpStreamHandler(tempOut);
|
||||
executor.setStreamHandler(streamHandler);
|
||||
int exitValue = executor.execute(cmdLine);
|
||||
return new ExecutionResult(command, exitValue, new String(tempOut.toByteArray()));
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.SEVERE, "Execute cmd failed: " + command.trim(), e);
|
||||
}
|
||||
return new ExecutionResult(command, -1, new String(tempOut.toByteArray()));
|
||||
}
|
||||
|
||||
public static record ExecutionResult(String command, Integer exitValue, String output) {
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue