Skip to content

Commit

Permalink
chore(camel): Fix environment variable setting for Camel JBang process
Browse files Browse the repository at this point in the history
  • Loading branch information
christophd committed Nov 13, 2024
1 parent 2efe88a commit 59d7cb9
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static JBangSupport jbang() {
* Get JBang version.
*/
public String version() {
ProcessAndOutput p = execute(jBang("version"), envVars);
ProcessAndOutput p = execute(jBang("version"), null);
return p.getOutput();
}

Expand Down Expand Up @@ -175,7 +175,7 @@ public ProcessAndOutput runAsync(String command, String... args) {
* Command can be a script file or an app command.
*/
public ProcessAndOutput runAsync(String command, List<String> args) {
return executeAsync(jBang(systemProperties, constructAllArgs(command, args)));
return executeAsync(jBang(systemProperties, constructAllArgs(command, args)), envVars);
}

/**
Expand All @@ -193,7 +193,7 @@ public ProcessAndOutput runAsync(String command, File output, String... args) {
* Redirect the process output to given file.
*/
public ProcessAndOutput runAsync(String command, File output, List<String> args) {
return executeAsync(jBang(systemProperties, constructAllArgs(command, args)), output);
return executeAsync(jBang(systemProperties, constructAllArgs(command, args)), output, envVars);
}

private List<String> constructAllArgs(String command, List<String> args) {
Expand Down Expand Up @@ -333,7 +333,7 @@ private static String getSystemPropertyArgs(Map<String, String> systemProperties

return systemProperties.entrySet()
.stream()
.map(entry -> "-D%s=%s".formatted(entry.getKey(), entry.getValue()))
.map(entry -> "-D%s=\"%s\"".formatted(entry.getKey(), entry.getValue()))
.collect(Collectors.joining(" ")) + " ";
}

Expand All @@ -353,7 +353,7 @@ private static ProcessAndOutput execute(List<String> command, Map<String, String
ProcessBuilder pBuilder = new ProcessBuilder(command)
.redirectErrorStream(true);

if (envVars != null && !envVars.isEmpty()) {
if (envVars != null) {
pBuilder.environment().putAll(envVars);
}

Expand Down Expand Up @@ -382,13 +382,19 @@ private static ProcessAndOutput execute(List<String> command, Map<String, String
* Execute JBang command using the process API. Waits for the process to complete and returns the process instance so
* caller is able to access the exit code and process output.
* @param command
* @param envVars
* @return
*/
private static ProcessAndOutput executeAsync(List<String> command) {
private static ProcessAndOutput executeAsync(List<String> command, Map<String, String> envVars) {
try {
Process p = new ProcessBuilder(command)
.redirectErrorStream(true)
.start();
ProcessBuilder pBuilder = new ProcessBuilder(command)
.redirectErrorStream(true);

if (envVars != null) {
pBuilder.environment().putAll(envVars);
}

Process p = pBuilder.start();
return new ProcessAndOutput(p);
} catch (IOException e) {
throw new CitrusRuntimeException("Error while executing JBang", e);
Expand All @@ -400,15 +406,20 @@ private static ProcessAndOutput executeAsync(List<String> command) {
* caller is able to access the exit code and process output.
* @param command
* @param outputFile
* @param envVars
* @return
*/
private static ProcessAndOutput executeAsync(List<String> command, File outputFile) {
private static ProcessAndOutput executeAsync(List<String> command, File outputFile, Map<String, String> envVars) {
try {
Process p = new ProcessBuilder(command)
ProcessBuilder pBuilder = new ProcessBuilder(command)
.redirectErrorStream(true)
.redirectOutput(outputFile)
.start();
.redirectOutput(outputFile);

if (envVars != null) {
pBuilder.environment().putAll(envVars);
}

Process p = pBuilder.start();
return new ProcessAndOutput(p, outputFile);
} catch (IOException e) {
throw new CitrusRuntimeException("Error while executing JBang", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public class CamelRunIntegrationAction extends AbstractCamelJBangAction {
private final boolean autoRemoveResources;

private final boolean waitForRunningState;
private final boolean dumpIntegrationOutput;

/**
* Default constructor.
Expand All @@ -89,6 +90,7 @@ public CamelRunIntegrationAction(Builder builder) {
this.systemProperties = builder.systemProperties;
this.autoRemoveResources = builder.autoRemoveResources;
this.waitForRunningState = builder.waitForRunningState;
this.dumpIntegrationOutput = builder.dumpIntegrationOutput;
}

@Override
Expand All @@ -113,6 +115,7 @@ public void doExecute(TestContext context) {
throw new CitrusRuntimeException("Missing Camel integration source code or file");
}

camelJBang().dumpIntegrationOutput(dumpIntegrationOutput);
camelJBang().camelApp().withEnvs(context.resolveDynamicValuesInMap(envVars));
camelJBang().camelApp().withSystemProperties(context.resolveDynamicValuesInMap(systemProperties));

Expand Down Expand Up @@ -191,6 +194,7 @@ public static final class Builder extends AbstractCamelJBangAction.Builder<Camel

private boolean autoRemoveResources = CamelJBangSettings.isAutoRemoveResources();
private boolean waitForRunningState = CamelJBangSettings.isWaitForRunningState();
private boolean dumpIntegrationOutput = CamelJBangSettings.isDumpIntegrationOutput();

/**
* Runs Camel integration from given source code.
Expand Down Expand Up @@ -351,6 +355,11 @@ public Builder withSystemProperties(Resource systemPropertiesFile) {
return this;
}

public Builder dumpIntegrationOutput(boolean enabled) {
this.dumpIntegrationOutput = enabled;
return this;
}

public Builder autoRemove(boolean enabled) {
this.autoRemoveResources = enabled;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public class CamelJBang {

private final JBangSupport camelApp = JBangSupport.jbang().app(CamelJBangSettings.getCamelApp());

private boolean dumpIntegrationOutput = CamelJBangSettings.isDumpIntegrationOutput();

/**
* Prevent direct instantiation.
*/
Expand Down Expand Up @@ -117,7 +119,7 @@ public ProcessAndOutput run(String name, String file, List<String> resources, St
runArgs.add(file);
runArgs.addAll(resources);

if (CamelJBangSettings.isCamelDumpIntegrationOutput()) {
if (dumpIntegrationOutput) {
Path workDir = CamelJBangSettings.getWorkDir();
File outputFile = workDir.resolve(String.format("i-%s-output.txt", name)).toFile();

Expand Down Expand Up @@ -232,4 +234,9 @@ public List<Map<String, String>> getAll() {
}
}

public CamelJBang dumpIntegrationOutput(boolean enabled) {
this.dumpIntegrationOutput = enabled;
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public static String[] getTrustUrl() {
* When set to true JBang process output for Camel integrations will be redirected to a file in the current working directory.
* @return
*/
public static boolean isCamelDumpIntegrationOutput() {
public static boolean isDumpIntegrationOutput() {
return Boolean.parseBoolean(System.getProperty(CAMEL_DUMP_INTEGRATION_OUTPUT_PROPERTY,
System.getenv(CAMEL_DUMP_INTEGRATION_OUTPUT_ENV) != null ? System.getenv(CAMEL_DUMP_INTEGRATION_OUTPUT_ENV) : CAMEL_DUMP_INTEGRATION_OUTPUT_DEFAULT));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,18 @@ public void runIntegrationWithSourceCodeIT() {
includeMetadata: true
steps:
- setBody:
simple: "Hello Camel #${header.CamelTimerCounter}"
simple: "{{greeting}} #${header.CamelTimerCounter}"
- transform:
simple: "${body.toUpperCase()}"
- to: "log:info"
"""));
""")
.withSystemProperty("greeting", "Hello Camel"));

then(camel().jbang()
.verify("hello")
.waitForLogMessage("HELLO CAMEL #10"));
}

@Test
@CitrusTest(name = "RunIntegration_Resource_IT")
public void runIntegrationWithResourceIT() {
Expand All @@ -62,7 +64,8 @@ public void runIntegrationWithResourceIT() {

when(camel().jbang()
.run()
.integration(Resources.fromClasspath("route.yaml", CamelJBangIT.class)));
.integration(Resources.fromClasspath("route.yaml", CamelJBangIT.class))
.withEnv("GREETING", "Hello Camel"));

then(camel().jbang()
.verify("route")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
includeMetadata: true
steps:
- setBody:
simple: "Hello Camel #${header.CamelTimerCounter}"
simple: "{{greeting}} #${header.CamelTimerCounter}"
- transform:
simple: "${body.toUpperCase()}"
- to: "log:info"
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
<jbang>
<run args="--verbose">
<integration name="hello-xml"
file="classpath:org/citrusframework/camel/integration/route.yaml"/>
file="classpath:org/citrusframework/camel/integration/route.yaml">
<system-properties>
<property name="greeting" value="Hello Camel"/>
</system-properties>
</integration>
</run>
</jbang>
</camel>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ actions:
integration:
name: "hello-yaml"
file: "classpath:org/citrusframework/camel/integration/route.yaml"
systemProperties:
properties:
- name: greeting
value: Hello Camel
- camel:
jbang:
verify:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,7 @@
<xs:attribute name="file" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="systemProperties" minOccurs="0">
<xs:element name="system-properties" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="property" maxOccurs="unbounded">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,7 @@
<xs:attribute name="file" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="systemProperties" minOccurs="0">
<xs:element name="system-properties" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="property" maxOccurs="unbounded">
Expand Down

0 comments on commit 59d7cb9

Please sign in to comment.