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

com.oneandone.cdi.weldstarter.CreationalContexts Maven / Gradle / Ivy

The newest version!
package com.oneandone.cdi.weldstarter;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;

import javax.enterprise.context.spi.Context;
import javax.enterprise.context.spi.Contextual;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Used to handle the initialisation of weld-beans
 *
 * @author aschoerk
 */
public class CreationalContexts implements AutoCloseable {


    private final BeanManager bm;
    private List> creationalContexts = new ArrayList<>();
    private Logger logger = null;

    /**
     * Create and search for BeanManager in InitialContext
     *
     * @throws NamingException thrown when problems with InitialContext
     */
    public CreationalContexts() throws NamingException {
        InitialContext initialContext = null;
        try {
            initialContext = new InitialContext();
            this.bm = (BeanManager) initialContext.lookup("java:comp/BeanManager");
        } finally {
            if (initialContext != null)
                initialContext.close();
        }
    }

    /**
     * Create it
     * @param bm the Weld-Beanmanager
     */
    public CreationalContexts(BeanManager bm) {
        this.bm = bm;
    }


    private Logger getLogger() {
        if (logger == null) {
            logger = LoggerFactory.getLogger("CreationalContexts");
        }
        return logger;
    }

    /**
     * create a bean of class clazz in context scope
     * @param clazz the clazz of the Bean to be created
     * @param scope either ApplicationScoped or Dependent
     * @return the created bean
     */
    public Object create(Class clazz, Class scope) {
        Bean bean = bm.resolve(bm.getBeans(clazz));
        if (bean != null) {
            Object result = create((Contextual) bean, scope);
            if (result == null) {
                throw new RuntimeException("Could not create Bean to be initialized of Class: " + clazz);
            }
            return result;
        } else {
            throw new RuntimeException("Could not resolve Bean to be initialized of Class: " + clazz);
        }
    }

    /**
     * create a bean in context
     * @param b the Bean as described and found by the weld init.
     * @param scope either ApplicationScoped or Dependent
     * @return the created bean
     */
    public Object create(Contextual b, Class scope) {
        try {
            final CreationalContext cb = bm.createCreationalContext(b);
            // assumes the bean will exist only once
            Context context = bm.getContext(scope);
            final Object o = context.get(b, cb);
            creationalContexts.add(cb);
            return o;
        } catch (Throwable thw) {
            getLogger().error("Exception during create of Bean {}", b);
            getLogger().error("Exception: ", thw);
            throw new RuntimeException(thw);
        }
    }

    /**
     * close without checked exception
     */
    public void closeIt() {
        for (CreationalContext cc: creationalContexts) {
            cc.release();
        }
    }

    /**
     * close according to AutoCloseable
     *
     * @throws Exception check Exception should not occur.
     */
    @Override
    public void close() throws Exception {
        closeIt();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy