You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by me...@apache.org on 2006/12/11 00:33:12 UTC

svn commit: r485310 - in /incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common: EntityManagerProxy.java PersistenceContextProcessor.java

Author: meerajk
Date: Sun Dec 10 15:33:11 2006
New Revision: 485310

URL: http://svn.apache.org/viewvc?view=rev&rev=485310
Log:
Added code to proxy entity manager.

Added:
    incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/EntityManagerProxy.java   (with props)
Modified:
    incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceContextProcessor.java

Added: incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/EntityManagerProxy.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/EntityManagerProxy.java?view=auto&rev=485310
==============================================================================
--- incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/EntityManagerProxy.java (added)
+++ incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/EntityManagerProxy.java Sun Dec 10 15:33:11 2006
@@ -0,0 +1,85 @@
+/*
+ * 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.tuscany.service.persistence.common;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.util.Properties;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.transaction.Synchronization;
+import javax.transaction.Transaction;
+import javax.transaction.TransactionManager;
+
+/**
+ * Proxy for entity manager.
+ *
+ */
+public class EntityManagerProxy implements InvocationHandler {
+    
+    // Thread local cache of entity managers
+    private ThreadLocal<EntityManager> entityManagers = new ThreadLocal<EntityManager>();
+
+    // Properties
+    private Properties prop;
+    
+    // Entity manager factory
+    private EntityManagerFactory emf;
+    
+    // Transaction manager
+    private TransactionManager txm;
+    
+    /**
+     * Initializes the artifacts required to create an EM.
+     * 
+     * @param prop EM creation porperty overrides.
+     * @param emf Entity manager factory to use.
+     * @param txm Transaction manager to use.
+     */
+    public EntityManagerProxy(Properties prop, EntityManagerFactory emf, TransactionManager txm) {
+        this.prop = prop;
+        this.txm = txm;
+        this.emf = emf;
+    }
+    
+    /**
+     * Proxies the entity manager.
+     */
+    public Object invoke(Object target, Method method, Object[] parameters) throws Throwable {
+        
+        EntityManager entityManager = entityManagers.get();
+        if(entityManager != null) {
+            entityManager = emf.createEntityManager(prop);
+            entityManagers.set(entityManager);
+            Transaction tx = txm.getTransaction();
+            if(tx != null) {
+                tx.registerSynchronization(new Synchronization() {
+                    public void afterCompletion(int arg0) {
+                        entityManagers.set(null);
+                    }
+                    public void beforeCompletion() {
+                    }
+                });
+            }
+        }
+        return method.invoke(entityManager, parameters);
+    }
+
+}

Propchange: incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/EntityManagerProxy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/EntityManagerProxy.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceContextProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceContextProcessor.java?view=diff&rev=485310&r1=485309&r2=485310
==============================================================================
--- incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceContextProcessor.java (original)
+++ incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceContextProcessor.java Sun Dec 10 15:33:11 2006
@@ -18,6 +18,8 @@
  */
 package org.apache.tuscany.service.persistence.common;
 
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Proxy;
 import java.util.Properties;
 
 import javax.persistence.EntityManager;
@@ -25,6 +27,7 @@
 import javax.persistence.PersistenceContext;
 import javax.persistence.PersistenceContextType;
 import javax.persistence.PersistenceProperty;
+import javax.transaction.TransactionManager;
 
 import org.apache.tuscany.spi.ObjectFactory;
 import org.apache.tuscany.spi.annotation.Autowire;
@@ -40,6 +43,10 @@
  *
  */
 public class PersistenceContextProcessor extends AbstractPropertyProcessor<PersistenceContext> {
+    
+    /** Transaction Manager */
+    @Autowire
+    private TransactionManager transactionManager;
 
     /** Persistence unit builder */
     private PersistenceUnitBuilder builder = new DefaultPersistenceUnitBuilder();
@@ -92,17 +99,21 @@
             
             PersistenceContextType type = annotation.type();
             if(type == PersistenceContextType.TRANSACTION) {
-                // TODO This needs to be proxied
+                
                 Properties props = new Properties();
                 for(PersistenceProperty property : annotation.properties()) {
                     props.put(property.name(), property.value());
                 }
-                EntityManager em = emf.createEntityManager(props);
-                em.joinTransaction();
+                
+                Class[] interfaces = new Class[] {EntityManager.class};
+                InvocationHandler handler = new EntityManagerProxy(props, emf, transactionManager);
+                EntityManager em = (EntityManager)Proxy.newProxyInstance(getClass().getClassLoader(), interfaces, handler);
                 return em;
+                
             } else {
                 throw new UnsupportedOperationException("Extended persistence contexts not supported");
             }
+            
         }
         
     }



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org