Package org.opensaml.storage.impl
Class JPAStorageService
-
- All Implemented Interfaces:
Component,DestructableComponent,IdentifiableComponent,IdentifiedComponent,InitializableComponent,StorageCapabilities,StorageCapabilitiesEx,StorageService
public class JPAStorageService extends AbstractStorageService implements StorageCapabilitiesEx
Implementation ofStorageServicethat uses JPA to persist to a database.
-
-
Field Summary
Fields Modifier and Type Field Description private javax.persistence.EntityManagerFactoryentityManagerFactoryEntity manager factory.private org.slf4j.LoggerlogClass logger.private inttransactionRetryNumber of times to retry a transaction if it rolls back.
-
Constructor Summary
Constructors Constructor Description JPAStorageService(javax.persistence.EntityManagerFactory factory)Creates a new JPA storage service.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidcloseEntityManager(javax.persistence.EntityManager manager)Closes the supplied entity manager ifEntityManager.isOpen().private voidcommitTransaction(javax.persistence.EntityTransaction transaction)Commits the supplied transaction ifEntityTransaction.isActive()and notEntityTransaction.getRollbackOnly().booleancreate(String context, String key, String value, Long expiration)booleandelete(String context, String key)voiddeleteContext(String context)protected voiddeleteContextImpl(String context, Long expiration)Deletes every record with the supplied context.protected voiddeleteImpl(Long expiration)Deletes every record with an expiration before the supplied expiration.protected booleandeleteImpl(Long version, String context, String key)Deletes the record matching the supplied parameters.booleandeleteWithVersion(long version, String context, String key)protected voiddoDestroy()private <T> List<T>executeNamedQuery(javax.persistence.EntityManager manager, String query, Map<String,Object> params, Class<T> clazz, javax.persistence.LockModeType lockMode)Executes the supplied named query.protected TimerTaskgetCleanupTask()intgetTransactionRetry()Returns the number of times a transaction will be retried if aRollbackExceptionis encountered.booleanisClustered()booleanisServerSide()<T> StorageRecord<T>read(String context, String key)<T> Pair<Long,StorageRecord<T>>read(String context, String key, long version)List<?>readAll()Returns all records from the store.List<?>readAll(String context)Returns all records from the store for the supplied context.List<String>readContexts()Returns all contexts from the store.protected <T> Pair<Long,StorageRecord<T>>readImpl(String context, String key, Long version)Reads the record matching the supplied parameters.voidreap(String context)private voidrollbackTransaction(javax.persistence.EntityTransaction transaction)Rolls back the supplied transaction ifEntityTransaction.isActive().voidsetTransactionRetry(int retry)Sets the number of times a transaction will be retried (default is 3).booleanupdate(String context, String key, String value, Long expiration)voidupdateContextExpiration(String context, Long expiration)booleanupdateExpiration(String context, String key, Long expiration)protected LongupdateImpl(Long version, String context, String key, String value, Long expiration)Updates the record matching the supplied parameters.LongupdateWithVersion(long version, String context, String key, String value, Long expiration)-
Methods inherited from class org.opensaml.storage.AbstractStorageService
create, create, delete, deleteWithVersion, doInitialize, getCapabilities, getCleanupInterval, getCleanupTaskTimer, getContextSize, getKeySize, getValueSize, read, setCleanupInterval, setCleanupTaskTimer, setContextSize, setKeySize, setValueSize, update, update, updateExpiration, updateWithVersion, updateWithVersion
-
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 org.opensaml.storage.StorageCapabilities
getContextSize, getKeySize, getValueSize
-
-
-
-
Field Detail
-
log
@Nonnull private final org.slf4j.Logger log
Class logger.
-
entityManagerFactory
@Nonnull private final javax.persistence.EntityManagerFactory entityManagerFactory
Entity manager factory.
-
transactionRetry
@NonNegative private int transactionRetry
Number of times to retry a transaction if it rolls back.
-
-
Method Detail
-
getTransactionRetry
public int getTransactionRetry()
Returns the number of times a transaction will be retried if aRollbackExceptionis encountered.- Returns:
- number of transaction retries
-
setTransactionRetry
public void setTransactionRetry(int retry)
Sets the number of times a transaction will be retried (default is 3).- Parameters:
retry- number of transaction retries
-
isServerSide
public boolean isServerSide()
- Specified by:
isServerSidein interfaceStorageCapabilitiesEx
-
isClustered
public boolean isClustered()
- Specified by:
isClusteredin interfaceStorageCapabilitiesEx
-
doDestroy
protected void doDestroy()
- Overrides:
doDestroyin classAbstractStorageService
-
create
public boolean create(@Nonnull @NotEmpty String context, @Nonnull @NotEmpty String key, @Nonnull @NotEmpty String value, @Nullable @Positive Long expiration) throws IOException
- Specified by:
createin interfaceStorageService- Throws:
IOException
-
readAll
@Nonnull @NonnullElements public List<?> readAll() throws IOException
Returns all records from the store.- Returns:
- all records or an empty list
- Throws:
IOException- if errors occur in the read process
-
readAll
@Nonnull @NonnullElements public List<?> readAll(@Nonnull @NotEmpty String context) throws IOException
Returns all records from the store for the supplied context.- Parameters:
context- a storage context label- Returns:
- all records in the context or an empty list
- Throws:
IOException- if errors occur in the read process
-
readContexts
@Nonnull @NonnullElements public List<String> readContexts() throws IOException
Returns all contexts from the store.- Returns:
- all contexts or an empty list
- Throws:
IOException- if errors occur in the read process
-
read
@Nullable public <T> StorageRecord<T> read(@Nonnull @NotEmpty String context, @Nonnull @NotEmpty String key) throws IOException
- Specified by:
readin interfaceStorageService- Throws:
IOException
-
read
@Nonnull public <T> Pair<Long,StorageRecord<T>> read(@Nonnull @NotEmpty String context, @Nonnull @NotEmpty String key, @Positive long version) throws IOException
- Specified by:
readin interfaceStorageService- Throws:
IOException
-
readImpl
@Nonnull protected <T> Pair<Long,StorageRecord<T>> readImpl(@Nonnull @NotEmpty String context, @Nonnull @NotEmpty String key, @Positive Long version) throws IOException
Reads the record matching the supplied parameters. Returns an empty pair if the record cannot be found or is expired.- Type Parameters:
T- type of object- Parameters:
context- to search forkey- to search forversion- to match- Returns:
- pair of version and storage record
- Throws:
IOException- if errors occur in the read process
-
update
public boolean update(@Nonnull @NotEmpty String context, @Nonnull @NotEmpty String key, @Nonnull @NotEmpty String value, @Nullable @Positive Long expiration) throws IOException
- Specified by:
updatein interfaceStorageService- Throws:
IOException
-
updateWithVersion
@Nullable public Long updateWithVersion(@Positive long version, @Nonnull @NotEmpty String context, @Nonnull @NotEmpty String key, @Nonnull @NotEmpty String value, @Nullable @Positive Long expiration) throws IOException, VersionMismatchException
- Specified by:
updateWithVersionin interfaceStorageService- Throws:
IOExceptionVersionMismatchException
-
updateExpiration
public boolean updateExpiration(@Nonnull @NotEmpty String context, @Nonnull @NotEmpty String key, @Nullable @Positive Long expiration) throws IOException
- Specified by:
updateExpirationin interfaceStorageService- Throws:
IOException
-
updateImpl
@Nullable protected Long updateImpl(@Nullable Long version, @Nonnull @NotEmpty String context, @Nonnull @NotEmpty String key, @Nonnull @NotEmpty String value, @Nullable @Positive Long expiration) throws IOException, VersionMismatchException
Updates the record matching the supplied parameters. Returns null if the record cannot be found or is expired.- Parameters:
version- to checkcontext- to search forkey- to search forvalue- to updateexpiration- to update- Returns:
- whether the record was updated
- Throws:
IOException- if errors occur in the update processVersionMismatchException- if the record found contains a version that does not match the parameter
-
deleteWithVersion
public boolean deleteWithVersion(@Positive long version, @Nonnull @NotEmpty String context, @Nonnull @NotEmpty String key) throws IOException, VersionMismatchException
- Specified by:
deleteWithVersionin interfaceStorageService- Throws:
IOExceptionVersionMismatchException
-
delete
public boolean delete(@Nonnull @NotEmpty String context, @Nonnull @NotEmpty String key) throws IOException
- Specified by:
deletein interfaceStorageService- Throws:
IOException
-
deleteImpl
protected boolean deleteImpl(@Nullable @Positive Long version, @Nonnull @NotEmpty String context, @Nonnull @NotEmpty String key) throws IOException, VersionMismatchException
Deletes the record matching the supplied parameters.- Parameters:
version- to checkcontext- to search forkey- to search for- Returns:
- whether the record was deleted
- Throws:
IOException- if errors occur in the delete processVersionMismatchException- if the record found contains a version that does not match the parameter
-
updateContextExpiration
public void updateContextExpiration(@Nonnull @NotEmpty String context, @Nullable @Positive Long expiration) throws IOException
- Specified by:
updateContextExpirationin interfaceStorageService- Throws:
IOException
-
deleteContext
public void deleteContext(@Nonnull @NotEmpty String context) throws IOException
- Specified by:
deleteContextin interfaceStorageService- Throws:
IOException
-
reap
public void reap(@Nonnull @NotEmpty String context) throws IOException
- Specified by:
reapin interfaceStorageService- Throws:
IOException
-
deleteContextImpl
protected void deleteContextImpl(@Nonnull @NotEmpty String context, @Nonnull Long expiration) throws IOException
Deletes every record with the supplied context. If expiration is supplied, only records with an expiration before the supplied expiration will be removed.- Parameters:
context- to deleteexpiration- (optional) to require for deletion- Throws:
IOException- if errors occur in the delete process
-
deleteImpl
protected void deleteImpl(@Nonnull Long expiration) throws IOExceptionDeletes every record with an expiration before the supplied expiration.- Parameters:
expiration- of records to delete- Throws:
IOException- if errors occur in the cleanup process
-
executeNamedQuery
private <T> List<T> executeNamedQuery(@Nonnull javax.persistence.EntityManager manager, @Nonnull @NotEmpty String query, @Nonnull Map<String,Object> params, @Nonnull Class<T> clazz, @Nonnull javax.persistence.LockModeType lockMode) throws IOException
Executes the supplied named query.- Type Parameters:
T- type of entity to return- Parameters:
manager- to execute the queryquery- to executeparams- parameters for the queryclazz- type of entity to returnlockMode- of the transaction- Returns:
- query results or an empty list
- Throws:
IOException- if an error occurs executing the query
-
getCleanupTask
@Nullable protected TimerTask getCleanupTask()
- Overrides:
getCleanupTaskin classAbstractStorageService
-
commitTransaction
private void commitTransaction(@Nullable javax.persistence.EntityTransaction transaction)Commits the supplied transaction ifEntityTransaction.isActive()and notEntityTransaction.getRollbackOnly(). Logs any exception that occurs.- Parameters:
transaction- to commit
-
rollbackTransaction
private void rollbackTransaction(@Nullable javax.persistence.EntityTransaction transaction)Rolls back the supplied transaction ifEntityTransaction.isActive(). Logs any exception that occurs.- Parameters:
transaction- to roll back
-
closeEntityManager
private void closeEntityManager(@Nullable javax.persistence.EntityManager manager)Closes the supplied entity manager ifEntityManager.isOpen(). Logs any exception that occurs.- Parameters:
manager- to close
-
-