Skip to content

Commit

Permalink
WELD-632, support static resource producer fields
Browse files Browse the repository at this point in the history
  • Loading branch information
pmuir committed Aug 24, 2010
1 parent c26819b commit 1874470
Show file tree
Hide file tree
Showing 13 changed files with 407 additions and 2 deletions.
Expand Up @@ -37,10 +37,13 @@
import org.jboss.weld.ejb.EJBApiAbstraction;
import org.jboss.weld.exceptions.DefinitionException;
import org.jboss.weld.exceptions.IllegalStateException;
import org.jboss.weld.injection.FieldInjectionPoint;
import org.jboss.weld.injection.WeldInjectionPoint;
import org.jboss.weld.introspector.WeldField;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.persistence.PersistenceApiAbstraction;
import org.jboss.weld.serialization.spi.ContextualStore;
import org.jboss.weld.util.Beans;
import org.jboss.weld.util.reflection.Reflections;
import org.jboss.weld.ws.WSApiAbstraction;

Expand Down Expand Up @@ -106,10 +109,13 @@ public static <X, T> EEResourceProducerField<X, T> of(WeldField<T, ? super X> fi
{
return new EEResourceProducerField<X, T>(field, declaringBean, manager);
}

private final WeldInjectionPoint<?, ?> injectionPoint;

protected EEResourceProducerField(WeldField<T, ? super X> field, AbstractClassBean<X> declaringBean, BeanManagerImpl manager)
{
super(field, declaringBean, manager);
this.injectionPoint = FieldInjectionPoint.of(declaringBean, field);
}

@Override
Expand Down Expand Up @@ -156,7 +162,15 @@ public T create(CreationalContext<T> creationalContext)
*/
private T createUnderlying(CreationalContext<T> creationalContext)
{
return super.create(creationalContext);
// Treat static fields as a special case, as they won't be injected, as the no bean is resolved, and normally there is no injection on static fields
if (getWeldAnnotated().isStatic())
{
return (T) Beans.resolveEEResource(getBeanManager(), injectionPoint);
}
else
{
return super.create(creationalContext);
}
}

@Override
Expand Down
44 changes: 44 additions & 0 deletions impl/src/main/java/org/jboss/weld/util/Beans.java
Expand Up @@ -765,6 +765,50 @@ public static <T> void injectEEFields(T beanInstance, BeanManagerImpl manager, I
}
}
}

/**
* Inspect an injection point, and try to retrieve a EE resource for it
*/
public static Object resolveEEResource(BeanManagerImpl manager, WeldInjectionPoint<?, ?> injectionPoint)
{
EjbInjectionServices ejbServices = manager.getServices().get(EjbInjectionServices.class);
JpaInjectionServices jpaServices = manager.getServices().get(JpaInjectionServices.class);
ResourceInjectionServices resourceServices = manager.getServices().get(ResourceInjectionServices.class);

if (ejbServices != null)
{
Class<? extends Annotation> ejbAnnotationType = manager.getServices().get(EJBApiAbstraction.class).EJB_ANNOTATION_CLASS;
if (injectionPoint.isAnnotationPresent(ejbAnnotationType))
{
return ejbServices.resolveEjb(injectionPoint);
}
}

if (jpaServices != null)
{
Class<? extends Annotation> persistenceUnitAnnotationType = manager.getServices().get(PersistenceApiAbstraction.class).PERSISTENCE_UNIT_ANNOTATION_CLASS;
if (injectionPoint.isAnnotationPresent(persistenceUnitAnnotationType))
{
return jpaServices.resolvePersistenceUnit(injectionPoint);
}

Class<? extends Annotation> persistenceContextAnnotationType = manager.getServices().get(PersistenceApiAbstraction.class).PERSISTENCE_CONTEXT_ANNOTATION_CLASS;
if (injectionPoint.isAnnotationPresent(persistenceContextAnnotationType))
{
return jpaServices.resolvePersistenceContext(injectionPoint);
}
}

if (resourceServices != null)
{
Class<? extends Annotation> resourceAnnotationType = manager.getServices().get(EJBApiAbstraction.class).RESOURCE_ANNOTATION_CLASS;
if (injectionPoint.isAnnotationPresent(resourceAnnotationType))
{
return resourceServices.resolveResource(injectionPoint);
}
}
return null;
}

/**
* Gets the declared bean type
Expand Down
@@ -0,0 +1,32 @@
package org.jboss.weld.tests.resources;

import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;

import java.io.IOException;

import javax.inject.Inject;
import javax.persistence.EntityManagerFactory;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("emfconsumer1")
public class EMFConsumer1 extends HttpServlet
{

@Inject
@ProducedViaStaticFieldOnEJB
private EntityManagerFactory emf;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
if (emf == null || emf.createEntityManager() == null)
{
resp.sendError(SC_INTERNAL_SERVER_ERROR);
}
}

}
@@ -0,0 +1,32 @@
package org.jboss.weld.tests.resources;

import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;

import java.io.IOException;

import javax.inject.Inject;
import javax.persistence.EntityManagerFactory;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("emfconsumer2")
public class EMFConsumer2 extends HttpServlet
{

@Inject
@ProducedViaInstanceFieldOnManagedBean
private EntityManagerFactory emf;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
if (emf == null || emf.createEntityManager() == null)
{
resp.sendError(SC_INTERNAL_SERVER_ERROR);
}
}

}
@@ -0,0 +1,32 @@
package org.jboss.weld.tests.resources;

import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;

import java.io.IOException;

import javax.inject.Inject;
import javax.persistence.EntityManagerFactory;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("emfconsumer3")
public class EMFConsumer3 extends HttpServlet
{

@Inject
@ProducedViaStaticFieldOnManagedBean
private EntityManagerFactory emf;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
if (emf == null || emf.createEntityManager() == null)
{
resp.sendError(SC_INTERNAL_SERVER_ERROR);
}
}

}
@@ -0,0 +1,102 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.tests.resources;

import static org.jboss.arquillian.api.RunModeType.AS_CLIENT;
import static org.junit.Assert.assertEquals;

import org.jboss.arquillian.api.Deployment;
import org.jboss.arquillian.api.Run;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.Asset;
import org.jboss.shrinkwrap.api.asset.ByteArrayAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.weld.tests.category.Integration;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;

@Category(Integration.class)
@RunWith(Arquillian.class)
@Run(AS_CLIENT)
public class EMFFactoryTest
{

public static final Asset PERSISTENCE_XML = new ByteArrayAsset("<persistence xmlns=\"http://java.sun.com/xml/ns/persistence\" version=\"1.0\"><persistence-unit name=\"pu1\"><jta-data-source>java:/DefaultDS</jta-data-source></persistence-unit></persistence>".getBytes());
public static final Asset EMPTY_BEANS_XML = new ByteArrayAsset("<beans />".getBytes());

@Deployment
public static Archive<?> deploy()
{
return ShrinkWrap.create(WebArchive.class, "test.war")
.addClasses(JPAResourceProducerSingletonEJB_StaticField.class, ProducedViaStaticFieldOnEJB.class, EMFConsumer1.class)
.addClasses(JPAResourceProducerManagedBean_InstanceField.class, ProducedViaInstanceFieldOnManagedBean.class, EMFConsumer2.class)
.addClasses(JPAResourceProducerManagedBean_StaticField.class, ProducedViaStaticFieldOnManagedBean.class, EMFConsumer3.class)
.addManifestResource(PERSISTENCE_XML, "persistence.xml")
.addWebResource(EMPTY_BEANS_XML, "beans.xml");
}

/*
* description = "WELD-632"
*/
@Test
public void testStaticEJBEMFProducerField() throws Exception
{
WebClient client = new WebClient();
client.setThrowExceptionOnFailingStatusCode(false);
Page page = client.getPage(getPath("emfconsumer1"));

assertEquals(200, page.getWebResponse().getStatusCode());
}

/*
* description = "WELD-632"
*/
@Test
public void testInstanceManagedBeanEMFProducerField() throws Exception
{
WebClient client = new WebClient();
client.setThrowExceptionOnFailingStatusCode(false);
Page page = client.getPage(getPath("emfconsumer2"));

assertEquals(200, page.getWebResponse().getStatusCode());
}

/*
* description = "WELD-632"
*/
@Test
public void testStaticManagedBeanEMFProducerField() throws Exception
{
WebClient client = new WebClient();
client.setThrowExceptionOnFailingStatusCode(false);
Page page = client.getPage(getPath("emfconsumer3"));

assertEquals(200, page.getWebResponse().getStatusCode());
}

protected String getPath(String viewId)
{
// TODO: this should be moved out and be handled by Arquillian
return "http://localhost:8080/test/" + viewId;
}
}
@@ -0,0 +1,13 @@
package org.jboss.weld.tests.resources;

import javax.enterprise.inject.Produces;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;

public class JPAResourceProducerManagedBean_InstanceField
{
@Produces
@PersistenceUnit(unitName = "pu1")
@ProducedViaInstanceFieldOnManagedBean
public EntityManagerFactory customerDatabasePersistenceUnit1;
}
@@ -0,0 +1,13 @@
package org.jboss.weld.tests.resources;

import javax.enterprise.inject.Produces;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;

public class JPAResourceProducerManagedBean_StaticField
{
@Produces
@PersistenceUnit(unitName = "pu1")
@ProducedViaStaticFieldOnManagedBean
public static EntityManagerFactory customerDatabasePersistenceUnit1;
}
@@ -0,0 +1,15 @@
package org.jboss.weld.tests.resources;

import javax.ejb.Singleton;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;

@Singleton
public class JPAResourceProducerSingletonEJB_StaticField
{
@Produces
@PersistenceUnit(unitName = "pu1")
@ProducedViaStaticFieldOnEJB
public static EntityManagerFactory customerDatabasePersistenceUnit1;
}
@@ -0,0 +1,36 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.tests.resources;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import javax.inject.Qualifier;

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({FIELD, METHOD, TYPE, PARAMETER})
public @interface ProducedViaInstanceFieldOnManagedBean
{

}

0 comments on commit 1874470

Please sign in to comment.