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

org.wildfly.clustering.session.cache.AbstractSessionManager Maven / Gradle / Ivy

There is a newer version: 4.0.0.Final
Show newest version
/*
 * Copyright The WildFly Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package org.wildfly.clustering.session.cache;

import java.util.concurrent.CompletionStage;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;

import org.jboss.logging.Logger;
import org.wildfly.clustering.cache.CacheConfiguration;
import org.wildfly.clustering.cache.batch.Batch;
import org.wildfly.clustering.server.expiration.Expiration;
import org.wildfly.clustering.session.ImmutableSession;
import org.wildfly.clustering.session.Session;
import org.wildfly.clustering.session.SessionManager;
import org.wildfly.clustering.session.SessionManagerConfiguration;
import org.wildfly.clustering.session.SessionStatistics;

/**
 * An abstract {@link SessionManager} implementation that delegates most implementation details to a {@link SessionFactory}.
 * @param  the session manager context type
 * @param  the session metadata value type
 * @param  the session attribute value type
 * @param  the session context type
 * @author Paul Ferraro
 */
public abstract class AbstractSessionManager implements SessionManager, SessionStatistics {
	protected final Logger logger = Logger.getLogger(this.getClass());

	private final SessionFactory factory;
	private final Consumer expirationListener;
	private final Expiration expiration;
	private final Supplier identifierFactory;
	private final C context;
	private final Supplier batchFactory;
	private final UnaryOperator> wrapper;

	protected AbstractSessionManager(SessionManagerConfiguration configuration, CacheConfiguration cacheConfiguration, SessionFactory factory, Consumer sessionCloseTask) {
		this.identifierFactory = configuration.getIdentifierFactory();
		this.context = configuration.getContext();
		this.batchFactory = cacheConfiguration.getBatchFactory();
		this.expiration = configuration;
		this.expirationListener = configuration.getExpirationListener();
		this.factory = factory;
		this.wrapper = session -> new ManagedSession<>(this, session, sessionCloseTask);
	}

	@Override
	public Supplier getIdentifierFactory() {
		return this.identifierFactory;
	}

	@Override
	public Supplier getBatchFactory() {
		return this.batchFactory;
	}

	@Override
	public CompletionStage> createSessionAsync(String id) {
		this.logger.tracef("Creating session %s", id);
		return this.factory.createValueAsync(id, this.expiration.getTimeout()).thenApply(entry -> this.wrapper.apply(this.factory.createSession(id, entry, this.context)));
	}

	@Override
	public CompletionStage> findSessionAsync(String id) {
		this.logger.tracef("Locating session %s", id);
		return this.factory.findValueAsync(id).thenApply(entry -> {
			if (entry == null) {
				this.logger.tracef("Session %s not found", id);
				return null;
			}
			ImmutableSession session = this.factory.createImmutableSession(id, entry);
			if (session.getMetaData().isExpired()) {
				this.logger.tracef("Session %s was found, but has expired", id);
				this.expirationListener.accept(session);
				this.factory.removeAsync(id);
				return null;
			}
			return this.wrapper.apply(this.factory.createSession(id, entry, this.context));
		});
	}

	@Override
	public CompletionStage findImmutableSessionAsync(String id) {
		return this.factory.findValueAsync(id).thenApply(entry -> (entry != null) ? new SimpleImmutableSession(this.factory.createImmutableSession(id, entry)) : null);
	}

	@Override
	public Session getDetachedSession(String id) {
		return new DetachedSession<>(this, id, this.factory.getContextFactory().get());
	}

	@Override
	public SessionStatistics getStatistics() {
		return this;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy