Class AbstractMetadataCache<IdentifierType,MetadataType>
java.lang.Object
net.shibboleth.shared.component.AbstractInitializableComponent
net.shibboleth.shared.component.AbstractIdentifiedInitializableComponent
net.shibboleth.shared.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 base
MetadataCache implementation. 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
FieldsModifier and TypeFieldDescriptionprivate final BackingStore<IdentifierType,MetadataType> Backing store for runtime metadata.private booleanWhether we created our own schedular during object construction.private Function<CriteriaSet,IdentifierType> Map criteria to identifiers to use as keys to the backing store.private ScheduledExecutorServiceA single threaded executor service for running background cache tasks.private Function<MetadataType,IdentifierType> Strategy used to extract an identifier from the given metadata.private final org.slf4j.LoggerClass logger.private StringCached log prefix.private BiConsumer<List<MetadataType>,IdentifierType> A hook that is executed just before a cache entry will been removed/invalidated/evicted.A strategy to filter metadata.private Predicate<MetadataType>Is the metadata valid?private FloatFactor used to compute when the next refresh interval will occur. -
Constructor Summary
ConstructorsConstructorDescriptionPackage private constructor.AbstractMetadataCache(BackingStore<IdentifierType, MetadataType> store, ScheduledExecutorService executor) Package private constructor. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidprotected voidprotected 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> Get the backing store.protected Function<CriteriaSet,IdentifierType> Get the identifier from criteria set extraction strategy.protected ScheduledExecutorServiceGet the background executor service.protected Function<MetadataType,IdentifierType> Get the identifier extraction strategy.protected StringReturn a prefix for logging messages for this component.protected BiFunction<MetadataType,MetadataFilterContext, MetadataType> Get the metadata filtering strategy.protected Predicate<MetadataType>Get the predicate which determines if a piece of metadata is valid or not.protected FloatGet the refresh delay factor.protected booleanhasExpired(MetadataManagementData<IdentifierType> mgmtData) Determine if the metadata has expired based on the expiration time set in the management metadata.protected voidinvalidate(IdentifierType identifier) Remove/discard from the backing store all metadata for the entity with the given identifier.protected voidRemove/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 identifier from the index.protected MetadataFilterContextGet a new instance ofMetadataFilterContextto be used when filtering metadata.voidSet theCriteriaSetto IdentifierType strategy.voidSet the MetadataType to IdentifierType extraction time strategy.voidSet a hook to run before a metadata cache entry is removed from the cache.voidSet 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 booleanDetermine 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.shared.component.AbstractIdentifiableInitializableComponent
setIdMethods inherited from class net.shibboleth.shared.component.AbstractIdentifiedInitializableComponent
ensureId, getId, ifDestroyedThrowDestroyedComponentException, ifInitializedThrowUnmodifiabledComponentException, ifNotInitializedThrowUninitializedComponentExceptionMethods inherited from class net.shibboleth.shared.component.AbstractInitializableComponent
checkComponentActive, checkSetterPreconditions, destroy, initialize, isDestroyed, isInitializedMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface net.shibboleth.shared.component.IdentifiedComponent
getIdMethods inherited from interface net.shibboleth.oidc.metadata.cache.MetadataCache
get
-
Field Details
-
log
@Nonnull private final org.slf4j.Logger logClass logger. -
logPrefix
Cached log prefix. -
refreshDelayFactor
Factor used to compute when the next refresh interval will occur. Default value: 0.75 -
backingStore
Backing store for runtime metadata. -
metadataBeforeRemovalHook
A hook that is executed just before a cache entry will been removed/invalidated/evicted. -
identifierExtractionStrategy
Strategy used to extract an identifier from the given metadata. -
criteriaToIdentifierStrategy
Map criteria to identifiers to use as keys to the backing store. -
metadataFilterStrategy
@NonnullAfterInit private BiFunction<MetadataType,MetadataFilterContext, metadataFilterStrategyMetadataType> A strategy to filter metadata. -
metadataValidPredicate
Is the metadata valid? -
executorService
A single threaded executor service for running background cache tasks. -
createOwnSchedular
private boolean createOwnSchedularWhether we created our own schedular during object construction.
-
-
Constructor Details
-
AbstractMetadataCache
AbstractMetadataCache(@Nonnull BackingStore<IdentifierType, MetadataType> store) Package private constructor. For safe-construction through the factory only.- Parameters:
store- the metadata backing store.
-
AbstractMetadataCache
AbstractMetadataCache(@Nonnull 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 Details
-
getLogPrefix
Return a prefix for logging messages for this component.- Returns:
- a string for insertion at the beginning of any log messages
-
doInitialize
- Overrides:
doInitializein classAbstractIdentifiedInitializableComponent- Throws:
ComponentInitializationException
-
doDestroy
protected void doDestroy()- Overrides:
doDestroyin classAbstractInitializableComponent
-
getExecutorService
Get the background executor service.- Returns:
- the executor service.
-
setMetadataValidPredicate
Set the predicate which determines if a piece of metadata is valid or not.- Parameters:
predicate- the predicate.
-
getMetadataValidPredicate
Get the predicate which determines if a piece of metadata is valid or not.- Returns:
- the predicate.
-
setCriteriaToIdentifierStrategy
Set theCriteriaSetto IdentifierType strategy.- Parameters:
strategy- the strategy.
-
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
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, getMetadataFilterStrategy()MetadataType> 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
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
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 identifier 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
Get the backing store.- Returns:
- the backing store. Can be null.
-
freshLoad
Clear the backing store and load each metadata entry one at a time.- Parameters:
metadataToStore- the metadata to load into the cache.
-
writeToBackingStore
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
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
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
Determine if the metadata has expired based on the expiration time set in the management metadata.- Parameters:
mgmtData- management data- Returns:
- true iff the metadata has expired
-
invalidate
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.
-
errorHandlingWrapper
Create a wrapper for runnables that catches any throwable and logs it. Useful to prevent thread death from an uncaught exception.- Parameters:
action- the runnable to wrap- Returns:
- the wrapped runnable.
-