Skip to content

Commit 6969e2c

Browse files
Consolidate parameter fallback utils (#236)
* Move duplicated getParameterWithFallback logic from AWS and HashiCorp modules into a shared ParameterUtils class in the common module. * Fix Javadoc
1 parent 855a1bf commit 6969e2c

File tree

3 files changed

+69
-52
lines changed

3 files changed

+69
-52
lines changed

ojdbc-provider-aws/src/main/java/oracle/jdbc/provider/aws/appconfig/AppConfigFactory.java

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import java.io.InputStream;
5353

5454
import static oracle.jdbc.provider.aws.configuration.AwsConfigurationParameters.*;
55+
import static oracle.jdbc.provider.util.ParameterUtils.getParameterWithFallback;
5556

5657
/**
5758
* A factory for retrieving <b>Freeform Configurations</b> data from AWS
@@ -132,29 +133,4 @@ public Resource<InputStream> request(AwsCredentials awsCredentials, ParameterSet
132133
false);
133134
}
134135
}
135-
136-
/**
137-
* Retrieves a parameter value with fallback to system property and environment variable.
138-
* @param parameter The Parameter object to retrieve from ParameterSet.
139-
* @param sysPropKey The system property key to check.
140-
* @param envVarKey The environment variable key to check.
141-
* @param paramSet The ParameterSet to check first.
142-
* @return The parameter value, or throws an exception if not found.
143-
* @throws IllegalArgumentException if the parameter is not found in any source.
144-
*/
145-
private static String getParameterWithFallback(Parameter<String> parameter, String sysPropKey, String envVarKey, ParameterSet paramSet) {
146-
String value = paramSet.getOptional(parameter);
147-
if (value == null) {
148-
value = System.getProperty(sysPropKey);
149-
if (value == null) {
150-
value = System.getenv(envVarKey);
151-
if (value == null) {
152-
throw new IllegalArgumentException(
153-
String.format("Parameter '%s' is required and not found in ParameterSet, system property '%s', or environment variable '%s'",
154-
paramSet.getName(parameter), sysPropKey, envVarKey));
155-
}
156-
}
157-
}
158-
return value;
159-
}
160136
}

ojdbc-provider-hashicorp/src/main/java/oracle/jdbc/provider/hashicorp/util/Parameterutil.java renamed to ojdbc-provider-common/src/main/java/oracle/jdbc/provider/util/ParameterUtils.java

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** Copyright (c) 2025 Oracle and/or its affiliates.
2+
** Copyright (c) 2026 Oracle and/or its affiliates.
33
**
44
** The Universal Permissive License (UPL), Version 1.0
55
**
@@ -36,14 +36,21 @@
3636
** SOFTWARE.
3737
*/
3838

39-
package oracle.jdbc.provider.hashicorp.util;
39+
package oracle.jdbc.provider.util;
40+
41+
import oracle.jdbc.provider.parameter.Parameter;
42+
import oracle.jdbc.provider.parameter.ParameterSet;
4043

4144
/**
42-
* Utility class for parameter resolution.
45+
* Utility class for parameter resolution with fallback support.
46+
* <p>
47+
* This class provides methods to retrieve parameter values from multiple sources
48+
* with a priority order: ParameterSet → System Properties → Environment Variables.
49+
* </p>
4350
*/
44-
public final class Parameterutil {
51+
public final class ParameterUtils {
4552

46-
private Parameterutil() {
53+
private ParameterUtils() {
4754
// Prevent instantiation.
4855
}
4956

@@ -57,4 +64,38 @@ private Parameterutil() {
5764
public static String getFallback(String key) {
5865
return System.getProperty(key, System.getenv(key));
5966
}
60-
}
67+
68+
/**
69+
* Retrieves a parameter value with cascading fallback to system property and environment variable.
70+
* <p>
71+
* The lookup order is:
72+
* <ol>
73+
* <li>ParameterSet</li>
74+
* <li>System property</li>
75+
* <li>Environment variable</li>
76+
* </ol>
77+
*
78+
* @param parameter The Parameter object to retrieve from ParameterSet
79+
* @param sysPropKey The system property key to check as fallback
80+
* @param envVarKey The environment variable key to check as fallback
81+
* @param paramSet The ParameterSet to check first
82+
* @return The parameter value from the first available source
83+
* @throws IllegalArgumentException if the parameter is not found in any source
84+
*/
85+
public static String getParameterWithFallback(Parameter<String> parameter,
86+
String sysPropKey, String envVarKey, ParameterSet paramSet) {
87+
String value = paramSet.getOptional(parameter);
88+
if (value == null) {
89+
value = System.getProperty(sysPropKey);
90+
if (value == null) {
91+
value = System.getenv(envVarKey);
92+
if (value == null) {
93+
throw new IllegalArgumentException(
94+
String.format("Parameter '%s' is required and not found in ParameterSet, system property '%s', or environment variable '%s'",
95+
paramSet.getName(parameter), sysPropKey, envVarKey));
96+
}
97+
}
98+
}
99+
return value;
100+
}
101+
}

ojdbc-provider-hashicorp/src/main/java/oracle/jdbc/provider/hashicorp/hcpvaultdedicated/authentication/DedicatedVaultParameters.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838

3939
package oracle.jdbc.provider.hashicorp.hcpvaultdedicated.authentication;
4040

41-
import oracle.jdbc.provider.hashicorp.util.Parameterutil;
4241
import oracle.jdbc.provider.parameter.Parameter;
4342
import oracle.jdbc.provider.parameter.ParameterSet;
4443
import oracle.jdbc.provider.parameter.ParameterSetParser;
44+
import oracle.jdbc.provider.util.ParameterUtils;
4545

4646
import java.util.HashMap;
4747
import java.util.Map;
@@ -213,37 +213,37 @@ public static ParameterSet buildResolvedParameterSet(Map<String, String> inputOp
213213
DedicatedVaultAuthenticationMethod authMethod =
214214
DedicatedVaultAuthenticationMethod.valueOf(authStr.toUpperCase());
215215

216-
opts.computeIfAbsent(PARAM_VAULT_ADDR, Parameterutil::getFallback);
217-
opts.computeIfAbsent(PARAM_VAULT_NAMESPACE, Parameterutil::getFallback);
216+
opts.computeIfAbsent(PARAM_VAULT_ADDR, ParameterUtils::getFallback);
217+
opts.computeIfAbsent(PARAM_VAULT_NAMESPACE, ParameterUtils::getFallback);
218218

219219
switch (authMethod) {
220220
case VAULT_TOKEN:
221-
opts.computeIfAbsent(PARAM_VAULT_TOKEN, Parameterutil::getFallback);
221+
opts.computeIfAbsent(PARAM_VAULT_TOKEN, ParameterUtils::getFallback);
222222
break;
223223
case GITHUB:
224-
opts.computeIfAbsent(PARAM_GITHUB_TOKEN, Parameterutil::getFallback);
225-
opts.computeIfAbsent(PARAM_GITHUB_AUTH_PATH, Parameterutil::getFallback);
224+
opts.computeIfAbsent(PARAM_GITHUB_TOKEN, ParameterUtils::getFallback);
225+
opts.computeIfAbsent(PARAM_GITHUB_AUTH_PATH, ParameterUtils::getFallback);
226226
break;
227227
case APPROLE:
228-
opts.computeIfAbsent(PARAM_VAULT_ROLE_ID, Parameterutil::getFallback);
229-
opts.computeIfAbsent(PARAM_VAULT_SECRET_ID, Parameterutil::getFallback);
230-
opts.computeIfAbsent(PARAM_APPROLE_AUTH_PATH, Parameterutil::getFallback);
228+
opts.computeIfAbsent(PARAM_VAULT_ROLE_ID, ParameterUtils::getFallback);
229+
opts.computeIfAbsent(PARAM_VAULT_SECRET_ID, ParameterUtils::getFallback);
230+
opts.computeIfAbsent(PARAM_APPROLE_AUTH_PATH, ParameterUtils::getFallback);
231231
break;
232232
case USERPASS:
233-
opts.computeIfAbsent(PARAM_VAULT_USERNAME, Parameterutil::getFallback);
234-
opts.computeIfAbsent(PARAM_VAULT_PASSWORD, Parameterutil::getFallback);
235-
opts.computeIfAbsent(PARAM_USERPASS_AUTH_PATH, Parameterutil::getFallback);
233+
opts.computeIfAbsent(PARAM_VAULT_USERNAME, ParameterUtils::getFallback);
234+
opts.computeIfAbsent(PARAM_VAULT_PASSWORD, ParameterUtils::getFallback);
235+
opts.computeIfAbsent(PARAM_USERPASS_AUTH_PATH, ParameterUtils::getFallback);
236236
break;
237237
case AUTO_DETECT:
238-
opts.computeIfAbsent(PARAM_VAULT_TOKEN, Parameterutil::getFallback);
239-
opts.computeIfAbsent(PARAM_VAULT_USERNAME, Parameterutil::getFallback);
240-
opts.computeIfAbsent(PARAM_VAULT_PASSWORD, Parameterutil::getFallback);
241-
opts.computeIfAbsent(PARAM_USERPASS_AUTH_PATH, Parameterutil::getFallback);
242-
opts.computeIfAbsent(PARAM_VAULT_ROLE_ID, Parameterutil::getFallback);
243-
opts.computeIfAbsent(PARAM_VAULT_SECRET_ID, Parameterutil::getFallback);
244-
opts.computeIfAbsent(PARAM_APPROLE_AUTH_PATH, Parameterutil::getFallback);
245-
opts.computeIfAbsent(PARAM_GITHUB_TOKEN, Parameterutil::getFallback);
246-
opts.computeIfAbsent(PARAM_GITHUB_AUTH_PATH, Parameterutil::getFallback);
238+
opts.computeIfAbsent(PARAM_VAULT_TOKEN, ParameterUtils::getFallback);
239+
opts.computeIfAbsent(PARAM_VAULT_USERNAME, ParameterUtils::getFallback);
240+
opts.computeIfAbsent(PARAM_VAULT_PASSWORD, ParameterUtils::getFallback);
241+
opts.computeIfAbsent(PARAM_USERPASS_AUTH_PATH, ParameterUtils::getFallback);
242+
opts.computeIfAbsent(PARAM_VAULT_ROLE_ID, ParameterUtils::getFallback);
243+
opts.computeIfAbsent(PARAM_VAULT_SECRET_ID, ParameterUtils::getFallback);
244+
opts.computeIfAbsent(PARAM_APPROLE_AUTH_PATH, ParameterUtils::getFallback);
245+
opts.computeIfAbsent(PARAM_GITHUB_TOKEN, ParameterUtils::getFallback);
246+
opts.computeIfAbsent(PARAM_GITHUB_AUTH_PATH, ParameterUtils::getFallback);
247247
break;
248248
default:
249249
break;

0 commit comments

Comments
 (0)