Skip to content

Commit d8e1fac

Browse files
refactor: collapse duplicate createConnection helpers in DataCloudDatasource
Both the OAuth and direct CDP-token flows built identical auth interceptor, gRPC channel, stub provider, and DataCloudConnection wiring. Merge them into a single createConnection that takes the already-constructed collaborators (TokenProcessorSupplier, host, userName, lakehouse supplier, dataspaces supplier).
1 parent 5aeb8f3 commit d8e1fac

1 file changed

Lines changed: 38 additions & 47 deletions

File tree

jdbc/src/main/java/com/salesforce/datacloud/jdbc/DataCloudDatasource.java

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919
import com.salesforce.datacloud.jdbc.util.JdbcURL;
2020
import com.salesforce.datacloud.jdbc.util.PropertyParsingUtils;
2121
import com.salesforce.datacloud.jdbc.util.SqlErrorCodes;
22+
import com.salesforce.datacloud.jdbc.util.ThrowingJdbcSupplier;
2223
import io.grpc.ManagedChannelBuilder;
2324
import java.io.PrintWriter;
2425
import java.net.URI;
2526
import java.net.URISyntaxException;
2627
import java.sql.Connection;
2728
import java.sql.SQLException;
2829
import java.util.Collections;
30+
import java.util.List;
2931
import java.util.Properties;
3032
import java.util.logging.Logger;
3133
import javax.sql.DataSource;
@@ -56,8 +58,17 @@ public class DataCloudDatasource implements DataSource {
5658

5759
@Override
5860
public Connection getConnection() throws SQLException {
61+
val tokenProvider = DataCloudTokenProvider.of(httpClientProperties, authProperties);
62+
val dataspaceClient = new DataspaceClient(httpClientProperties, tokenProvider);
5963
return createConnection(
60-
connectionProperties, grpcChannelProperties, httpClientProperties, authProperties, null);
64+
connectionProperties,
65+
grpcChannelProperties,
66+
new TokenProcessorSupplier(tokenProvider),
67+
tokenProvider.getDataCloudToken().getTenantUrl(),
68+
authProperties.getUserName() != null ? authProperties.getUserName() : "",
69+
tokenProvider::getLakehouseName,
70+
dataspaceClient,
71+
null);
6172
}
6273

6374
/**
@@ -108,15 +119,31 @@ public static DataCloudConnection connectUsingProperties(@NonNull String url, Pr
108119
log.info("Using direct CDP token authentication");
109120
val cdpTokenProcessor = DirectCdpTokenProcessor.ofDestructive(properties);
110121
PropertyParsingUtils.validateRemainingProperties(properties);
111-
return createConnectionWithCdpToken(
112-
connectionProperties, grpcChannelProperties, httpClientProperties, cdpTokenProcessor, jdbcUrl);
122+
return createConnection(
123+
connectionProperties,
124+
grpcChannelProperties,
125+
new TokenProcessorSupplier(cdpTokenProcessor),
126+
cdpTokenProcessor.getDataCloudToken().getTenantUrl(),
127+
"",
128+
cdpTokenProcessor::getLakehouse,
129+
Collections::emptyList,
130+
jdbcUrl);
113131
}
114132

115133
val authProperties = SalesforceAuthProperties.ofDestructive(loginUrl, properties);
116134
PropertyParsingUtils.validateRemainingProperties(properties);
117135

136+
val tokenProvider = DataCloudTokenProvider.of(httpClientProperties, authProperties);
137+
val dataspaceClient = new DataspaceClient(httpClientProperties, tokenProvider);
118138
return createConnection(
119-
connectionProperties, grpcChannelProperties, httpClientProperties, authProperties, jdbcUrl);
139+
connectionProperties,
140+
grpcChannelProperties,
141+
new TokenProcessorSupplier(tokenProvider),
142+
tokenProvider.getDataCloudToken().getTenantUrl(),
143+
authProperties.getUserName() != null ? authProperties.getUserName() : "",
144+
tokenProvider::getLakehouseName,
145+
dataspaceClient,
146+
jdbcUrl);
120147
} catch (SQLException e) {
121148
log.error("Failed to connect with URL {}: {}", url, e.getMessage(), e);
122149
throw e;
@@ -136,65 +163,29 @@ private static void normalizeUsernameProperty(Properties properties) throws SQLE
136163
}
137164

138165
/**
139-
* Internal utility function to create a DataCloudConnection with the given properties.
166+
* Internal utility function to create a DataCloudConnection with the given collaborators.
140167
*
141168
* The jdbcUrl is optional and will only influence `DatabaseMetaData.getURL()`.
142-
* The actual connection will be created with the properties provided.
143169
*/
144170
private static DataCloudConnection createConnection(
145171
@NonNull ConnectionProperties connectionProperties,
146172
@NonNull GrpcChannelProperties grpcChannelProperties,
147-
@NonNull HttpClientProperties httpClientProperties,
148-
@NonNull SalesforceAuthProperties authProperties,
149-
JdbcURL jdbcUrl)
150-
throws SQLException {
151-
// Setup the authInterceptor
152-
val tokenProvider = DataCloudTokenProvider.of(httpClientProperties, authProperties);
153-
val tokenSupplier = new TokenProcessorSupplier(tokenProvider);
154-
val authInterceptor = AuthorizationHeaderInterceptor.of(tokenSupplier);
155-
156-
// Setup the dataspace client
157-
val dataspaceClient = new DataspaceClient(httpClientProperties, tokenProvider);
158-
159-
// Setup the gRPC stub provider
160-
val host = tokenProvider.getDataCloudToken().getTenantUrl();
161-
final ManagedChannelBuilder<?> builder = ManagedChannelBuilder.forAddress(host, 443)
162-
.intercept(authInterceptor)
163-
.intercept(TracingHeadersInterceptor.of());
164-
val stubProvider = JdbcDriverStubProvider.of(builder, grpcChannelProperties);
165-
166-
return DataCloudConnection.of(
167-
stubProvider,
168-
connectionProperties,
169-
jdbcUrl,
170-
authProperties.getUserName() != null ? authProperties.getUserName() : "",
171-
tokenProvider::getLakehouseName,
172-
dataspaceClient);
173-
}
174-
175-
private static DataCloudConnection createConnectionWithCdpToken(
176-
@NonNull ConnectionProperties connectionProperties,
177-
@NonNull GrpcChannelProperties grpcChannelProperties,
178-
@NonNull HttpClientProperties httpClientProperties,
179-
@NonNull DirectCdpTokenProcessor cdpTokenProcessor,
173+
@NonNull TokenProcessorSupplier tokenSupplier,
174+
@NonNull String host,
175+
String userName,
176+
@NonNull ThrowingJdbcSupplier<String> lakehouseSupplier,
177+
@NonNull ThrowingJdbcSupplier<List<String>> dataspacesSupplier,
180178
JdbcURL jdbcUrl)
181179
throws SQLException {
182-
val tokenSupplier = new TokenProcessorSupplier(cdpTokenProcessor);
183180
val authInterceptor = AuthorizationHeaderInterceptor.of(tokenSupplier);
184181

185-
val host = cdpTokenProcessor.getDataCloudToken().getTenantUrl();
186182
final ManagedChannelBuilder<?> builder = ManagedChannelBuilder.forAddress(host, 443)
187183
.intercept(authInterceptor)
188184
.intercept(TracingHeadersInterceptor.of());
189185
val stubProvider = JdbcDriverStubProvider.of(builder, grpcChannelProperties);
190186

191187
return DataCloudConnection.of(
192-
stubProvider,
193-
connectionProperties,
194-
jdbcUrl,
195-
"",
196-
cdpTokenProcessor::getLakehouse,
197-
Collections::emptyList);
188+
stubProvider, connectionProperties, jdbcUrl, userName, lakehouseSupplier, dataspacesSupplier);
198189
}
199190

200191
@Override

0 commit comments

Comments
 (0)