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

com.flowlogix.cdi.CDIFactory Maven / Gradle / Ivy

There is a newer version: 4.0.9
Show newest version
/**
 * @(#)CDIFactory.java
 *
 * Copyright 2009 by Movellas ApS All rights reserved.
 */
package com.flowlogix.cdi;

import java.util.Set;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;

/**
 *
 * @author Magnus
 */
@RequiredArgsConstructor
public class CDIFactory
{
    public  T get(Class clazz)
    {
        Set> beans;
        try
        {
            beans = beanManager.getBeans(clazz);
        } catch (UnsupportedOperationException e)
        {
            return null;
        }
        if (beans != null && beans.size() > 0)
        {
            @SuppressWarnings("unchecked")
            Bean bean = (Bean) beans.iterator().next();
            CreationalContext ctx = beanManager.createCreationalContext(bean);
            try
            {
                T o = clazz.cast(beanManager.getReference(bean, clazz, ctx));
                log.debug("Found and returning: " + clazz.getCanonicalName());
                return o;
            } catch (Exception e)
            {
                log.trace("CDI Retrieval Error", e);
            }
        }
        return null;
    }

    
    private final Logger log;
    private final BeanManager beanManager;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy