All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nimbusds.infinispan.persistence.ldap.ExpiredEntryReaper Maven / Gradle / Ivy

There is a newer version: 3.1.3
Show newest version
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 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();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy