You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by st...@apache.org on 2009/04/20 00:27:18 UTC

svn commit: r766537 - in /incubator/openwebbeans/trunk/webbeans-jpa: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/webbeans/ src/main/java/org/apache/webbeans/jpa/ src/main/java/org/apache/webbea...

Author: struberg
Date: Sun Apr 19 22:27:17 2009
New Revision: 766537

URL: http://svn.apache.org/viewvc?rev=766537&view=rev
Log:
OWB-91 factor out JPA stuff into own plugin module

Added:
    incubator/openwebbeans/trunk/webbeans-jpa/
    incubator/openwebbeans/trunk/webbeans-jpa/pom.xml
    incubator/openwebbeans/trunk/webbeans-jpa/src/
    incubator/openwebbeans/trunk/webbeans-jpa/src/main/
    incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/
    incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/
    incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/
    incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/
    incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/
    incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/plugin/
    incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/plugin/OpenWebBeansJpaPlugin.java
    incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/
    incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/JPAService.java
    incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/se/
    incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/se/EntityManagersManager.java
    incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/se/JPAServicePersistenceImpl.java
    incubator/openwebbeans/trunk/webbeans-jpa/src/main/resources/
    incubator/openwebbeans/trunk/webbeans-jpa/src/main/resources/META-INF/
    incubator/openwebbeans/trunk/webbeans-jpa/src/main/resources/META-INF/services/
    incubator/openwebbeans/trunk/webbeans-jpa/src/main/resources/META-INF/services/org.apache.webbeans.plugins.OpenWebBeansPlugin
    incubator/openwebbeans/trunk/webbeans-jpa/src/test/
    incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/
    incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/
    incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/
    incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/
    incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/test/
    incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/test/component/
    incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/test/component/resource/
    incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/test/component/resource/TstResourcePersistenceBean.java
    incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/test/unittests/
    incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/test/unittests/resource/
    incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/test/unittests/resource/ResourceInjectionTest.java
    incubator/openwebbeans/trunk/webbeans-jpa/src/test/resources/

Added: incubator/openwebbeans/trunk/webbeans-jpa/pom.xml
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-jpa/pom.xml?rev=766537&view=auto
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-jpa/pom.xml (added)
+++ incubator/openwebbeans/trunk/webbeans-jpa/pom.xml Sun Apr 19 22:27:17 2009
@@ -0,0 +1,78 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+	
+	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.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+	<parent>
+		<groupId>org.apache.openwebbeans</groupId>
+		<artifactId>openwebbeans</artifactId>
+		<version>1.0.0-incubating-SNAPSHOT</version>
+	</parent>
+    
+	<artifactId>openwebbeans-jpa</artifactId>
+	<name>OpenWebBeans :: JPA integration</name>
+	<version>1.0.0-incubating-SNAPSHOT</version>
+	<description>JPA plugin for  OpenWebBeans</description>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.openwebbeans</groupId>
+            <artifactId>openwebbeans-impl</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.openwebbeans</groupId>
+            <artifactId>openwebbeans-impl</artifactId>
+            <version>1.0.0-incubating-SNAPSHOT</version>
+            <classifier>tests</classifier>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.geronimo.specs</groupId>
+            <artifactId>geronimo-jpa_3.0_spec</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>log4j</groupId>
+            <artifactId>log4j</artifactId>
+            <scope>test</scope>
+        </dependency>
+      
+        <dependency>
+            <groupId>org.apache.openjpa</groupId>
+            <artifactId>openjpa</artifactId>
+            <version>1.2.0</version>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>hsqldb</groupId>
+            <artifactId>hsqldb</artifactId>
+            <version>1.8.0.7</version>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+</project>

Added: incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/plugin/OpenWebBeansJpaPlugin.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/plugin/OpenWebBeansJpaPlugin.java?rev=766537&view=auto
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/plugin/OpenWebBeansJpaPlugin.java (added)
+++ incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/plugin/OpenWebBeansJpaPlugin.java Sun Apr 19 22:27:17 2009
@@ -0,0 +1,172 @@
+package org.apache.webbeans.jpa.plugin;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+
+import javax.inject.BindingType;
+
+import org.apache.webbeans.exception.WebBeansConfigurationException;
+import org.apache.webbeans.jpa.spi.JPAService;
+
+import javax.persistence.Entity;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.PersistenceContext;
+import javax.persistence.PersistenceContextType;
+import javax.persistence.PersistenceUnit;
+import org.apache.webbeans.plugins.OpenWebBeansPlugin;
+import org.apache.webbeans.spi.ServiceLoader;
+import org.apache.webbeans.util.AnnotationUtil;
+import org.apache.webbeans.util.Asserts;
+import org.apache.webbeans.xml.XMLAnnotationTypeManager;
+
+public class OpenWebBeansJpaPlugin implements OpenWebBeansPlugin 
+{
+
+    /** {@inheritDoc} */
+    public void startUp() throws WebBeansConfigurationException 
+    {
+        // nothing to do 
+    }
+
+    /** {@inheritDoc} */
+    public void shutDown() throws WebBeansConfigurationException 
+    {
+        // nothing to do
+    }
+
+    /** {@inheritDoc} */
+    public void isSimpleBeanClass( Class<?> clazz ) throws WebBeansConfigurationException 
+    {
+        if (AnnotationUtil.isAnnotationExistOnClass(clazz, Entity.class))
+            throw new WebBeansConfigurationException("Web Beans component implementation class : " + clazz.getName() + " can not be JPA Entity class");
+
+        if (AnnotationUtil.isAnnotationExistOnClass(clazz, PersistenceContext.class))
+            throw new WebBeansConfigurationException("Web Beans component implementation class : " + clazz.getName() + " can not be JPA Entity class");
+
+        if (AnnotationUtil.isAnnotationExistOnClass(clazz, PersistenceUnit.class))
+            throw new WebBeansConfigurationException("Web Beans component implementation class : " + clazz.getName() + " can not be JPA Entity class");
+        
+    }
+
+    /** {@inheritDoc} */
+    public Object injectResource(Type type, Annotation[] annotations)
+    {
+        if (!isResource(annotations))
+        {
+            return null;
+        }
+        
+        Annotation annot = AnnotationUtil.getAnnotation(annotations, PersistenceUnit.class);
+        if (annot != null)
+        {
+            PersistenceUnit pu = (PersistenceUnit) annot;
+            String unitName = pu.unitName();
+            
+            //X TODO what if the EntityManagerFactory is null?
+            return getJPAService().getPersistenceUnit(unitName);
+        }
+        
+        annot = AnnotationUtil.getAnnotation(annotations, PersistenceContext.class);
+        if (annot != null)
+        {
+            PersistenceContext pc = (PersistenceContext) annot;
+            String unitName = pc.unitName();
+            String name = pc.name();
+            
+            //X TODO what if the EntityManager is null?
+            return getJPAService().getPersistenceContext(unitName, name);
+        }
+        return null;
+    }
+
+    /**
+     * {@inheritDoc}
+     * Returns true if the annotation is a valid JPA Resource handled
+     * by this very Plugin.
+     * The following annotations indicate such resources:
+     * <ol>
+     * <li>&#x0040;CustomerDataservice</li>
+     * <li>&#x0040;Resource</li>
+     * <li>&#x0040;PersistenceContext</li>
+     * <li>&#x0040;PersistenceUnit</li>
+     * <li>&#x0040;</li>
+     * </ol>
+     * 
+     * Please note that &#x0040;PersistenceContext(type=EXTENDED) 
+     * is not supported for simple beans.
+     * 
+     * @param clazz type of the annotation
+     * @return true if the annotation is defined in xml or annotated with
+     *         {@link BindingType} false otherwise
+     */
+    public boolean isResourceAnnotation(Class<? extends Annotation> clazz)
+    {
+        Asserts.assertNotNull(clazz, "clazz parameter can not be null");
+        XMLAnnotationTypeManager manager = XMLAnnotationTypeManager.getInstance();
+        if (manager.isResourceExist(clazz))
+        {
+            return true;
+        }
+        else if (clazz.equals(PersistenceContext.class) ||
+                 clazz.equals(PersistenceUnit.class) )
+        {
+            return true;
+        }
+
+        return false;
+    }
+
+    /** {@inheritDoc} */
+    public void checkForValidResources(Type type, Class<?> clazz, String name, Annotation[] annotations)
+    {
+        for (Annotation a : annotations)
+        {
+            if (a.annotationType().equals(PersistenceUnit.class))
+            {
+                if (!type.equals(EntityManagerFactory.class))
+                {
+                    throw new WebBeansConfigurationException("@PersistenceUnit must only be injected into field/method with type EntityManagerFactory! class : "  
+                                                             + clazz.getName() + " in field/method : " + name);
+                }
+            }
+            
+            if (a.annotationType().equals(PersistenceContext.class))
+            {
+                PersistenceContext pc = (PersistenceContext) a;
+                
+                if (!type.equals(EntityManager.class))
+                {
+                    throw new WebBeansConfigurationException("@PersistenceContext must only be injected into field/method with type EntityManager! class : "  
+                                                             + clazz.getName() + " in field/method : " + name);
+                }
+             
+                if (pc.type().equals(PersistenceContextType.EXTENDED))
+                {
+                    throw new WebBeansConfigurationException("type of @PersistenceContext must not be 'EXTENDED'! class : "  
+                            + clazz.getName() + " in field/method : " + name);
+                    
+                }
+            }
+        }
+    }
+
+    private JPAService getJPAService()
+    {
+        return ServiceLoader.getService(JPAService.class);
+    }
+
+    private boolean isResource(Annotation[] annotations)
+    {
+        for (Annotation anno : annotations)
+        {
+            if (isResourceAnnotation(anno.annotationType()))
+            {
+                return true;
+            }
+        }
+        
+        return false;
+    }
+
+}

Added: incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/JPAService.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/JPAService.java?rev=766537&view=auto
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/JPAService.java (added)
+++ incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/JPAService.java Sun Apr 19 22:27:17 2009
@@ -0,0 +1,33 @@
+package org.apache.webbeans.jpa.spi;
+
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+
+/**
+ * SPI for getting EntityManager and EntityManagerFactory depending on the  
+ * System we are running on. This could be plain JPA Persistence or might
+ * be retrieved from the EJB container. 
+ */
+public interface JPAService
+{
+
+    /**
+     * get the EntityManagerFactory with the given name.
+     * @param unitName JPA persistence unit name
+     * @return EntityManagerFactory or <code>null</code> if not found
+     */
+    public abstract EntityManagerFactory getPersistenceUnit(String unitName);
+
+    /**
+     * Get a transactional EntityManager for the current thread using a 
+     * ThreadLocal.
+     * TODO: from the SPEC: the EntityManger must have dependent scope, but this 
+     * does not make sense for e.g. &#x0040;ApplicationScoped.
+     * @param unitName the name of the persistence unit. Can be empty or <code>null</code>
+     * @param name the name of the EntityManager. Can be empty or <code>null</code>
+     * @return a transactional EntityManager
+     */
+    public abstract EntityManager getPersistenceContext(String unitName, String name);
+
+}
\ No newline at end of file

Added: incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/se/EntityManagersManager.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/se/EntityManagersManager.java?rev=766537&view=auto
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/se/EntityManagersManager.java (added)
+++ incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/se/EntityManagersManager.java Sun Apr 19 22:27:17 2009
@@ -0,0 +1,93 @@
+/*
+ * 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.webbeans.jpa.spi.se;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.persistence.EntityManager;
+
+public class EntityManagersManager
+{
+
+    /**
+     * key = unitName + "." + "name"
+     * value = ThreadLocal&lt;EntityManager&gt;
+     */
+    private Map<String, ThreadLocal<EntityManager>> entityManagers = new HashMap<String, ThreadLocal<EntityManager>>();
+    
+    
+    /**
+     * @param unitName
+     * @param name
+     * @return the EntityManager or <code>null</code> if not yet set
+     */
+    public EntityManager get(String unitName, String name)
+    {
+        EntityManager em = null;
+        String key = getKey(unitName, name);
+        
+        ThreadLocal<EntityManager> tl = entityManagers.get(key);
+        if (tl != null)
+        {
+            return tl.get();
+        }
+        
+        return em;
+    }
+
+    /**
+     * Set the given EntityManager for the current Thread.
+     * This will replace any previously stored EntityManager for this
+     * unitName and name.
+     *  
+     * @param unitName
+     * @param name
+     * @param em
+     */
+    public void set(String unitName, String name, EntityManager em)
+    {
+        String key = getKey(unitName, name);
+
+        ThreadLocal<EntityManager> tl = entityManagers.get(key);
+        if (tl == null)
+        {
+            tl = new ThreadLocal<EntityManager>();
+            entityManagers.put(key, tl);
+        }
+
+        tl.set(em);
+    }
+
+    
+    /**
+     * construct the key for the {@link #entityManagers} Map
+     * @return String key for the map
+     */
+    private String getKey(String unitName, String name)
+    {
+        if (unitName == null)
+        {
+            unitName = "";
+        }
+        
+        if (name == null)
+        {
+            name = "";
+        }
+        
+        return unitName + "." + name;
+    }
+    
+}

Added: incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/se/JPAServicePersistenceImpl.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/se/JPAServicePersistenceImpl.java?rev=766537&view=auto
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/se/JPAServicePersistenceImpl.java (added)
+++ incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/se/JPAServicePersistenceImpl.java Sun Apr 19 22:27:17 2009
@@ -0,0 +1,63 @@
+/*
+ * 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.webbeans.jpa.spi.se;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.Persistence;
+
+import org.apache.webbeans.config.WebBeansFinder;
+import org.apache.webbeans.jpa.spi.JPAService;
+import org.apache.webbeans.jpa.spi.se.EntityManagersManager;
+
+/**
+ * SPI Implementation which accesses the PersistenceManagerFactory via the Persistence class
+ */
+public class JPAServicePersistenceImpl implements JPAService
+{
+    
+    public static final String SINGLETON_WEBBEANS_ENTITYMANAGERS_MAP = EntityManagersManager.class.getName();
+
+    /**
+     * {@inheritDoc}
+     * 
+     */
+    public EntityManagerFactory getPersistenceUnit(String unitName)
+    {
+        //X TODO this currently ignores JNDI
+        EntityManagerFactory emf = Persistence.createEntityManagerFactory(unitName);
+            
+        return emf;
+    }
+
+    /** 
+     * {@inheritDoc}
+     * TODO: currently this returns an extended EntityManager, so we have to wrap it
+     */
+    public EntityManager getPersistenceContext(String unitName, String name)
+    {
+        EntityManagerFactory emf = getPersistenceUnit(unitName);
+        
+        EntityManagersManager entityManagersMgr = (EntityManagersManager) WebBeansFinder.getSingletonInstance(SINGLETON_WEBBEANS_ENTITYMANAGERS_MAP);
+        
+        EntityManager em = entityManagersMgr.get(unitName, name);
+        if (em == null)
+        {
+            em = emf.createEntityManager();
+            entityManagersMgr.set(unitName, name, em);
+        }
+        
+        return em;
+    }
+}

Added: incubator/openwebbeans/trunk/webbeans-jpa/src/main/resources/META-INF/services/org.apache.webbeans.plugins.OpenWebBeansPlugin
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-jpa/src/main/resources/META-INF/services/org.apache.webbeans.plugins.OpenWebBeansPlugin?rev=766537&view=auto
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-jpa/src/main/resources/META-INF/services/org.apache.webbeans.plugins.OpenWebBeansPlugin (added)
+++ incubator/openwebbeans/trunk/webbeans-jpa/src/main/resources/META-INF/services/org.apache.webbeans.plugins.OpenWebBeansPlugin Sun Apr 19 22:27:17 2009
@@ -0,0 +1,4 @@
+# this file contains the service implementation for the 
+# JSF integration into OpenWebBeans
+# this file contains information for java.util.ServiceLoader
+org.apache.webbeans.jpa.plugin.OpenWebBeansJpaPlugin
\ No newline at end of file

Added: incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/test/component/resource/TstResourcePersistenceBean.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/test/component/resource/TstResourcePersistenceBean.java?rev=766537&view=auto
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/test/component/resource/TstResourcePersistenceBean.java (added)
+++ incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/test/component/resource/TstResourcePersistenceBean.java Sun Apr 19 22:27:17 2009
@@ -0,0 +1,80 @@
+/*
+ * 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.webbeans.test.component.resource;
+
+import javax.annotation.Named;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.PersistenceContext;
+import javax.persistence.PersistenceUnit;
+
+/**
+ * This simple web bean gets a &#x0040;PersistenceContext injected 
+ *
+ */
+@Named
+public class TstResourcePersistenceBean
+{
+
+    @PersistenceUnit(unitName="openwebbeanstest")
+    private EntityManagerFactory emf;
+
+    @PersistenceContext(unitName="openwebbeanstest")
+    private EntityManager em;
+
+    @PersistenceContext(unitName="openwebbeanstest", name="anotherEm")
+    private EntityManager emNamed;
+
+    private EntityManagerFactory emfMethodInjected;
+
+    private EntityManager emMethodInjected;
+
+    public EntityManagerFactory getFieldInjectedEntityManagerFactory()
+    {
+        return emf;
+    }
+
+    public EntityManager getFieldInjectedEntityManager()
+    {
+        return em;
+    }
+    
+    public EntityManager getFieldInjectedEntityManager2()
+    {
+        return emNamed;
+    }
+
+    public EntityManagerFactory getMethodInjectedEntityManagerFactory()
+    {
+        return emfMethodInjected;
+    }
+
+    @PersistenceUnit(unitName="openwebbeanstest")
+    public void setMethodInjectedEntityManagerFactory(EntityManagerFactory emf2)
+    {
+        this.emfMethodInjected = emf2;
+    }
+
+    public EntityManager getMethodInjectedEntityManager()
+    {
+        return emMethodInjected;
+    }
+
+    @PersistenceContext(unitName="openwebbeanstest")
+    public void setMethodInjectedEntityManager(EntityManager em3)
+    {
+        this.emMethodInjected = em3;
+    }
+
+}

Added: incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/test/unittests/resource/ResourceInjectionTest.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/test/unittests/resource/ResourceInjectionTest.java?rev=766537&view=auto
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/test/unittests/resource/ResourceInjectionTest.java (added)
+++ incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/test/unittests/resource/ResourceInjectionTest.java Sun Apr 19 22:27:17 2009
@@ -0,0 +1,77 @@
+/*
+ * 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.webbeans.test.unittests.resource;
+
+import javax.inject.manager.Manager;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+
+import org.apache.webbeans.component.AbstractComponent;
+import org.apache.webbeans.container.ManagerImpl;
+import org.apache.webbeans.context.ContextFactory;
+import org.apache.webbeans.test.component.resource.TstResourcePersistenceBean;
+import org.apache.webbeans.test.servlet.TestContext;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public class ResourceInjectionTest extends TestContext
+{
+    Manager container = null;
+
+    public ResourceInjectionTest()
+    {
+        super(ResourceInjectionTest.class.getSimpleName());
+    }
+    
+    @Before
+    public void init()
+    {
+        super.init();
+        this.container = ManagerImpl.getManager();
+        clear();
+    }
+
+    @Test
+    public void testPersistenceContextInjection() throws Exception
+    {
+        AbstractComponent<?> tstComponent = defineSimpleWebBean(TstResourcePersistenceBean.class);
+        Assert.assertNotNull(tstComponent);
+        
+        ContextFactory.initRequestContext(null);
+        ContextFactory.initApplicationContext(null);
+        
+        TstResourcePersistenceBean persBean = (TstResourcePersistenceBean) getInstanceByName("tstResourcePersistenceBean");
+        Assert.assertNotNull(persBean);
+        
+        // test field injection
+        EntityManagerFactory emf = persBean.getFieldInjectedEntityManagerFactory();
+        Assert.assertNotNull(emf);
+
+        EntityManager em = persBean.getFieldInjectedEntityManager();
+        Assert.assertNotNull(em);
+    
+        EntityManager em2 = persBean.getFieldInjectedEntityManager2();
+        Assert.assertNotNull(em2);
+
+        Assert.assertTrue(em != em2);
+        
+        // test method injection
+        EntityManagerFactory emf2 = persBean.getMethodInjectedEntityManagerFactory();
+        Assert.assertNotNull(emf2);
+        
+        EntityManager em3 = persBean.getMethodInjectedEntityManager();
+        Assert.assertNotNull(em3);
+    }
+}