Skip to content

Commit

Permalink
Read ORACLE_HOME and TNS_ADMIN from the Windows registry
Browse files Browse the repository at this point in the history
  • Loading branch information
felipebz committed Mar 18, 2019
1 parent 31dd9b7 commit 3dbe4d5
Show file tree
Hide file tree
Showing 3 changed files with 449 additions and 5 deletions.
67 changes: 63 additions & 4 deletions src/main/java/org/utplsql/cli/DataSourceProvider.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,77 @@
package org.utplsql.cli;

import com.zaxxer.hikari.HikariDataSource;
import org.utplsql.api.EnvironmentVariableUtil;
import org.utplsql.cli.datasource.TestedDataSourceProvider;

import javax.sql.DataSource;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

/** Helper class to give you a ready-to-use datasource
*
* @author pesse
*/
public class DataSourceProvider {

private static String oracleHome = null;
private static String tnsAdmin = null;

static {
String oracleHome = System.getenv("ORACLE_HOME");
if (oracleHome != null && System.getProperty("oracle.net.tns_admin") == null) {
System.setProperty("oracle.net.tns_admin",
String.join(File.separator, oracleHome, "NETWORK", "ADMIN"));
String tnsAdmin = loadOracleHomeAndTnsAdmin();
if (tnsAdmin != null && System.getProperty("oracle.net.tns_admin") == null) {
System.setProperty("oracle.net.tns_admin", tnsAdmin);
}
}

private static String loadOracleHomeAndTnsAdmin() {
tnsAdmin = EnvironmentVariableUtil.getEnvValue("TNS_ADMIN");
if (directoryExists(tnsAdmin)) {
return tnsAdmin;
}

oracleHome = EnvironmentVariableUtil.getEnvValue("ORACLE_HOME");
if (oracleHome != null) {
tnsAdmin = String.join(File.separator, oracleHome, "NETWORK", "ADMIN");

if (directoryExists(tnsAdmin)) {
return tnsAdmin;
}
}

try {
final int root = WinRegistry.HKEY_LOCAL_MACHINE;
final String key = "SOFTWARE\\Oracle";

List<String> oracleKeys = WinRegistry.readStringSubKeys(root, key);
for (String oracleKey : oracleKeys) {
String temp = key + "\\" + oracleKey;

// check for HKEY_LOCAL_MACHINE\SOFTWARE\Oracle\<oracle client>\TNS_ADMIN
tnsAdmin = WinRegistry.readString(root, temp, "TNS_ADMIN");
if (directoryExists(tnsAdmin)) {
return tnsAdmin;
}

// check for HKEY_LOCAL_MACHINE\SOFTWARE\Oracle\<oracle client>\ORACLE_HOME
oracleHome = WinRegistry.readString(root, temp, "ORACLE_HOME");
tnsAdmin = String.join(File.separator, oracleHome, "NETWORK", "ADMIN");
if (directoryExists(tnsAdmin)) {
return tnsAdmin;
}
}
} catch (ExceptionInInitializerError | IllegalAccessException | InvocationTargetException e) {
}

return null;
}

private static boolean directoryExists(String path) {
return path != null && new File(path).exists();
}

public static DataSource getDataSource(ConnectionInfo info, int maxConnections ) throws SQLException {

requireOjdbc();
Expand All @@ -33,6 +84,14 @@ public static DataSource getDataSource(ConnectionInfo info, int maxConnections )
return pds;
}

public static String getOracleHome() {
return oracleHome;
}

public static String getTnsAdmin() {
return tnsAdmin;
}

private static void requireOjdbc() {
if ( !OracleLibraryChecker.checkOjdbcExists() )
{
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/utplsql/cli/RunCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ private void outputMainInformation() {
formatter.appendLine(CliVersionInfo.getInfo());
formatter.appendLine(JavaApiVersionInfo.getInfo());
formatter.appendLine("Java-Version: " + System.getProperty("java.version"));
formatter.appendLine("ORACLE_HOME: " + EnvironmentVariableUtil.getEnvValue("ORACLE_HOME"));
formatter.appendLine("ORACLE_HOME: " + DataSourceProvider.getOracleHome());
formatter.appendLine("TNS_ADMIN: " + DataSourceProvider.getTnsAdmin());
formatter.appendLine("NLS_LANG: " + EnvironmentVariableUtil.getEnvValue("NLS_LANG"));
formatter.appendLine("");
formatter.appendLine("Thanks for testing!");
Expand Down
Loading

0 comments on commit 3dbe4d5

Please sign in to comment.