3131
3232import java .io .IOException ;
3333import java .io .InputStream ;
34+ import java .io .ObjectInputStream ;
35+ import java .io .ObjectStreamException ;
3436import java .io .Serializable ;
3537import java .security .GeneralSecurityException ;
3638import java .security .PrivateKey ;
4042/**
4143 * Credentials for accessing Google Cloud services.
4244 */
43- public abstract class AuthCredentials implements Restorable < AuthCredentials > {
45+ public abstract class AuthCredentials implements Serializable {
4446
45- private static class AppEngineAuthCredentials extends AuthCredentials {
46-
47- 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 {
47+ private static final long serialVersionUID = 236297804453464604L ;
5348
54- private static final long serialVersionUID = 3558563960848658928L ;
55-
56- @ Override
57- public AuthCredentials restore () {
58- return INSTANCE ;
59- }
49+ private static class AppEngineAuthCredentials extends AuthCredentials {
6050
61- @ Override
62- public int hashCode () {
63- return getClass ().getName ().hashCode ();
64- }
51+ private static final long serialVersionUID = 7931300552744202954L ;
6552
66- @ Override
67- public boolean equals (Object obj ) {
68- return obj instanceof AppEngineAuthCredentialsState ;
69- }
70- }
53+ private static final AuthCredentials INSTANCE = new AppEngineAuthCredentials ();
7154
7255 @ Override
7356 protected HttpRequestInitializer httpRequestInitializer (HttpTransport transport ,
7457 Set <String > scopes ) {
7558 return new AppIdentityCredential (scopes );
7659 }
7760
78- @ Override
79- public RestorableState <AuthCredentials > capture () {
80- return STATE ;
61+ private Object readResolve () throws ObjectStreamException {
62+ return INSTANCE ;
8163 }
8264 }
8365
8466 public static class ServiceAccountAuthCredentials extends AuthCredentials {
8567
68+ private static final long serialVersionUID = 8007708734318445901L ;
8669 private final String account ;
8770 private final PrivateKey privateKey ;
8871
8972 private static final AuthCredentials NO_CREDENTIALS = new ServiceAccountAuthCredentials ();
9073
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-
12874 ServiceAccountAuthCredentials (String account , PrivateKey privateKey ) {
12975 this .account = checkNotNull (account );
13076 this .privateKey = checkNotNull (privateKey );
@@ -158,94 +104,59 @@ public PrivateKey privateKey() {
158104 }
159105
160106 @ Override
161- public RestorableState <AuthCredentials > capture () {
162- return new ServiceAccountAuthCredentialsState (account , privateKey );
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 );
163119 }
164120 }
165121
166122 private static class ComputeEngineAuthCredentials extends AuthCredentials {
167123
168- private ComputeCredential computeCredential ;
124+ private static final long serialVersionUID = - 5217355402127260144L ;
169125
170- private static final ComputeEngineAuthCredentialsState STATE =
171- new ComputeEngineAuthCredentialsState ();
172-
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- }
192-
193- @ Override
194- public boolean equals (Object obj ) {
195- return obj instanceof ComputeEngineAuthCredentialsState ;
196- }
197- }
126+ private transient ComputeCredential computeCredential ;
198127
199128 ComputeEngineAuthCredentials () throws IOException , GeneralSecurityException {
200129 computeCredential = getComputeCredential ();
201130 }
202131
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 );
138+ }
139+ }
140+
203141 @ Override
204142 protected HttpRequestInitializer httpRequestInitializer (HttpTransport transport ,
205143 Set <String > scopes ) {
206144 return computeCredential ;
207145 }
208-
209- @ Override
210- public RestorableState <AuthCredentials > capture () {
211- return STATE ;
212- }
213146 }
214147
215148 private static class ApplicationDefaultAuthCredentials extends AuthCredentials {
216149
217- private GoogleCredentials googleCredentials ;
218-
219- private static final ApplicationDefaultAuthCredentialsState STATE =
220- new ApplicationDefaultAuthCredentialsState ();
221-
222- private static class ApplicationDefaultAuthCredentialsState
223- implements RestorableState <AuthCredentials >, Serializable {
224-
225- private static final long serialVersionUID = -8839085552021212257L ;
150+ private static final long serialVersionUID = -8306873864136099893L ;
226151
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- }
152+ private transient GoogleCredentials googleCredentials ;
241153
242- @ Override
243- public boolean equals (Object obj ) {
244- return obj instanceof ApplicationDefaultAuthCredentialsState ;
245- }
154+ ApplicationDefaultAuthCredentials () throws IOException {
155+ googleCredentials = GoogleCredentials .getApplicationDefault ();
246156 }
247157
248- ApplicationDefaultAuthCredentials () throws IOException {
158+ private void readObject (ObjectInputStream in ) throws IOException , ClassNotFoundException {
159+ in .defaultReadObject ();
249160 googleCredentials = GoogleCredentials .getApplicationDefault ();
250161 }
251162
@@ -254,11 +165,6 @@ protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
254165 Set <String > scopes ) {
255166 return new HttpCredentialsAdapter (googleCredentials );
256167 }
257-
258- @ Override
259- public RestorableState <AuthCredentials > capture () {
260- return STATE ;
261- }
262168 }
263169
264170 protected abstract HttpRequestInitializer httpRequestInitializer (HttpTransport transport ,
0 commit comments