Skip to content

Commit 76dc17a

Browse files
committed
---
yaml --- r: 591 b: refs/heads/travis c: d1d8d71 h: refs/heads/master i: 589: 4f8d1f3 587: 5db5cf0 583: b9e07c1 575: 68a2ac3 v: v3
1 parent ce93094 commit 76dc17a

31 files changed

Lines changed: 241 additions & 211 deletions

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
---
22
refs/heads/master: 92537786d4fa0a9fb51215f27d3d0581f4fe3a65
3-
refs/heads/travis: 0b9cb9bd6fadb16e3e7869168830c58d0ec808aa
3+
refs/heads/travis: d1d8d71b67d9b76efbd03d45f94312e87f0c35f6
44
refs/heads/gh-pages: 2cc964c1ecc5d653c449b81ee9e924182b221d18

branches/travis/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/target/
22
.settings
33
.idea
4+
.DS_Store
45
*.iml
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<FindBugsFilter>
2+
</FindBugsFilter>

branches/travis/git-demo.iml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
1111
<excludeFolder url="file://$MODULE_DIR$/target" />
1212
</content>
13-
<orderEntry type="inheritedJdk" />
13+
<orderEntry type="jdk" jdkName="1.7 (1)" jdkType="JavaSDK" />
1414
<orderEntry type="sourceFolder" forTests="false" />
1515
<orderEntry type="library" name="Maven: com.google.http-client:google-http-client:1.19.0" level="project" />
1616
<orderEntry type="library" name="Maven: com.google.code.findbugs:jsr305:1.3.9" level="project" />
@@ -244,4 +244,4 @@
244244
</map>
245245
</option>
246246
</component>
247-
</module>
247+
</module>

branches/travis/src/main/java/com/google/gcloud/AuthConfig.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,8 @@ protected HttpRequestInitializer httpRequestInitializer(
5757
}
5858
}
5959

60-
protected abstract HttpRequestInitializer httpRequestInitializer(
61-
HttpTransport transport, Set<String> scopes);
62-
60+
protected abstract HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
61+
Set<String> scopes);
6362

6463
public static AuthConfig createForAppEngine() {
6564
return new AppEngineAuthConfig();
@@ -69,7 +68,8 @@ public static AuthConfig createForComputeEngine() throws IOException, GeneralSec
6968
final ComputeCredential cred = getComputeCredential();
7069
return new AuthConfig() {
7170
@Override
72-
protected HttpRequestInitializer httpRequestInitializer(HttpTransport ts, Set<String> sc) {
71+
protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
72+
Set<String> scopes) {
7373
return cred;
7474
}
7575
};

branches/travis/src/main/java/com/google/gcloud/ExceptionHandler.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public final class ExceptionHandler implements Serializable {
2727
private final ImmutableList<Interceptor> interceptors;
2828
private final ImmutableSet<Class<? extends Exception>> retriableExceptions;
2929
private final ImmutableSet<Class<? extends Exception>> nonRetriableExceptions;
30-
private final Set<RetryInfo> retryInfos = Sets.newHashSet();
30+
private final Set<RetryInfo> retryInfo = Sets.newHashSet();
3131

3232
public interface Interceptor extends Serializable {
3333

@@ -38,7 +38,7 @@ enum RetryResult {
3838

3939
private final boolean booleanValue;
4040

41-
private RetryResult(boolean booleanValue) {
41+
RetryResult(boolean booleanValue) {
4242
this.booleanValue = booleanValue;
4343
}
4444

@@ -55,7 +55,7 @@ boolean booleanValue() {
5555
* ({@link RetryResult#RETRY}), propagated ({@link RetryResult#ABORT}),
5656
* or evaluation should proceed ({@code null}).
5757
*/
58-
RetryResult shouldRetry(Exception exception);
58+
RetryResult beforeEval(Exception exception);
5959

6060
/**
6161
* This method is called after the evaluation and could alter its result.
@@ -66,7 +66,7 @@ boolean booleanValue() {
6666
* ({@link RetryResult#RETRY}), propagated ({@link RetryResult#ABORT}),
6767
* or evaluation should proceed ({@code null}).
6868
*/
69-
RetryResult shouldRetry(Exception exception, RetryResult retryResult);
69+
RetryResult afterEval(Exception exception, RetryResult retryResult);
7070
}
7171

7272
/**
@@ -173,31 +173,30 @@ private ExceptionHandler(Builder builder) {
173173
Sets.intersection(retriableExceptions, nonRetriableExceptions).isEmpty(),
174174
"Same exception was found in both retriable and non-retriable sets");
175175
for (Class<? extends Exception> exception : retriableExceptions) {
176-
addToRetryInfos(retryInfos, new RetryInfo(exception, Interceptor.RetryResult.RETRY));
176+
addRetryInfo(new RetryInfo(exception, Interceptor.RetryResult.RETRY), retryInfo);
177177
}
178178
for (Class<? extends Exception> exception : nonRetriableExceptions) {
179-
addToRetryInfos(retryInfos, new RetryInfo(exception, Interceptor.RetryResult.ABORT));
179+
addRetryInfo(new RetryInfo(exception, Interceptor.RetryResult.ABORT), retryInfo);
180180
}
181181
}
182182

183-
private static void addToRetryInfos(Set<RetryInfo> retryInfos, RetryInfo retryInfo) {
184-
for (RetryInfo current : retryInfos) {
183+
private static void addRetryInfo(RetryInfo retryInfo, Set<RetryInfo> dest) {
184+
for (RetryInfo current : dest) {
185185
if (current.exception.isAssignableFrom(retryInfo.exception)) {
186-
addToRetryInfos(current.children, retryInfo);
186+
addRetryInfo(retryInfo, current.children);
187187
return;
188188
}
189189
if (retryInfo.exception.isAssignableFrom(current.exception)) {
190190
retryInfo.children.add(current);
191191
}
192192
}
193-
retryInfos.removeAll(retryInfo.children);
194-
retryInfos.add(retryInfo);
193+
dest.removeAll(retryInfo.children);
194+
dest.add(retryInfo);
195195
}
196196

197-
198-
private static RetryInfo findMostSpecificRetryInfo(Set<RetryInfo> retryInfos,
197+
private static RetryInfo findMostSpecificRetryInfo(Set<RetryInfo> retryInfo,
199198
Class<? extends Exception> exception) {
200-
for (RetryInfo current : retryInfos) {
199+
for (RetryInfo current : retryInfo) {
201200
if (current.exception.isAssignableFrom(exception)) {
202201
RetryInfo match = findMostSpecificRetryInfo(current.children, exception);
203202
return match == null ? current : match;
@@ -223,10 +222,10 @@ void verifyCaller(Callable<?> callable) {
223222
Method callMethod = getCallableMethod(callable.getClass());
224223
for (Class<?> exceptionOrError : callMethod.getExceptionTypes()) {
225224
Preconditions.checkArgument(Exception.class.isAssignableFrom(exceptionOrError),
226-
"Callable method exceptions must be dervied from Exception");
225+
"Callable method exceptions must be derived from Exception");
227226
@SuppressWarnings("unchecked") Class<? extends Exception> exception =
228227
(Class<? extends Exception>) exceptionOrError;
229-
Preconditions.checkArgument(findMostSpecificRetryInfo(retryInfos, exception) != null,
228+
Preconditions.checkArgument(findMostSpecificRetryInfo(retryInfo, exception) != null,
230229
"Declared exception '" + exception + "' is not covered by exception handler");
231230
}
232231
}
@@ -241,16 +240,16 @@ public Set<Class<? extends Exception>> getNonRetriableExceptions() {
241240

242241
boolean shouldRetry(Exception ex) {
243242
for (Interceptor interceptor : interceptors) {
244-
Interceptor.RetryResult retryResult = interceptor.shouldRetry(ex);
243+
Interceptor.RetryResult retryResult = interceptor.beforeEval(ex);
245244
if (retryResult != null) {
246245
return retryResult.booleanValue();
247246
}
248247
}
249-
RetryInfo retryInfo = findMostSpecificRetryInfo(retryInfos, ex.getClass());
248+
RetryInfo retryInfo = findMostSpecificRetryInfo(this.retryInfo, ex.getClass());
250249
Interceptor.RetryResult retryResult =
251250
retryInfo == null ? Interceptor.RetryResult.ABORT : retryInfo.retry;
252251
for (Interceptor interceptor : interceptors) {
253-
retryResult = firstNonNull(interceptor.shouldRetry(ex, retryResult), retryResult);
252+
retryResult = firstNonNull(interceptor.afterEval(ex, retryResult), retryResult);
254253
}
255254
return retryResult.booleanValue();
256255
}

branches/travis/src/main/java/com/google/gcloud/RetryHelper.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.io.InterruptedIOException;
1616
import java.nio.channels.ClosedByInterruptException;
1717
import java.util.concurrent.Callable;
18+
import java.util.logging.Level;
1819
import java.util.logging.Logger;
1920

2021
/**
@@ -67,7 +68,7 @@ public static final class RetryInterruptedException extends RetryHelperException
6768
RetryInterruptedException() {}
6869

6970
/**
70-
* Sets the caller thread interrupt flag and throws {@code RetryInteruptedException}.
71+
* Sets the caller thread interrupt flag and throws {@code RetryInterruptedException}.
7172
*/
7273
public static void propagate() throws RetryInterruptedException {
7374
Thread.currentThread().interrupt();
@@ -147,9 +148,11 @@ static Context getContext() {
147148
@Override
148149
public String toString() {
149150
ToStringHelper toStringHelper = MoreObjects.toStringHelper(this);
151+
toStringHelper.add("params", params);
150152
toStringHelper.add("stopwatch", stopwatch);
151153
toStringHelper.add("attemptNumber", attemptNumber);
152154
toStringHelper.add("callable", callable);
155+
toStringHelper.add("exceptionHandler", exceptionHandler);
153156
return toStringHelper.toString();
154157
}
155158

@@ -160,7 +163,7 @@ private V doRetry() throws RetryHelperException {
160163
Exception exception;
161164
try {
162165
V value = callable.call();
163-
if (attemptNumber > 1) {
166+
if (attemptNumber > 1 && log.isLoggable(Level.FINE)) {
164167
log.fine(this + ": attempt #" + attemptNumber + " succeeded");
165168
}
166169
return value;
@@ -181,11 +184,14 @@ private V doRetry() throws RetryHelperException {
181184
throw new RetriesExhaustedException(this + ": Too many failures, giving up", exception);
182185
}
183186
long sleepDurationMillis = getSleepDuration(params, attemptNumber);
184-
log.fine(this + ": Attempt #" + attemptNumber + " failed [" + exception + "], sleeping for "
185-
+ sleepDurationMillis + " ms");
187+
if (log.isLoggable(Level.FINE)) {
188+
log.fine(this + ": Attempt #" + attemptNumber + " failed [" + exception + "], sleeping for "
189+
+ sleepDurationMillis + " ms");
190+
}
186191
try {
187192
Thread.sleep(sleepDurationMillis);
188193
} catch (InterruptedException e) {
194+
// propagate as RetryInterruptedException
189195
RetryInterruptedException.propagate();
190196
}
191197
}

branches/travis/src/main/java/com/google/gcloud/ServiceOptions.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import static com.google.common.base.MoreObjects.firstNonNull;
5+
import static java.nio.charset.StandardCharsets.UTF_8;
56

67
import com.google.api.client.extensions.appengine.http.UrlFetchTransport;
78
import com.google.api.client.http.HttpRequestInitializer;
@@ -11,6 +12,7 @@
1112
import java.io.BufferedReader;
1213
import java.io.IOException;
1314
import java.io.InputStreamReader;
15+
import java.lang.reflect.Method;
1416
import java.net.URL;
1517
import java.net.URLConnection;
1618
import java.util.Set;
@@ -121,10 +123,29 @@ protected static String googleCloudProjectId() {
121123
URLConnection connection = url.openConnection();
122124
connection.setRequestProperty("X-Google-Metadata-Request", "True");
123125
try (BufferedReader reader =
124-
new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
126+
new BufferedReader(new InputStreamReader(connection.getInputStream(), UTF_8))) {
125127
return reader.readLine();
126128
}
127-
} catch (IOException e) {
129+
} catch (IOException ignore) {
130+
// return null if can't determine
131+
return null;
132+
}
133+
}
134+
135+
protected static String getAppEngineProjectId() {
136+
// TODO(ozarov): An alternative to reflection would be to depend on AE api jar:
137+
// http://mvnrepository.com/artifact/com.google.appengine/appengine-api-1.0-sdk/1.2.0
138+
try {
139+
Class<?> factoryClass =
140+
Class.forName("com.google.appengine.api.appidentity.AppIdentityServiceFactory");
141+
Method method = factoryClass.getMethod("getAppIdentityService");
142+
Object appIdentityService = method.invoke(null);
143+
method = appIdentityService.getClass().getMethod("getServiceAccountName");
144+
String serviceAccountName = (String) method.invoke(appIdentityService);
145+
int indexOfAtSign = serviceAccountName.indexOf('@');
146+
return serviceAccountName.substring(0, indexOfAtSign);
147+
} catch (Exception ignore) {
148+
// return null if can't determine
128149
return null;
129150
}
130151
}

branches/travis/src/main/java/com/google/gcloud/datastore/BaseEntity.java

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@ abstract class BaseEntity extends Serializable<DatastoreV1.Entity> {
2828

2929
private final transient ImmutableSortedMap<String, Value<?>> properties;
3030

31-
protected abstract static class Builder<B extends Builder<B>> {
31+
abstract static class Builder<B extends Builder<B>> {
3232

33-
protected final Map<String, Value<?>> properties;
33+
final Map<String, Value<?>> properties;
3434

35-
protected Builder() {
35+
Builder() {
3636
properties = new HashMap<>();
3737
}
3838

39-
protected Builder(BaseEntity entity) {
39+
Builder(BaseEntity entity) {
4040
properties = new HashMap<>(entity.properties());
4141
}
4242

4343
@SuppressWarnings("unchecked")
44-
protected B self() {
44+
B self() {
4545
return (B) this;
4646
}
4747

@@ -124,7 +124,7 @@ public B setNull(String name) {
124124
public abstract BaseEntity build();
125125
}
126126

127-
protected BaseEntity(ImmutableSortedMap<String, Value<?>> properties) {
127+
BaseEntity(ImmutableSortedMap<String, Value<?>> properties) {
128128
this.properties = properties;
129129
}
130130

@@ -153,41 +153,49 @@ public boolean isNull(String name) {
153153
return getValue(name) instanceof NullValue;
154154
}
155155

156+
@SuppressWarnings("unchecked")
156157
public String getString(String name) {
157-
return ((StringValue) getValue(name)).get();
158+
return ((Value<String>) getValue(name)).get();
158159
}
159160

161+
@SuppressWarnings("unchecked")
160162
public long getLong(String name) {
161-
return ((LongValue) getValue(name)).get();
163+
return ((Value<Long>) getValue(name)).get();
162164
}
163165

166+
@SuppressWarnings("unchecked")
164167
public double getDouble(String name) {
165-
return ((DoubleValue) getValue(name)).get();
168+
return ((Value<Double>) getValue(name)).get();
166169
}
167170

171+
@SuppressWarnings("unchecked")
168172
public boolean getBoolean(String name) {
169-
return ((BooleanValue) getValue(name)).get();
173+
return ((Value<Boolean>) getValue(name)).get();
170174
}
171175

176+
@SuppressWarnings("unchecked")
172177
public DateTime getDateTime(String name) {
173-
return ((DateTimeValue) getValue(name)).get();
178+
return ((Value<DateTime>) getValue(name)).get();
174179
}
175180

181+
@SuppressWarnings("unchecked")
176182
public Key getKey(String name) {
177-
return ((KeyValue) getValue(name)).get();
183+
return ((Value<Key>) getValue(name)).get();
178184
}
179185

180186
@SuppressWarnings("unchecked")
181187
public <T extends PartialEntity> T getEntity(String name) {
182-
return (T) ((EntityValue) getValue(name)).get();
188+
return (T) ((Value<PartialEntity>) getValue(name)).get();
183189
}
184190

191+
@SuppressWarnings("unchecked")
185192
public List<? extends Value<?>> getList(String name) {
186-
return ((ListValue) getValue(name)).get();
193+
return ((Value<List<? extends Value<?>>>) getValue(name)).get();
187194
}
188195

196+
@SuppressWarnings("unchecked")
189197
public Blob getBlob(String name) {
190-
return ((BlobValue) getValue(name)).get();
198+
return ((Value<Blob>) getValue(name)).get();
191199
}
192200

193201
/**

0 commit comments

Comments
 (0)