org.wildfly.clustering.ejb.cache.bean.CompositeBean Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of wildfly-clustering-ejb-cache Show documentation
Show all versions of wildfly-clustering-ejb-cache Show documentation
Contains code common to the wildfly-clustering-ejb-hotrod and wildfly-clustering-ejb-infinispan modules.
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/
package org.wildfly.clustering.ejb.cache.bean;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import org.wildfly.clustering.cache.CacheEntryRemover;
import org.wildfly.clustering.ejb.bean.BeanInstance;
import org.wildfly.clustering.ejb.bean.BeanMetaData;
/**
* A {@link MutableBean} implementation composed from a {@link BeanMetaData} and a {@link BeanGroup}.
* @author Paul Ferraro
* @param the bean identifier type
* @param the bean instance type
*/
public class CompositeBean> extends CompositeImmutableBean implements MutableBean {
private final BeanMetaData metaData;
private final BeanGroup group;
private final CacheEntryRemover remover;
private final AtomicBoolean valid = new AtomicBoolean(true);
public CompositeBean(K id, BeanMetaData metaData, BeanGroup group, CacheEntryRemover remover) {
super(id, group.getBeanInstance(id), metaData);
this.metaData = metaData;
this.group = group;
this.remover = remover;
}
@Override
public BeanMetaData getMetaData() {
return this.metaData;
}
@Override
public void setInstance(V instance) {
this.group.addBeanInstance(instance);
}
@Override
public boolean isValid() {
return this.valid.get();
}
@Override
public void remove(Consumer removeTask) {
// Ensure we only close group once
if (this.valid.compareAndSet(true, false)) {
try {
K id = this.getId();
V instance = this.group.removeBeanInstance(id);
this.remover.remove(id);
if (instance != null) {
removeTask.accept(instance);
}
} finally {
this.group.close();
}
}
}
@Override
public void close() {
if (this.valid.compareAndSet(true, false)) {
this.group.close();
}
}
}