|
1 | 1 | package com.databricks.sdk.core.oauth; |
2 | 2 |
|
3 | 3 | import com.databricks.sdk.core.http.HttpClient; |
| 4 | +import com.google.auto.value.AutoValue; |
4 | 5 | import java.util.Objects; |
5 | 6 | import java.util.concurrent.ConcurrentHashMap; |
6 | 7 |
|
7 | 8 | /** |
8 | | - * Manages and provides Databricks data plane tokens. This class is responsible for acquiring and |
9 | | - * caching OAuth tokens that are specific to a particular Databricks data plane service endpoint and |
10 | | - * a set of authorization details. It utilizes a {@link DatabricksOAuthTokenSource} for obtaining |
11 | | - * control plane tokens, which may then be exchanged or used to authorize requests for data plane |
12 | | - * tokens. Cached {@link EndpointTokenSource} instances are used to efficiently reuse tokens for |
13 | | - * repeated requests to the same endpoint with the same authorization context. |
| 9 | + * Manages and provides Databricks data plane tokens. This class is responsible |
| 10 | + * for acquiring and caching OAuth tokens that are specific to a particular |
| 11 | + * Databricks data plane service endpoint and a set of authorization details. |
| 12 | + * It utilizes a {@link DatabricksOAuthTokenSource} for obtaining control plane |
| 13 | + * tokens, which may then be exchanged or used to authorize requests for data |
| 14 | + * plane tokens. Cached {@link EndpointTokenSource} instances are used to |
| 15 | + * efficiently reuse tokens for repeated requests to the same endpoint with the |
| 16 | + * same authorization context. |
14 | 17 | */ |
15 | 18 | public class DataPlaneTokenSource { |
16 | 19 | private final HttpClient httpClient; |
17 | 20 | private final TokenSource cpTokenSource; |
18 | 21 | private final String host; |
19 | 22 | private final ConcurrentHashMap<TokenSourceKey, EndpointTokenSource> sourcesCache; |
| 23 | + |
20 | 24 | /** |
21 | | - * Caching key for {@link EndpointTokenSource}, based on endpoint and authorization details. This |
22 | | - * is a value object that uniquely identifies a token source configuration. |
| 25 | + * Caching key for {@link EndpointTokenSource}, based on endpoint and |
| 26 | + * authorization details. This is a value object that uniquely identifies |
| 27 | + * a token source configuration. |
23 | 28 | */ |
24 | | - private static final class TokenSourceKey { |
25 | | - /** The target service endpoint URL. */ |
26 | | - private final String endpoint; |
27 | | - |
28 | | - /** Specific authorization details for the endpoint. */ |
29 | | - private final String authDetails; |
30 | | - |
31 | | - /** |
32 | | - * Constructs a TokenSourceKey. |
33 | | - * |
34 | | - * @param endpoint The target service endpoint URL. |
35 | | - * @param authDetails Specific authorization details. |
36 | | - */ |
37 | | - public TokenSourceKey(String endpoint, String authDetails) { |
38 | | - this.endpoint = endpoint; |
39 | | - this.authDetails = authDetails; |
40 | | - } |
41 | | - |
42 | | - @Override |
43 | | - public boolean equals(Object o) { |
44 | | - if (this == o) { |
45 | | - return true; |
46 | | - } |
47 | | - if (o == null || getClass() != o.getClass()) { |
48 | | - return false; |
49 | | - } |
50 | | - TokenSourceKey that = (TokenSourceKey) o; |
51 | | - return Objects.equals(endpoint, that.endpoint) |
52 | | - && Objects.equals(authDetails, that.authDetails); |
53 | | - } |
| 29 | + @AutoValue |
| 30 | + static abstract class TokenSourceKey { |
| 31 | + abstract String endpoint(); |
| 32 | + abstract String authDetails(); |
54 | 33 |
|
55 | | - @Override |
56 | | - public int hashCode() { |
57 | | - return Objects.hash(endpoint, authDetails); |
| 34 | + static TokenSourceKey create(String endpoint, String authDetails) { |
| 35 | + return new AutoValue_DataPlaneTokenSource_TokenSourceKey(endpoint, authDetails); |
58 | 36 | } |
59 | 37 | } |
60 | 38 |
|
@@ -101,14 +79,14 @@ public Token getToken(String endpoint, String authDetails) { |
101 | 79 | throw new IllegalArgumentException("Authorization details cannot be empty"); |
102 | 80 | } |
103 | 81 |
|
104 | | - TokenSourceKey key = new TokenSourceKey(endpoint, authDetails); |
| 82 | + TokenSourceKey key = TokenSourceKey.create(endpoint, authDetails); |
105 | 83 |
|
106 | 84 | EndpointTokenSource specificSource = |
107 | 85 | sourcesCache.computeIfAbsent( |
108 | 86 | key, |
109 | 87 | k -> |
110 | 88 | new EndpointTokenSource( |
111 | | - this.cpTokenSource, k.authDetails, this.httpClient, this.host)); |
| 89 | + this.cpTokenSource, k.authDetails(), this.httpClient, this.host)); |
112 | 90 |
|
113 | 91 | return specificSource.getToken(); |
114 | 92 | } |
|
0 commit comments