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

org.wildfly.clustering.ejb.cache.bean.CompositeBean Maven / Gradle / Ivy

Go to download

Contains code common to the wildfly-clustering-ejb-hotrod and wildfly-clustering-ejb-infinispan modules.

There is a newer version: 34.0.1.Final
Show newest version
/*
 * 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();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy