@@ -29,35 +29,103 @@ private OracleConstants() {
2929 public static final String PLUGIN_NAME = "Oracle" ;
3030 public static final String ORACLE_CONNECTION_STRING_SID_FORMAT = "jdbc:oracle:thin:@%s:%s:%s" ;
3131 public static final String ORACLE_CONNECTION_STRING_SERVICE_NAME_FORMAT = "jdbc:oracle:thin:@//%s:%s/%s" ;
32+ // Connection formats to accept protocol (e.g., jdbc:oracle:thin:@<protocol>://<host>:<port>/<SID>)
33+ public static final String ORACLE_CONNECTION_STRING_SID_FORMAT_WITH_PROTOCOL = "jdbc:oracle:thin:@%s:%s:%s/%s" ;
34+ public static final String ORACLE_CONNECTION_STRING_SERVICE_NAME_FORMAT_WITH_PROTOCOL =
35+ "jdbc:oracle:thin:@%s://%s:%s/%s" ;
3236 public static final String ORACLE_CONNECTION_STRING_TNS_FORMAT = "jdbc:oracle:thin:@%s" ;
3337 public static final String DEFAULT_BATCH_VALUE = "defaultBatchValue" ;
3438 public static final String DEFAULT_ROW_PREFETCH = "defaultRowPrefetch" ;
3539 public static final String SERVICE_CONNECTION_TYPE = "service" ;
3640 public static final String CONNECTION_TYPE = "connectionType" ;
3741 public static final String ROLE = "role" ;
3842 public static final String NAME_DATABASE = "database" ;
39- public static final String TNS_CONNECTION_TYPE = "TNS " ;
43+ public static final String TNS_CONNECTION_TYPE = "tns " ;
4044 public static final String TRANSACTION_ISOLATION_LEVEL = "transactionIsolationLevel" ;
45+ public static final String USE_SSL = "useSSL" ;
4146
4247 /**
43- * Returns the Connection String for the given ConnectionType.
48+ * Constructs the Oracle connection string based on the provided connection type, host, port, and database.
49+ * If SSL is enabled, the connection protocol will be "tcps" instead of "tcp".
4450 *
4551 * @param connectionType TNS/Service/SID
4652 * @param host Host name of the oracle server
4753 * @param port Port of the oracle server
4854 * @param database Database to connect to
49- * @return Connection String based on the given ConnectionType
55+ * @param useSSL Whether SSL/TLS is required(YES/NO)
56+ * @return Connection String based on the given parameters and connection type.
5057 */
5158 public static String getConnectionString (String connectionType ,
5259 @ Nullable String host ,
5360 @ Nullable int port ,
54- String database ) {
55- if (OracleConstants .TNS_CONNECTION_TYPE .equalsIgnoreCase (connectionType )) {
56- return String .format (OracleConstants .ORACLE_CONNECTION_STRING_TNS_FORMAT , database );
61+ String database ,
62+ @ Nullable Boolean useSSL ) {
63+ // Use protocol as "tcps" when SSL is requested or else use "tcp".
64+ String connectionProtocol ;
65+ boolean isSSLEnabled = false ;
66+ if (useSSL != null && useSSL ) {
67+ connectionProtocol = "tcps" ;
68+ isSSLEnabled = true ;
69+ } else {
70+ connectionProtocol = "tcp" ;
5771 }
58- if (OracleConstants .SERVICE_CONNECTION_TYPE .equalsIgnoreCase (connectionType )) {
59- return String .format (OracleConstants .ORACLE_CONNECTION_STRING_SERVICE_NAME_FORMAT ,
60- host , port , database );
72+
73+ switch (connectionType .toLowerCase ()) {
74+ case OracleConstants .TNS_CONNECTION_TYPE :
75+ // TNS connection doesn't require protocol
76+ return String .format (OracleConstants .ORACLE_CONNECTION_STRING_TNS_FORMAT , database );
77+ case OracleConstants .SERVICE_CONNECTION_TYPE :
78+ // Create connection string for SERVICE type.
79+ return getConnectionStringWithService (host , port , database , connectionProtocol , isSSLEnabled );
80+ default :
81+ // Default to SID format if no matching case is found.
82+ return getConnectionStringWithSID (host , port , database , connectionProtocol , isSSLEnabled );
83+ }
84+ }
85+
86+ /**
87+ * Constructs the connection string for a SERVICE connection type.
88+ *
89+ * @param host Host name of the Oracle server.
90+ * @param port Port of the Oracle server.
91+ * @param database Database name to connect to.
92+ * @param connectionProtocol Protocol to use for the connection ("tcp" or "tcps").
93+ * @param isSSLEnabled Indicates if SSL is enabled.
94+ * @return Formatted connection string for a SERVICE connection.
95+ */
96+ private static String getConnectionStringWithService (@ Nullable String host ,
97+ @ Nullable int port ,
98+ String database ,
99+ String connectionProtocol ,
100+ boolean isSSLEnabled ) {
101+ // Choose the appropriate format based on whether SSL is enabled.
102+ if (isSSLEnabled ) {
103+ return String .format (OracleConstants .ORACLE_CONNECTION_STRING_SERVICE_NAME_FORMAT_WITH_PROTOCOL ,
104+ connectionProtocol , host , port , database );
105+ }
106+ return String .format (OracleConstants .ORACLE_CONNECTION_STRING_SERVICE_NAME_FORMAT ,
107+ host , port , database );
108+ }
109+
110+ /**
111+ * Constructs the connection string for a SID connection type.
112+ *
113+ * @param host Host name of the Oracle server.
114+ * @param port Port of the Oracle server.
115+ * @param database Database name to connect to.
116+ * @param connectionProtocol Protocol to use for the connection ("tcp" or "tcps").
117+ * @param isSSLEnabled Indicates if SSL is enabled.
118+ * @return Formatted connection string for a SID connection.
119+ */
120+ private static String getConnectionStringWithSID (@ Nullable String host ,
121+ @ Nullable int port ,
122+ String database ,
123+ String connectionProtocol ,
124+ boolean isSSLEnabled ) {
125+ // Choose the appropriate format based on whether SSL is enabled.
126+ if (isSSLEnabled ) {
127+ return String .format (OracleConstants .ORACLE_CONNECTION_STRING_SID_FORMAT_WITH_PROTOCOL ,
128+ connectionProtocol , host , port , database );
61129 }
62130 return String .format (OracleConstants .ORACLE_CONNECTION_STRING_SID_FORMAT ,
63131 host , port , database );
0 commit comments