Hi all
I created a simple weld spring intergration. and use it to graniteds cdi under tomcat 6.0.18. I hope someone can help me to improve it. Thanks.
Spring JpaInjectionService
package com.yofc.weldboot;
import java.util.Enumeration;
import java.util.Map;
import java.util.Map.Entry;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;
import javax.servlet.ServletContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.weld.injection.spi.JpaInjectionServices;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean;
import org.springframework.orm.jpa.ExtendedEntityManagerCreator;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
public class SpringJpaInjectionService extends ContextLoader implements JpaInjectionServices{
private static final Log logger = LogFactory.getLog(SpringJpaInjectionService.class);
public static WebApplicationContext webApplicationContext;
private ContextLoader contextLoader;
public static ServletContext servletContext;
public SpringJpaInjectionService(ServletContext servletCtx) {
servletContext=servletCtx;
try
{
if (this.contextLoader == null) {
this.contextLoader = this;
}
webApplicationContext=this.contextLoader.initWebApplicationContext(servletContext);
}
catch (Exception e)
{
throw new RuntimeException("Spring startup error:"+e.getMessage());
}
}
@Override
public EntityManager resolvePersistenceContext(InjectionPoint injectionPoint) {
if (!injectionPoint.getAnnotated().isAnnotationPresent(PersistenceContext.class))
{
throw new IllegalArgumentException("No @PersistenceContext annotation found on injection point " + injectionPoint);
}
String unitname=injectionPoint.getAnnotated().getAnnotation(PersistenceContext.class).unitName();
Map<String, AbstractEntityManagerFactoryBean> emfbeans=webApplicationContext.getBeansOfType(AbstractEntityManagerFactoryBean.class, true, true);
for(Entry<String, AbstractEntityManagerFactoryBean> entry:emfbeans.entrySet())
{
AbstractEntityManagerFactoryBean absenfBean=entry.getValue();
if(unitname==null || unitname.equals(""))
return ExtendedEntityManagerCreator.createContainerManagedEntityManager(absenfBean.getNativeEntityManagerFactory());
else if(absenfBean.getPersistenceUnitName()!=null && absenfBean.getPersistenceUnitName().equals(unitname))
return ExtendedEntityManagerCreator.createContainerManagedEntityManager(absenfBean.getNativeEntityManagerFactory());
}
return null;
}
@Override
public EntityManagerFactory resolvePersistenceUnit(InjectionPoint injectionPoint) {
if (!injectionPoint.getAnnotated().isAnnotationPresent(PersistenceUnit.class))
{
throw new IllegalArgumentException("No @PersistenceContext annotation found on injection point " + injectionPoint);
}
String unitname=injectionPoint.getAnnotated().getAnnotation(PersistenceUnit.class).unitName();
Map<String, AbstractEntityManagerFactoryBean> emfbeans=webApplicationContext.getBeansOfType(AbstractEntityManagerFactoryBean.class, true, true);
for(Entry<String, AbstractEntityManagerFactoryBean> entry:emfbeans.entrySet())
{
AbstractEntityManagerFactoryBean absenfBean=entry.getValue();
if(unitname==null || unitname.equals(""))
return absenfBean.getNativeEntityManagerFactory();
else if(absenfBean.getPersistenceUnitName()!=null && absenfBean.getPersistenceUnitName().equals(unitname))
return absenfBean.getNativeEntityManagerFactory();
}
return null;
}
@Override
public void cleanup() {
if (this.contextLoader != null) {
this.contextLoader.closeWebApplicationContext(servletContext);
}
cleanupAttributes(servletContext);
}
private void cleanupAttributes(ServletContext sc) {
Enumeration<?> attrNames = sc.getAttributeNames();
while (attrNames.hasMoreElements()) {
String attrName = (String) attrNames.nextElement();
if (attrName.startsWith("org.springframework.")) {
Object attrValue = sc.getAttribute(attrName);
if (attrValue instanceof DisposableBean) {
try {
((DisposableBean) attrValue).destroy();
}
catch (Throwable ex) {
logger.error("Couldn't invoke destroy method of attribute with name '" + attrName + "'", ex);
}
}
}
}
}
}