Skip to content

Commit 62bf267

Browse files
authored
Merge pull request #25732 from OndroMih/ondromih-fix-prefer-env-vars
Fixes in env vars references
2 parents d2a27ca + b9b46c9 commit 62bf267

File tree

5 files changed

+28
-21
lines changed

5 files changed

+28
-21
lines changed

docs/administration-guide/src/main/asciidoc/overview.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,8 @@ If a variable reference is not resolved, the reference will not be replaced. So,
313313

314314
**System properties that adjust variable resolution:**
315315

316-
* `org.glassfish.variableexpansion.envvars.disabled` - disables resolution from environment variables and allows only resolving from system properties. This allows keeping the behavior of older versions of {productName}
317-
* `org.glassfish.variableexpansion.envvars.preferred` - changes the priority - values are resolved first from environment variables and then from system properties
316+
* `org.glassfish.variableExpansion.envDisabled` - disables resolution from environment variables and allows only resolving from system properties. This allows keeping the behavior of older versions of {productName}
317+
* `org.glassfish.variableExpansion.envPreferred` - changes the priority - values are resolved first from environment variables and then from system properties
318318

319319
The following is a list of variables defined by {productName} that can be used in variable references:
320320

docs/reference-manual/src/main/asciidoc/create-jvm-options.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ Variable references, e.g. `${variable}`, can be used anywhere in JVM options. In
7070
5. `asenv` properties
7171
6. Environment variables
7272

73-
If a property `org.glassfish.envPreferredToProperties` is defined as `true` (in any source except environment variables), then environment variables take precedence over other sources, instead of being the last resolved source. It's also possible to override options and system properties defined in JVM options with an environment variable that either has the same name, or same name if case is ignored and non-alphanumeric characters are replaced with the `_` character. For example, a property `-Dmy.name` can be defined by redefined by environment variables `my.name`, `my_name`, or `MY_NAME`.
73+
If a property `org.glassfish.variableExpansion.envPreferred` is defined as `true` (in any source except environment variables), then environment variables take precedence over other sources, instead of being the last resolved source. It's also possible to override options and system properties defined in JVM options with an environment variable that either has the same name, or same name if case is ignored and non-alphanumeric characters are replaced with the `_` character. For example, a property `-Dmy.name` can be defined by redefined by environment variables `my.name`, `my_name`, or `MY_NAME`. If a property `org.glassfish.variableExpansion.envDisabled` is defined as `true`, then environment variables are ignored.
7474

7575
[CAUTION]
7676
====

nucleus/admin/config-api/src/main/java/org/glassfish/config/support/TranslatedConfigView.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.glassfish.config.support;
1818

1919
import com.sun.enterprise.security.store.DomainScopedPasswordAliasStore;
20+
import com.sun.enterprise.util.SystemPropertyConstants;
2021

2122
import java.io.IOException;
2223
import java.lang.reflect.Method;
@@ -47,8 +48,6 @@ public class TranslatedConfigView implements ConfigView {
4748

4849
private static final String ALIAS_TOKEN = "ALIAS";
4950
private static final int MAX_SUBSTITUTION_DEPTH = 100;
50-
private static final String DISABLE_ENV_VAR_EXPANSION_PROPERTY = "org.glassfish.variableexpansion.envvars.disabled";
51-
private static final String ENV_VAR_EXPANSION_PREFERRED_PROPERTY = "org.glassfish.variableexpansion.envvars.preferred";
5251

5352
public static String expandValue(String value) {
5453
return (String) getTranslatedValue(value);
@@ -113,10 +112,10 @@ public static Object getTranslatedValue(Object value) {
113112
// such as a=${a} or a=foo ${b} and b=bar {$a}
114113
Matcher m = p.matcher(stringValue);
115114
String origValue = stringValue;
116-
boolean useEnvVars = !Boolean.getBoolean(DISABLE_ENV_VAR_EXPANSION_PROPERTY);
115+
boolean useEnvVars = !Boolean.getBoolean(SystemPropertyConstants.DISABLE_ENV_VAR_EXPANSION_PROPERTY);
117116
boolean preferEnvVars = false;
118117
if (useEnvVars) {
119-
preferEnvVars = Boolean.getBoolean(ENV_VAR_EXPANSION_PREFERRED_PROPERTY);
118+
preferEnvVars = Boolean.getBoolean(SystemPropertyConstants.PREFER_ENV_VARS_OVER_PROPERTIES);
120119
}
121120
int i = 0;
122121
while (m.find() && i < MAX_SUBSTITUTION_DEPTH) {
@@ -146,12 +145,12 @@ private static String getPropertyValue(String variableName, boolean useEnvVars,
146145
if (value == null && useEnvVars) {
147146
value = System.getenv(variableName);
148147
if (value == null) {
149-
variableName = INVALID_ENV_VAR_CHARS_PATTERN.matcher(variableName)
148+
String modifiedVariableName = INVALID_ENV_VAR_CHARS_PATTERN.matcher(variableName)
150149
.replaceAll("_");
151-
value = System.getenv(variableName);
150+
value = System.getenv(modifiedVariableName);
152151
if (value == null) {
153-
variableName = variableName.toUpperCase();
154-
value = System.getenv(variableName);
152+
modifiedVariableName = modifiedVariableName.toUpperCase();
153+
value = System.getenv(modifiedVariableName);
155154
}
156155
if (value == null && preferEnvVars) {
157156
value = System.getProperty(variableName);

nucleus/admin/launcher/src/main/java/com/sun/enterprise/admin/launcher/GFLauncher.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import static com.sun.enterprise.universal.process.ProcessStreamDrainer.save;
6161
import static com.sun.enterprise.util.OS.isDarwin;
6262
import static com.sun.enterprise.util.SystemPropertyConstants.DEBUG_MODE_PROPERTY;
63+
import static com.sun.enterprise.util.SystemPropertyConstants.DISABLE_ENV_VAR_EXPANSION_PROPERTY;
6364
import static com.sun.enterprise.util.SystemPropertyConstants.DROP_INTERRUPTED_COMMANDS;
6465
import static com.sun.enterprise.util.SystemPropertyConstants.PREFER_ENV_VARS_OVER_PROPERTIES;
6566
import static java.lang.Boolean.TRUE;
@@ -640,15 +641,17 @@ private void resolveAllTokens() {
640641
all.putAll(domainXMLjvmOptions.getCombinedMap());
641642
all.putAll(domainXMLJavaConfigProfiler.getConfig());
642643

643-
if (isPreferEnvOverProperties(all)) {
644-
all.putAll(System.getenv());
645-
replacePropertiesWithEnvVars(domainXMLjvmOptions.xProps);
646-
replacePropertiesWithEnvVars(domainXMLjvmOptions.xxProps);
647-
replacePropertiesWithEnvVars(domainXMLjvmOptions.plainProps);
648-
replacePropertiesWithEnvVars(domainXMLjvmOptions.longProps);
649-
replacePropertiesWithEnvVars(domainXMLjvmOptions.sysProps);
650-
} else {
651-
System.getenv().forEach((name, value) -> all.putIfAbsent(name, value));
644+
if (!isEnvVarExpansionDisabled(all)) {
645+
if (isPreferEnvOverProperties(all)) {
646+
all.putAll(System.getenv());
647+
replacePropertiesWithEnvVars(domainXMLjvmOptions.xProps);
648+
replacePropertiesWithEnvVars(domainXMLjvmOptions.xxProps);
649+
replacePropertiesWithEnvVars(domainXMLjvmOptions.plainProps);
650+
replacePropertiesWithEnvVars(domainXMLjvmOptions.longProps);
651+
replacePropertiesWithEnvVars(domainXMLjvmOptions.sysProps);
652+
} else {
653+
System.getenv().forEach((name, value) -> all.putIfAbsent(name, value));
654+
}
652655
}
653656

654657
TokenResolver resolver = new TokenResolver(all);
@@ -693,6 +696,10 @@ private static Boolean isPreferEnvOverProperties(Map<String, String> properties)
693696
return Boolean.parseBoolean(properties.get(PREFER_ENV_VARS_OVER_PROPERTIES));
694697
}
695698

699+
private static boolean isEnvVarExpansionDisabled(Map<String, String> all) {
700+
return Boolean.parseBoolean(all.get(DISABLE_ENV_VAR_EXPANSION_PROPERTY));
701+
}
702+
696703
private void fixLogFilename() throws GFLauncherException {
697704
if (!ok(logFilename)) {
698705
logFilename = DEFAULT_LOGFILE;

nucleus/common/common-util/src/main/java/com/sun/enterprise/util/SystemPropertyConstants.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ public class SystemPropertyConstants {
7777
public static final String DEFAULT_ADMIN_TIMEOUT_PROPERTY = "org.glassfish.admin.timeout";
7878
private static final int DEFAULT_ADMIN_TIMEOUT_VALUE = 5000;
7979

80-
public static final String PREFER_ENV_VARS_OVER_PROPERTIES = "org.glassfish.envPreferredToProperties";
80+
public static final String PREFER_ENV_VARS_OVER_PROPERTIES = "org.glassfish.variableExpansion.envPreferred";
81+
public static final String DISABLE_ENV_VAR_EXPANSION_PROPERTY = "org.glassfish.variableExpansion.envDisabled";
8182

8283
public static final String TRUSTSTORE_FILENAME_DEFAULT = "cacerts.p12";
8384
public static final String KEYSTORE_FILENAME_DEFAULT = "keystore.p12";

0 commit comments

Comments
 (0)