You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@freemarker.apache.org by dd...@apache.org on 2021/09/12 22:27:53 UTC
[freemarker-generator] 03/04: Allow overriding environment
variables with a system properties with special prefix for env:\\ data
source. It's useful when running examples for the documentation,
and in tests, because Java can't set environment variables.
This is an automated email from the ASF dual-hosted git repository.
ddekany pushed a commit to branch FREEMARKER-154
in repository https://gitbox.apache.org/repos/asf/freemarker-generator.git
commit 6a8d2e0e825ba0f02ed0ca16bfdffb81bb82f4d5
Author: ddekany <dd...@apache.org>
AuthorDate: Mon Sep 13 00:26:20 2021 +0200
Allow overriding environment variables with a system properties with special prefix for env:\\ data source. It's useful when running examples for the documentation, and in tests, because Java can't set environment variables.
---
.../generator/base/datasource/DataSourceFactory.java | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/datasource/DataSourceFactory.java b/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/datasource/DataSourceFactory.java
index 6930e91..e6f37f5 100644
--- a/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/datasource/DataSourceFactory.java
+++ b/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/datasource/DataSourceFactory.java
@@ -49,6 +49,8 @@ import static org.apache.freemarker.generator.base.FreeMarkerConstants.DEFAULT_G
*/
public abstract class DataSourceFactory {
+ private static final String ENV_VARIABLE_OVERRIDE_SYSTEM_PROPERTY_PREFIX = "freemarker.generator.datasource.envOverride.";
+
private DataSourceFactory() {
}
@@ -161,9 +163,15 @@ public abstract class DataSourceFactory {
}
public static DataSource fromEnvironment(String name, String group, String key, String contentType) {
- Validate.notEmpty(System.getenv(key), "Environment variable not found: " + key);
+ // Overriding the value with system property is useful when running examples for the documentation, and in tests,
+ // because Java can't set environment variables.
+ String value = System.getProperty(ENV_VARIABLE_OVERRIDE_SYSTEM_PROPERTY_PREFIX + key);
+ if (value == null) {
+ value = System.getenv(key);
+ }
+ Validate.notEmpty(value, "Environment variable not found: " + key);
- final StringDataSource dataSource = new StringDataSource(name, System.getenv(key), contentType, UTF_8);
+ final StringDataSource dataSource = new StringDataSource(name, value, contentType, UTF_8);
final URI uri = UriUtils.toUri(Location.ENVIRONMENT, key);
return create(name, group, uri, dataSource, contentType, UTF_8, noProperties());
}