Class BatchMetadataCache<IdentifierType,MetadataType>
- java.lang.Object
-
- net.shibboleth.utilities.java.support.component.AbstractInitializableComponent
-
- net.shibboleth.utilities.java.support.component.AbstractIdentifiedInitializableComponent
-
- net.shibboleth.utilities.java.support.component.AbstractIdentifiableInitializableComponent
-
- net.shibboleth.oidc.metadata.cache.impl.AbstractMetadataCache<IdentifierType,MetadataType>
-
- net.shibboleth.oidc.metadata.cache.impl.BatchMetadataCache<IdentifierType,MetadataType>
-
- Type Parameters:
IdentifierType- the metadata identifier type.MetadataType- the metadata type.
- All Implemented Interfaces:
MetadataCache<MetadataType>,Component,DestructableComponent,IdentifiableComponent,IdentifiedComponent,InitializableComponent
@ThreadSafe public class BatchMetadataCache<IdentifierType,MetadataType> extends AbstractMetadataCache<IdentifierType,MetadataType>
Ametadata cacheimplementation that supports 'refresh-ahead' semantics for batch cache updates. Does not support 'read-through' semantics if an entry does not exist in the cache.The metadata source could either be an aggregate with more than one metadata entry, or a single source which contains a single metadata entry.
Supports the following:
- Batch reloading of metadata from the source using the supplied loading strategy. The cache is wiped and reloaded during each successful refresh cycle.
The schedule for batch metadata reloads are based on:
- If no expiry is set on the metadata source, or one can not be computed, the max refresh delay is used.
- If metadata expiry exists but is less than the min refresh delay, use the min refresh delay.
- If metadata expiry exists and is greater than the min, use the metadata expiry.
Does not support:
- Manual metadata refresh. Reloads only occur via the scheduled task. Supporting manual refresh would require alterations to remove/cancel existing tasks.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classBatchMetadataCache.AsynchronousRefreshAHeadTaskA runnable that triggers a cache reload.
-
Field Summary
Fields Modifier and Type Field Description private LoadingStrategyloadingStrategyThe function to use to load metadata.private org.slf4j.LoggerlogClass logger.private booleanmatchOnIdentifierRequiredIs a match based on an identifier required? If not, all known metadata will be returned.private DurationmaxRefreshDelayRefresh interval used when metadata does not contain any validUntil or cacheDuration information.private DurationminRefreshDelayFloor, in milliseconds, for the refresh interval.private Function<byte[],List<MetadataType>>parsingStrategyHow to parse the loaded metadata from the loadingStrategy into a usable metadatatype.private ReadWriteLockreadWriteLockA lock to use when reading from and loading the cache.private Function<byte[],Instant>sourceMetadataExpiryStrategyDetermine the expiration time of the source batch loaded metadata.private Predicate<byte[]>sourceMetadataValidPredicateIs the raw metadata bytes from the source valid?
-
Constructor Summary
Constructors Modifier Constructor Description protectedBatchMetadataCache(BatchBackingStore<IdentifierType,MetadataType> store)Constructor.protectedBatchMetadataCache(BatchBackingStore<IdentifierType,MetadataType> store, ScheduledExecutorService executor)Protected constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private DurationcomputeNextRefreshDelay(Instant expectedExpiration)Computes the delay until the next refresh time based on the current metadata's expiration time and the refresh interval floor.private CacheLoadingContextcreateLoadingContext()Create a cache loading context based on the last refresh and last update time of the backing store.protected voiddoInitialize()List<MetadataType>get(CriteriaSet criteria)protected BatchBackingStore<IdentifierType,MetadataType>getBackingStore()Get the backing store.protected LoadingStrategygetLoadingStrategy()Get the loading strategy.protected DurationgetMaxRefreshDelay()Get the maximum amount of time between refresh intervals.protected DurationgetMinRefreshDelay()Get the minimum amount of time between refreshes.protected Function<byte[],List<MetadataType>>getParsingStrategy()Get the parsing strategy.protected Function<byte[],Instant>getSourceMetadataExpiryStrategy()Get the source metadata expiry strategy.protected Predicate<byte[]>getSourceMetadataValidPredicate()Get the predicate which determines if the source metadata is valid or not.protected booleanisMatchOnIdentifierRequired()Is a match on identifier required?private voidloadCache()Reload the entire backing-store cache using the loading strategy.private voidscheduleNextRefresh(Duration delay)Schedules the next refresh.voidsetLoadingStrategy(LoadingStrategy strategy)Set the strategy used to batch load metadata from a source.voidsetMatchOnIdentifierRequired(boolean required)Set if a match on identifier is required in order to return results.voidsetMaxRefreshDelay(Duration delay)Sets the maximum amount of time between refresh intervals.voidsetMinRefreshDelay(Duration delay)Sets the minimum amount of time between refreshes.voidsetParsingStrategy(Function<byte[],List<MetadataType>> strategy)Set the strategy used to convert raw metadata in bytes to the given metadata type.voidsetSourceMetadataExpiryStrategy(Function<byte[],Instant> strategy)Set a strategy to find the metadata expiry date from a source metadata document as bytes.voidsetSourceMetadataValidPredicate(Predicate<byte[]> predicate)Set the predicate which determines if the source metadata is valid or not.-
Methods inherited from class net.shibboleth.oidc.metadata.cache.impl.AbstractMetadataCache
doDestroy, errorHandlingWrapper, freshLoad, getCriteriaToIdentifierStrategy, getExecutorService, getIdentifierExtractionStrategy, getLogPrefix, getMetadataFilterStrategy, getMetadataValidPredicate, getRefreshDelayFactor, hasExpired, invalidate, invalidateAll, lookupIdentifier, lookupIndexedIdentifier, newFilterContext, setCriteriaToIdentifierStrategy, setIdentifierExtractionStrategy, setMetadataBeforeRemovalHook, setMetadataFilterStrategy, setMetadataValidPredicate, setRefreshDelayFactor, shouldAttemptRefresh, writeToBackingStore
-
Methods inherited from class net.shibboleth.utilities.java.support.component.AbstractIdentifiableInitializableComponent
setId
-
Methods inherited from class net.shibboleth.utilities.java.support.component.AbstractIdentifiedInitializableComponent
getId
-
Methods inherited from class net.shibboleth.utilities.java.support.component.AbstractInitializableComponent
destroy, initialize, isDestroyed, isInitialized
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface net.shibboleth.utilities.java.support.component.IdentifiedComponent
getId
-
-
-
-
Field Detail
-
log
private final org.slf4j.Logger log
Class logger.
-
maxRefreshDelay
@NonnullAfterInit @Positive private Duration maxRefreshDelay
Refresh interval used when metadata does not contain any validUntil or cacheDuration information.
-
minRefreshDelay
@NonnullAfterInit @Positive private Duration minRefreshDelay
Floor, in milliseconds, for the refresh interval.
-
loadingStrategy
@NonnullAfterInit private LoadingStrategy loadingStrategy
The function to use to load metadata.
-
parsingStrategy
@NonnullAfterInit private Function<byte[],List<MetadataType>> parsingStrategy
How to parse the loaded metadata from the loadingStrategy into a usable metadatatype.
-
sourceMetadataExpiryStrategy
@NonnullAfterInit private Function<byte[],Instant> sourceMetadataExpiryStrategy
Determine the expiration time of the source batch loaded metadata.
-
sourceMetadataValidPredicate
@NonnullAfterInit private Predicate<byte[]> sourceMetadataValidPredicate
Is the raw metadata bytes from the source valid?
-
matchOnIdentifierRequired
@Nonnull private boolean matchOnIdentifierRequired
Is a match based on an identifier required? If not, all known metadata will be returned. Defaults to true - a match on identifier is required.
-
readWriteLock
@Nonnull private final ReadWriteLock readWriteLock
A lock to use when reading from and loading the cache.
-
-
Constructor Detail
-
BatchMetadataCache
protected BatchMetadataCache(@Nonnull BatchBackingStore<IdentifierType,MetadataType> store)Constructor.- Parameters:
store- the backing store.
-
BatchMetadataCache
protected BatchMetadataCache(@Nonnull BatchBackingStore<IdentifierType,MetadataType> store, @Nullable ScheduledExecutorService executor)Protected constructor.- Parameters:
store- the backing store.executor- the scheduled executor
-
-
Method Detail
-
doInitialize
protected void doInitialize() throws ComponentInitializationException- Overrides:
doInitializein classAbstractMetadataCache<IdentifierType,MetadataType>- Throws:
ComponentInitializationException
-
setSourceMetadataValidPredicate
public void setSourceMetadataValidPredicate(@Nonnull Predicate<byte[]> predicate)Set the predicate which determines if the source metadata is valid or not.- Parameters:
predicate- the predicate.
-
getSourceMetadataValidPredicate
@NonnullAfterInit protected Predicate<byte[]> getSourceMetadataValidPredicate()
Get the predicate which determines if the source metadata is valid or not.- Returns:
- the predicate.
-
setLoadingStrategy
public void setLoadingStrategy(@Nonnull LoadingStrategy strategy)Set the strategy used to batch load metadata from a source.- Parameters:
strategy- the loading strategy.
-
getLoadingStrategy
@NonnullAfterInit protected LoadingStrategy getLoadingStrategy()
Get the loading strategy.- Returns:
- the loading strategy
-
setParsingStrategy
public void setParsingStrategy(@Nonnull Function<byte[],List<MetadataType>> strategy)Set the strategy used to convert raw metadata in bytes to the given metadata type.- Parameters:
strategy- the parsing strategy.
-
getParsingStrategy
@NonnullAfterInit protected Function<byte[],List<MetadataType>> getParsingStrategy()
Get the parsing strategy.- Returns:
- the parsing strategy
-
setSourceMetadataExpiryStrategy
public void setSourceMetadataExpiryStrategy(@Nonnull Function<byte[],Instant> strategy)Set a strategy to find the metadata expiry date from a source metadata document as bytes.- Parameters:
strategy- the strategy.
-
getSourceMetadataExpiryStrategy
@NonnullAfterInit protected Function<byte[],Instant> getSourceMetadataExpiryStrategy()
Get the source metadata expiry strategy.- Returns:
- the source metadata expiry strategy
-
setMatchOnIdentifierRequired
public void setMatchOnIdentifierRequired(boolean required)
Set if a match on identifier is required in order to return results. If false and there are no identifiers in the criteria to match on, all cached entries will be returned.- Parameters:
required- does the metadata lookup need to match the given criteria.
-
isMatchOnIdentifierRequired
protected boolean isMatchOnIdentifierRequired()
Is a match on identifier required?- Returns:
- true if it is, false otherwise
-
getBackingStore
@Nonnull protected BatchBackingStore<IdentifierType,MetadataType> getBackingStore()
Get the backing store.Cast the backing store to the type used by this cache type.
- Overrides:
getBackingStorein classAbstractMetadataCache<IdentifierType,MetadataType>- Returns:
- the backing store. Can be null.
-
setMinRefreshDelay
public void setMinRefreshDelay(@Positive @Nonnull Duration delay)
Sets the minimum amount of time between refreshes.- Parameters:
delay- minimum amount of time between refreshes
-
getMinRefreshDelay
@NonnullAfterInit protected Duration getMinRefreshDelay()
Get the minimum amount of time between refreshes.- Returns:
- the minimum refresh delay
-
setMaxRefreshDelay
public void setMaxRefreshDelay(@Positive @Nonnull Duration delay)
Sets the maximum amount of time between refresh intervals.- Parameters:
delay- maximum amount of time, in milliseconds, between refresh intervals
-
getMaxRefreshDelay
@NonnullAfterInit protected Duration getMaxRefreshDelay()
Get the maximum amount of time between refresh intervals.- Returns:
- the delay maximum amount of time, in milliseconds, between refresh intervals
-
get
@Nonnull @NonnullElements public List<MetadataType> get(@Nonnull CriteriaSet criteria) throws MetadataCacheException
Acquires a read lock to prevent reading while the cache is loading.
- Throws:
MetadataCacheException
-
createLoadingContext
private CacheLoadingContext createLoadingContext()
Create a cache loading context based on the last refresh and last update time of the backing store.- Returns:
- the cache loading context
-
loadCache
private void loadCache() throws MetadataCacheExceptionReload the entire backing-store cache using the loading strategy.Acquires a write lock to prevent loading while the cache is being read from.
- Throws:
MetadataCacheException- on loading error.
-
scheduleNextRefresh
private void scheduleNextRefresh(@Nullable Duration delay)Schedules the next refresh. If the given delay is 0 or null, thenmaxRefreshDelayis used.- Parameters:
delay- The delay before the next refresh.
-
computeNextRefreshDelay
@Nonnull private Duration computeNextRefreshDelay(Instant expectedExpiration)
Computes the delay until the next refresh time based on the current metadata's expiration time and the refresh interval floor.- Parameters:
expectedExpiration- the time when the metadata is expected to expire and needs refreshing- Returns:
- delay until the next refresh time
-
-