openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r740022 - in /incubator/openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/inject/ main/java/org/apache/webbeans/jpa/ test/java/org/apache/webbeans/test/component/resource/ test/java/org/apache/webbeans/test/unittests/resou...
Date Mon, 02 Feb 2009 16:22:12 GMT
Author: struberg
Date: Mon Feb  2 16:22:12 2009
New Revision: 740022

URL: http://svn.apache.org/viewvc?rev=740022&view=rev
Log:
OWB-74 allow multiple EntityManagers with different name and unitName

Added:
    incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/jpa/EntityManagersManager.java
Modified:
    incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/AbstractInjectable.java
    incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/jpa/JPAUtil.java
    incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/resource/TstResourcePersistenceBean.java
    incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/resource/ResourceInjectionTest.java

Modified: incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/AbstractInjectable.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/AbstractInjectable.java?rev=740022&r1=740021&r2=740022&view=diff
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/AbstractInjectable.java
(original)
+++ incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/AbstractInjectable.java
Mon Feb  2 16:22:12 2009
@@ -176,9 +176,10 @@
         {
             PersistenceContext pc = (PersistenceContext) annot;
             String unitName = pc.unitName();
+            String name = pc.name();
             
             //X TODO what if the EntityManager is null?
-            return JPAUtil.getPersistenceContext(unitName);
+            return JPAUtil.getPersistenceContext(unitName, name);
         }
 
         return ret;

Added: incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/jpa/EntityManagersManager.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/jpa/EntityManagersManager.java?rev=740022&view=auto
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/jpa/EntityManagersManager.java
(added)
+++ incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/jpa/EntityManagersManager.java
Mon Feb  2 16:22:12 2009
@@ -0,0 +1,80 @@
+package org.apache.webbeans.jpa;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.persistence.EntityManager;
+
+public class EntityManagersManager
+{
+
+    /**
+     * key = unitName + "." + "name"
+     * value = ThreadLocal<EntityManager>
+     */
+    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;
+    }
+    
+}

Modified: incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/jpa/JPAUtil.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/jpa/JPAUtil.java?rev=740022&r1=740021&r2=740022&view=diff
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/jpa/JPAUtil.java
(original)
+++ incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/jpa/JPAUtil.java
Mon Feb  2 16:22:12 2009
@@ -13,16 +13,23 @@
  */
 package org.apache.webbeans.jpa;
 
+import java.util.Map;
+
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.Persistence;
 
+import org.apache.webbeans.config.WebBeansFinder;
+
 /**
  * Helper class which contains various static functions for accessing JPA functionality.
  */
 public class JPAUtil
 {
 
+    
+    public static final String SINGLETON_WEBBEANS_ENTITYMANAGERS_MAP = "org.apache.webbeans.jpa.EntityManagersManager";
+
     /**
      * get the EntityManagerFactory with the given name.
      * TODO: this should later be implemented via a SPI and either look at 
@@ -44,19 +51,21 @@
      * TODO: from the SPEC: the EntityManger must have dependent scope, but this 
      * does not make sense for e.g. &#x0040;ApplicationScoped.
      * TODO: currently this returns an extended EntityManager, so we have to wrap it
-     * @param unitName
+     * @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 static EntityManager getPersistenceContext(String unitName)
+    public static EntityManager getPersistenceContext(String unitName, String name)
     {
         EntityManagerFactory emf = getPersistenceUnit(unitName);
-        ThreadLocal<EntityManager> tl = new ThreadLocal<EntityManager>();
         
-        EntityManager em = tl.get();
+        EntityManagersManager entityManagersMgr = (EntityManagersManager) WebBeansFinder.getSingletonInstance(SINGLETON_WEBBEANS_ENTITYMANAGERS_MAP);
+        
+        EntityManager em = entityManagersMgr.get(unitName, name);
         if (em == null)
         {
             em = emf.createEntityManager();
-            tl.set(em);
+            entityManagersMgr.set(unitName, name, em);
         }
         
         return em;

Modified: incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/resource/TstResourcePersistenceBean.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/resource/TstResourcePersistenceBean.java?rev=740022&r1=740021&r2=740022&view=diff
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/resource/TstResourcePersistenceBean.java
(original)
+++ incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/resource/TstResourcePersistenceBean.java
Mon Feb  2 16:22:12 2009
@@ -32,7 +32,10 @@
 
     @PersistenceContext(unitName="openwebbeanstest")
     private EntityManager em;
-    
+
+    @PersistenceContext(unitName="openwebbeanstest", name="anotherEm")
+    private EntityManager em2;
+
     public EntityManagerFactory getEntityManagerFactory()
     {
         return emf;
@@ -43,4 +46,9 @@
         return em;
     }
     
+    public EntityManager getEntityManager2()
+    {
+        return em2;
+    }
+    
 }

Modified: incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/resource/ResourceInjectionTest.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/resource/ResourceInjectionTest.java?rev=740022&r1=740021&r2=740022&view=diff
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/resource/ResourceInjectionTest.java
(original)
+++ incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/resource/ResourceInjectionTest.java
Mon Feb  2 16:22:12 2009
@@ -60,5 +60,10 @@
 
         EntityManager em = persBean.getEntityManager();
         Assert.assertNotNull(em);
-     }
+    
+        EntityManager em2 = persBean.getEntityManager2();
+        Assert.assertNotNull(em2);
+
+        Assert.assertTrue(em != em2);
+    }
 }



Mime
View raw message