Skip to content

Commit f995b12

Browse files
author
Ajay Kannan
committed
Merge to get AuthCredentials updates
2 parents a560777 + a4c4273 commit f995b12

54 files changed

Lines changed: 1419 additions & 469 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ If you are using Maven, add this to your pom.xml file
2525
<dependency>
2626
<groupId>com.google.gcloud</groupId>
2727
<artifactId>gcloud-java</artifactId>
28-
<version>0.0.10</version>
28+
<version>0.0.12</version>
2929
</dependency>
3030
```
3131
If you are using Gradle, add this to your dependencies
3232
```Groovy
33-
compile 'com.google.gcloud:gcloud-java:jar:0.0.10'
33+
compile 'com.google.gcloud:gcloud-java:jar:0.0.12'
3434
```
3535
If you are using SBT, add this to your dependencies
3636
```Scala
37-
libraryDependencies += "com.google.gcloud" % "gcloud-java" % "0.0.10"
37+
libraryDependencies += "com.google.gcloud" % "gcloud-java" % "0.0.12"
3838
```
3939

4040
Example Applications
@@ -121,7 +121,7 @@ import com.google.gcloud.datastore.Entity;
121121
import com.google.gcloud.datastore.Key;
122122
import com.google.gcloud.datastore.KeyFactory;
123123
124-
Datastore datastore = DatastoreOptions.getDefaultInstance().service();
124+
Datastore datastore = DatastoreOptions.defaultInstance().service();
125125
KeyFactory keyFactory = datastore.newKeyFactory().kind(KIND);
126126
Key key = keyFactory.newKey(keyName);
127127
Entity entity = datastore.get(key);
@@ -164,8 +164,7 @@ import com.google.gcloud.storage.StorageOptions;
164164
import java.nio.ByteBuffer;
165165
import java.nio.channels.WritableByteChannel;
166166
167-
StorageOptions options = StorageOptions.builder().projectId("project").build();
168-
Storage storage = options.service();
167+
Storage storage = StorageOptions.defaultInstance().service();
169168
BlobId blobId = BlobId.of("bucket", "blob_name");
170169
Blob blob = Blob.load(storage, blobId);
171170
if (blob == null) {

RELEASING.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ This script takes an optional argument denoting the new version. By default, if
1010
2. Create a PR to update the pom.xml version.
1111
The PR should look something like [#225](https://github.com/GoogleCloudPlatform/gcloud-java/pull/225). After this PR is merged into GoogleCloudPlatform/gcloud-java, Travis CI will push a new website to GoogleCloudPlatform/gh-pages, push a new artifact to the Maven Central Repository, and update versions in the README files.
1212

13-
3. Create a release on Github manually.
13+
3. Before moving on, verify that the artifacts have successfully been pushed to the Maven Central Repository. Open Travis CI, click the ["Build History" tab](https://travis-ci.org/GoogleCloudPlatform/gcloud-java/builds), and open the second build's logs for Step 2's PR. Be sure that you are not opening the "Pull Request" build logs. When the build finishes, scroll to the end of the log and verify that the artifacts were successfully staged and deployed. You can also search for `gcloud-java` on the [Sonatype website](https://oss.sonatype.org/#nexus-search;quick~gcloud-java) and check the latest version number. If the deployment didn't succeed because of a flaky test, rerun the build.
14+
15+
4. Create a release on Github manually.
1416
Go to the [releases page](https://github.com/GoogleCloudPlatform/gcloud-java/releases) and click "Draft a new release." Use `vX.Y.Z` as the "Tag Version" and `X.Y.Z` as the "Release Title", where `X.Y.Z` is the release version as listed in the `pom.xml` files.
1517

16-
4. Run `utilities/update_pom_version.sh` again (to include "-SNAPSHOT" in the project version).
18+
5. Run `utilities/update_pom_version.sh` again (to include "-SNAPSHOT" in the project version).
1719
As mentioned before, there is an optional version argument. By default, the script will update the version from "X.Y.Z" to "X.Y.Z+1-SNAPSHOT". Suppose a different version is desired, for example X+1.0.0-SNAPSHOT. Then the appropriate command to run would be `utilities/update_pom_version.sh X+1.0.0-SNAPSHOT`.
1820

19-
5. Create and merge in another PR to reflect the updated project version. For an example of what this PR should look like, see [#227](https://github.com/GoogleCloudPlatform/gcloud-java/pull/227).
21+
6. Create and merge in another PR to reflect the updated project version. For an example of what this PR should look like, see [#227](https://github.com/GoogleCloudPlatform/gcloud-java/pull/227).
2022

2123
### To push a snapshot version
2224

gcloud-java-core/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ If you are using Maven, add this to your pom.xml file
1717
<dependency>
1818
<groupId>com.google.gcloud</groupId>
1919
<artifactId>gcloud-java-core</artifactId>
20-
<version>0.0.10</version>
20+
<version>0.0.12</version>
2121
</dependency>
2222
```
2323
If you are using Gradle, add this to your dependencies
2424
```Groovy
25-
compile 'com.google.gcloud:gcloud-java-core:jar:0.0.10'
25+
compile 'com.google.gcloud:gcloud-java-core:jar:0.0.12'
2626
```
2727
If you are using SBT, add this to your dependencies
2828
```Scala
29-
libraryDependencies += "com.google.gcloud" % "gcloud-java-core" % "0.0.10"
29+
libraryDependencies += "com.google.gcloud" % "gcloud-java-core" % "0.0.12"
3030
```
3131

3232
Java Versions

gcloud-java-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<parent>
1212
<groupId>com.google.gcloud</groupId>
1313
<artifactId>gcloud-java-pom</artifactId>
14-
<version>0.0.11-SNAPSHOT</version>
14+
<version>0.0.13-SNAPSHOT</version>
1515
</parent>
1616
<properties>
1717
<site.installationModule>gcloud-java-core</site.installationModule>

gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.api.client.json.jackson.JacksonFactory;
2525
import com.google.auth.http.HttpCredentialsAdapter;
2626
import com.google.auth.oauth2.GoogleCredentials;
27+
import com.google.auth.oauth2.ServiceAccountCredentials;
2728

2829
import java.io.IOException;
2930
import java.io.InputStream;
@@ -119,7 +120,7 @@ public RestorableState<AuthCredentials> capture() {
119120
}
120121
}
121122

122-
private static class ApplicationDefaultAuthCredentials extends AuthCredentials {
123+
public static class ApplicationDefaultAuthCredentials extends AuthCredentials {
123124

124125
private GoogleCredentials googleCredentials;
125126

@@ -159,7 +160,16 @@ public boolean equals(Object obj) {
159160
@Override
160161
protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
161162
Set<String> scopes) {
162-
return new HttpCredentialsAdapter(googleCredentials);
163+
return new HttpCredentialsAdapter(googleCredentials.createScoped(scopes));
164+
}
165+
166+
public ServiceAccountAuthCredentials toServiceAccountCredentials() {
167+
if (googleCredentials instanceof ServiceAccountCredentials) {
168+
ServiceAccountCredentials credentials = (ServiceAccountCredentials) googleCredentials;
169+
return new ServiceAccountAuthCredentials(credentials.getClientEmail(),
170+
credentials.getPrivateKey());
171+
}
172+
return null;
163173
}
164174

165175
@Override

gcloud-java-core/src/main/java/com/google/gcloud/ExceptionHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,11 @@ void verifyCaller(Callable<?> callable) {
231231
}
232232
}
233233

234-
public Set<Class<? extends Exception>> getRetriableExceptions() {
234+
public Set<Class<? extends Exception>> retriableExceptions() {
235235
return retriableExceptions;
236236
}
237237

238-
public Set<Class<? extends Exception>> getNonRetriableExceptions() {
238+
public Set<Class<? extends Exception>> nonRetriableExceptions() {
239239
return nonRetriableExceptions;
240240
}
241241

@@ -262,7 +262,7 @@ boolean shouldRetry(Exception ex) {
262262
/**
263263
* Returns an instance which retry any checked exception and abort on any runtime exception.
264264
*/
265-
public static ExceptionHandler getDefaultInstance() {
265+
public static ExceptionHandler defaultInstance() {
266266
return DEFAULT_INSTANCE;
267267
}
268268

gcloud-java-core/src/main/java/com/google/gcloud/Page.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,30 @@
1616

1717
package com.google.gcloud;
1818

19+
import java.util.Iterator;
20+
1921
/**
2022
* Interface for Google Cloud paginated results.
2123
*
2224
* <p>
23-
* A typical {@code Page} usage:
25+
* Use {@code Page} to iterate through all values (also in next pages):
26+
* <pre> {@code
27+
* Page<T> page = ...; // get a Page<T> instance
28+
* Iterator<T> iterator = page.iterateAll();
29+
* while (iterator.hasNext()) {
30+
* T value = iterator.next();
31+
* // do something with value
32+
* }}</pre>
33+
* <p>
34+
* Or handle pagination explicitly:
2435
* <pre> {@code
2536
* Page<T> page = ...; // get a Page<T> instance
2637
* while (page != null) {
2738
* for (T value : page.values()) {
2839
* // do something with value
2940
* }
3041
* page = page.nextPage();
31-
* }
32-
* }</pre>
42+
* }}</pre>
3343
*/
3444
public interface Page<T> {
3545

@@ -38,6 +48,12 @@ public interface Page<T> {
3848
*/
3949
Iterable<T> values();
4050

51+
/**
52+
* Returns an iterator for all values, possibly also in the next pages. Once current page's values
53+
* are traversed the iterator fetches next page, if any.
54+
*/
55+
Iterator<T> iterateAll();
56+
4157
/**
4258
* Returns the cursor for the nextPage or {@code null} if no more results.
4359
*/

gcloud-java-core/src/main/java/com/google/gcloud/PageImpl.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616

1717
package com.google.gcloud;
1818

19+
import com.google.common.collect.AbstractIterator;
20+
1921
import java.io.Serializable;
2022
import java.util.Collections;
23+
import java.util.Iterator;
2124
import java.util.Objects;
2225

2326
/**
@@ -35,6 +38,29 @@ public interface NextPageFetcher<T> extends Serializable {
3538
Page<T> nextPage();
3639
}
3740

41+
static class PageIterator<T> extends AbstractIterator<T> {
42+
43+
private Iterator<T> currentPageIterator;
44+
private Page<T> currentPage;
45+
46+
PageIterator(Page<T> currentPage) {
47+
this.currentPageIterator = currentPage.values().iterator();
48+
this.currentPage = currentPage;
49+
}
50+
51+
@Override
52+
protected T computeNext() {
53+
while (!currentPageIterator.hasNext()) {
54+
currentPage = currentPage.nextPage();
55+
if (currentPage == null) {
56+
return endOfData();
57+
}
58+
currentPageIterator = currentPage.values().iterator();
59+
}
60+
return currentPageIterator.next();
61+
}
62+
}
63+
3864
/**
3965
* Creates a {@code PageImpl} object. In order for the object to be serializable the {@code
4066
* results} parameter must be serializable.
@@ -50,6 +76,11 @@ public Iterable<T> values() {
5076
return results == null ? Collections.EMPTY_LIST : results;
5177
}
5278

79+
@Override
80+
public Iterator<T> iterateAll() {
81+
return new PageIterator<T>(this);
82+
}
83+
5384
@Override
5485
public String nextPageCursor() {
5586
return cursor;

gcloud-java-core/src/main/java/com/google/gcloud/RetryHelper.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,9 @@ private V doRetry() throws RetryHelperException {
194194
}
195195
exception = e;
196196
}
197-
if (attemptNumber >= params.getRetryMaxAttempts()
198-
|| attemptNumber >= params.getRetryMinAttempts()
199-
&& stopwatch.elapsed(MILLISECONDS) >= params.getTotalRetryPeriodMillis()) {
197+
if (attemptNumber >= params.retryMaxAttempts()
198+
|| attemptNumber >= params.retryMinAttempts()
199+
&& stopwatch.elapsed(MILLISECONDS) >= params.totalRetryPeriodMillis()) {
200200
throw new RetriesExhaustedException(this + ": Too many failures, giving up", exception);
201201
}
202202
long sleepDurationMillis = getSleepDuration(params, attemptNumber);
@@ -215,9 +215,9 @@ private V doRetry() throws RetryHelperException {
215215

216216
@VisibleForTesting
217217
static long getSleepDuration(RetryParams retryParams, int attemptsSoFar) {
218-
long initialDelay = retryParams.getInitialRetryDelayMillis();
219-
double backoffFactor = retryParams.getRetryDelayBackoffFactor();
220-
long maxDelay = retryParams.getMaxRetryDelayMillis();
218+
long initialDelay = retryParams.initialRetryDelayMillis();
219+
double backoffFactor = retryParams.retryDelayBackoffFactor();
220+
long maxDelay = retryParams.maxRetryDelayMillis();
221221
long retryDelay = getExponentialValue(initialDelay, backoffFactor, maxDelay, attemptsSoFar);
222222
return (long) ((random() / 2.0 + .75) * retryDelay);
223223
}
@@ -228,8 +228,8 @@ private static long getExponentialValue(long initialDelay, double backoffFactor,
228228
}
229229

230230
public static <V> V runWithRetries(Callable<V> callable) throws RetryHelperException {
231-
return runWithRetries(callable, RetryParams.getDefaultInstance(),
232-
ExceptionHandler.getDefaultInstance());
231+
return runWithRetries(callable, RetryParams.defaultInstance(),
232+
ExceptionHandler.defaultInstance());
233233
}
234234

235235
public static <V> V runWithRetries(Callable<V> callable, RetryParams params,

gcloud-java-core/src/main/java/com/google/gcloud/RetryParams.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
* {@code RetryParams}, first create a {@link RetryParams.Builder}. The builder is mutable and each
3939
* of the parameters can be set (any unset parameters will fallback to the defaults). The
4040
* {@code Builder} can be then used to create an immutable {@code RetryParams} object. For default
41-
* {@code RetryParams} use {@link #getDefaultInstance}. Default settings are subject to change
42-
* release to release. If you require specific settings, explicitly create an instance of
41+
* {@code RetryParams} use {@link #defaultInstance}. Default settings are subject to change release
42+
* to release. If you require specific settings, explicitly create an instance of
4343
* {@code RetryParams} with all the required settings.
4444
*
4545
* @see RetryHelper
@@ -91,12 +91,12 @@ private Builder() {
9191
retryDelayBackoffFactor = DEFAULT_RETRY_DELAY_BACKOFF_FACTOR;
9292
totalRetryPeriodMillis = DEFAULT_TOTAL_RETRY_PERIOD_MILLIS;
9393
} else {
94-
retryMinAttempts = retryParams.getRetryMinAttempts();
95-
retryMaxAttempts = retryParams.getRetryMaxAttempts();
96-
initialRetryDelayMillis = retryParams.getInitialRetryDelayMillis();
97-
maxRetryDelayMillis = retryParams.getMaxRetryDelayMillis();
98-
retryDelayBackoffFactor = retryParams.getRetryDelayBackoffFactor();
99-
totalRetryPeriodMillis = retryParams.getTotalRetryPeriodMillis();
94+
retryMinAttempts = retryParams.retryMinAttempts();
95+
retryMaxAttempts = retryParams.retryMaxAttempts();
96+
initialRetryDelayMillis = retryParams.initialRetryDelayMillis();
97+
maxRetryDelayMillis = retryParams.maxRetryDelayMillis();
98+
retryDelayBackoffFactor = retryParams.retryDelayBackoffFactor();
99+
totalRetryPeriodMillis = retryParams.totalRetryPeriodMillis();
100100
}
101101
}
102102

@@ -201,7 +201,7 @@ private RetryParams(Builder builder) {
201201
/**
202202
* Returns an instance with the default parameters.
203203
*/
204-
public static RetryParams getDefaultInstance() {
204+
public static RetryParams defaultInstance() {
205205
return DEFAULT_INSTANCE;
206206
}
207207

@@ -216,45 +216,45 @@ public static RetryParams noRetries() {
216216
/**
217217
* Returns the retryMinAttempts. Default value is {@value #DEFAULT_RETRY_MIN_ATTEMPTS}.
218218
*/
219-
public int getRetryMinAttempts() {
219+
public int retryMinAttempts() {
220220
return retryMinAttempts;
221221
}
222222

223223
/**
224224
* Returns the retryMaxAttempts. Default value is {@value #DEFAULT_RETRY_MAX_ATTEMPTS}.
225225
*/
226-
public int getRetryMaxAttempts() {
226+
public int retryMaxAttempts() {
227227
return retryMaxAttempts;
228228
}
229229

230230
/**
231231
* Returns the initialRetryDelayMillis. Default value is
232232
* {@value #DEFAULT_INITIAL_RETRY_DELAY_MILLIS}.
233233
*/
234-
public long getInitialRetryDelayMillis() {
234+
public long initialRetryDelayMillis() {
235235
return initialRetryDelayMillis;
236236
}
237237

238238
/**
239239
* Returns the maxRetryDelayMillis. Default values is {@value #DEFAULT_MAX_RETRY_DELAY_MILLIS}.
240240
*/
241-
public long getMaxRetryDelayMillis() {
241+
public long maxRetryDelayMillis() {
242242
return maxRetryDelayMillis;
243243
}
244244

245245
/**
246246
* Returns the maxRetryDelayBackoffFactor. Default values is
247247
* {@value #DEFAULT_RETRY_DELAY_BACKOFF_FACTOR}.
248248
*/
249-
public double getRetryDelayBackoffFactor() {
249+
public double retryDelayBackoffFactor() {
250250
return retryDelayBackoffFactor;
251251
}
252252

253253
/**
254254
* Returns the totalRetryPeriodMillis. Default value is
255255
* {@value #DEFAULT_TOTAL_RETRY_PERIOD_MILLIS}.
256256
*/
257-
public long getTotalRetryPeriodMillis() {
257+
public long totalRetryPeriodMillis() {
258258
return totalRetryPeriodMillis;
259259
}
260260

0 commit comments

Comments
 (0)