17
17
18
18
import com .datastax .oss .driver .api .core .ConsistencyLevel ;
19
19
import com .datastax .oss .driver .internal .core .loadbalancing .DefaultLoadBalancingPolicy ;
20
- import com .ing .data .cassandra .jdbc .utils .JdbcUrlUtil ;
20
+ import com .ing .data .cassandra .jdbc .utils .ContactPoint ;
21
21
22
22
import javax .sql .ConnectionPoolDataSource ;
23
23
import javax .sql .DataSource ;
24
24
import java .io .PrintWriter ;
25
25
import java .sql .DriverManager ;
26
26
import java .sql .SQLException ;
27
27
import java .sql .SQLFeatureNotSupportedException ;
28
- import java .sql .SQLNonTransientConnectionException ;
28
+ import java .util .Collections ;
29
+ import java .util .List ;
29
30
import java .util .Properties ;
30
31
import java .util .logging .Logger ;
31
32
32
- import static com .ing .data .cassandra .jdbc .utils .ErrorConstants .HOST_REQUIRED ;
33
33
import static com .ing .data .cassandra .jdbc .utils .ErrorConstants .NOT_SUPPORTED ;
34
34
import static com .ing .data .cassandra .jdbc .utils .ErrorConstants .NO_INTERFACE ;
35
- import static com .ing .data .cassandra .jdbc .utils .JdbcUrlUtil .DEFAULT_PORT ;
36
35
import static com .ing .data .cassandra .jdbc .utils .JdbcUrlUtil .PROTOCOL ;
37
36
import static com .ing .data .cassandra .jdbc .utils .JdbcUrlUtil .TAG_CONSISTENCY_LEVEL ;
37
+ import static com .ing .data .cassandra .jdbc .utils .JdbcUrlUtil .TAG_CONTACT_POINTS ;
38
38
import static com .ing .data .cassandra .jdbc .utils .JdbcUrlUtil .TAG_CQL_VERSION ;
39
39
import static com .ing .data .cassandra .jdbc .utils .JdbcUrlUtil .TAG_DATABASE_NAME ;
40
40
import static com .ing .data .cassandra .jdbc .utils .JdbcUrlUtil .TAG_LOCAL_DATACENTER ;
41
41
import static com .ing .data .cassandra .jdbc .utils .JdbcUrlUtil .TAG_PASSWORD ;
42
- import static com .ing .data .cassandra .jdbc .utils .JdbcUrlUtil .TAG_PORT_NUMBER ;
43
- import static com .ing .data .cassandra .jdbc .utils .JdbcUrlUtil .TAG_SERVER_NAME ;
44
42
import static com .ing .data .cassandra .jdbc .utils .JdbcUrlUtil .TAG_USER ;
45
43
import static com .ing .data .cassandra .jdbc .utils .JdbcUrlUtil .createSubName ;
46
44
@@ -63,13 +61,9 @@ public class CassandraDataSource implements ConnectionPoolDataSource, DataSource
63
61
*/
64
62
protected static final String DATA_SOURCE_DESCRIPTION = "Cassandra Data Source" ;
65
63
/**
66
- * The server host name where the data source is located .
64
+ * The contact points of the data source.
67
65
*/
68
- protected String serverName ;
69
- /**
70
- * The port number of the data source, by default {@value JdbcUrlUtil#DEFAULT_PORT}.
71
- */
72
- protected int portNumber = DEFAULT_PORT ;
66
+ protected List <ContactPoint > contactPoints ;
73
67
/**
74
68
* The database name. In case of Cassandra, i.e. the keyspace used as data source.
75
69
*/
@@ -110,10 +104,26 @@ public class CassandraDataSource implements ConnectionPoolDataSource, DataSource
110
104
* @param user The username used to connect.
111
105
* @param password The password used to connect.
112
106
* @param consistency The consistency level.
107
+ * @deprecated For removal. Use {@link #CassandraDataSource(List, String, String, String, String)} instead.
113
108
*/
109
+ @ Deprecated
114
110
public CassandraDataSource (final String host , final int port , final String keyspace , final String user ,
115
111
final String password , final String consistency ) {
116
- this (host , port , keyspace , user , password , null , consistency , null );
112
+ this (Collections .singletonList (ContactPoint .of (host , port )), keyspace , user , password , null , consistency , null );
113
+ }
114
+
115
+ /**
116
+ * Constructor.
117
+ *
118
+ * @param contactPoints The contact points.
119
+ * @param keyspace The keyspace.
120
+ * @param user The username used to connect.
121
+ * @param password The password used to connect.
122
+ * @param consistency The consistency level.
123
+ */
124
+ public CassandraDataSource (final List <ContactPoint > contactPoints , final String keyspace , final String user ,
125
+ final String password , final String consistency ) {
126
+ this (contactPoints , keyspace , user , password , null , consistency , null );
117
127
}
118
128
119
129
/**
@@ -126,34 +136,31 @@ public CassandraDataSource(final String host, final int port, final String keysp
126
136
* @param password The password used to connect.
127
137
* @param version The CQL version. Deprecated, do not use anymore.
128
138
* @param consistency The consistency level.
129
- * @deprecated For removal. Use {@link #CassandraDataSource(String, int , String, String, String, String)} instead.
139
+ * @deprecated For removal. Use {@link #CassandraDataSource(List , String, String, String, String)} instead.
130
140
*/
131
141
@ Deprecated
132
142
public CassandraDataSource (final String host , final int port , final String keyspace , final String user ,
133
143
final String password , final String version , final String consistency ) {
134
- this (host , port , keyspace , user , password , version , consistency , null );
144
+ this (Collections .singletonList (ContactPoint .of (host , port )),
145
+ keyspace , user , password , version , consistency , null );
135
146
}
136
147
137
148
/**
138
149
* Constructor specifying a local datacenter (required to use {@link DefaultLoadBalancingPolicy}).
139
150
*
140
- * @param host The host name.
141
- * @param port The port.
151
+ * @param contactPoints The contact points.
142
152
* @param keyspace The keyspace.
143
153
* @param user The username used to connect.
144
154
* @param password The password used to connect.
145
155
* @param version The CQL version. Deprecated, do not use anymore.
146
156
* @param consistency The consistency level.
147
157
* @param localDataCenter The local datacenter.
148
158
*/
149
- public CassandraDataSource (final String host , final int port , final String keyspace , final String user ,
159
+ public CassandraDataSource (final List < ContactPoint > contactPoints , final String keyspace , final String user ,
150
160
final String password , final String version , final String consistency ,
151
161
final String localDataCenter ) {
152
- if (host != null ) {
153
- setServerName (host );
154
- }
155
- if (port >= 0 ) {
156
- setPortNumber (port );
162
+ if (contactPoints != null && !contactPoints .isEmpty ()) {
163
+ setContactPoints (contactPoints );
157
164
}
158
165
if (version != null ) {
159
166
setVersion (version );
@@ -179,21 +186,21 @@ public String getDescription() {
179
186
}
180
187
181
188
/**
182
- * Gets the server host name where the data source is located .
189
+ * Gets the contact points of the data source.
183
190
*
184
- * @return The server host name where the data source is located .
191
+ * @return The contact points of the data source.
185
192
*/
186
- public String getServerName () {
187
- return this .serverName ;
193
+ public List < ContactPoint > getContactPoints () {
194
+ return this .contactPoints ;
188
195
}
189
196
190
197
/**
191
- * Sets the server host name where the data source is located .
198
+ * Sets the contact points of the data source.
192
199
*
193
- * @param serverName The host name .
200
+ * @param contactPoints The contact points of the data source .
194
201
*/
195
- public void setServerName (final String serverName ) {
196
- this .serverName = serverName ;
202
+ public void setContactPoints (final List < ContactPoint > contactPoints ) {
203
+ this .contactPoints = contactPoints ;
197
204
}
198
205
199
206
/**
@@ -246,24 +253,6 @@ public void setConsistency(final String consistency) {
246
253
this .consistency = consistency ;
247
254
}
248
255
249
- /**
250
- * Gets the port number of the data source.
251
- *
252
- * @return The port number of the data source.
253
- */
254
- public int getPortNumber () {
255
- return this .portNumber ;
256
- }
257
-
258
- /**
259
- * Sets the port number of the data source.
260
- *
261
- * @param portNumber The port number of the data source.
262
- */
263
- public void setPortNumber (final int portNumber ) {
264
- this .portNumber = portNumber ;
265
- }
266
-
267
256
/**
268
257
* Gets the database name. In case of Cassandra, i.e. the keyspace used as data source.
269
258
*
@@ -347,12 +336,9 @@ public CassandraConnection getConnection(final String user, final String passwor
347
336
this .user = user ;
348
337
this .password = password ;
349
338
350
- if (this .serverName != null ) {
351
- props .setProperty (TAG_SERVER_NAME , this .serverName );
352
- } else {
353
- throw new SQLNonTransientConnectionException (HOST_REQUIRED );
339
+ if (this .contactPoints != null && !this .contactPoints .isEmpty ()) {
340
+ props .put (TAG_CONTACT_POINTS , this .contactPoints );
354
341
}
355
- props .setProperty (TAG_PORT_NUMBER , String .valueOf (this .portNumber ));
356
342
if (this .databaseName != null ) {
357
343
props .setProperty (TAG_DATABASE_NAME , this .databaseName );
358
344
}
0 commit comments