org.wildfly.clustering.session.cache.AttachedSession Maven / Gradle / Ivy
/*
* 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());
}
}
}