Skip to content

Commit 2854b31

Browse files
committed
feat: Separate env-utils module
1 parent edf2370 commit 2854b31

11 files changed

Lines changed: 119 additions & 46 deletions

File tree

env-utils/pom.xml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright © 2026 DataSQRL (contact@datasqrl.com)
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<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">
20+
<modelVersion>4.0.0</modelVersion>
21+
22+
<parent>
23+
<groupId>com.datasqrl.flinkrunner</groupId>
24+
<artifactId>flink-sql-runner-parent</artifactId>
25+
<version>1.0-SNAPSHOT</version>
26+
</parent>
27+
28+
<artifactId>env-utils</artifactId>
29+
<name>Environment Utilities</name>
30+
31+
<dependencies>
32+
<!-- Add logging framework, to produce console output when running in the IDE. -->
33+
<!-- These dependencies are excluded from the application JAR by default. -->
34+
<dependency>
35+
<groupId>org.slf4j</groupId>
36+
<artifactId>slf4j-api</artifactId>
37+
<scope>provided</scope>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>com.fasterxml.jackson.core</groupId>
42+
<artifactId>jackson-databind</artifactId>
43+
</dependency>
44+
</dependencies>
45+
</project>

flink-sql-runner/src/main/java/com/datasqrl/flinkrunner/EnvUtils.java renamed to env-utils/src/main/java/com/datasqrl/flinkrunner/utils/EnvUtils.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.datasqrl.flinkrunner;
16+
package com.datasqrl.flinkrunner.utils;
1717

1818
import java.util.HashMap;
1919
import java.util.Map;
2020
import java.util.UUID;
21+
import lombok.AccessLevel;
22+
import lombok.NoArgsConstructor;
2123

2224
/** Utility class for environment variable operations. */
23-
final class EnvUtils {
24-
25+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
26+
public final class EnvUtils {
2527
/**
2628
* Returns a map of environment variables with deployment-specific defaults.
2729
*
@@ -35,15 +37,18 @@ final class EnvUtils {
3537
*
3638
* @return an immutable map containing all environment variables with defaults applied
3739
*/
38-
static Map<String, String> getEnvWithDefaults() {
40+
public static Map<String, String> getEnvWithDeploymentDefaults() {
3941
var env = new HashMap<>(System.getenv());
40-
env.putIfAbsent("DEPLOYMENT_ID", UUID.randomUUID().toString());
41-
env.putIfAbsent("DEPLOYMENT_TIMESTAMP", String.valueOf(System.currentTimeMillis()));
42+
getDeploymentDefaults().forEach(env::putIfAbsent);
4243

4344
return Map.copyOf(env);
4445
}
4546

46-
private EnvUtils() {
47-
throw new UnsupportedOperationException();
47+
public static Map<String, String> getDeploymentDefaults() {
48+
return Map.of(
49+
"DEPLOYMENT_ID",
50+
UUID.randomUUID().toString(),
51+
"DEPLOYMENT_TIMESTAMP",
52+
String.valueOf(System.currentTimeMillis()));
4853
}
4954
}

flink-sql-runner/src/main/java/com/datasqrl/flinkrunner/EnvVarResolver.java renamed to env-utils/src/main/java/com/datasqrl/flinkrunner/utils/EnvVarResolver.java

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,20 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.datasqrl.flinkrunner;
16+
package com.datasqrl.flinkrunner.utils;
1717

18+
import com.fasterxml.jackson.core.JsonParser;
19+
import com.fasterxml.jackson.databind.DeserializationContext;
20+
import com.fasterxml.jackson.databind.JsonDeserializer;
21+
import com.fasterxml.jackson.databind.ObjectMapper;
22+
import com.fasterxml.jackson.databind.module.SimpleModule;
1823
import java.io.IOException;
24+
import java.util.HashMap;
1925
import java.util.HashSet;
2026
import java.util.Map;
2127
import java.util.regex.Matcher;
2228
import java.util.regex.Pattern;
2329
import lombok.extern.slf4j.Slf4j;
24-
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser;
25-
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext;
26-
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonDeserializer;
27-
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
28-
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.module.SimpleModule;
2930

3031
/** Environment variable resolving functionality. */
3132
@Slf4j
@@ -35,14 +36,25 @@ public class EnvVarResolver {
3536

3637
private final Map<String, String> envVars;
3738
private final ObjectMapper objectMapper;
39+
private final boolean strict;
3840

3941
public EnvVarResolver() {
40-
this(EnvUtils.getEnvWithDefaults());
42+
this(EnvUtils.getEnvWithDeploymentDefaults());
43+
}
44+
45+
public EnvVarResolver(boolean strict) {
46+
this(EnvUtils.getEnvWithDeploymentDefaults(), strict);
4147
}
4248

4349
public EnvVarResolver(Map<String, String> envVars) {
44-
this.envVars = envVars;
50+
this(envVars, true);
51+
}
52+
53+
public EnvVarResolver(Map<String, String> envVars, boolean strict) {
54+
this.envVars = new HashMap<>(envVars);
55+
this.strict = strict;
4556
objectMapper = initObjectMapper();
57+
EnvUtils.getDeploymentDefaults().forEach(this.envVars::putIfAbsent);
4658
}
4759

4860
/**
@@ -55,11 +67,11 @@ public EnvVarResolver(Map<String, String> envVars) {
5567
* @throws IllegalStateException if any referenced environment variable are not available
5668
*/
5769
public String resolve(String src) {
58-
if (StringUtils.isBlank(src)) {
70+
if (src == null || src.isBlank()) {
5971
return src;
6072
}
6173

62-
var res = new StringBuffer();
74+
var res = new StringBuilder();
6375
// First pass to replace environment variables
6476
var matcher = ENVIRONMENT_VARIABLE_PATTERN.matcher(src);
6577
var missingEnvVars = new HashSet<String>();
@@ -92,7 +104,7 @@ public String resolve(String src) {
92104
}
93105
matcher.appendTail(res);
94106

95-
if (!missingEnvVars.isEmpty()) {
107+
if (strict && !missingEnvVars.isEmpty()) {
96108
throw new IllegalStateException(
97109
String.format(
98110
"The following environment variables were referenced, but not found: %s",
@@ -117,17 +129,19 @@ public String resolveInJson(String jsonSrc) throws IOException {
117129
return objectMapper.writeValueAsString(res);
118130
}
119131

120-
private ObjectMapper initObjectMapper() {
121-
var objectMapper = new ObjectMapper();
132+
public ObjectMapper initObjectMapper() {
133+
return initObjectMapper(new ObjectMapper());
134+
}
122135

136+
public ObjectMapper initObjectMapper(ObjectMapper mapper) {
123137
var module = new SimpleModule();
124138
module.addDeserializer(String.class, new JsonEnvVarDeserializer());
125-
objectMapper.registerModule(module);
139+
mapper.registerModule(module);
126140

127-
return objectMapper;
141+
return mapper;
128142
}
129143

130-
private class JsonEnvVarDeserializer extends JsonDeserializer<String> {
144+
public class JsonEnvVarDeserializer extends JsonDeserializer<String> {
131145

132146
@Override
133147
public String deserialize(JsonParser parser, DeserializationContext ctx) throws IOException {

flink-sql-runner/src/test/java/com/datasqrl/flinkrunner/EnvUtilsTest.java renamed to env-utils/src/test/java/com/datasqrl/flinkrunner/utils/EnvUtilsTest.java

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.datasqrl.flinkrunner;
16+
package com.datasqrl.flinkrunner.utils;
1717

1818
import static org.assertj.core.api.Assertions.assertThat;
1919
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@@ -25,7 +25,7 @@ class EnvUtilsTest {
2525

2626
@Test
2727
void givenSystemEnv_whenGetEnvWithDefaults_thenReturnsEnvWithDefaults() {
28-
var env = EnvUtils.getEnvWithDefaults();
28+
var env = EnvUtils.getEnvWithDeploymentDefaults();
2929

3030
assertThat(env).isNotNull();
3131
assertThat(env).containsKey("DEPLOYMENT_ID");
@@ -34,7 +34,7 @@ void givenSystemEnv_whenGetEnvWithDefaults_thenReturnsEnvWithDefaults() {
3434

3535
@Test
3636
void givenSystemEnv_whenGetEnvWithDefaults_thenDeploymentIdIsValidUuid() {
37-
var env = EnvUtils.getEnvWithDefaults();
37+
var env = EnvUtils.getEnvWithDeploymentDefaults();
3838

3939
String deploymentId = env.get("DEPLOYMENT_ID");
4040
assertThat(deploymentId)
@@ -44,7 +44,7 @@ void givenSystemEnv_whenGetEnvWithDefaults_thenDeploymentIdIsValidUuid() {
4444

4545
@Test
4646
void givenSystemEnv_whenGetEnvWithDefaults_thenDeploymentTimestampIsValidLong() {
47-
var env = EnvUtils.getEnvWithDefaults();
47+
var env = EnvUtils.getEnvWithDeploymentDefaults();
4848

4949
String timestamp = env.get("DEPLOYMENT_TIMESTAMP");
5050
assertThat(timestamp).isNotNull();
@@ -53,15 +53,15 @@ void givenSystemEnv_whenGetEnvWithDefaults_thenDeploymentTimestampIsValidLong()
5353

5454
@Test
5555
void givenSystemEnv_whenGetEnvWithDefaults_thenReturnsImmutableMap() {
56-
var env = EnvUtils.getEnvWithDefaults();
56+
var env = EnvUtils.getEnvWithDeploymentDefaults();
5757

5858
assertThatThrownBy(() -> env.put("NEW_KEY", "value"))
5959
.isInstanceOf(UnsupportedOperationException.class);
6060
}
6161

6262
@Test
6363
void givenSystemEnv_whenGetEnvWithDefaults_thenIncludesSystemEnvironmentVariables() {
64-
var env = EnvUtils.getEnvWithDefaults();
64+
var env = EnvUtils.getEnvWithDeploymentDefaults();
6565
var systemEnv = System.getenv();
6666

6767
// Verify that all system environment variables are present
@@ -72,32 +72,21 @@ void givenSystemEnv_whenGetEnvWithDefaults_thenIncludesSystemEnvironmentVariable
7272

7373
@Test
7474
void givenMultipleCalls_whenGetEnvWithDefaults_thenGeneratesDifferentDeploymentIds() {
75-
var env1 = EnvUtils.getEnvWithDefaults();
76-
var env2 = EnvUtils.getEnvWithDefaults();
75+
var env1 = EnvUtils.getEnvWithDeploymentDefaults();
76+
var env2 = EnvUtils.getEnvWithDeploymentDefaults();
7777

7878
assertThat(env1.get("DEPLOYMENT_ID")).isNotEqualTo(env2.get("DEPLOYMENT_ID"));
7979
}
8080

8181
@Test
8282
void givenMultipleCalls_whenGetEnvWithDefaults_thenGeneratesDifferentTimestamps()
8383
throws InterruptedException {
84-
var env1 = EnvUtils.getEnvWithDefaults();
84+
var env1 = EnvUtils.getEnvWithDeploymentDefaults();
8585
Thread.sleep(2); // Small delay to ensure different timestamps
86-
var env2 = EnvUtils.getEnvWithDefaults();
86+
var env2 = EnvUtils.getEnvWithDeploymentDefaults();
8787

8888
long timestamp1 = Long.parseLong(env1.get("DEPLOYMENT_TIMESTAMP"));
8989
long timestamp2 = Long.parseLong(env2.get("DEPLOYMENT_TIMESTAMP"));
9090
assertThat(timestamp2).isGreaterThanOrEqualTo(timestamp1);
9191
}
92-
93-
@Test
94-
void givenConstructor_whenInvoked_thenThrowsUnsupportedOperationException() {
95-
assertThatThrownBy(
96-
() -> {
97-
var constructor = EnvUtils.class.getDeclaredConstructor();
98-
constructor.setAccessible(true);
99-
constructor.newInstance();
100-
})
101-
.hasCauseInstanceOf(UnsupportedOperationException.class);
102-
}
10392
}

flink-sql-runner/src/test/java/com/datasqrl/flinkrunner/EnvVarResolverTest.java renamed to env-utils/src/test/java/com/datasqrl/flinkrunner/utils/EnvVarResolverTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.datasqrl.flinkrunner;
16+
package com.datasqrl.flinkrunner.utils;
1717

1818
import static org.assertj.core.api.Assertions.assertThat;
1919
import static org.assertj.core.api.Assertions.assertThatThrownBy;

flink-sql-runner/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@
118118
</dependency>
119119

120120
<!-- Compile -->
121+
<dependency>
122+
<groupId>com.datasqrl.flinkrunner</groupId>
123+
<artifactId>env-utils</artifactId>
124+
<version>${project.version}</version>
125+
</dependency>
126+
121127
<!-- Custom Connectors -->
122128
<dependency>
123129
<groupId>com.datasqrl.flinkrunner</groupId>

flink-sql-runner/src/main/java/com/datasqrl/flinkrunner/BaseRunner.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.datasqrl.flinkrunner;
1717

18+
import com.datasqrl.flinkrunner.utils.EnvVarResolver;
1819
import java.io.File;
1920
import java.io.IOException;
2021
import java.util.function.Supplier;

flink-sql-runner/src/main/java/com/datasqrl/flinkrunner/CliRunner.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.datasqrl.flinkrunner;
1717

18+
import com.datasqrl.flinkrunner.utils.EnvVarResolver;
1819
import java.util.Arrays;
1920
import java.util.concurrent.Callable;
2021
import javax.annotation.Nullable;

flink-sql-runner/src/main/java/com/datasqrl/flinkrunner/SqrlRunner.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.datasqrl.flinkrunner;
1717

18+
import com.datasqrl.flinkrunner.utils.EnvVarResolver;
1819
import javax.annotation.Nullable;
1920
import org.apache.flink.api.common.RuntimeExecutionMode;
2021
import org.apache.flink.configuration.Configuration;

flink-sql-runner/src/test/java/com/datasqrl/flinkrunner/CliRunnerTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static org.mockito.Mockito.verifyNoMoreInteractions;
2323
import static org.mockito.Mockito.when;
2424

25+
import com.datasqrl.flinkrunner.utils.EnvVarResolver;
2526
import java.io.IOException;
2627
import java.nio.file.Files;
2728
import java.nio.file.Path;

0 commit comments

Comments
 (0)