3131
3232import java .io .IOException ;
3333import java .io .InputStream ;
34- import java .io .ObjectInputStream ;
35- import java .io .ObjectStreamException ;
3634import java .io .Serializable ;
3735import java .security .GeneralSecurityException ;
3836import java .security .PrivateKey ;
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 ,
0 commit comments