@@ -120,28 +120,23 @@ public class ComputeEngineCredentials extends GoogleCredentials
120120 private transient String serviceAccountEmail ;
121121
122122 /**
123- * Constructor with overridden transport.
123+ * Constructor with builder
124124 *
125- * @param transportFactory HTTP transport factory, creates the transport used to get access
126- * tokens.
127- * @param scopes scope strings for the APIs to be called. May be null or an empty collection.
128- * @param defaultScopes default scope strings for the APIs to be called. May be null or an empty
129- * collection. Default scopes are ignored if scopes are provided.
125+ * @param builder builder to use for constructing the credential
130126 */
131- private ComputeEngineCredentials (
132- HttpTransportFactory transportFactory ,
133- Collection <String > scopes ,
134- Collection <String > defaultScopes ) {
135- super (/* accessToken= */ null , COMPUTE_REFRESH_MARGIN , COMPUTE_EXPIRATION_MARGIN );
127+ private ComputeEngineCredentials (Builder builder ) {
128+ super (builder );
136129
137130 this .transportFactory =
138131 firstNonNull (
139- transportFactory ,
132+ builder . getHttpTransportFactory () ,
140133 getFromServiceLoader (HttpTransportFactory .class , OAuth2Utils .HTTP_TRANSPORT_FACTORY ));
141134 this .transportFactoryClassName = this .transportFactory .getClass ().getName ();
135+
136+ Collection <String > scopes = builder .getScopes ();
142137 // Use defaultScopes only when scopes don't exist.
143138 if (scopes == null || scopes .isEmpty ()) {
144- scopes = defaultScopes ;
139+ scopes = builder . getDefaultScopes () ;
145140 }
146141 if (scopes == null ) {
147142 this .scopes = ImmutableSet .<String >of ();
@@ -155,14 +150,14 @@ private ComputeEngineCredentials(
155150 /** Clones the compute engine account with the specified scopes. */
156151 @ Override
157152 public GoogleCredentials createScoped (Collection <String > newScopes ) {
158- return new ComputeEngineCredentials ( this . transportFactory , newScopes , null );
153+ return toBuilder (). setScopes ( newScopes ). build ( );
159154 }
160155
161156 /** Clones the compute engine account with the specified scopes. */
162157 @ Override
163158 public GoogleCredentials createScoped (
164159 Collection <String > newScopes , Collection <String > newDefaultScopes ) {
165- return new ComputeEngineCredentials ( this . transportFactory , newScopes , newDefaultScopes );
160+ return toBuilder (). setScopes ( newScopes ). setDefaultScopes ( newDefaultScopes ). build ( );
166161 }
167162
168163 /**
@@ -171,7 +166,7 @@ public GoogleCredentials createScoped(
171166 * @return new ComputeEngineCredentials
172167 */
173168 public static ComputeEngineCredentials create () {
174- return new ComputeEngineCredentials ( null , null , null );
169+ return newBuilder (). build ( );
175170 }
176171
177172 public final Collection <String > getScopes () {
@@ -541,10 +536,16 @@ private String getDefaultServiceAccount() throws IOException {
541536 public static class Builder extends GoogleCredentials .Builder {
542537 private HttpTransportFactory transportFactory ;
543538 private Collection <String > scopes ;
539+ private Collection <String > defaultScopes ;
544540
545- protected Builder () {}
541+ protected Builder () {
542+ // Override the default refresh and expiration margin set by OAuth2Credentials.Builder
543+ this .refreshMargin = COMPUTE_REFRESH_MARGIN ;
544+ this .expirationMargin = COMPUTE_EXPIRATION_MARGIN ;
545+ }
546546
547547 protected Builder (ComputeEngineCredentials credentials ) {
548+ this ();
548549 this .transportFactory = credentials .transportFactory ;
549550 this .scopes = credentials .scopes ;
550551 }
@@ -559,6 +560,11 @@ public Builder setScopes(Collection<String> scopes) {
559560 return this ;
560561 }
561562
563+ public Builder setDefaultScopes (Collection <String > defaultScopes ) {
564+ this .defaultScopes = defaultScopes ;
565+ return this ;
566+ }
567+
562568 public HttpTransportFactory getHttpTransportFactory () {
563569 return transportFactory ;
564570 }
@@ -567,8 +573,12 @@ public Collection<String> getScopes() {
567573 return scopes ;
568574 }
569575
576+ public Collection <String > getDefaultScopes () {
577+ return defaultScopes ;
578+ }
579+
570580 public ComputeEngineCredentials build () {
571- return new ComputeEngineCredentials (transportFactory , scopes , null );
581+ return new ComputeEngineCredentials (this );
572582 }
573583 }
574584}
0 commit comments