com.flowlogix.cdi.CDIFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of flowlogix-tapestry Show documentation
Show all versions of flowlogix-tapestry Show documentation
Flow Logix Utility Library for Tapestry
/**
* @(#)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;
}