diff --git a/oracle-plugin/src/main/java/io/cdap/plugin/oracle/OracleAction.java b/oracle-plugin/src/main/java/io/cdap/plugin/oracle/OracleAction.java index 81604858a..9b5331d11 100644 --- a/oracle-plugin/src/main/java/io/cdap/plugin/oracle/OracleAction.java +++ b/oracle-plugin/src/main/java/io/cdap/plugin/oracle/OracleAction.java @@ -57,16 +57,9 @@ public static class OracleActionConfig extends DBSpecificQueryConfig { @Override public String getConnectionString() { - if (OracleConstants.TNS_CONNECTION_TYPE.equals(this.connectionType)) { - return String.format(OracleConstants.ORACLE_CONNECTION_STRING_TNS_FORMAT, database); - } else if (OracleConstants.SERVICE_CONNECTION_TYPE.equals(this.connectionType)) { - return String.format(OracleConstants.ORACLE_CONNECTION_STRING_SERVICE_NAME_FORMAT, host, port, database); - } else { - return String.format(OracleConstants.ORACLE_CONNECTION_STRING_SID_FORMAT, host, port, database); - } + return OracleConstants.getConnectionString(this.connectionType, host, port, database); } - @Override protected Map getDBSpecificArguments() { return ImmutableMap.of(OracleConstants.DEFAULT_BATCH_VALUE, String.valueOf(defaultBatchValue)); diff --git a/oracle-plugin/src/main/java/io/cdap/plugin/oracle/OracleConnector.java b/oracle-plugin/src/main/java/io/cdap/plugin/oracle/OracleConnector.java index fde72c8ad..bc7907b26 100644 --- a/oracle-plugin/src/main/java/io/cdap/plugin/oracle/OracleConnector.java +++ b/oracle-plugin/src/main/java/io/cdap/plugin/oracle/OracleConnector.java @@ -125,15 +125,8 @@ protected String getConnectionString(@Nullable String database) { if (database == null) { return config.getConnectionString(); } - if (OracleConstants.TNS_CONNECTION_TYPE.equals(config.getConnectionType())) { - return String.format(OracleConstants.ORACLE_CONNECTION_STRING_TNS_FORMAT, database); - } else if (OracleConstants.SERVICE_CONNECTION_TYPE.equals(config.getConnectionType())) { - return String.format(OracleConstants.ORACLE_CONNECTION_STRING_SERVICE_NAME_FORMAT, config.getHost(), - config.getPort(), database); - } else { - return String.format(OracleConstants.ORACLE_CONNECTION_STRING_SID_FORMAT, - config.getHost(), config.getPort(), database); - } + return OracleConstants.getConnectionString(config.getConnectionType(), + config.getHost(), config.getPort(), database); } @Override diff --git a/oracle-plugin/src/main/java/io/cdap/plugin/oracle/OracleConnectorConfig.java b/oracle-plugin/src/main/java/io/cdap/plugin/oracle/OracleConnectorConfig.java index 73b005243..716cbd310 100644 --- a/oracle-plugin/src/main/java/io/cdap/plugin/oracle/OracleConnectorConfig.java +++ b/oracle-plugin/src/main/java/io/cdap/plugin/oracle/OracleConnectorConfig.java @@ -63,13 +63,7 @@ public OracleConnectorConfig(String host, int port, String user, String password @Override public String getConnectionString() { - if (OracleConstants.TNS_CONNECTION_TYPE.equals(getConnectionType())) { - return String.format(OracleConstants.ORACLE_CONNECTION_STRING_TNS_FORMAT, database); - } else if (OracleConstants.SERVICE_CONNECTION_TYPE.equals(getConnectionType())) { - return String.format(OracleConstants.ORACLE_CONNECTION_STRING_SERVICE_NAME_FORMAT, host, getPort(), database); - } else { - return String.format(OracleConstants.ORACLE_CONNECTION_STRING_SID_FORMAT, host, getPort(), database); - } + return OracleConstants.getConnectionString(database, host, getPort(), database); } @Name(OracleConstants.CONNECTION_TYPE) diff --git a/oracle-plugin/src/main/java/io/cdap/plugin/oracle/OracleConstants.java b/oracle-plugin/src/main/java/io/cdap/plugin/oracle/OracleConstants.java index 040780a89..ec44d7b94 100644 --- a/oracle-plugin/src/main/java/io/cdap/plugin/oracle/OracleConstants.java +++ b/oracle-plugin/src/main/java/io/cdap/plugin/oracle/OracleConstants.java @@ -16,6 +16,8 @@ package io.cdap.plugin.oracle; +import javax.annotation.Nullable; + /** * Oracle Constants. */ @@ -36,4 +38,28 @@ private OracleConstants() { public static final String NAME_DATABASE = "database"; public static final String TNS_CONNECTION_TYPE = "TNS"; public static final String TRANSACTION_ISOLATION_LEVEL = "transactionIsolationLevel"; + + /** + * Returns the Connection String for the given ConnectionType. + * + * @param connectionType TNS/Service/SID + * @param host Host name of the oracle server + * @param port Port of the oracle server + * @param database Database to connect to + * @return Connection String based on the given ConnectionType + */ + public static String getConnectionString(String connectionType, + @Nullable String host, + @Nullable int port, + String database) { + if (OracleConstants.TNS_CONNECTION_TYPE.equalsIgnoreCase(connectionType)) { + return String.format(OracleConstants.ORACLE_CONNECTION_STRING_TNS_FORMAT, database); + } + if (OracleConstants.SERVICE_CONNECTION_TYPE.equalsIgnoreCase(connectionType)) { + return String.format(OracleConstants.ORACLE_CONNECTION_STRING_SERVICE_NAME_FORMAT, + host, port, database); + } + return String.format(OracleConstants.ORACLE_CONNECTION_STRING_SID_FORMAT, + host, port, database); + } } diff --git a/oracle-plugin/src/main/java/io/cdap/plugin/oracle/OraclePostAction.java b/oracle-plugin/src/main/java/io/cdap/plugin/oracle/OraclePostAction.java index f8ebd9ac2..4862aebfa 100644 --- a/oracle-plugin/src/main/java/io/cdap/plugin/oracle/OraclePostAction.java +++ b/oracle-plugin/src/main/java/io/cdap/plugin/oracle/OraclePostAction.java @@ -57,13 +57,7 @@ public static class OracleQueryActionConfig extends DBSpecificQueryActionConfig @Override public String getConnectionString() { - if (OracleConstants.TNS_CONNECTION_TYPE.equals(this.connectionType)) { - return String.format(OracleConstants.ORACLE_CONNECTION_STRING_TNS_FORMAT, database); - } else if (OracleConstants.SERVICE_CONNECTION_TYPE.equals(this.connectionType)) { - return String.format(OracleConstants.ORACLE_CONNECTION_STRING_SERVICE_NAME_FORMAT, host, port, database); - } else { - return String.format(OracleConstants.ORACLE_CONNECTION_STRING_SID_FORMAT, host, port, database); - } + return OracleConstants.getConnectionString(this.connectionType, host, port, database); } @Override diff --git a/oracle-plugin/src/main/java/io/cdap/plugin/oracle/OracleSource.java b/oracle-plugin/src/main/java/io/cdap/plugin/oracle/OracleSource.java index 9a554a4a4..eca7e2532 100644 --- a/oracle-plugin/src/main/java/io/cdap/plugin/oracle/OracleSource.java +++ b/oracle-plugin/src/main/java/io/cdap/plugin/oracle/OracleSource.java @@ -131,15 +131,8 @@ public OracleSourceConfig(String host, int port, String user, String password, S @Override public String getConnectionString() { - if (OracleConstants.TNS_CONNECTION_TYPE.equals(connection.getConnectionType())) { - return String.format(OracleConstants.ORACLE_CONNECTION_STRING_TNS_FORMAT, connection.getDatabase()); - } else if (OracleConstants.SERVICE_CONNECTION_TYPE.equals(connection.getConnectionType())) { - return String.format(OracleConstants.ORACLE_CONNECTION_STRING_SERVICE_NAME_FORMAT, connection.getHost(), - connection.getPort(), connection.getDatabase()); - } else { - return String.format(OracleConstants.ORACLE_CONNECTION_STRING_SID_FORMAT, - connection.getHost(), connection.getPort(), connection.getDatabase()); - } + return OracleConstants.getConnectionString(connection.getConnectionType(), connection.getHost(), + connection.getPort(), connection.getDatabase()); } @Override