1919import org .slf4j .LoggerFactory ;
2020
2121import java .time .Duration ;
22- import java .util .Date ;
2322import java .util .Iterator ;
2423import java .util .Map ;
2524import java .util .UUID ;
3534 * <p>
3635 * This implementation keeps track of entities (typically AMQP connections) that need
3736 * to renew credentials. Token renewal is scheduled based on token expiration, using
38- * a <code>Function<Date , Long> refreshDelayStrategy</code>. Once credentials
37+ * a <code>Function<Duration , Long> refreshDelayStrategy</code>. Once credentials
3938 * for a {@link CredentialsProvider} have been renewed, the callback registered
4039 * by each entity/connection is performed. This callback typically propagates
4140 * the new credentials in the entity state, e.g. sending the new password to the
@@ -51,11 +50,11 @@ public class DefaultCredentialsRefreshService implements CredentialsRefreshServi
5150
5251 private final boolean privateScheduler ;
5352
54- private final Function <Date , Long > refreshDelayStrategy ;
53+ private final Function <Duration , Long > refreshDelayStrategy ;
5554
56- private final Function <Date , Boolean > needRefreshStrategy ;
55+ private final Function <Duration , Boolean > needRefreshStrategy ;
5756
58- public DefaultCredentialsRefreshService (ScheduledExecutorService scheduler , Function <Date , Long > refreshDelayStrategy , Function <Date , Boolean > needRefreshStrategy ) {
57+ public DefaultCredentialsRefreshService (ScheduledExecutorService scheduler , Function <Duration , Long > refreshDelayStrategy , Function <Duration , Boolean > needRefreshStrategy ) {
5958 this .refreshDelayStrategy = refreshDelayStrategy ;
6059 this .needRefreshStrategy = needRefreshStrategy ;
6160 if (scheduler == null ) {
@@ -76,7 +75,7 @@ public DefaultCredentialsRefreshService(ScheduledExecutorService scheduler, Func
7675 * @param duration
7776 * @return
7877 */
79- public static Function <Date , Long > fixedDelayBeforeExpirationRefreshDelayStrategy (Duration duration ) {
78+ public static Function <Duration , Long > fixedDelayBeforeExpirationRefreshDelayStrategy (Duration duration ) {
8079 return new FixedDelayBeforeExpirationRefreshDelayStrategy (duration .toMillis ());
8180 }
8281
@@ -86,20 +85,21 @@ public static Function<Date, Long> fixedDelayBeforeExpirationRefreshDelayStrateg
8685 * @param limitBeforeExpiration
8786 * @return
8887 */
89- public static Function <Date , Boolean > fixedTimeNeedRefreshStrategy (Duration limitBeforeExpiration ) {
88+ public static Function <Duration , Boolean > fixedTimeNeedRefreshStrategy (Duration limitBeforeExpiration ) {
9089 return new FixedTimeNeedRefreshStrategy (limitBeforeExpiration .toMillis ());
9190 }
9291
9392 // TODO add a delay refresh strategy that bases the time on a percentage of the TTL, use it as default with 80% TTL
9493
9594 private static Runnable refresh (ScheduledExecutorService scheduler , CredentialsProviderState credentialsProviderState ,
96- Function <Date , Long > refreshDelayStrategy ) {
95+ Function <Duration , Long > refreshDelayStrategy ) {
9796 return () -> {
9897 LOGGER .debug ("Refreshing token" );
9998 credentialsProviderState .refresh ();
10099
101- Date expirationAfterRefresh = credentialsProviderState .credentialsProvider .getExpiration ();
102- long newDelay = refreshDelayStrategy .apply (expirationAfterRefresh );
100+ Duration timeBeforeExpiration = credentialsProviderState .credentialsProvider .getTimeBeforeExpiration ();
101+
102+ long newDelay = refreshDelayStrategy .apply (timeBeforeExpiration );
103103
104104 LOGGER .debug ("Scheduling refresh in {} milliseconds" , newDelay );
105105
@@ -122,8 +122,7 @@ public String register(CredentialsProvider credentialsProvider, Callable<Boolean
122122 credentialsProviderState .add (registration );
123123
124124 credentialsProviderState .maybeSetRefreshTask (() -> {
125- Date expiration = credentialsProvider .getExpiration ();
126- long delay = refreshDelayStrategy .apply (expiration );
125+ long delay = refreshDelayStrategy .apply (credentialsProvider .getTimeBeforeExpiration ());
127126 LOGGER .debug ("Scheduling refresh in {} milliseconds" , delay );
128127 return scheduler .schedule (refresh (scheduler , credentialsProviderState , refreshDelayStrategy ), delay , TimeUnit .MILLISECONDS );
129128 });
@@ -140,8 +139,8 @@ public void unregister(CredentialsProvider credentialsProvider, String registrat
140139 }
141140
142141 @ Override
143- public boolean needRefresh (Date expiration ) {
144- return this .needRefreshStrategy .apply (expiration );
142+ public boolean needRefresh (Duration timeBeforeExpiration ) {
143+ return this .needRefreshStrategy .apply (timeBeforeExpiration );
145144 }
146145
147146 public void close () {
@@ -150,7 +149,7 @@ public void close() {
150149 }
151150 }
152151
153- private static class FixedTimeNeedRefreshStrategy implements Function <Date , Boolean > {
152+ private static class FixedTimeNeedRefreshStrategy implements Function <Duration , Boolean > {
154153
155154 private final long limitBeforeExpiration ;
156155
@@ -159,13 +158,12 @@ private FixedTimeNeedRefreshStrategy(long limitBeforeExpiration) {
159158 }
160159
161160 @ Override
162- public Boolean apply (Date expiration ) {
163- long ttl = expiration .getTime () - new Date ().getTime ();
164- return ttl <= limitBeforeExpiration ;
161+ public Boolean apply (Duration timeBeforeExpiration ) {
162+ return timeBeforeExpiration .toMillis () <= limitBeforeExpiration ;
165163 }
166164 }
167165
168- private static class FixedDelayBeforeExpirationRefreshDelayStrategy implements Function <Date , Long > {
166+ private static class FixedDelayBeforeExpirationRefreshDelayStrategy implements Function <Duration , Long > {
169167
170168 private final long delay ;
171169
@@ -174,11 +172,10 @@ private FixedDelayBeforeExpirationRefreshDelayStrategy(long delay) {
174172 }
175173
176174 @ Override
177- public Long apply (Date expiration ) {
178- long ttl = expiration .getTime () - new Date ().getTime ();
179- long refreshTimeBeforeExpiration = ttl - delay ;
175+ public Long apply (Duration timeBeforeExpiration ) {
176+ long refreshTimeBeforeExpiration = timeBeforeExpiration .toMillis () - delay ;
180177 if (refreshTimeBeforeExpiration < 0 ) {
181- return ttl ;
178+ return timeBeforeExpiration . toMillis () ;
182179 } else {
183180 return refreshTimeBeforeExpiration ;
184181 }
@@ -279,21 +276,21 @@ public static class DefaultCredentialsRefreshServiceBuilder {
279276
280277 private ScheduledExecutorService scheduler ;
281278
282- private Function <Date , Long > refreshDelayStrategy = fixedDelayBeforeExpirationRefreshDelayStrategy (Duration .ofSeconds (60 ));
279+ private Function <Duration , Long > refreshDelayStrategy = fixedDelayBeforeExpirationRefreshDelayStrategy (Duration .ofSeconds (60 ));
283280
284- private Function <Date , Boolean > needRefreshStrategy = fixedTimeNeedRefreshStrategy (Duration .ofSeconds (60 ));
281+ private Function <Duration , Boolean > needRefreshStrategy = fixedTimeNeedRefreshStrategy (Duration .ofSeconds (60 ));
285282
286283 public DefaultCredentialsRefreshServiceBuilder scheduler (ScheduledThreadPoolExecutor scheduler ) {
287284 this .scheduler = scheduler ;
288285 return this ;
289286 }
290287
291- public DefaultCredentialsRefreshServiceBuilder refreshDelayStrategy (Function <Date , Long > refreshDelayStrategy ) {
288+ public DefaultCredentialsRefreshServiceBuilder refreshDelayStrategy (Function <Duration , Long > refreshDelayStrategy ) {
292289 this .refreshDelayStrategy = refreshDelayStrategy ;
293290 return this ;
294291 }
295292
296- public DefaultCredentialsRefreshServiceBuilder needRefreshStrategy (Function <Date , Boolean > needRefreshStrategy ) {
293+ public DefaultCredentialsRefreshServiceBuilder needRefreshStrategy (Function <Duration , Boolean > needRefreshStrategy ) {
297294 this .needRefreshStrategy = needRefreshStrategy ;
298295 return this ;
299296 }
0 commit comments