com.nimbusds.infinispan.persistence.ldap.ExpiredEntryReaper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of infinispan-cachestore-ldap Show documentation
Show all versions of infinispan-cachestore-ldap Show documentation
Infinispan module for persisting cache data to an LDAPv3 directory
package com.nimbusds.infinispan.persistence.ldap;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import com.nimbusds.infinispan.persistence.common.InfinispanEntry;
import com.nimbusds.infinispan.persistence.ldap.backend.LDAPConnector;
import com.unboundid.ldap.sdk.DN;
import com.unboundid.ldap.sdk.LDAPException;
import net.jcip.annotations.ThreadSafe;
import org.apache.commons.lang3.tuple.Pair;
import org.infinispan.metadata.InternalMetadata;
import org.infinispan.persistence.spi.AdvancedCacheWriter;
import org.infinispan.persistence.spi.PersistenceException;
/**
* Expired entry reaper.
*/
@ThreadSafe
class ExpiredEntryReaper {
/**
* The LDAP connector.
*/
private final LDAPConnector ldapConnector;
/**
* The LDAP entity transformer.
*/
private final LDAPEntryTransformer entityTransformer;
/**
* Creates a new reaper for expired entries.
*
* @param ldapConnector The LDAP connector. Must not be
* {@code null}.
* @param entityTransformer The LDAP entity transformer. Must not be
* {@code null}.
*/
public ExpiredEntryReaper(final LDAPConnector ldapConnector,
final LDAPEntryTransformer entityTransformer) {
assert ldapConnector != null;
this.ldapConnector = ldapConnector;
assert entityTransformer != null;
this.entityTransformer = entityTransformer;
}
/**
* Purges the expired persisted entries according to their metadata
* timestamps (if set / persisted).
*
* @param purgeListener The purge listener. Must not be {@code null}.
*
* @return The number of purged entries.
*/
public int purge(final AdvancedCacheWriter.PurgeListener super K> purgeListener) {
final long now = new Date().getTime();
// Marks DN + key of entries for deletion
List> forDeletion = new LinkedList<>();
ldapConnector.retrieveEntries(ldapEntry -> {
InfinispanEntry infinispanEntry = entityTransformer.toInfinispanEntry(new LDAPEntry(ldapEntry));
InternalMetadata metadata = infinispanEntry.getMetadata();
if (metadata == null) {
return; // no metadata found
}
if (metadata.isExpired(now)) {
// Mark entry for deletion
try {
Pair pair = Pair.of(
new DN(ldapEntry.getDN()),
infinispanEntry.getKey());
forDeletion.add(pair);
} catch (LDAPException e) {
throw new PersistenceException(e.getMessage(), e);
}
}
});
final AtomicInteger deleteCounter = new AtomicInteger(0);
for (Pair pair: forDeletion) {
// Delete LDAP entry by its DN
if (ldapConnector.deleteEntry(pair.getLeft())) {
// Notify listener, interested in the Infinispan entry key
purgeListener.entryPurged(pair.getRight());
deleteCounter.incrementAndGet();
}
}
return deleteCounter.get();
}
}