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

org.wildfly.clustering.session.cache.AttachedSession 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.Map;
import java.util.function.Consumer;

import org.wildfly.clustering.session.ImmutableSession;
import org.wildfly.clustering.session.Session;
import org.wildfly.clustering.session.SessionMetaData;

/**
 * Decorated {@link Session} whose methods throw an {@link IllegalStateException} if the session is not valid.
 * @param  the session context type
 * @author Paul Ferraro
 */
public class AttachedSession extends DecoratedSession {
	private final Consumer closeTask;

	public AttachedSession(Session session, Consumer closeTask) {
		super(session);
		this.closeTask = closeTask;
	}

	private void validate() {
		if (!this.isValid()) {
			throw new IllegalStateException(this.getId());
		}
	}

	@Override
	public SessionMetaData getMetaData() {
		this.validate();
		return super.getMetaData();
	}

	@Override
	public Map getAttributes() {
		this.validate();
		return super.getAttributes();
	}

	@Override
	public void invalidate() {
		this.validate();
		super.invalidate();
	}

	@Override
	public void close() {
		try {
			super.close();
		} finally {
			this.closeTask.accept(this.get());
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy