Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix environment variables #11

Merged
merged 3 commits into from
Sep 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 57 additions & 35 deletions virage/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,17 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.version>3.8.1</maven.version>
<maven.compiler.release>11</maven.compiler.release>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<jdk.version>11</jdk.version>
<exec.mainClass>edu.kit.kastel.formal.virage.core.VirageMain</exec.mainClass>
<maven.clean.version>3.3.1</maven.clean.version>
<maven.install.version>3.1.1</maven.install.version>
<maven.jar.version>3.3.0</maven.jar.version>
<maven.compiler.release>17</maven.compiler.release>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<jdk.version>17</jdk.version>
<log4j.version>2.20.0</log4j.version>
<spotbugs.version>4.7.3</spotbugs.version>
<swi.home.dir>/usr/lib/swi-prolog</swi.home.dir>
<main.class>edu.kit.kastel.formal.virage.core.VirageMain</main.class>
</properties>

<distributionManagement>
Expand Down Expand Up @@ -83,18 +89,23 @@
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.20.0</version>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.20.0</version>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- JPL bidirectional Prolog-Java: https://github.com/SWI-Prolog/packages-jpl -->
<dependency>
<groupId>com.github.SWI-Prolog</groupId>
<artifactId>packages-jpl</artifactId>
<version>V9.1.7</version> <!-- version 7.6.1 of JPL -->
<version>V9.1.14</version> <!-- version 7.6.1 of JPL -->
</dependency>
<!-- https://mvnrepository.com/artifact/commons-cli/commons-cli -->
<dependency>
Expand All @@ -105,12 +116,12 @@
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.14.2</version>
<version>2.15.2</version>
</dependency>
<!-- <dependency> <groupId>com.github.VeriVote</groupId> <artifactId>ViRAGe</artifactId>
<version>master-SNAPSHOT</version> </dependency> -->
Expand All @@ -125,7 +136,7 @@
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-configuration2</artifactId>
<version>2.8.0</version>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
Expand Down Expand Up @@ -181,7 +192,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.2.1</version>
<version>3.4.0</version>
<executions>
<execution>
<id>enforce-versions</id>
Expand All @@ -205,21 +216,22 @@
<version>3.2.4</version> <executions> <execution> <phase>package</phase>
<goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>edu.kit.kastel.formal.virage.core.VirageMain</mainClass> <manifestEntries>
<mainClass>${main.class}</mainClass> <manifestEntries>
<Multi-Release>true</Multi-Release> </manifestEntries> </transformer> <transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"
/> </transformers> </configuration> </execution> </executions> </plugin> -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.15.0</version>
<version>2.16.0</version>
<configuration>
<rulesUri>file:///${basedir}/rules.xml</rulesUri>
<allowSnapshots>false</allowSnapshots>
<generateBackupPoms>false</generateBackupPoms>
</configuration>
<executions>
<execution>
<id>versions-display</id>
<phase>compile</phase>
<goals>
<goal>display-dependency-updates</goal>
Expand All @@ -231,10 +243,11 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.1.0</version>
<version>1.2.0</version>
<executions>
<execution>
<phase>process-resources</phase>
<id>read-settings</id>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
Expand All @@ -253,7 +266,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
<version>3.1.2</version>
<configuration>
<!-- Unit tests take ages, deployment tests are obsolete as ViRAGe performs
these checks at runtime. -->
Expand All @@ -266,21 +279,24 @@
<version>3.1.0</version>
<executions>
<execution>
<phase>test</phase>
<id>execute</id>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>edu.kit.kastel.formal.virage.core.VirageMain</mainClass>
<environmentVariables>
<LD_PRELOAD>${SWI_PROLOG_LIBSWIPL_PATH}</LD_PRELOAD>
<LD_LIBRARY_PATH>${SWI_PROLOG_LIBRARIES_PATH}</LD_LIBRARY_PATH>
</environmentVariables>
<executable>java</executable>
<mainClass>${main.class}</mainClass>
<arguments>
<argument>-classpath</argument>
<classpath />
<argument>edu.kit.kastel.formal.virage.core.VirageMain</argument>
<argument>${main.class}</argument>
</arguments>
<environmentVariables>
<SWI_HOME_DIR>${swi.home.dir}</SWI_HOME_DIR>
<LD_PRELOAD>${swi.home.dir}/lib/x86_64-linux/libswipl.so</LD_PRELOAD>
</environmentVariables>
</configuration>
</plugin>
</plugins>
Expand All @@ -289,12 +305,12 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>3.2.0</version>
<version>${maven.clean.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.0</version>
<version>${maven.clean.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -304,17 +320,24 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<version>${maven.jar.version}</version>
<configuration>
<archive>
<manifest>
<mainClass>${main.class}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>3.1.0</version>
<version>${maven.install.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.1.0</version>
<version>${maven.install.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -327,14 +350,13 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.2.1</version>
<version>${maven.jar.version}</version>
<configuration>
<failsOnError>false</failsOnError>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
<includeResources>true</includeResources>
<includeTestResources>true</includeTestResources>
<configLocation>checks.xml</configLocation>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<linkXRef>false</linkXRef>
Expand All @@ -343,14 +365,14 @@
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>10.9.2</version>
<version>10.12.3</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<version>4.7.3.2</version>
<version>${spotbugs.version}.5</version>
<configuration>
<xmlOutput>true</xmlOutput>
<failOnError>false</failOnError>
Expand All @@ -366,7 +388,7 @@
<dependency>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs</artifactId>
<version>4.7.3</version>
<version>${spotbugs.version}</version>
</dependency>
</dependencies>
</plugin>
Expand All @@ -378,7 +400,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.4.2</version>
<version>3.4.5</version>
<reportSets>
<reportSet>
<reports>
Expand Down
97 changes: 58 additions & 39 deletions virage/src/main/java/edu/kit/kastel/formal/util/ProcessUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ public class ProcessUtils {
private static final Logger LOGGER = LogManager.getLogger(ProcessUtils.class);

/**
* Executes a terminating command and logs it outputs, stderr foing to logger.warn(), stdout to
* logger.info(). <b>Does not return if the command is non-terminating!</b>
* Executes a terminating command and prints its output to System.out/System.err, respectively.
* <b>Does not return if the command is non-terminating!</b>
*
* @param command the command to be executed (as is, i.e. the String has to contain all
* parameters etc.)
* @return the exit code (usually 0 on success, but depending on the command)
* @return a Pair of strings representing stdout and stderr of the process
* @throws IOException if reading the outputs fails
* @throws InterruptedException if command execution is interrupted
*/
public static int runTerminatingProcessAndLogOutput(final String command)
public static Output runTerminatingProcess(final String command)
throws IOException, InterruptedException {
final Runtime rt = Runtime.getRuntime();

Expand All @@ -37,44 +37,29 @@ public static int runTerminatingProcessAndLogOutput(final String command)
final String stdErr = new String(p.getErrorStream().readAllBytes(), StandardCharsets.UTF_8);
final String stdOut = new String(p.getInputStream().readAllBytes(), StandardCharsets.UTF_8);

if (!stdErr.isEmpty()) {
LOGGER.warn(stdErr);
}
if (!stdOut.isEmpty()) {
LOGGER.info(stdOut);
}

return status;
return new Output(stdOut, stdErr, status);
}

/**
* Executes a terminating command and prints its output to System.out/System.err, respectively.
* <b>Does not return if the command is non-terminating!</b>
* Executes a terminating command and logs it outputs, stderr going to logger.warn(), stdout to
* logger.info(). <b>Does not return if the command is non-terminating!</b>
*
* @param command the command to be executed (as is, i.e. the String has to contain all
* parameters etc.)
* @return the exit code (usually 0 on success, but depending on the command)
* @throws IOException if reading the outputs fails
* @throws InterruptedException if command execution is interrupted
*/
public static int runTerminatingProcessAndPrintOutput(final String command)
public static int runTerminatingProcessAndLogOutput(final String command)
throws IOException, InterruptedException {
final Runtime rt = Runtime.getRuntime();

final Process p = rt.exec(StringUtils.stripAndEscape(command));
final int status = p.waitFor();

final String stdErr = new String(p.getErrorStream().readAllBytes(), StandardCharsets.UTF_8);
final String stdOut = new String(p.getInputStream().readAllBytes(), StandardCharsets.UTF_8);

if (!stdErr.isEmpty()) {
System.err.print(stdErr);
final Output output = runTerminatingProcess(command);
if (!output.stdErr.isEmpty()) {
LOGGER.warn(output.stdErr);
}
if (!stdOut.isEmpty()) {
System.out.print(stdOut);
if (!output.stdOut.isEmpty()) {
LOGGER.info(output.stdOut);
}

return status;
return output.status;
}

/**
Expand All @@ -83,20 +68,54 @@ public static int runTerminatingProcessAndPrintOutput(final String command)
*
* @param command the command to be executed (as is, i.e. the String has to contain all
* parameters etc.)
* @return a Pair of strings representing stdout and stderr of the process
* @return the exit code (usually 0 on success, but depending on the command)
* @throws IOException if reading the outputs fails
* @throws InterruptedException if command execution is interrupted
*/
public static Pair<String, String> runTerminatingProcess(final String command)
public static int runTerminatingProcessAndPrintOutput(final String command)
throws IOException, InterruptedException {
final Runtime rt = Runtime.getRuntime();

final Process p = rt.exec(StringUtils.stripAndEscape(command));
p.waitFor();

final String stdErr = new String(p.getErrorStream().readAllBytes(), StandardCharsets.UTF_8);
final String stdOut = new String(p.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
final Output output = runTerminatingProcess(command);
if (!output.stdErr.isEmpty()) {
System.err.print(output.stdErr);
}
if (!output.stdOut.isEmpty()) {
System.out.print(output.stdOut);
}
return output.status;
}

return new Pair<String, String>(stdOut, stdErr);
/**
* The static data structure to store the text from the standard output and the error output
* stream as well as the integer status value.
*
* @author VeriVote
*/
public static class Output {
/**
* The standard output stream as a string.
*/
public final String stdOut;
/**
* The error output stream as a string.
*/
public final String stdErr;
/**
* The status output as an integer value.
*/
public final int status;

/**
* Constructor method for the static data structure to store the text from the standard
* output and the error output stream as well as the integer status value.
*
* @param outStream The standard output stream as a string.
* @param errStream The error output stream as a string.
* @param statusValue The status output as an integer value.
*/
public Output(final String outStream, final String errStream, final int statusValue) {
stdOut = outStream;
stdErr = errStream;
status = statusValue;
}
}
}
Loading
Loading