1818
1919import static com .google .common .base .Preconditions .checkArgument ;
2020import static com .google .common .base .Preconditions .checkNotNull ;
21- import static com .google .common .base .Preconditions .checkState ;
2221import static com .google .common .base .Strings .isNullOrEmpty ;
2322
2423import com .google .errorprone .annotations .CanIgnoreReturnValue ;
2524import io .grpc .Channel ;
2625import io .grpc .ChannelCredentials ;
2726import io .grpc .ExperimentalApi ;
28- import io .grpc .InsecureChannelCredentials ;
2927import io .grpc .internal .ObjectPool ;
3028import io .grpc .internal .SharedResourcePool ;
3129import io .grpc .netty .InternalNettyChannelCredentials ;
3230import io .grpc .netty .InternalProtocolNegotiator ;
3331import io .grpc .s2a .channel .S2AHandshakerServiceChannel ;
3432import io .grpc .s2a .handshaker .S2AIdentity ;
3533import io .grpc .s2a .handshaker .S2AProtocolNegotiatorFactory ;
34+ import java .io .IOException ;
3635import javax .annotation .concurrent .NotThreadSafe ;
3736import org .checkerframework .checker .nullness .qual .Nullable ;
3837
@@ -46,25 +45,27 @@ public final class S2AChannelCredentials {
4645 * Creates a channel credentials builder for establishing an S2A-secured connection.
4746 *
4847 * @param s2aAddress the address of the S2A server used to secure the connection.
48+ * @param s2aChannelCredentials the credentials to be used when connecting to the S2A.
4949 * @return a {@code S2AChannelCredentials.Builder} instance.
5050 */
51- public static Builder newBuilder (String s2aAddress ) {
51+ public static Builder newBuilder (String s2aAddress , ChannelCredentials s2aChannelCredentials ) {
5252 checkArgument (!isNullOrEmpty (s2aAddress ), "S2A address must not be null or empty." );
53- return new Builder (s2aAddress );
53+ checkNotNull (s2aChannelCredentials , "S2A channel credentials must not be null" );
54+ return new Builder (s2aAddress , s2aChannelCredentials );
5455 }
5556
5657 /** Builds an {@code S2AChannelCredentials} instance. */
5758 @ NotThreadSafe
5859 public static final class Builder {
5960 private final String s2aAddress ;
61+ private final ChannelCredentials s2aChannelCredentials ;
6062 private ObjectPool <Channel > s2aChannelPool ;
61- private ChannelCredentials s2aChannelCredentials ;
6263 private @ Nullable S2AIdentity localIdentity = null ;
6364
64- Builder (String s2aAddress ) {
65+ Builder (String s2aAddress , ChannelCredentials s2aChannelCredentials ) {
6566 this .s2aAddress = s2aAddress ;
67+ this .s2aChannelCredentials = s2aChannelCredentials ;
6668 this .s2aChannelPool = null ;
67- this .s2aChannelCredentials = InsecureChannelCredentials .create ();
6869 }
6970
7071 /**
@@ -106,15 +107,7 @@ public Builder setLocalUid(String localUid) {
106107 return this ;
107108 }
108109
109- /** Sets the credentials to be used when connecting to the S2A. */
110- @ CanIgnoreReturnValue
111- public Builder setS2AChannelCredentials (ChannelCredentials s2aChannelCredentials ) {
112- this .s2aChannelCredentials = s2aChannelCredentials ;
113- return this ;
114- }
115-
116- public ChannelCredentials build () {
117- checkState (!isNullOrEmpty (s2aAddress ), "S2A address must not be null or empty." );
110+ public ChannelCredentials build () throws IOException {
118111 ObjectPool <Channel > s2aChannelPool =
119112 SharedResourcePool .forResource (
120113 S2AHandshakerServiceChannel .getChannelResource (s2aAddress , s2aChannelCredentials ));
0 commit comments