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/12/11 01:40:44 UTC

svn commit: r889483 - in /incubator/openwebbeans/trunk: ./ distribution/ webbeans-geronimo/src/main/java/org/apache/webbeans/spi/ee/openejb/ webbeans-geronimo/src/main/java/org/apache/webbeans/spi/ee/openejb/jpa/ webbeans-geronimo/src/test/java/org/apa...

Author: struberg
Date: Fri Dec 11 00:40:43 2009
New Revision: 889483

URL: http://svn.apache.org/viewvc?rev=889483&view=rev
Log:
OWB-188 cleanup webbeans-resource and webbeans-geronimo.

I removed unused code and moved the functionality of the obsolete JPAService to the ResourceServices.

Removed:
    incubator/openwebbeans/trunk/webbeans-geronimo/src/main/java/org/apache/webbeans/spi/ee/openejb/jpa/
    incubator/openwebbeans/trunk/webbeans-resource/src/main/java/org/apache/webbeans/resource/spi/se/EntityManagersManager.java
    incubator/openwebbeans/trunk/webbeans-resource/src/main/java/org/apache/webbeans/resource/spi/se/JPAServicePersistenceImpl.java
Modified:
    incubator/openwebbeans/trunk/distribution/pom.xml
    incubator/openwebbeans/trunk/pom.xml
    incubator/openwebbeans/trunk/webbeans-geronimo/src/main/java/org/apache/webbeans/spi/ee/openejb/ResourceFactory.java
    incubator/openwebbeans/trunk/webbeans-geronimo/src/main/java/org/apache/webbeans/spi/ee/openejb/ResourceInjectionProcessor.java
    incubator/openwebbeans/trunk/webbeans-geronimo/src/test/java/org/apache/webbeans/test/geronimo/openejb/OpenEJBIntegrationTest.java
    incubator/openwebbeans/trunk/webbeans-geronimo/src/test/resources/META-INF/openwebbeans/openwebbeans.properties
    incubator/openwebbeans/trunk/webbeans-resource/src/main/java/org/apache/webbeans/resource/spi/se/ResourceServiceImpl.java

Modified: incubator/openwebbeans/trunk/distribution/pom.xml
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/distribution/pom.xml?rev=889483&r1=889482&r2=889483&view=diff
==============================================================================
--- incubator/openwebbeans/trunk/distribution/pom.xml (original)
+++ incubator/openwebbeans/trunk/distribution/pom.xml Fri Dec 11 00:40:43 2009
@@ -136,11 +136,6 @@
         </dependency>
         <dependency>
             <groupId>${project.groupId}</groupId>
-            <artifactId>openwebbeans-jpa</artifactId>
-            <version>${project.version}</version>
-        </dependency>
-        <dependency>
-            <groupId>${project.groupId}</groupId>
             <artifactId>openwebbeans-ejb</artifactId>
             <version>${project.version}</version>
         </dependency>

Modified: incubator/openwebbeans/trunk/pom.xml
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/pom.xml?rev=889483&r1=889482&r2=889483&view=diff
==============================================================================
--- incubator/openwebbeans/trunk/pom.xml (original)
+++ incubator/openwebbeans/trunk/pom.xml Fri Dec 11 00:40:43 2009
@@ -338,7 +338,6 @@
         <module>webbeans-ejb</module>
         <module>webbeans-jms</module>
         <module>webbeans-jsf</module>
-        <module>webbeans-jpa</module>
         <module>webbeans-resource</module>
         <module>webbeans-geronimo</module>
         <module>webbeans-porting</module>
@@ -543,12 +542,6 @@
 
 
 			<dependency>
-				<groupId>org.apache.openwebbeans</groupId>
-				<artifactId>openwebbeans-jpa</artifactId>
-				<version>${openwebbeans.version}</version>
-			</dependency>
-
-			<dependency>
 				<groupId>org.apache.openejb</groupId>
 				<artifactId>openejb-core</artifactId>
 				<version>3.1.1</version>

Modified: incubator/openwebbeans/trunk/webbeans-geronimo/src/main/java/org/apache/webbeans/spi/ee/openejb/ResourceFactory.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-geronimo/src/main/java/org/apache/webbeans/spi/ee/openejb/ResourceFactory.java?rev=889483&r1=889482&r2=889483&view=diff
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-geronimo/src/main/java/org/apache/webbeans/spi/ee/openejb/ResourceFactory.java (original)
+++ incubator/openwebbeans/trunk/webbeans-geronimo/src/main/java/org/apache/webbeans/spi/ee/openejb/ResourceFactory.java Fri Dec 11 00:40:43 2009
@@ -58,7 +58,7 @@
         {
             return this.processor.getResourceObject(field);   
             
-        }catch(Exception e)
+        } catch(Exception e)
         {
             throw new RuntimeException(e);
         }

Modified: incubator/openwebbeans/trunk/webbeans-geronimo/src/main/java/org/apache/webbeans/spi/ee/openejb/ResourceInjectionProcessor.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-geronimo/src/main/java/org/apache/webbeans/spi/ee/openejb/ResourceInjectionProcessor.java?rev=889483&r1=889482&r2=889483&view=diff
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-geronimo/src/main/java/org/apache/webbeans/spi/ee/openejb/ResourceInjectionProcessor.java (original)
+++ incubator/openwebbeans/trunk/webbeans-geronimo/src/main/java/org/apache/webbeans/spi/ee/openejb/ResourceInjectionProcessor.java Fri Dec 11 00:40:43 2009
@@ -18,25 +18,29 @@
 
 import javax.annotation.Resource;
 import javax.ejb.EJB;
+import javax.enterprise.inject.CreationException;
 import javax.naming.Context;
+import javax.naming.NameClassPair;
+import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
 import javax.persistence.PersistenceContext;
 import javax.persistence.PersistenceUnit;
 import javax.xml.ws.WebServiceRef;
 
-import org.apache.webbeans.spi.ee.openejb.jpa.JPAServiceOpenEJBImpl;
+import org.apache.openejb.loader.SystemInstance;
+import org.apache.openejb.persistence.JtaEntityManager;
+import org.apache.openejb.persistence.JtaEntityManagerRegistry;
 
 
 class ResourceInjectionProcessor
 {
     private Context context = null;
 
-    private JPAServiceOpenEJBImpl jpaService = null;
-    
     ResourceInjectionProcessor(Context context)
     {
         this.context = context;
-        this.jpaService = new JPAServiceOpenEJBImpl(this.context);
     }
 
     public Object getResourceObject(Field field) throws IllegalAccessException, InvocationTargetException, NamingException
@@ -73,7 +77,7 @@
         {
             PersistenceContext annotation = field.getAnnotation(PersistenceContext.class);
             
-            return this.jpaService.getPersistenceContext(annotation.unitName());
+            return this.getPersistenceContext(context, annotation.unitName());
             
         }
         
@@ -81,7 +85,7 @@
         {
             PersistenceUnit annotation = field.getAnnotation(PersistenceUnit.class);
             
-            return this.jpaService.getPersistenceUnit(annotation.unitName());
+            return this.getPersistenceUnit(context, annotation.unitName());
         }
         
         return null;
@@ -101,4 +105,59 @@
         
         return lookedupResource;
     }    
+    
+    private  EntityManager getPersistenceContext(Context context, String unitName) {
+        // get JtaEntityManagerRegistry
+        JtaEntityManagerRegistry jtaEntityManagerRegistry = SystemInstance.get().getComponent(JtaEntityManagerRegistry.class);
+
+        EntityManagerFactory factory = getPersistenceUnit(context, unitName);
+
+        JtaEntityManager jtaEntityManager = new JtaEntityManager(jtaEntityManagerRegistry, factory, null, false);
+
+        return jtaEntityManager;
+    }
+
+    private  EntityManagerFactory getPersistenceUnit(Context context, String unitName) {
+        EntityManagerFactory factory;
+        try {
+
+            NamingEnumeration<NameClassPair> persUnits = context.list("java:openejb/PersistenceUnit");
+            
+            if (persUnits == null)
+            {
+                throw new CreationException("No PersistenceUnit found in java:openejb/PersistenceUnit!");
+            }
+
+            String shortestMatch = null;
+            
+            while (persUnits.hasMore())
+            {
+                NameClassPair puNc = persUnits.next();
+                
+                if (puNc.getName().startsWith(unitName))
+                {
+                    if (shortestMatch == null || shortestMatch.length() > puNc.getName().length())
+                    {
+                        shortestMatch = puNc.getName();
+                    }
+                }
+                
+            }
+            
+            if (shortestMatch == null)
+            {
+                throw new CreationException("PersistenceUnit '" + unitName + "' not found");
+            }
+            
+            factory = (EntityManagerFactory) context.lookup("java:openejb/PersistenceUnit/" + shortestMatch);
+            
+            
+        } catch (NamingException e) {
+            throw new CreationException("PersistenceUnit '" + unitName + "' not found", e );
+        }
+        return factory;
+    }
+
+
+
 }

Modified: incubator/openwebbeans/trunk/webbeans-geronimo/src/test/java/org/apache/webbeans/test/geronimo/openejb/OpenEJBIntegrationTest.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-geronimo/src/test/java/org/apache/webbeans/test/geronimo/openejb/OpenEJBIntegrationTest.java?rev=889483&r1=889482&r2=889483&view=diff
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-geronimo/src/test/java/org/apache/webbeans/test/geronimo/openejb/OpenEJBIntegrationTest.java (original)
+++ incubator/openwebbeans/trunk/webbeans-geronimo/src/test/java/org/apache/webbeans/test/geronimo/openejb/OpenEJBIntegrationTest.java Fri Dec 11 00:40:43 2009
@@ -19,10 +19,12 @@
 import javax.naming.InitialContext;
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
+import javax.persistence.PersistenceContext;
+import javax.persistence.PersistenceUnit;
 
 import junit.framework.Assert;
 
-import org.apache.webbeans.spi.ee.openejb.jpa.JPAServiceOpenEJBImpl;
+import org.apache.webbeans.spi.ee.openejb.ResourceFactory;
 import org.junit.Test;
 
 
@@ -45,14 +47,19 @@
         
         Context context = new InitialContext(p);
 
-        //X Movies movies = (Movies) context.lookup("MoviesLocal");
-        JPAServiceOpenEJBImpl jpaService = new JPAServiceOpenEJBImpl(context); 
+        ResourceFactory rf = ResourceFactory.getInstance();
         
-        EntityManagerFactory emf = jpaService.getPersistenceUnit( "TestUnit" );
+        Class<TestBean> c = TestBean.class;
+        EntityManagerFactory emf = (EntityManagerFactory) rf.getResourceObject(c.getField("emf"));
         Assert.assertNotNull( emf );
         
-        EntityManager em = jpaService.getPersistenceContext("TestUnit");
+        EntityManager em = (EntityManager) rf.getResourceObject(c.getField("em"));
         Assert.assertNotNull( em );
     }
 
+    public class TestBean 
+    {
+    	public @PersistenceUnit(unitName="TestUnit") EntityManagerFactory emf;
+    	public @PersistenceContext(unitName="TestUnit") EntityManager em;
+    }
 }

Modified: incubator/openwebbeans/trunk/webbeans-geronimo/src/test/resources/META-INF/openwebbeans/openwebbeans.properties
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-geronimo/src/test/resources/META-INF/openwebbeans/openwebbeans.properties?rev=889483&r1=889482&r2=889483&view=diff
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-geronimo/src/test/resources/META-INF/openwebbeans/openwebbeans.properties (original)
+++ incubator/openwebbeans/trunk/webbeans-geronimo/src/test/resources/META-INF/openwebbeans/openwebbeans.properties Fri Dec 11 00:40:43 2009
@@ -35,9 +35,6 @@
 # lookup the javax.transaction.TransactionManager via JNDI as default 
 org.apache.webbeans.spi.TransactionService=org.apache.webbeans.spi.ee.TransactionServiceJndiImpl
 
-# use the JPAService which retrieves the PersistenceManagerFactory from the Persistence class as default 
-org.apache.webbeans.spi.JPAService=org.apache.webbeans.spi.ee.openejb.jpa.JPAServiceOpenEJBImpl
-
 #use the web metadata as default
 org.apache.webbeans.spi.deployer.MetaDataDiscoveryService=org.apache.webbeans.spi.ee.deployer.WarMetaDataDiscoveryImpl
 

Modified: incubator/openwebbeans/trunk/webbeans-resource/src/main/java/org/apache/webbeans/resource/spi/se/ResourceServiceImpl.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-resource/src/main/java/org/apache/webbeans/resource/spi/se/ResourceServiceImpl.java?rev=889483&r1=889482&r2=889483&view=diff
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-resource/src/main/java/org/apache/webbeans/resource/spi/se/ResourceServiceImpl.java (original)
+++ incubator/openwebbeans/trunk/webbeans-resource/src/main/java/org/apache/webbeans/resource/spi/se/ResourceServiceImpl.java Fri Dec 11 00:40:43 2009
@@ -14,9 +14,14 @@
 package org.apache.webbeans.resource.spi.se;
 
 import java.lang.reflect.Field;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 import javax.annotation.Resource;
 import javax.naming.InitialContext;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.Persistence;
 import javax.persistence.PersistenceContext;
 import javax.persistence.PersistenceUnit;
 
@@ -47,19 +52,17 @@
     {
         Object obj = null;
         
-        JPAServicePersistenceImpl impl = new JPAServicePersistenceImpl();
-                
         if (field.isAnnotationPresent(PersistenceContext.class))
         {
             PersistenceContext context = field.getAnnotation(PersistenceContext.class);            
-            obj = impl.getPersistenceContext(context.unitName());
+            obj = getPersistenceContext(context.unitName());
         }
         
         else if (field.isAnnotationPresent(PersistenceUnit.class))
         {
             PersistenceUnit annotation = field.getAnnotation(PersistenceUnit.class);
             
-            obj = impl.getPersistenceUnit(annotation.unitName());
+            obj = getPersistenceUnit(annotation.unitName());
         }
         else if(field.isAnnotationPresent(Resource.class))
         {
@@ -77,4 +80,41 @@
         return obj;
     }
 
+    /**
+     *  A cache for EntityManagerFactories.
+     */
+    private Map<String, EntityManagerFactory> factoryCache = new ConcurrentHashMap<String, EntityManagerFactory>();
+
+    /**
+     * {@inheritDoc}
+     * 
+     */
+    private EntityManagerFactory getPersistenceUnit(String unitName)
+    {
+        if(factoryCache.get(unitName) != null)
+        {
+            return factoryCache.get(unitName);
+        }
+        
+        //X TODO this currently ignores JNDI
+        EntityManagerFactory emf = Persistence.createEntityManagerFactory(unitName);
+        
+        factoryCache.put(unitName, emf);
+            
+        return emf;
+    }
+
+    /** 
+     * TODO: currently this returns an extended EntityManager, so we have to wrap it
+     * We have to create a Proxy for injecting entity managers. So, whenever method is called
+     * on the entity managers, look at current Transaction, if exist call joinTransaction();
+     */
+    private EntityManager getPersistenceContext(String unitName)
+    {
+        EntityManagerFactory emf = getPersistenceUnit(unitName);        
+        EntityManager em = emf.createEntityManager();
+        
+        return em;
+    }
+
 }