1818
1919import  static  com .google .cloud .datastore .Validator .validateNamespace ;
2020
21+ import  com .google .api .gax .core .CredentialsProvider ;
22+ import  com .google .api .gax .grpc .InstantiatingGrpcChannelProvider ;
23+ import  com .google .api .gax .rpc .TransportChannelProvider ;
2124import  com .google .cloud .ServiceDefaults ;
2225import  com .google .cloud .ServiceOptions ;
2326import  com .google .cloud .ServiceRpc ;
@@ -46,6 +49,9 @@ public class DatastoreOptions extends ServiceOptions<Datastore, DatastoreOptions
4649  public  static  final  String  PROJECT_ID_ENV_VAR  = "DATASTORE_PROJECT_ID" ;
4750  public  static  final  String  LOCAL_HOST_ENV_VAR  = "DATASTORE_EMULATOR_HOST" ;
4851
52+   private  transient  TransportChannelProvider  channelProvider  = null ;
53+   private  transient  CredentialsProvider  credentialsProvider  = null ;
54+ 
4955  private  final  String  namespace ;
5056  private  final  String  databaseId ;
5157
@@ -69,6 +75,10 @@ public ServiceRpc create(DatastoreOptions options) {
6975        if  (options .getTransportOptions () instanceof  GrpcTransportOptions ) {
7076          return  new  GrpcDatastoreRpc (options );
7177        } else  if  (options .getTransportOptions () instanceof  HttpTransportOptions ) {
78+           // todo see if we can remove this check 
79+           if  (DatastoreUtils .isEmulator (options )) {
80+             throw  new  IllegalArgumentException ("Only GRPC channels are allowed for emulator." );
81+           }
7282          return  new  HttpDatastoreRpc (options );
7383        } else  {
7484          throw  new  IllegalArgumentException (
@@ -84,20 +94,46 @@ public static class Builder extends ServiceOptions.Builder<Datastore, DatastoreO
8494
8595    private  String  namespace ;
8696    private  String  databaseId ;
97+     private  TransportChannelProvider  channelProvider  = null ;
98+     private  CredentialsProvider  credentialsProvider  = null ;
8799
88100    private  Builder () {}
89101
90102    private  Builder (DatastoreOptions  options ) {
91103      super (options );
92-       namespace  = options .namespace ;
93-       databaseId  = options .databaseId ;
104+       this .namespace  = options .namespace ;
105+       this .databaseId  = options .databaseId ;
106+       this .channelProvider  = validateChannelProvider (options .channelProvider );
107+       this .credentialsProvider  = options .credentialsProvider ;
94108    }
95109
96110    @ Override 
97111    public  Builder  setTransportOptions (TransportOptions  transportOptions ) {
98112      return  super .setTransportOptions (transportOptions );
99113    }
100114
115+     /** 
116+      * Sets the {@link TransportChannelProvider} to use with this Datastore client. 
117+      * 
118+      * @param channelProvider A InstantiatingGrpcChannelProvider object that defines the transport 
119+      *     provider for this client. 
120+      */ 
121+     public  Builder  setChannelProvider (TransportChannelProvider  channelProvider ) {
122+       this .channelProvider  = validateChannelProvider (channelProvider );
123+       return  this ;
124+     }
125+ 
126+     /** 
127+      * Sets the {@link CredentialsProvider} to use with this Datastore client. 
128+      * 
129+      * @param credentialsProvider A CredentialsProvider object that defines the credential provider 
130+      *     for this client. 
131+      */ 
132+     public  Builder  setCredentialsProvider (CredentialsProvider  credentialsProvider ) {
133+       this .credentialsProvider  = credentialsProvider ;
134+       return  this ;
135+     }
136+ 
101137    @ Override 
102138    public  DatastoreOptions  build () {
103139      return  new  DatastoreOptions (this );
@@ -115,10 +151,45 @@ public Builder setDatabaseId(String databaseId) {
115151    }
116152  }
117153
154+   private  static  TransportChannelProvider  validateChannelProvider (
155+       TransportChannelProvider  channelProvider ) {
156+     if  (!(channelProvider  instanceof  InstantiatingGrpcChannelProvider )) {
157+       throw  new  IllegalArgumentException (
158+           "Only GRPC channels are allowed for "  + API_SHORT_NAME  + "." );
159+     }
160+     return  channelProvider ;
161+   }
162+ 
118163  private  DatastoreOptions (Builder  builder ) {
119164    super (DatastoreFactory .class , DatastoreRpcFactory .class , builder , new  DatastoreDefaults ());
120165    namespace  = MoreObjects .firstNonNull (builder .namespace , defaultNamespace ());
121166    databaseId  = MoreObjects .firstNonNull (builder .databaseId , DEFAULT_DATABASE_ID );
167+ 
168+     // todo see if we can update this 
169+     if  (getTransportOptions () instanceof  HttpTransportOptions 
170+         && (builder .channelProvider  != null  || builder .credentialsProvider  != null )) {
171+       throw  new  IllegalArgumentException (
172+           "Only gRPC transport allows setting of channel provider or credentials provider" );
173+     } else  if  (getTransportOptions () instanceof  GrpcTransportOptions ) {
174+       this .channelProvider  =
175+           builder .channelProvider  != null 
176+               ? builder .channelProvider 
177+               : GrpcTransportOptions .setUpChannelProvider (
178+                   DatastoreSettings .defaultGrpcTransportProviderBuilder (), this );
179+ 
180+       this .credentialsProvider  =
181+           builder .credentialsProvider  != null 
182+               ? builder .credentialsProvider 
183+               : GrpcTransportOptions .setUpCredentialsProvider (this );
184+     }
185+   }
186+ 
187+   public  CredentialsProvider  getCredentialsProvider () {
188+     return  credentialsProvider ;
189+   }
190+ 
191+   public  TransportChannelProvider  getTransportChannelProvider () {
192+     return  channelProvider ;
122193  }
123194
124195  @ Override 
@@ -134,6 +205,7 @@ protected String getDefaultProject() {
134205  }
135206
136207  private  static  class  DatastoreDefaults  implements  ServiceDefaults <Datastore , DatastoreOptions > {
208+     private  final  TransportOptions  TRANSPORT_OPTIONS  = getDefaultTransportOptionsBuilder ().build ();
137209
138210    @ Override 
139211    public  DatastoreFactory  getDefaultServiceFactory () {
@@ -147,7 +219,11 @@ public DatastoreRpcFactory getDefaultRpcFactory() {
147219
148220    @ Override 
149221    public  TransportOptions  getDefaultTransportOptions () {
150-       return  getDefaultGrpcTransportOptions ();
222+       return  TRANSPORT_OPTIONS ;
223+     }
224+ 
225+     public  static  GrpcTransportOptions .Builder  getDefaultTransportOptionsBuilder () {
226+       return  GrpcTransportOptions .newBuilder ();
151227    }
152228  }
153229
0 commit comments