-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SED-526: Refactoring libraries to remove dependencies to other libraries
- Loading branch information
1 parent
e69c5d3
commit 30c7205
Showing
14 changed files
with
245 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<!-- | ||
Copyright (C) 2020, exense GmbH | ||
This file is part of STEP | ||
STEP is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Affero General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
STEP is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Affero General Public License for more details. | ||
You should have received a copy of the GNU Affero General Public License | ||
along with STEP. If not, see <http://www.gnu.org/licenses/>. | ||
--> | ||
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>step-library-commons</artifactId> | ||
|
||
<parent> | ||
<groupId>ch.exense.step.library</groupId> | ||
<artifactId>step-library-parent</artifactId> | ||
<version>0.0.0-SNAPSHOT</version> | ||
<relativePath>../../../step-library-parent</relativePath> | ||
</parent> | ||
|
||
<dependencies> | ||
</dependencies> | ||
</project> |
40 changes: 20 additions & 20 deletions
40
...ommon/helper/AbstractEnhancedKeyword.java → ...rary/commons/AbstractEnhancedKeyword.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
package ch.exense.step.examples.common.helper; | ||
|
||
import step.handlers.javahandler.AbstractKeyword; | ||
|
||
/** | ||
* @author Jonathan Rubiero | ||
*/ | ||
public class AbstractEnhancedKeyword extends AbstractKeyword { | ||
|
||
@Override | ||
public boolean onError(Exception e) { | ||
if(e instanceof BusinessException) { | ||
output.setBusinessError(e.getMessage()); | ||
return false; | ||
} else { | ||
return super.onError(e); | ||
} | ||
} | ||
|
||
} | ||
package ch.exense.step.library.commons; | ||
|
||
import step.handlers.javahandler.AbstractKeyword; | ||
|
||
/** | ||
* @author Jonathan Rubiero | ||
*/ | ||
public class AbstractEnhancedKeyword extends AbstractKeyword { | ||
|
||
@Override | ||
public boolean onError(Exception e) { | ||
if(e instanceof BusinessException) { | ||
output.setBusinessError(e.getMessage()); | ||
return false; | ||
} else { | ||
return super.onError(e); | ||
} | ||
} | ||
|
||
} |
155 changes: 155 additions & 0 deletions
155
...-library-commons/src/main/java/ch/exense/step/library/commons/AbstractProcessKeyword.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/******************************************************************************* | ||
* Copyright (C) 2020, exense GmbH | ||
* | ||
* This file is part of STEP | ||
* | ||
* STEP is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* STEP is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with STEP. If not, see <http://www.gnu.org/licenses/>. | ||
******************************************************************************/ | ||
package ch.exense.step.library.commons; | ||
|
||
import ch.exense.commons.processes.ManagedProcess; | ||
import ch.exense.commons.processes.ManagedProcess.ManagedProcessException; | ||
import step.grid.io.Attachment; | ||
import step.grid.io.AttachmentHelper; | ||
import step.handlers.javahandler.AbstractKeyword; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
import java.nio.file.Files; | ||
import java.util.List; | ||
import java.util.concurrent.TimeoutException; | ||
import java.util.function.Consumer; | ||
|
||
public abstract class AbstractProcessKeyword extends AbstractKeyword { | ||
|
||
protected static final int PROCESS_TIMEOUT = 60000; | ||
|
||
public AbstractProcessKeyword() { | ||
super(); | ||
} | ||
|
||
public static class OutputConfiguration { | ||
|
||
private final boolean alwaysAttachOutput; | ||
private final int maxOutputPayloadSize; | ||
private final int maxOutputAttachmentSize; | ||
private final boolean printExitCode; | ||
private final boolean checkExitCode; | ||
|
||
public OutputConfiguration() { | ||
this(true, 1000, 1000000, true, true); | ||
} | ||
|
||
public OutputConfiguration(boolean alwaysAttachOutput, int maxOutputPayloadSize, int maxOutputAttachmentSize, | ||
boolean printExitCode, boolean checkExitCode) { | ||
super(); | ||
this.alwaysAttachOutput = alwaysAttachOutput; | ||
this.maxOutputPayloadSize = maxOutputPayloadSize; | ||
this.maxOutputAttachmentSize = maxOutputAttachmentSize; | ||
this.printExitCode = printExitCode; | ||
this.checkExitCode = checkExitCode; | ||
} | ||
|
||
protected boolean isAlwaysAttachOutput() { | ||
return alwaysAttachOutput; | ||
} | ||
|
||
protected int getMaxOutputPayloadSize() { | ||
return maxOutputPayloadSize; | ||
} | ||
|
||
protected int getMaxOutputAttachmentSize() { | ||
return maxOutputAttachmentSize; | ||
} | ||
|
||
protected boolean isCheckExitCode() { | ||
return checkExitCode; | ||
} | ||
} | ||
|
||
protected void executeManagedCommand(String cmd, int timeoutMs) throws Exception { | ||
executeManagedCommand(cmd, timeoutMs, new OutputConfiguration(), null); | ||
} | ||
|
||
protected void executeManagedCommand(String cmd, int timeoutMs, OutputConfiguration outputConfiguration) throws Exception { | ||
executeManagedCommand(cmd, timeoutMs, outputConfiguration, null); | ||
} | ||
|
||
protected void executeManagedCommand(List<String> cmd, int timeoutMs, OutputConfiguration outputConfiguration, Consumer<ManagedProcess> postProcess) throws Exception { | ||
ManagedProcess process = new ManagedProcess(cmd); | ||
executeManagedCommand(timeoutMs, outputConfiguration, postProcess, process); | ||
} | ||
|
||
protected void executeManagedCommand(String cmd, int timeoutMs, OutputConfiguration outputConfiguration, Consumer<ManagedProcess> postProcess) throws Exception { | ||
ManagedProcess process = new ManagedProcess(cmd); | ||
executeManagedCommand(timeoutMs, outputConfiguration, postProcess, process); | ||
} | ||
|
||
protected void executeManagedCommand(int timeoutMs, OutputConfiguration outputConfiguration, | ||
Consumer<ManagedProcess> postProcess, ManagedProcess process) | ||
throws ManagedProcessException, InterruptedException, IOException { | ||
try { | ||
boolean hasError = false; | ||
process.start(); | ||
try { | ||
int exitCode = process.waitFor(timeoutMs); | ||
if (outputConfiguration.isCheckExitCode() && exitCode != 0) { | ||
output.setBusinessError("Process exited with code " + exitCode); | ||
hasError = true; | ||
} | ||
if(outputConfiguration.printExitCode) { | ||
output.add("Exit_code", Integer.toString(exitCode)); | ||
} | ||
if(postProcess != null) { | ||
postProcess.accept(process); | ||
} | ||
} catch (TimeoutException e) { | ||
output.setBusinessError("Process didn't exit within the defined timeout of "+timeoutMs+"ms"); | ||
hasError = true; | ||
} | ||
|
||
if(hasError || outputConfiguration.isAlwaysAttachOutput()) { | ||
attachOutputs(process, outputConfiguration); | ||
} | ||
} finally { | ||
process.close(); | ||
} | ||
} | ||
|
||
protected void attachOutputs(ManagedProcess process, OutputConfiguration outputConfiguration) throws IOException { | ||
attachOutput("stdout", process.getProcessOutputLog(), outputConfiguration); | ||
attachOutput("stderr", process.getProcessErrorLog(), outputConfiguration); | ||
} | ||
|
||
protected void attachOutput(String outputName, File file, OutputConfiguration outputConfiguration) throws IOException { | ||
StringBuilder processOutputBuilder = new StringBuilder(); | ||
Files.readAllLines(file.toPath(), Charset.defaultCharset()).forEach(l -> processOutputBuilder.append(l).append("\n")); | ||
|
||
String processOutput = processOutputBuilder.toString(); | ||
|
||
output.add(outputName, processOutput.substring(0, Math.min(processOutput.length(), outputConfiguration.maxOutputPayloadSize))); | ||
|
||
if(processOutput.length() > outputConfiguration.maxOutputPayloadSize) { | ||
Attachment attachment = AttachmentHelper.generateAttachmentFromByteArray( | ||
processOutput.substring(0, Math.min(processOutput.length(), outputConfiguration.maxOutputAttachmentSize)).getBytes(), outputName + ".log"); | ||
output.addAttachment(attachment); | ||
|
||
if (file.length() > outputConfiguration.maxOutputAttachmentSize) { | ||
output.add("technicalWarning", | ||
outputName + " size exceeded. " + outputName + " has been attached and truncated."); | ||
} | ||
} | ||
} | ||
} |
18 changes: 9 additions & 9 deletions
18
...ples/common/helper/BusinessException.java → ...ep/library/commons/BusinessException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
package ch.exense.step.examples.common.helper; | ||
|
||
public class BusinessException extends RuntimeException { | ||
|
||
public BusinessException(String message) { | ||
super(message); | ||
} | ||
|
||
} | ||
package ch.exense.step.library.commons; | ||
|
||
public class BusinessException extends RuntimeException { | ||
|
||
public BusinessException(String message) { | ||
super(message); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...ibrary-kw-step-client/src/main/java/ch/exense/step/library/kw/step/StepClientKeyword.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.