Skip to content

Commit 9968308

Browse files
committed
---
yaml --- r: 5953 b: refs/heads/tswast-patch-1 c: 5707915 h: refs/heads/master i: 5951: b020ceb
1 parent 59fc2aa commit 9968308

6 files changed

Lines changed: 50 additions & 38 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ refs/tags/v0.18.0: 9d193c4c4b9d1c6f21515dd8e50836b9194ec9bb
5757
refs/tags/v0.19.0: e67b56e4d8dad5f9a7b38c9b2107c23c828f2ed5
5858
refs/tags/v0.20.0: 839f7fb7156535146aa1cb2c5aadd8d375d854e8
5959
refs/tags/v0.20.1: 370471f437f1f4f68a11e068df5cd6bf39edb1fa
60-
refs/heads/tswast-patch-1: af43ec053c7c813911228957f6f3074d1a47d441
60+
refs/heads/tswast-patch-1: 5707915a9809a736c5cf5e5b34906d25eb512186
6161
refs/heads/pubsub-streaming-pull: 19262b752ee874eb2ca3b950eb2aef44d5a5267b

branches/tswast-patch-1/src/main/java/com/google/gcloud/ServiceOptions.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.api.client.http.HttpRequestInitializer;
2525
import com.google.api.client.http.HttpTransport;
2626
import com.google.api.client.http.javanet.NetHttpTransport;
27+
import com.google.gcloud.spi.ServiceRpcFactory;
2728

2829
import java.io.BufferedReader;
2930
import java.io.IOException;
@@ -34,7 +35,7 @@
3435
import java.net.URLConnection;
3536
import java.util.Set;
3637

37-
public abstract class ServiceOptions implements Serializable {
38+
public abstract class ServiceOptions<R, O extends ServiceOptions<R, O>> implements Serializable {
3839

3940
private static final String DEFAULT_HOST = "https://www.googleapis.com";
4041
private static final long serialVersionUID = 1203687993961393350L;
@@ -43,6 +44,7 @@ public abstract class ServiceOptions implements Serializable {
4344
private final HttpTransportFactory httpTransportFactory;
4445
private final AuthCredentials authCredentials;
4546
private final RetryParams retryParams;
47+
private final ServiceRpcFactory<R, O> serviceRpcFactory;
4648

4749
public interface HttpTransportFactory extends Serializable {
4850
HttpTransport create();
@@ -72,12 +74,14 @@ public HttpTransport create() {
7274
}
7375
}
7476

75-
protected abstract static class Builder<B extends Builder<B>> {
77+
protected abstract static class Builder<R, O extends ServiceOptions<R, O>,
78+
B extends Builder<R, O, B>> {
7679

7780
private String host;
7881
private HttpTransportFactory httpTransportFactory;
7982
private AuthCredentials authCredentials;
8083
private RetryParams retryParams;
84+
private ServiceRpcFactory<R, O> serviceRpcFactory;
8185

8286
protected Builder() {}
8387

@@ -86,6 +90,7 @@ protected Builder(ServiceOptions options) {
8690
httpTransportFactory = options.httpTransportFactory;
8791
authCredentials = options.authCredentials;
8892
retryParams = options.retryParams;
93+
serviceRpcFactory = options.serviceRpcFactory;
8994
}
9095

9196
protected abstract ServiceOptions build();
@@ -114,14 +119,20 @@ public B retryParams(RetryParams retryParams) {
114119
this.retryParams = retryParams;
115120
return self();
116121
}
122+
123+
public B serviceRpcFactory(ServiceRpcFactory<R, O> serviceRpcFactory) {
124+
this.serviceRpcFactory = serviceRpcFactory;
125+
return self();
126+
}
117127
}
118128

119-
protected ServiceOptions(Builder<?> builder) {
129+
protected ServiceOptions(Builder<R, O, ?> builder) {
120130
host = firstNonNull(builder.host, DEFAULT_HOST);
121131
httpTransportFactory =
122132
firstNonNull(builder.httpTransportFactory, new DefaultHttpTransportFactory());
123133
authCredentials = firstNonNull(builder.authCredentials, defaultAuthCredentials());
124134
retryParams = builder.retryParams;
135+
serviceRpcFactory = builder.serviceRpcFactory;
125136
}
126137

127138
private static AuthCredentials defaultAuthCredentials() {
@@ -204,10 +215,14 @@ public RetryParams retryParams() {
204215
return retryParams;
205216
}
206217

218+
public ServiceRpcFactory<R, O> serviceRpcFactory() {
219+
return serviceRpcFactory;
220+
}
221+
207222
public HttpRequestInitializer httpRequestInitializer() {
208223
HttpTransport httpTransport = httpTransportFactory.create();
209224
return authCredentials().httpRequestInitializer(httpTransport, scopes());
210225
}
211226

212-
public abstract Builder<?> toBuilder();
227+
public abstract Builder<R, O, ?> toBuilder();
213228
}

branches/tswast-patch-1/src/main/java/com/google/gcloud/datastore/DatastoreServiceOptions.java

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,25 @@
3434
import java.util.Iterator;
3535
import java.util.Set;
3636

37-
public class DatastoreServiceOptions extends ServiceOptions {
37+
public class DatastoreServiceOptions extends ServiceOptions<DatastoreRpc, DatastoreServiceOptions> {
3838

39+
private static final long serialVersionUID = -8636602944160689193L;
3940
private static final String DATASET_ENV_NAME = "DATASTORE_DATASET";
4041
private static final String DATASTORE_SCOPE = "https://www.googleapis.com/auth/datastore";
4142
private static final String USERINFO_SCOPE = "https://www.googleapis.com/auth/userinfo.email";
4243
private static final Set<String> SCOPES = ImmutableSet.of(DATASTORE_SCOPE, USERINFO_SCOPE);
44+
4345
private final String dataset;
4446
private final String namespace;
4547
private final boolean force;
46-
private final DatastoreRpc datastoreRpc;
4748
private final boolean normalizeDataset;
48-
private final boolean defaultDatastoreRpc;
4949

50-
public static class Builder extends ServiceOptions.Builder<Builder> {
50+
public static class Builder extends
51+
ServiceOptions.Builder<DatastoreRpc, DatastoreServiceOptions, Builder> {
5152

5253
private String dataset;
5354
private String namespace;
5455
private boolean force;
55-
private DatastoreRpc datastoreRpc;
5656
private boolean normalizeDataset = true;
5757

5858
private Builder() {
@@ -63,7 +63,6 @@ private Builder(DatastoreServiceOptions options) {
6363
dataset = options.dataset;
6464
force = options.force;
6565
namespace = options.namespace;
66-
datastoreRpc = options.datastoreRpc;
6766
normalizeDataset = options.normalizeDataset;
6867
}
6968

@@ -73,11 +72,6 @@ public DatastoreServiceOptions build() {
7372
return normalizeDataset ? options.normalize() : options;
7473
}
7574

76-
public Builder datastoreRpc(DatastoreRpc datastoreRpc) {
77-
this.datastoreRpc = datastoreRpc;
78-
return this;
79-
}
80-
8175
public Builder dataset(String dataset) {
8276
this.dataset = validateDataset(dataset);
8377
return this;
@@ -105,8 +99,6 @@ private DatastoreServiceOptions(Builder builder) {
10599
namespace = builder.namespace != null ? builder.namespace : defaultNamespace();
106100
force = builder.force;
107101
dataset = firstNonNull(builder.dataset, defaultDataset());
108-
datastoreRpc = firstNonNull(builder.datastoreRpc, ServiceRpcProvider.datastore(this));
109-
defaultDatastoreRpc = builder.datastoreRpc == null;
110102
}
111103

112104
private DatastoreServiceOptions normalize() {
@@ -123,7 +115,7 @@ private DatastoreServiceOptions normalize() {
123115
.build();
124116
requestPb.addKey(key);
125117
try {
126-
LookupResponse responsePb = datastoreRpc.lookup(requestPb.build());
118+
LookupResponse responsePb = datastoreRpc().lookup(requestPb.build());
127119
if (responsePb.getDeferredCount() > 0) {
128120
key = responsePb.getDeferred(0);
129121
} else {
@@ -132,9 +124,6 @@ private DatastoreServiceOptions normalize() {
132124
key = combinedIter.next().getEntity().getKey();
133125
}
134126
builder.dataset(key.getPartitionId().getDatasetId());
135-
if (defaultDatastoreRpc && !dataset.equals(builder.dataset)) {
136-
builder.datastoreRpc(ServiceRpcProvider.datastore(builder.build()));
137-
}
138127
return new DatastoreServiceOptions(builder);
139128
} catch (DatastoreRpcException e) {
140129
throw DatastoreServiceException.translateAndThrow(e);
@@ -185,8 +174,11 @@ public Builder toBuilder() {
185174
return new Builder(this);
186175
}
187176

188-
public DatastoreRpc datastoreRpc() {
189-
return datastoreRpc;
177+
DatastoreRpc datastoreRpc() {
178+
if (serviceRpcFactory() != null) {
179+
return serviceRpcFactory().create(this);
180+
}
181+
return ServiceRpcProvider.datastore(this);
190182
}
191183

192184
public static Builder builder() {

branches/tswast-patch-1/src/main/java/com/google/gcloud/spi/ServiceRpcFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
/* * Copyright 2015 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.google.gcloud.spi;import com.google.gcloud.ServiceOptions;public interface ServiceRpcFactory<S, O extends ServiceOptions> { S create(O options);}
1+
/* * Copyright 2015 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.google.gcloud.spi;import com.google.gcloud.ServiceOptions;import java.io.Serializable;public interface ServiceRpcFactory<S, O extends ServiceOptions> extends Serializable { S create(O options);}

branches/tswast-patch-1/src/main/java/com/google/gcloud/storage/StorageServiceOptions.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,22 @@
2525

2626
import java.util.Set;
2727

28-
public class StorageServiceOptions extends ServiceOptions {
28+
public class StorageServiceOptions extends ServiceOptions<StorageRpc, StorageServiceOptions> {
2929

30+
private static final long serialVersionUID = -7804860602287801084L;
3031
private static final String GCS_SCOPE = "https://www.googleapis.com/auth/devstorage.full_control";
3132
private static final Set<String> SCOPES = ImmutableSet.of(GCS_SCOPE);
3233
private static final String DEFAULT_PATH_DELIMITER = "/";
3334
private static final String PROJECT_ENV_NAME = "default_project_id";
3435

35-
private final StorageRpc storageRpc;
3636
private final String project;
3737
private final String pathDelimiter;
3838

39-
public static class Builder extends ServiceOptions.Builder<Builder> {
39+
public static class Builder extends
40+
ServiceOptions.Builder<StorageRpc, StorageServiceOptions, Builder> {
4041

4142
private String project;
4243
private String pathDelimiter;
43-
private StorageRpc storageRpc;
4444

4545
private Builder() {}
4646

@@ -58,11 +58,6 @@ public Builder pathDelimiter(String pathDelimiter) {
5858
return this;
5959
}
6060

61-
public Builder storageRpc(StorageRpc storageRpc) {
62-
this.storageRpc = storageRpc;
63-
return this;
64-
}
65-
6661
@Override
6762
public StorageServiceOptions build() {
6863
return new StorageServiceOptions(this);
@@ -74,16 +69,18 @@ private StorageServiceOptions(Builder builder) {
7469
pathDelimiter = MoreObjects.firstNonNull(builder.pathDelimiter, DEFAULT_PATH_DELIMITER);
7570
project = builder.project != null ? builder.project : defaultProject();
7671
Preconditions.checkArgument(project != null, "Missing required project id");
77-
storageRpc = MoreObjects.firstNonNull(builder.storageRpc, ServiceRpcProvider.storage(this));
7872
}
7973

8074
@Override
8175
protected Set<String> scopes() {
8276
return SCOPES;
8377
}
8478

85-
public StorageRpc storageRpc() {
86-
return storageRpc;
79+
StorageRpc storageRpc() {
80+
if (serviceRpcFactory() != null) {
81+
return serviceRpcFactory().create(this);
82+
}
83+
return ServiceRpcProvider.storage(this);
8784
}
8885

8986
public String project() {

branches/tswast-patch-1/src/test/java/com/google/gcloud/datastore/DatastoreServiceOptionsTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import static org.junit.Assert.assertTrue;
2424

2525
import com.google.gcloud.spi.DatastoreRpc;
26+
import com.google.gcloud.spi.DatastoreRpcFactory;
2627

2728
import org.easymock.EasyMock;
2829
import org.junit.Before;
@@ -33,17 +34,23 @@
3334
public class DatastoreServiceOptionsTest {
3435

3536
private static final String DATASET = "dataset";
37+
private DatastoreRpcFactory datastoreRpcFactory;
3638
private DatastoreRpc datastoreRpc;
3739
private DatastoreServiceOptions.Builder options;
3840

3941
@Before
4042
public void setUp() throws IOException, InterruptedException {
43+
datastoreRpcFactory = EasyMock.createMock(DatastoreRpcFactory.class);
4144
datastoreRpc = EasyMock.createMock(DatastoreRpc.class);
4245
options = DatastoreServiceOptions.builder()
4346
.normalizeDataset(false)
44-
.datastoreRpc(datastoreRpc)
47+
.serviceRpcFactory(datastoreRpcFactory)
4548
.dataset(DATASET)
4649
.host("http://localhost:" + LocalGcdHelper.PORT);
50+
EasyMock.expect(datastoreRpcFactory.create(EasyMock.anyObject(DatastoreServiceOptions.class)))
51+
.andReturn(datastoreRpc)
52+
.anyTimes();
53+
EasyMock.replay(datastoreRpcFactory, datastoreRpc);
4754
}
4855

4956
@Test
@@ -70,6 +77,7 @@ public void testForce() throws Exception {
7077

7178
@Test
7279
public void testDatastore() throws Exception {
80+
assertSame(datastoreRpcFactory, options.build().serviceRpcFactory());
7381
assertSame(datastoreRpc, options.build().datastoreRpc());
7482
}
7583

0 commit comments

Comments
 (0)