2121import com .google .appengine .api .appidentity .AppIdentityService ;
2222import com .google .appengine .api .appidentity .AppIdentityServiceFactory ;
2323import com .google .appengine .api .appidentity .AppIdentityServiceFailureException ;
24- import com .google .common .base .Preconditions ;
2524import com .google .common .collect .ImmutableList ;
2625
2726import java .io .IOException ;
@@ -63,24 +62,42 @@ public class AppIdentityCredential implements HttpRequestInitializer, HttpExecut
6362 * @param scopes OAuth scopes
6463 */
6564 public AppIdentityCredential (Iterable <String > scopes ) {
66- this (AppIdentityServiceFactory . getAppIdentityService (), ImmutableList . copyOf (scopes ));
65+ this (new Builder (scopes ));
6766 }
6867
6968 /**
7069 * @param scopes OAuth scopes
7170 */
7271 public AppIdentityCredential (String ... scopes ) {
73- this (AppIdentityServiceFactory .getAppIdentityService (), ImmutableList .copyOf (scopes ));
72+ this (new Builder (scopes ));
73+ }
74+
75+ /**
76+ * @param builder builder
77+ *
78+ * @since 1.14
79+ */
80+ protected AppIdentityCredential (Builder builder ) {
81+ // Lazily retrieved rather than setting as the default value in order to not add runtime
82+ // dependencies on AppIdentityServiceFactory unless it is actually being used.
83+ appIdentityService = builder .appIdentityService == null
84+ ? AppIdentityServiceFactory .getAppIdentityService () : builder .appIdentityService ;
85+ scopes = ImmutableList .copyOf (builder .scopes );
7486 }
7587
7688 /**
7789 * @param appIdentityService App Identity Service that provides the access token
7890 * @param scopes OAuth scopes
7991 *
8092 * @since 1.12
93+ * @deprecated (scheduled to be removed in 1.15) Use {@link #AppIdentityCredential(Builder)}
8194 */
95+ @ Deprecated
8296 protected AppIdentityCredential (AppIdentityService appIdentityService , List <String > scopes ) {
83- this .appIdentityService = Preconditions .checkNotNull (appIdentityService );
97+ // Lazily retrieved rather than setting as the default value in order to not add runtime
98+ // dependencies on AppIdentityServiceFactory unless it is actually being used.
99+ this .appIdentityService = appIdentityService == null
100+ ? AppIdentityServiceFactory .getAppIdentityService () : appIdentityService ;
84101 this .scopes = ImmutableList .copyOf (scopes );
85102 }
86103
@@ -133,11 +150,14 @@ public final List<String> getScopes() {
133150 */
134151 public static class Builder {
135152
136- /** App Identity Service that provides the access token. */
137- private AppIdentityService appIdentityService ;
153+ /**
154+ * App Identity Service that provides the access token or {@code null} to use
155+ * {@link AppIdentityServiceFactory#getAppIdentityService()}.
156+ */
157+ AppIdentityService appIdentityService ;
138158
139159 /** OAuth scopes. */
140- private final ImmutableList <String > scopes ;
160+ final ImmutableList <String > scopes ;
141161
142162 /**
143163 * Returns an instance of a new builder.
@@ -158,33 +178,34 @@ public Builder(String... scopes) {
158178 }
159179
160180 /**
161- * Sets the App Identity Service that provides the access token.
162- * <p>
163- * If not explicitly set, the {@link AppIdentityServiceFactory#getAppIdentityService()} method
164- * will be used to provide the App Identity Service.
165- * </p>
181+ * Returns the App Identity Service that provides the access token or {@code null} to use
182+ * {@link AppIdentityServiceFactory#getAppIdentityService()}.
183+ *
184+ * @since 1.14
185+ */
186+ public final AppIdentityService getAppIdentityService () {
187+ return appIdentityService ;
188+ }
189+
190+ /**
191+ * Sets the App Identity Service that provides the access token or {@code null} to use
192+ * {@link AppIdentityServiceFactory#getAppIdentityService()}.
166193 *
167194 * <p>
168195 * Overriding is only supported for the purpose of calling the super implementation and changing
169196 * the return type, but nothing else.
170197 * </p>
171198 */
172199 public Builder setAppIdentityService (AppIdentityService appIdentityService ) {
173- this .appIdentityService = Preconditions . checkNotNull ( appIdentityService ) ;
200+ this .appIdentityService = appIdentityService ;
174201 return this ;
175202 }
176203
177204 /**
178205 * Returns a new {@link AppIdentityCredential}.
179206 */
180207 public AppIdentityCredential build () {
181- AppIdentityService appIdentityService = this .appIdentityService ;
182- if (appIdentityService == null ) {
183- // Lazily retrieved rather than setting as the default value in order to not add runtime
184- // dependencies on AppIdentityServiceFactory unless it is actually being used.
185- appIdentityService = AppIdentityServiceFactory .getAppIdentityService ();
186- }
187- return new AppIdentityCredential (appIdentityService , scopes );
208+ return new AppIdentityCredential (this );
188209 }
189210 }
190211}
0 commit comments