Author: rmannibucau Date: Thu Nov 1 19:05:15 2012 New Revision: 1404727 URL: http://svn.apache.org/viewvc?rev=1404727&view=rev Log: TOMEE-509 allowing to produce EntityManager from ejbs (to valid against TCKs) + adding a test to be able to work on @Produces for extended em
Added: openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/cdi/ProducedExtendedEmTest.java Modified: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/cdi/CdiEjbBean.java Modified: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/cdi/CdiEjbBean.java URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/cdi/CdiEjbBean.java?rev=1404727&r1=1404726&r2=1404727&view=diff ============================================================================== --- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/cdi/CdiEjbBean.java (original) +++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/cdi/CdiEjbBean.java Thu Nov 1 19:05:15 2012 @@ -24,16 +24,19 @@ import org.apache.webbeans.component.Owb import org.apache.webbeans.component.WebBeansType; import org.apache.webbeans.config.WebBeansContext; import org.apache.webbeans.ejb.common.component.BaseEjbBean; +import org.apache.webbeans.exception.WebBeansConfigurationException; import javax.ejb.NoSuchEJBException; import javax.ejb.Remove; import javax.enterprise.context.Dependent; import javax.enterprise.context.spi.Context; import javax.enterprise.context.spi.CreationalContext; +import javax.enterprise.inject.Disposes; import javax.enterprise.inject.Typed; import javax.enterprise.inject.spi.Bean; import javax.enterprise.inject.spi.InjectionPoint; import javax.enterprise.inject.spi.SessionBeanType; +import javax.persistence.EntityManager; import java.lang.annotation.Annotation; import java.lang.reflect.Member; import java.lang.reflect.Method; @@ -62,6 +65,28 @@ public class CdiEjbBean<T> extends BaseE beanContext.set(Bean.class, this); } + @Override // copied to be able to produce EM (should be fixed in OWB for next CDI spec) + public void validatePassivationDependencies() { + if(isPassivationCapable()) { + final Set<InjectionPoint> beanInjectionPoints = getInjectionPoints(); + for(InjectionPoint injectionPoint : beanInjectionPoints) { + if(!injectionPoint.isTransient()) { + if(!getWebBeansContext().getWebBeansUtil().isPassivationCapableDependency(injectionPoint)) { + if(injectionPoint.getAnnotated().isAnnotationPresent(Disposes.class) + // here is the hack, this is temporary until OWB manages correctly serializable instances + || EntityManager.class.equals(injectionPoint.getAnnotated().getBaseType())) { + continue; + } + throw new WebBeansConfigurationException( + "Passivation capable beans must satisfy passivation capable dependencies. " + + "Bean : " + toString() + " does not satisfy. Details about the Injection-point: " + + injectionPoint.toString()); + } + } + } + } + } + @Override public void addApiType(final Class<?> apiType) { if (apiType == null) return; Added: openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/cdi/ProducedExtendedEmTest.java URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/cdi/ProducedExtendedEmTest.java?rev=1404727&view=auto ============================================================================== --- openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/cdi/ProducedExtendedEmTest.java (added) +++ openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/cdi/ProducedExtendedEmTest.java Thu Nov 1 19:05:15 2012 @@ -0,0 +1,108 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.openejb.cdi; + +import org.apache.openejb.jee.jpa.unit.Persistence; +import org.apache.openejb.jee.jpa.unit.PersistenceUnit; +import org.apache.openejb.junit.ApplicationComposer; +import org.apache.openejb.junit.Configuration; +import org.apache.openejb.junit.Module; +import org.junit.Test; +import org.junit.runner.RunWith; + +import javax.ejb.EJBException; +import javax.ejb.Stateful; +import javax.enterprise.context.SessionScoped; +import javax.enterprise.inject.Produces; +import javax.inject.Inject; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.PersistenceContextType; +import java.io.Serializable; +import java.util.Properties; + +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +@RunWith(ApplicationComposer.class) +public class ProducedExtendedEmTest { + @Module + public Class<?>[] app() throws Exception { + return new Class<?>[] { EntityManagerProducer.class, A.class }; + } + + @Module + public Persistence persistence() throws Exception { + final PersistenceUnit unit = new PersistenceUnit("cdi-em-extended"); + unit.setProperty("openjpa.jdbc.SynchronizeMappings", "buildSchema(ForeignKeys=true)"); + unit.getProperties().setProperty("openjpa.RuntimeUnenhancedClasses", "supported"); + unit.getProperties().setProperty("openjpa.DatCache", "false"); + unit.setExcludeUnlistedClasses(true); + + final Persistence persistence = new org.apache.openejb.jee.jpa.unit.Persistence(unit); + persistence.setVersion("2.0"); + return persistence; + } + + @Configuration + public Properties config() { + final Properties p = new Properties(); + p.put("ProducedExtendedEmTest", "new://Resource?type=DataSource"); + p.put("ProducedExtendedEmTest.JdbcDriver", "org.hsqldb.jdbcDriver"); + p.put("ProducedExtendedEmTest.JdbcUrl", "jdbc:hsqldb:mem:produce-em-cdi"); + return p; + } + + @SessionScoped + @Stateful + public static class EntityManagerProducer implements Serializable { + @PersistenceContext(type = PersistenceContextType.EXTENDED) + private EntityManager em; + + @Produces + public EntityManager produceEm() { + return em; + } + } + + @SessionScoped + @Stateful + public static class A implements Serializable { + @Inject + private EntityManager em; + + public String getDelegateClassName() { + return em.getDelegate().getClass().getCanonicalName(); + } + + } + + @Inject + private A a; + + @Test + public void checkEm() { + try { + a.getDelegateClassName(); + } catch (EJBException ee) { + assertNotNull(ee); + assertThat(ee.getCause(), instanceOf(IllegalStateException.class)); + } + } +}