Skip to content

Commit 8f5dee2

Browse files
committed
Remove Serializable from AuthCredentials, ServiceFactory and ServiceRpcFactory and set factories on ServiceOptions
1 parent 6e25709 commit 8f5dee2

37 files changed

+582
-535
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,13 @@ Here is a code snippet showing a simple usage example from within Compute/App En
6161

6262
```java
6363
import com.google.gcloud.datastore.Datastore;
64-
import com.google.gcloud.datastore.DatastoreFactory;
6564
import com.google.gcloud.datastore.DatastoreOptions;
6665
import com.google.gcloud.datastore.DateTime;
6766
import com.google.gcloud.datastore.Entity;
6867
import com.google.gcloud.datastore.Key;
6968
import com.google.gcloud.datastore.KeyFactory;
7069

71-
Datastore datastore = DatastoreFactory.instance().get(DatastoreOptions.getDefaultInstance());
70+
Datastore datastore = DatastoreOptions.getDefaultInstance().service();
7271
KeyFactory keyFactory = datastore.newKeyFactory().kind(KIND);
7372
Key key = keyFactory.newKey(keyName);
7473
Entity entity = datastore.get(key);
@@ -106,14 +105,13 @@ import static java.nio.charset.StandardCharsets.UTF_8;
106105
import com.google.gcloud.storage.Blob;
107106
import com.google.gcloud.storage.BlobId;
108107
import com.google.gcloud.storage.Storage;
109-
import com.google.gcloud.storage.StorageFactory;
110108
import com.google.gcloud.storage.StorageOptions;
111109

112110
import java.nio.ByteBuffer;
113111
import java.nio.channels.WritableByteChannel;
114112

115113
StorageOptions options = StorageOptions.builder().projectId("project").build();
116-
Storage storage = StorageFactory.instance().get(options);
114+
Storage storage = options.service();
117115
BlobId blobId = BlobId.of("bucket", "blob_name");
118116
Blob blob = Blob.load(storage, blobId);
119117
if (blob == null) {

TESTING.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ You can test against a temporary local datastore by following these steps:
1818
.projectId(PROJECT_ID)
1919
.host("http://localhost:8080")
2020
.build();
21-
Datastore localDatastore = DatastoreFactory.instance().get(options);
21+
Datastore localDatastore = options.service();
2222
```
2323
3. Run your tests.
2424

@@ -35,7 +35,7 @@ You can test against a remote datastore emulator as well. To do this, set the `
3535
.projectId(PROJECT_ID)
3636
.host("http://<hostname of machine>:<port>")
3737
.build();
38-
Datastore localDatastore = DatastoreFactory.instance().get(options);
38+
Datastore localDatastore = options.service();
3939
```
4040

4141
Note that the remote datastore must be running before your tests are run.
@@ -52,7 +52,7 @@ Currently, there isn't an emulator for Google Cloud Storage, so an alternative i
5252
Here is an example that uses the `RemoteGcsHelper` to create a bucket.
5353
```java
5454
RemoteGcsHelper gcsHelper = RemoteGcsHelper.create(PROJECT_ID, "/path/to/my/JSON/key.json");
55-
Storage storage = StorageFactory.instance().get(gcsHelper.options());
55+
Storage storage = gcsHelper.options().service();
5656
String bucket = RemoteGcsHelper.generateBucketName();
5757
storage.create(BucketInfo.of(bucket));
5858
```

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

Lines changed: 133 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131

3232
import java.io.IOException;
3333
import java.io.InputStream;
34-
import java.io.ObjectInputStream;
35-
import java.io.ObjectStreamException;
3634
import java.io.Serializable;
3735
import java.security.GeneralSecurityException;
3836
import java.security.PrivateKey;
@@ -42,35 +40,91 @@
4240
/**
4341
* Credentials for accessing Google Cloud services.
4442
*/
45-
public abstract class AuthCredentials implements Serializable {
46-
47-
private static final long serialVersionUID = 236297804453464604L;
43+
public abstract class AuthCredentials implements Restorable<AuthCredentials> {
4844

4945
private static class AppEngineAuthCredentials extends AuthCredentials {
5046

51-
private static final long serialVersionUID = 7931300552744202954L;
52-
5347
private static final AuthCredentials INSTANCE = new AppEngineAuthCredentials();
48+
private static final AppEngineAuthCredentialsState STATE =
49+
new AppEngineAuthCredentialsState();
50+
51+
private static class AppEngineAuthCredentialsState
52+
implements RestorableState<AuthCredentials>, Serializable {
53+
54+
private static final long serialVersionUID = 3558563960848658928L;
55+
56+
@Override
57+
public AuthCredentials restore() {
58+
return INSTANCE;
59+
}
60+
61+
@Override
62+
public int hashCode() {
63+
return getClass().getName().hashCode();
64+
}
65+
66+
@Override
67+
public boolean equals(Object obj) {
68+
return obj instanceof AppEngineAuthCredentialsState;
69+
}
70+
}
5471

5572
@Override
5673
protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
5774
Set<String> scopes) {
5875
return new AppIdentityCredential(scopes);
5976
}
6077

61-
private Object readResolve() throws ObjectStreamException {
62-
return INSTANCE;
78+
@Override
79+
public RestorableState<AuthCredentials> capture() {
80+
return STATE;
6381
}
6482
}
6583

6684
public static class ServiceAccountAuthCredentials extends AuthCredentials {
6785

68-
private static final long serialVersionUID = 8007708734318445901L;
6986
private final String account;
7087
private final PrivateKey privateKey;
7188

7289
private static final AuthCredentials NO_CREDENTIALS = new ServiceAccountAuthCredentials();
7390

91+
private static class ServiceAccountAuthCredentialsState
92+
implements RestorableState<AuthCredentials>, Serializable {
93+
94+
private static final long serialVersionUID = -7302180782414633639L;
95+
96+
private final String account;
97+
private final PrivateKey privateKey;
98+
99+
private ServiceAccountAuthCredentialsState(String account, PrivateKey privateKey) {
100+
this.account = account;
101+
this.privateKey = privateKey;
102+
}
103+
104+
@Override
105+
public AuthCredentials restore() {
106+
if (account == null && privateKey == null) {
107+
return NO_CREDENTIALS;
108+
}
109+
return new ServiceAccountAuthCredentials(account, privateKey);
110+
}
111+
112+
@Override
113+
public int hashCode() {
114+
return Objects.hash(account, privateKey);
115+
}
116+
117+
@Override
118+
public boolean equals(Object obj) {
119+
if (!(obj instanceof ServiceAccountAuthCredentialsState)) {
120+
return false;
121+
}
122+
ServiceAccountAuthCredentialsState other = (ServiceAccountAuthCredentialsState) obj;
123+
return Objects.equals(account, other.account)
124+
&& Objects.equals(privateKey, other.privateKey);
125+
}
126+
}
127+
74128
ServiceAccountAuthCredentials(String account, PrivateKey privateKey) {
75129
this.account = checkNotNull(account);
76130
this.privateKey = checkNotNull(privateKey);
@@ -104,59 +158,94 @@ public PrivateKey privateKey() {
104158
}
105159

106160
@Override
107-
public int hashCode() {
108-
return Objects.hash(account, privateKey);
109-
}
110-
111-
@Override
112-
public boolean equals(Object obj) {
113-
if (!(obj instanceof ServiceAccountAuthCredentials)) {
114-
return false;
115-
}
116-
ServiceAccountAuthCredentials other = (ServiceAccountAuthCredentials) obj;
117-
return Objects.equals(account, other.account)
118-
&& Objects.equals(privateKey, other.privateKey);
161+
public RestorableState<AuthCredentials> capture() {
162+
return new ServiceAccountAuthCredentialsState(account, privateKey);
119163
}
120164
}
121165

122166
private static class ComputeEngineAuthCredentials extends AuthCredentials {
123167

124-
private static final long serialVersionUID = -5217355402127260144L;
168+
private ComputeCredential computeCredential;
125169

126-
private transient ComputeCredential computeCredential;
170+
private static final ComputeEngineAuthCredentialsState STATE =
171+
new ComputeEngineAuthCredentialsState();
127172

128-
ComputeEngineAuthCredentials() throws IOException, GeneralSecurityException {
129-
computeCredential = getComputeCredential();
130-
}
173+
private static class ComputeEngineAuthCredentialsState
174+
implements RestorableState<AuthCredentials>, Serializable {
175+
176+
private static final long serialVersionUID = -6168594072854417404L;
177+
178+
@Override
179+
public AuthCredentials restore() {
180+
try {
181+
return new ComputeEngineAuthCredentials();
182+
} catch (IOException | GeneralSecurityException e) {
183+
throw new IllegalStateException(
184+
"Could not restore " + ComputeEngineAuthCredentials.class.getSimpleName(), e);
185+
}
186+
}
187+
188+
@Override
189+
public int hashCode() {
190+
return getClass().getName().hashCode();
191+
}
131192

132-
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
133-
in.defaultReadObject();
134-
try {
135-
computeCredential = getComputeCredential();
136-
} catch (GeneralSecurityException e) {
137-
throw new IOException(e);
193+
@Override
194+
public boolean equals(Object obj) {
195+
return obj instanceof ComputeEngineAuthCredentialsState;
138196
}
139197
}
140198

199+
ComputeEngineAuthCredentials() throws IOException, GeneralSecurityException {
200+
computeCredential = getComputeCredential();
201+
}
202+
141203
@Override
142204
protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
143205
Set<String> scopes) {
144206
return computeCredential;
145207
}
208+
209+
@Override
210+
public RestorableState<AuthCredentials> capture() {
211+
return STATE;
212+
}
146213
}
147214

148215
private static class ApplicationDefaultAuthCredentials extends AuthCredentials {
149216

150-
private static final long serialVersionUID = -8306873864136099893L;
217+
private GoogleCredentials googleCredentials;
151218

152-
private transient GoogleCredentials googleCredentials;
219+
private static final ApplicationDefaultAuthCredentialsState STATE =
220+
new ApplicationDefaultAuthCredentialsState();
153221

154-
ApplicationDefaultAuthCredentials() throws IOException {
155-
googleCredentials = GoogleCredentials.getApplicationDefault();
222+
private static class ApplicationDefaultAuthCredentialsState
223+
implements RestorableState<AuthCredentials>, Serializable {
224+
225+
private static final long serialVersionUID = -8839085552021212257L;
226+
227+
@Override
228+
public AuthCredentials restore() {
229+
try {
230+
return new ApplicationDefaultAuthCredentials();
231+
} catch (IOException e) {
232+
throw new IllegalStateException(
233+
"Could not restore " + ApplicationDefaultAuthCredentials.class.getSimpleName(), e);
234+
}
235+
}
236+
237+
@Override
238+
public int hashCode() {
239+
return getClass().getName().hashCode();
240+
}
241+
242+
@Override
243+
public boolean equals(Object obj) {
244+
return obj instanceof ApplicationDefaultAuthCredentialsState;
245+
}
156246
}
157247

158-
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
159-
in.defaultReadObject();
248+
ApplicationDefaultAuthCredentials() throws IOException {
160249
googleCredentials = GoogleCredentials.getApplicationDefault();
161250
}
162251

@@ -165,6 +254,11 @@ protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
165254
Set<String> scopes) {
166255
return new HttpCredentialsAdapter(googleCredentials);
167256
}
257+
258+
@Override
259+
public RestorableState<AuthCredentials> capture() {
260+
return STATE;
261+
}
168262
}
169263

170264
protected abstract HttpRequestInitializer httpRequestInitializer(HttpTransport transport,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.google.gcloud;
1818

19-
public abstract class BaseService<OptionsT extends ServiceOptions<?, OptionsT>>
19+
public abstract class BaseService<OptionsT extends ServiceOptions<?, ?, OptionsT>>
2020
implements Service<OptionsT> {
2121

2222
private final OptionsT options;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.google.gcloud;
2+
3+
/**
4+
* Implementation of this interface can persist their state and restore from it.
5+
*/
6+
public interface Restorable<T extends Restorable<T>> {
7+
8+
/**
9+
* Capture the state of this object.
10+
*
11+
* @return a {@link RestorableState} instance that contains the state for this object and can
12+
* restore it afterwards.
13+
*/
14+
RestorableState<T> capture();
15+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* Implementations of this class must implement {@link java.io.Serializable} to ensure that the
2424
* state of a the object can be correctly serialized.
2525
*/
26-
public interface RestorableState<T> {
26+
public interface RestorableState<T extends Restorable<T>> {
2727

2828
/**
2929
* Returns an object whose internal state reflects the one saved in the invocation object.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616

1717
package com.google.gcloud;
1818

19-
public interface Service<OptionsT extends ServiceOptions<?, OptionsT>> {
19+
public interface Service<OptionsT extends ServiceOptions<?, ?, OptionsT>> {
2020
OptionsT options();
2121
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.gcloud;
18+
19+
/**
20+
* A base interface for all service factories.
21+
*
22+
* Implementation must provide a public no-arg constructor.
23+
* Loading of a factory implementation is done via {@link java.util.ServiceLoader}.
24+
*/
25+
public interface ServiceFactory<ServiceT extends Service, ServiceOptionsT extends ServiceOptions> {
26+
27+
ServiceT create(ServiceOptionsT serviceOptions);
28+
}

0 commit comments

Comments
 (0)