17
17
package com .google .cloud .datastore ;
18
18
19
19
import static com .google .cloud .datastore .Validator .validateNamespace ;
20
- import static com .google .cloud .datastore .spi .v1 .DatastoreRpc .Transport .GRPC ;
21
20
22
21
import com .google .cloud .ServiceDefaults ;
23
22
import com .google .cloud .ServiceOptions ;
24
23
import com .google .cloud .ServiceRpc ;
25
24
import com .google .cloud .TransportOptions ;
26
25
import com .google .cloud .datastore .spi .DatastoreRpcFactory ;
27
26
import com .google .cloud .datastore .spi .v1 .DatastoreRpc ;
28
- import com .google .cloud .datastore .spi .v1 .DatastoreRpc .Transport ;
29
27
import com .google .cloud .datastore .spi .v1 .GrpcDatastoreRpc ;
30
28
import com .google .cloud .datastore .spi .v1 .HttpDatastoreRpc ;
31
29
import com .google .cloud .datastore .v1 .DatastoreSettings ;
30
+ import com .google .cloud .grpc .GrpcTransportOptions ;
32
31
import com .google .cloud .http .HttpTransportOptions ;
33
- import com .google .common .annotations .VisibleForTesting ;
34
32
import com .google .common .base .MoreObjects ;
35
33
import com .google .common .collect .ImmutableSet ;
36
34
import java .io .IOException ;
@@ -50,7 +48,6 @@ public class DatastoreOptions extends ServiceOptions<Datastore, DatastoreOptions
50
48
51
49
private final String namespace ;
52
50
private final String databaseId ;
53
- private final Transport transport ;
54
51
55
52
public static class DefaultDatastoreFactory implements DatastoreFactory {
56
53
@@ -69,9 +66,14 @@ public static class DefaultDatastoreRpcFactory implements DatastoreRpcFactory {
69
66
@ Override
70
67
public ServiceRpc create (DatastoreOptions options ) {
71
68
try {
72
- return options .transport == GRPC
73
- ? new GrpcDatastoreRpc (options )
74
- : new HttpDatastoreRpc (options );
69
+ if (options .getTransportOptions () instanceof GrpcTransportOptions ) {
70
+ return new GrpcDatastoreRpc (options );
71
+ } else if (options .getTransportOptions () instanceof HttpTransportOptions ) {
72
+ return new HttpDatastoreRpc (options );
73
+ } else {
74
+ throw new IllegalArgumentException (
75
+ "unknown transport type: " + options .getTransportOptions ());
76
+ }
75
77
} catch (IOException e ) {
76
78
throw new RuntimeException (e );
77
79
}
@@ -82,23 +84,17 @@ public static class Builder extends ServiceOptions.Builder<Datastore, DatastoreO
82
84
83
85
private String namespace ;
84
86
private String databaseId ;
85
- private Transport transport = GRPC ;
86
87
87
88
private Builder () {}
88
89
89
90
private Builder (DatastoreOptions options ) {
90
91
super (options );
91
92
namespace = options .namespace ;
92
93
databaseId = options .databaseId ;
93
- transport = options .transport ;
94
94
}
95
95
96
96
@ Override
97
97
public Builder setTransportOptions (TransportOptions transportOptions ) {
98
- if (!(transportOptions instanceof HttpTransportOptions )) {
99
- throw new IllegalArgumentException (
100
- "Only http transport is allowed for " + API_SHORT_NAME + "." );
101
- }
102
98
return super .setTransportOptions (transportOptions );
103
99
}
104
100
@@ -117,18 +113,12 @@ public Builder setDatabaseId(String databaseId) {
117
113
this .databaseId = databaseId ;
118
114
return this ;
119
115
}
120
-
121
- public Builder setTransport (Transport transport ) {
122
- this .transport = transport ;
123
- return this ;
124
- }
125
116
}
126
117
127
118
private DatastoreOptions (Builder builder ) {
128
119
super (DatastoreFactory .class , DatastoreRpcFactory .class , builder , new DatastoreDefaults ());
129
120
namespace = MoreObjects .firstNonNull (builder .namespace , defaultNamespace ());
130
121
databaseId = MoreObjects .firstNonNull (builder .databaseId , DEFAULT_DATABASE_ID );
131
- transport = builder .transport ;
132
122
}
133
123
134
124
@ Override
@@ -157,14 +147,18 @@ public DatastoreRpcFactory getDefaultRpcFactory() {
157
147
158
148
@ Override
159
149
public TransportOptions getDefaultTransportOptions () {
160
- return getDefaultHttpTransportOptions ();
150
+ return getDefaultGrpcTransportOptions ();
161
151
}
162
152
}
163
153
164
154
public static HttpTransportOptions getDefaultHttpTransportOptions () {
165
155
return HttpTransportOptions .newBuilder ().build ();
166
156
}
167
157
158
+ public static GrpcTransportOptions getDefaultGrpcTransportOptions () {
159
+ return GrpcTransportOptions .newBuilder ().build ();
160
+ }
161
+
168
162
/** Returns the default namespace to be used by the datastore service. */
169
163
public String getNamespace () {
170
164
return namespace ;
@@ -174,12 +168,6 @@ public String getDatabaseId() {
174
168
return this .databaseId ;
175
169
}
176
170
177
- /** Returns the current transport */
178
- @ VisibleForTesting
179
- Transport getTransport () {
180
- return this .transport ;
181
- }
182
-
183
171
/** Returns a default {@code DatastoreOptions} instance. */
184
172
public static DatastoreOptions getDefaultInstance () {
185
173
return newBuilder ().build ();
0 commit comments