Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make easier to use TNS connections #119

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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