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

org.wildfly.clustering.session.cache.attributes.DetachedSessionAttributes 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.attributes;

import java.util.Collection;
import java.util.Set;
import java.util.function.Supplier;

import org.wildfly.clustering.cache.batch.Batch;
import org.wildfly.clustering.session.Session;

/**
 * A {@link SessionAttributes} implementation for detached sessions.
 * @param  the session context type
 * @param  the batch type
 * @author Paul Ferraro
 */
public class DetachedSessionAttributes implements SessionAttributes {
	private final Supplier batchFactory;
	private final Supplier> sessionFactory;

	public DetachedSessionAttributes(Supplier batchFactory, Supplier> sessionFactory) {
		this.batchFactory = batchFactory;
		this.sessionFactory = sessionFactory;
	}

	@Override
	public Set keySet() {
		try (B batch = this.batchFactory.get()) {
			try (Session session = this.sessionFactory.get()) {
				return session.getAttributes().keySet();
			}
		}
	}

	@Override
	public Set> entrySet() {
		try (B batch = this.batchFactory.get()) {
			try (Session session = this.sessionFactory.get()) {
				return session.getAttributes().entrySet();
			}
		}
	}

	@Override
	public Collection values() {
		try (B batch = this.batchFactory.get()) {
			try (Session session = this.sessionFactory.get()) {
				return session.getAttributes().values();
			}
		}
	}

	@Override
	public Object get(Object name) {
		try (B batch = this.batchFactory.get()) {
			try (Session session = this.sessionFactory.get()) {
				return session.getAttributes().get(name);
			}
		}
	}

	@Override
	public Object put(String name, Object value) {
		try (B batch = this.batchFactory.get()) {
			try (Session session = this.sessionFactory.get()) {
				return session.getAttributes().put(name, value);
			}
		}
	}

	@Override
	public Object remove(Object key) {
		try (B batch = this.batchFactory.get()) {
			try (Session session = this.sessionFactory.get()) {
				return session.getAttributes().remove(key);
			}
		}
	}

	@Override
	public void close() {
		// A detached session has no lifecycle
	}
}