Skip to content

Commit e94f1a2

Browse files
committed
Changes in the way TNS_ASMIN logic is handled as TNS_ADMIN is resolved automatically by jdbc driver. So cli will only use the ORACLE_HOME resolution when TNS_ADMIN is not defined.
Removed `oraclepki` as pom dependency. It is now delivered together with jdbc as part of java-api 3.2.5.
1 parent b9abd0b commit e94f1a2

3 files changed

Lines changed: 61 additions & 18 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1515
<maven.compiler.release>17</maven.compiler.release>
1616

17-
<utplsql-java-api.version>3.2.4</utplsql-java-api.version>
18-
<!-- Temporarly here - it needs to be moved to java-api. Keep in sync with the ojdbc version used by utplsql-java-api -->
19-
<oraclepki.version>23.7.0.25.01</oraclepki.version>
17+
<utplsql-java-api.version>3.2.5-SNAPSHOT</utplsql-java-api.version>
2018

2119
<junit.jupiter.version>5.12.2</junit.jupiter.version>
2220
<picocli.version>4.7.7</picocli.version>
@@ -75,12 +73,6 @@
7573
<artifactId>utplsql-java-api</artifactId>
7674
<version>${utplsql-java-api.version}</version>
7775
</dependency>
78-
<!-- Temporarly here - it needs to be moved to java-api. Required by ojdbc to read Oracle Wallets (Secure External Password Store) -->
79-
<dependency>
80-
<groupId>com.oracle.database.security</groupId>
81-
<artifactId>oraclepki</artifactId>
82-
<version>${oraclepki.version}</version>
83-
</dependency>
8476
<dependency>
8577
<groupId>javax.xml.bind</groupId>
8678
<artifactId>jaxb-api</artifactId>

‎src/main/java/org/utplsql/cli/DataSourceProvider.java‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,32 @@
1313
*/
1414
public class DataSourceProvider {
1515

16+
private static final String TNS_ADMIN_PROPERTY = "oracle.net.tns_admin";
17+
1618
static {
17-
if (System.getProperty("oracle.net.tns_admin") == null) {
18-
String tnsAdmin = System.getenv("TNS_ADMIN");
19-
String oracleHome = System.getenv("ORACLE_HOME");
20-
if (tnsAdmin != null && !tnsAdmin.isEmpty()) {
21-
System.setProperty("oracle.net.tns_admin", tnsAdmin);
22-
} else if (oracleHome != null) {
23-
System.setProperty("oracle.net.tns_admin",
24-
String.join(File.separator, oracleHome, "NETWORK", "ADMIN"));
25-
}
19+
String tnsAdminFallback = getTnsAdminFallback(
20+
System.getProperty(TNS_ADMIN_PROPERTY), System.getenv("TNS_ADMIN"), System.getenv("ORACLE_HOME"));
21+
if (tnsAdminFallback != null) {
22+
System.setProperty(TNS_ADMIN_PROPERTY, tnsAdminFallback);
23+
}
24+
}
25+
26+
/**
27+
* The JDBC driver resolves tnsnames.ora / ojdbc.properties from the {@value TNS_ADMIN_PROPERTY} property
28+
* or the TNS_ADMIN environment variable on its own, but it doesn't look into ORACLE_HOME.
29+
* The property takes precedence over the environment variable, so it must not be set when TNS_ADMIN is.
30+
*
31+
* @return ORACLE_HOME/NETWORK/ADMIN when neither {@value TNS_ADMIN_PROPERTY} nor TNS_ADMIN is set, otherwise null
32+
*/
33+
static String getTnsAdminFallback(String tnsAdminProperty, String tnsAdminEnv, String oracleHome) {
34+
if (isEmpty(tnsAdminProperty) && isEmpty(tnsAdminEnv) && !isEmpty(oracleHome)) {
35+
return String.join(File.separator, oracleHome, "NETWORK", "ADMIN");
2636
}
37+
return null;
38+
}
39+
40+
private static boolean isEmpty(String value) {
41+
return value == null || value.isEmpty();
2742
}
2843

2944
public static DataSource getDataSource(String connectString, int maxConnections) throws SQLException {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.utplsql.cli;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.io.File;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
import static org.junit.jupiter.api.Assertions.assertNull;
9+
10+
class DataSourceProviderTest {
11+
12+
private static final String ORACLE_HOME = "/opt/oracle";
13+
14+
@Test
15+
void tnsAdminFallsBackToOracleHome() {
16+
assertEquals(String.join(File.separator, ORACLE_HOME, "NETWORK", "ADMIN"),
17+
DataSourceProvider.getTnsAdminFallback(null, null, ORACLE_HOME));
18+
}
19+
20+
@Test
21+
void tnsAdminEnvVariableIsLeftToJdbcDriver() {
22+
// Setting the property would override the TNS_ADMIN environment variable in the JDBC driver
23+
assertNull(DataSourceProvider.getTnsAdminFallback(null, "/my/tns_admin", ORACLE_HOME));
24+
}
25+
26+
@Test
27+
void tnsAdminPropertyIsNotOverridden() {
28+
assertNull(DataSourceProvider.getTnsAdminFallback("/my/tns_admin", null, ORACLE_HOME));
29+
}
30+
31+
@Test
32+
void noFallbackWithoutOracleHome() {
33+
assertNull(DataSourceProvider.getTnsAdminFallback(null, null, null));
34+
assertNull(DataSourceProvider.getTnsAdminFallback("", "", ""));
35+
}
36+
}

0 commit comments

Comments
 (0)