Class AbstractMetadataCache<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>
-
- Type Parameters:
IdentifierType- the metadata identifier typeMetadataType- the metadata type
- All Implemented Interfaces:
MetadataCache<MetadataType>,Component,DestructableComponent,IdentifiableComponent,IdentifiedComponent,InitializableComponent
- Direct Known Subclasses:
BatchMetadataCache,DynamicMetadataCache,FetchThroughMetadataCache
public abstract class AbstractMetadataCache<IdentifierType,MetadataType> extends AbstractIdentifiableInitializableComponent implements MetadataCache<MetadataType>
A baseMetadataCacheimplementation. Supports the following:- A configurable backing store to store metadata.
- Individual entity metadata filtering during cache save.
- Configuration of type specific functions via strategies.
-
-
Field Summary
Fields Modifier and Type Field Description private BackingStore<IdentifierType,MetadataType>backingStoreBacking store for runtime metadata.private booleancreateOwnSchedularWhether we created our own schedular during object construction.private Function<CriteriaSet,IdentifierType>criteriaToIdentifierStrategyMap criteria to identifiers to use as keys to the backing store.private ScheduledExecutorServiceexecutorServiceA single threaded executor service for running background cache tasks.private Function<MetadataType,IdentifierType>identifierExtractionStrategyStrategy used to extract an identifier from the given metadata.private org.slf4j.LoggerlogClass logger.private StringlogPrefixCached log prefix.private BiConsumer<List<MetadataType>,IdentifierType>metadataBeforeRemovalHookA hook that is executed just before a cache entry will been removed/invalidated/evicted.private BiFunction<MetadataType,MetadataFilterContext,MetadataType>metadataFilterStrategyA strategy to filter metadata.private Predicate<MetadataType>metadataValidPredicateIs the metadata valid?private FloatrefreshDelayFactorFactor used to compute when the next refresh interval will occur.
-
Constructor Summary
Constructors Constructor Description AbstractMetadataCache(BackingStore<IdentifierType,MetadataType> store)Package private constructor.AbstractMetadataCache(BackingStore<IdentifierType,MetadataType> store, ScheduledExecutorService executor)Package private constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voiddoDestroy()protected voiddoInitialize()protected RunnableerrorHandlingWrapper(Runnable action)Create a wrapper for runnables that catches any throwable and logs it.protected voidfreshLoad(List<MetadataType> metadataToStore)Clear the backing store and load each metadata entry one at a time.protected BackingStore<IdentifierType,MetadataType>getBackingStore()Get the backing store.protected Function<CriteriaSet,IdentifierType>getCriteriaToIdentifierStrategy()Get the identifier from criteria set extraction strategy.protected ScheduledExecutorServicegetExecutorService()Get the background executor service.protected Function<MetadataType,IdentifierType>getIdentifierExtractionStrategy()Get the identifier extraction strategy.protected StringgetLogPrefix()Return a prefix for logging messages for this component.protected BiFunction<MetadataType,MetadataFilterContext,MetadataType>getMetadataFilterStrategy()Get the metadata filtering strategy.protected Predicate<MetadataType>getMetadataValidPredicate()Get the predicate which determines if a piece of metadata is valid or not.protected FloatgetRefreshDelayFactor()Get the refresh delay factor.protected booleanhasExpired(MetadataManagementData<IdentifierType> mgmtData)Determine if the metadata has expired based on the expiration time set in the managment metadata.protected voidinvalidate(IdentifierType identifier)Remove/discard from the backing store all metadata for the entity with the given identifier.protected voidinvalidateAll()Remove/discard all metadata for the backing store.protected List<MetadataType>lookupIdentifier(IdentifierType identifier)Get list of descriptors matching the identifier.protected List<MetadataType>lookupIndexedIdentifier(IdentifierType identifier)Lookup the specified entityID from the index.protected MetadataFilterContextnewFilterContext()Get a new instance ofMetadataFilterContextto be used when filtering metadata.voidsetCriteriaToIdentifierStrategy(Function<CriteriaSet,IdentifierType> strategy)Set theCriteriaSetto IdentifierType strategy.voidsetIdentifierExtractionStrategy(Function<MetadataType,IdentifierType> strategy)Set the MetadataType to IdentifierType extraction time strategy.voidsetMetadataBeforeRemovalHook(BiConsumer<List<MetadataType>,IdentifierType> hook)Set a hook to run before a metadata cache entry is removed from the cache.voidsetMetadataFilterStrategy(BiFunction<MetadataType,MetadataFilterContext,MetadataType> strategy)Set the metadata filtering strategy.voidsetMetadataValidPredicate(Predicate<MetadataType> predicate)Set the predicate which determines if a piece of metadata is valid or not.voidsetRefreshDelayFactor(Float factor)Sets the delay factor used to compute the next refresh time.protected booleanshouldAttemptRefresh(MetadataManagementData<IdentifierType> mgmtData)Determine if the metadata should be refreshed based on stored refresh trigger time.protected voidwriteToBackingStore(MetadataType metadata)Write the given metadata to the backing store index.-
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
-
Methods inherited from interface net.shibboleth.oidc.metadata.cache.MetadataCache
get
-
-
-
-
Field Detail
-
log
private final org.slf4j.Logger log
Class logger.
-
logPrefix
@Nullable private String logPrefix
Cached log prefix.
-
refreshDelayFactor
@NonnullAfterInit @Positive private Float refreshDelayFactor
Factor used to compute when the next refresh interval will occur. Default value: 0.75
-
backingStore
@Nullable private final BackingStore<IdentifierType,MetadataType> backingStore
Backing store for runtime metadata.
-
metadataBeforeRemovalHook
@Nullable private BiConsumer<List<MetadataType>,IdentifierType> metadataBeforeRemovalHook
A hook that is executed just before a cache entry will been removed/invalidated/evicted.
-
identifierExtractionStrategy
@NonnullAfterInit private Function<MetadataType,IdentifierType> identifierExtractionStrategy
Strategy used to extract an identifier from the given metadata.
-
criteriaToIdentifierStrategy
@NonnullAfterInit private Function<CriteriaSet,IdentifierType> criteriaToIdentifierStrategy
Map criteria to identifiers to use as keys to the backing store.
-
metadataFilterStrategy
@NonnullAfterInit private BiFunction<MetadataType,MetadataFilterContext,MetadataType> metadataFilterStrategy
A strategy to filter metadata.
-
metadataValidPredicate
@NonnullAfterInit private Predicate<MetadataType> metadataValidPredicate
Is the metadata valid?
-
executorService
@NonnullAfterInit private ScheduledExecutorService executorService
A single threaded executor service for running background cache tasks.
-
createOwnSchedular
private boolean createOwnSchedular
Whether we created our own schedular during object construction.
-
-
Constructor Detail
-
AbstractMetadataCache
AbstractMetadataCache(@Nullable BackingStore<IdentifierType,MetadataType> store)Package private constructor. For safe-construction through the factory only.- Parameters:
store- the metadata backing store.
-
AbstractMetadataCache
AbstractMetadataCache(@Nullable BackingStore<IdentifierType,MetadataType> store, @Nullable ScheduledExecutorService executor)Package private constructor. For safe-construction through the factory only.Accepts an executor. Mostly used for testing.
- Parameters:
store- the backing store.executor- the scheduled executor
-
-
Method Detail
-
getLogPrefix
@Nonnull @NotEmpty protected String getLogPrefix()
Return a prefix for logging messages for this component.- Returns:
- a string for insertion at the beginning of any log messages
-
doInitialize
protected void doInitialize() throws ComponentInitializationException- Overrides:
doInitializein classAbstractIdentifiedInitializableComponent- Throws:
ComponentInitializationException
-
doDestroy
protected void doDestroy()
- Overrides:
doDestroyin classAbstractInitializableComponent
-
getExecutorService
@NonnullAfterInit protected ScheduledExecutorService getExecutorService()
Get the background executor service.- Returns:
- the executor service.
-
setMetadataValidPredicate
public void setMetadataValidPredicate(@Nonnull Predicate<MetadataType> predicate)Set the predicate which determines if a piece of metadata is valid or not.- Parameters:
predicate- the predicate.
-
getMetadataValidPredicate
@Nonnull protected Predicate<MetadataType> getMetadataValidPredicate()
Get the predicate which determines if a piece of metadata is valid or not.- Returns:
- the predicate.
-
setCriteriaToIdentifierStrategy
public void setCriteriaToIdentifierStrategy(@Nonnull Function<CriteriaSet,IdentifierType> strategy)Set theCriteriaSetto IdentifierType strategy.- Parameters:
strategy- the strategy.
-
getCriteriaToIdentifierStrategy
@NonnullAfterInit protected Function<CriteriaSet,IdentifierType> getCriteriaToIdentifierStrategy()
Get the identifier from criteria set extraction strategy.- Returns:
- the identifier extraction strategy.
-
setIdentifierExtractionStrategy
public void setIdentifierExtractionStrategy(@Nonnull Function<MetadataType,IdentifierType> strategy)Set the MetadataType to IdentifierType extraction time strategy.- Parameters:
strategy- the strategy.
-
getIdentifierExtractionStrategy
@NonnullAfterInit protected Function<MetadataType,IdentifierType> getIdentifierExtractionStrategy()
Get the identifier extraction strategy.- Returns:
- the identifier extraction strategy.
-
setMetadataFilterStrategy
public void setMetadataFilterStrategy(@Nonnull BiFunction<MetadataType,MetadataFilterContext,MetadataType> strategy)Set the metadata filtering strategy.- Parameters:
strategy- the metadata filtering strategy.
-
getMetadataFilterStrategy
@NonnullAfterInit protected BiFunction<MetadataType,MetadataFilterContext,MetadataType> getMetadataFilterStrategy()
Get the metadata filtering strategy.- Returns:
- the filtering strategy.
-
setMetadataBeforeRemovalHook
public void setMetadataBeforeRemovalHook(@Nullable BiConsumer<List<MetadataType>,IdentifierType> hook)Set a hook to run before a metadata cache entry is removed from the cache.The hook is required to gracefully handle null metadata lists.
- Parameters:
hook- the hook to run.
-
setRefreshDelayFactor
public void setRefreshDelayFactor(@Nonnull Float factor)Sets the delay factor used to compute the next refresh time. The delay must be between 0.0 and 1.0, exclusive.Defaults to: 0.75.
- Parameters:
factor- delay factor used to compute the next refresh time
-
getRefreshDelayFactor
@NonnullAfterInit protected Float getRefreshDelayFactor()
Get the refresh delay factor.- Returns:
- the refresh delay factor.
-
lookupIdentifier
@Nonnull @NonnullElements protected List<MetadataType> lookupIdentifier(@Nonnull @NotEmpty IdentifierType identifier) throws MetadataCacheException
Get list of descriptors matching the identifier.- Parameters:
identifier- the identifier to lookup- Returns:
- a list of metadata
- Throws:
MetadataCacheException- if an error occurs.
-
lookupIndexedIdentifier
@Nonnull @NonnullElements protected List<MetadataType> lookupIndexedIdentifier(@Nonnull IdentifierType identifier)
Lookup the specified entityID from the index. The returned list will be a copy of what is stored in the backing index, and is safe to be manipulated by callers.- Parameters:
identifier- the identifier to lookup- Returns:
- list copy of indexed metadata, may be empty, will never be null
-
getBackingStore
@Nullable protected BackingStore<IdentifierType,MetadataType> getBackingStore()
Get the backing store.- Returns:
- the backing store. Can be null.
-
freshLoad
protected void freshLoad(@Nonnull List<MetadataType> metadataToStore)Clear the backing store and load each metadata entry one at a time.- Parameters:
metadataToStore- the metadata to load into the cache.
-
writeToBackingStore
protected void writeToBackingStore(@Nonnull MetadataType metadata)Write the given metadata to the backing store index.Should happen within an appropriate lock to ensure thread-safety.
- Parameters:
metadata- the metadata to add to the backing store.
-
newFilterContext
@Nonnull protected MetadataFilterContext newFilterContext()
Get a new instance ofMetadataFilterContextto be used when filtering metadata.This default implementation just returns an empty context with as metadatasource for passing to the filter strategy. The strategy can then add to the context as appropriate.
- Returns:
- the new filter context instance
-
shouldAttemptRefresh
protected boolean shouldAttemptRefresh(@Nonnull MetadataManagementData<IdentifierType> mgmtData)Determine if the metadata should be refreshed based on stored refresh trigger time.- Parameters:
mgmtData- the entity'd management data- Returns:
- true if should attempt refresh, false otherwise
-
hasExpired
protected boolean hasExpired(@Nonnull MetadataManagementData<IdentifierType> mgmtData)Determine if the metadata has expired based on the expiration time set in the managment metadata.- Parameters:
mgmtData- management data- Returns:
- true iff the metadata has expired
-
invalidate
protected void invalidate(@Nonnull IdentifierType identifier)Remove/discard from the backing store all metadata for the entity with the given identifier.- Parameters:
identifier- the ID of the metadata to remove
-
invalidateAll
protected void invalidateAll()
Remove/discard all metadata for the backing store.Ensure thread-safety is observed in the calling method.
TODO check lock.
-
-