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

br.com.caelum.vraptor.ioc.cdi.BeanManagerUtil Maven / Gradle / Ivy

The newest version!
package br.com.caelum.vraptor.ioc.cdi;

import java.util.Set;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;

public class BeanManagerUtil {

	private BeanManager beanManager;

	public BeanManagerUtil(BeanManager beanManager) {
		this.beanManager = beanManager;
	}

	public  T instanceFor(Class type) {
		Set beans = getBeans(type);
		Bean bean = (Bean) beanManager.resolve(beans);
		return instanceFor(bean,type);
	}
	
	public  T instanceFor(Bean bean){		
		return instanceFor(bean,bean.getBeanClass());		
	}
	
	public  T instanceFor(Bean bean,Class specificType){
		if(bean==null){
			throw new IllegalArgumentException("Bean must not be null. Class "+specificType);
		}
		CreationalContext ctx = beanManager.createCreationalContext(bean);
		return (T) beanManager.getReference(bean, specificType, ctx);		
	}	
	
	public Set> getBeans(Class type){
		return beanManager.getBeans(type);
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy