aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cschnei...@apache.org
Subject svn commit: r1694015 - in /aries/trunk/jpa: itests/jpa-container-itest/ itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/container/itest/ itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/itest/ jpa-api/src/main/java/org/apache...
Date Tue, 04 Aug 2015 08:52:03 GMT
Author: cschneider
Date: Tue Aug  4 08:52:02 2015
New Revision: 1694015

URL: http://svn.apache.org/r1694015
Log:
[ARIES-1346] Hold a reference to coordination to avoid it to be considered ORPHANED

Added:
    aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/impl/JPAParticipant.java
Modified:
    aries/trunk/jpa/itests/jpa-container-itest/pom.xml
    aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/container/itest/JPAContainerTest.java
    aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/itest/AbstractJPAItest.java
    aries/trunk/jpa/jpa-api/src/main/java/org/apache/aries/jpa/supplier/EmSupplier.java
    aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/impl/JpaBeanProcessor.java
    aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/impl/JpaInterceptor.java
    aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/supplier/impl/ServiceProxy.java
    aries/trunk/jpa/jpa-support/src/main/java/org/apache/aries/jpa/support/impl/EMSupplierImpl.java
    aries/trunk/jpa/jpa-support/src/main/java/org/apache/aries/jpa/support/impl/XAJpaTemplate.java

Modified: aries/trunk/jpa/itests/jpa-container-itest/pom.xml
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/itests/jpa-container-itest/pom.xml?rev=1694015&r1=1694014&r2=1694015&view=diff
==============================================================================
--- aries/trunk/jpa/itests/jpa-container-itest/pom.xml (original)
+++ aries/trunk/jpa/itests/jpa-container-itest/pom.xml Tue Aug  4 08:52:02 2015
@@ -49,9 +49,9 @@
             <version>5.0.0</version>
         </dependency>
         <dependency>
-            <groupId>org.eclipse.equinox</groupId>
-            <artifactId>org.eclipse.equinox.coordinator</artifactId>
-            <version>1.1.0.v20120522-1841</version>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.coordinator</artifactId>
+            <version>1.0.0</version>
             <scope>test</scope>
         </dependency>
         <dependency>

Modified: aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/container/itest/JPAContainerTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/container/itest/JPAContainerTest.java?rev=1694015&r1=1694014&r2=1694015&view=diff
==============================================================================
--- aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/container/itest/JPAContainerTest.java
(original)
+++ aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/container/itest/JPAContainerTest.java
Tue Aug  4 08:52:02 2015
@@ -21,6 +21,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import javax.inject.Inject;
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.Query;
@@ -30,9 +31,13 @@ import org.apache.aries.jpa.container.it
 import org.apache.aries.jpa.itest.AbstractCarJPAITest;
 import org.apache.aries.jpa.supplier.EmSupplier;
 import org.junit.Test;
+import org.osgi.service.coordinator.Coordination;
+import org.osgi.service.coordinator.Coordinator;
 import org.osgi.service.jpa.EntityManagerFactoryBuilder;
 
 public abstract class JPAContainerTest extends AbstractCarJPAITest {
+    @Inject
+    Coordinator coordinator;
 
     @Test
     public void testCarEMFBuilder() throws Exception {
@@ -71,8 +76,8 @@ public abstract class JPAContainerTest e
     @Test
     public void testEmSupplier() throws Exception {
         EmSupplier emSupplier = getService(EmSupplier.class, "(osgi.unit.name=" + XA_TEST_UNIT
+ ")");
+        Coordination coordination = coordinator.begin("test", 0);
         try {
-            emSupplier.preCall();
             EntityManager em = emSupplier.get();
             carLifecycleXA(ut, em);
 
@@ -110,7 +115,7 @@ public abstract class JPAContainerTest e
 
             cleanup(em);
         } finally {
-            emSupplier.postCall();
+            coordination.end();
         }
     }
 

Modified: aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/itest/AbstractJPAItest.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/itest/AbstractJPAItest.java?rev=1694015&r1=1694014&r2=1694015&view=diff
==============================================================================
--- aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/itest/AbstractJPAItest.java
(original)
+++ aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/itest/AbstractJPAItest.java
Tue Aug  4 08:52:02 2015
@@ -185,7 +185,7 @@ public abstract class AbstractJPAItest {
 
                              mvnBundle("org.ow2.asm", "asm-all"),
                              mvnBundle("org.apache.felix", "org.apache.felix.configadmin"),
-                             mvnBundle("org.eclipse.equinox", "org.eclipse.equinox.coordinator"),
+                             mvnBundle("org.apache.felix", "org.apache.felix.coordinator"),
 
                              mvnBundle("org.apache.aries.proxy", "org.apache.aries.proxy.api"),
                              mvnBundle("org.apache.aries.proxy", "org.apache.aries.proxy.impl"),

Modified: aries/trunk/jpa/jpa-api/src/main/java/org/apache/aries/jpa/supplier/EmSupplier.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/jpa-api/src/main/java/org/apache/aries/jpa/supplier/EmSupplier.java?rev=1694015&r1=1694014&r2=1694015&view=diff
==============================================================================
--- aries/trunk/jpa/jpa-api/src/main/java/org/apache/aries/jpa/supplier/EmSupplier.java (original)
+++ aries/trunk/jpa/jpa-api/src/main/java/org/apache/aries/jpa/supplier/EmSupplier.java Tue
Aug  4 08:52:02 2015
@@ -30,13 +30,17 @@ public interface EmSupplier {
 
     /**
      * Is called before first access to get() in a method
+     * @deprecated Use a Coordination instead
      */
+    @Deprecated
     void preCall();
     
     EntityManager get();
 
     /**
      * Is called after last access to get() in a method
+     * @deprecated Use a Coordination instead
      */
+    @Deprecated
     void postCall();
 }

Added: aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/impl/JPAParticipant.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/impl/JPAParticipant.java?rev=1694015&view=auto
==============================================================================
--- aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/impl/JPAParticipant.java
(added)
+++ aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/impl/JPAParticipant.java
Tue Aug  4 08:52:02 2015
@@ -0,0 +1,50 @@
+/*
+ * 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.aries.jpa.blueprint.impl;
+
+import javax.persistence.EntityManager;
+
+import org.osgi.service.coordinator.Coordination;
+import org.osgi.service.coordinator.Participant;
+
+final class JPAParticipant implements Participant {
+    private final EntityManager em;
+
+    JPAParticipant(EntityManager em) {
+        this.em = em;
+    }
+
+    @Override
+    public void failed(Coordination coordination) throws Exception {
+        em.getTransaction().setRollbackOnly();
+    }
+
+    @Override
+    public void ended(Coordination coordination) throws Exception {
+        if (em.getTransaction().getRollbackOnly()) {
+            try {
+                em.getTransaction().rollback();
+             } catch (Exception e1) {
+                 // Ignore
+             }
+        } else {
+            em.getTransaction().commit();
+        }
+    }
+}
\ No newline at end of file

Modified: aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/impl/JpaBeanProcessor.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/impl/JpaBeanProcessor.java?rev=1694015&r1=1694014&r2=1694015&view=diff
==============================================================================
--- aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/impl/JpaBeanProcessor.java
(original)
+++ aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/impl/JpaBeanProcessor.java
Tue Aug  4 08:52:02 2015
@@ -44,6 +44,7 @@ import org.apache.aries.jpa.supplier.EmS
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.FrameworkUtil;
 import org.osgi.service.blueprint.reflect.BeanMetadata;
+import org.osgi.service.coordinator.Coordinator;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -115,8 +116,8 @@ public class JpaBeanProcessor implements
                 EmSupplier supplierProxy = ServiceProxy.create(context, EmSupplier.class,
filter);
                 jpaAnnotatedMember.handleSupplierMember(member, pcAnn.unitName(), supplierProxy);
                 beanProxies.add((Closeable)supplierProxy);
-
-                Interceptor interceptor = new JpaInterceptor(supplierProxy);
+                Coordinator coordinator = ServiceProxy.create(context, Coordinator.class);
+                Interceptor interceptor = new JpaInterceptor(supplierProxy, coordinator);
                 cdr.registerInterceptorWithComponent(beanData, interceptor);
             } else {
                 PersistenceUnit puAnn = member.getAnnotation(PersistenceUnit.class);

Modified: aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/impl/JpaInterceptor.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/impl/JpaInterceptor.java?rev=1694015&r1=1694014&r2=1694015&view=diff
==============================================================================
--- aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/impl/JpaInterceptor.java
(original)
+++ aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/impl/JpaInterceptor.java
Tue Aug  4 08:52:02 2015
@@ -26,16 +26,20 @@ import javax.persistence.spi.Persistence
 import org.apache.aries.blueprint.Interceptor;
 import org.apache.aries.jpa.supplier.EmSupplier;
 import org.osgi.service.blueprint.reflect.ComponentMetadata;
+import org.osgi.service.coordinator.Coordination;
+import org.osgi.service.coordinator.Coordinator;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class JpaInterceptor implements Interceptor {
     private static Logger LOG = LoggerFactory.getLogger(JpaInterceptor.class);
-    private EmSupplier emSupplier;
+    EmSupplier emSupplier;
     private Boolean cachedIsResourceLocal;
+    private Coordinator coordinator;
 
-    public JpaInterceptor(EmSupplier emSupplier) {
+    public JpaInterceptor(EmSupplier emSupplier, Coordinator coordinator) {
         this.emSupplier = emSupplier;
+        this.coordinator = coordinator;
     }
 
     public int getRank() {
@@ -45,13 +49,14 @@ public class JpaInterceptor implements I
     public Object preCall(ComponentMetadata cm, Method m, Object... parameters) throws Throwable
{
         try {
             LOG.debug("PreCall for bean {}, method {}", cm.getId(), m.getName());
-            emSupplier.preCall();
-            EntityManager em = emSupplier.get();
+            Coordination coordination = coordinator.begin("jpa", 0);
+            final EntityManager em = emSupplier.get();
             boolean weControlTx = isResourceLocal(em) && !em.getTransaction().isActive();
             if (weControlTx) {
                 em.getTransaction().begin();
+                coordination.addParticipant(new JPAParticipant(em));
             }
-            return weControlTx;
+            return coordination;
         } catch (Exception e) {
             LOG.warn("Exception from EmSupplier.preCall", e);
             throw new RuntimeException(e);
@@ -60,35 +65,16 @@ public class JpaInterceptor implements I
 
     public void postCallWithException(ComponentMetadata cm, Method m, Throwable ex, Object
preCallToken) {
         LOG.debug("PostCallWithException for bean {}, method {}", cm.getId(), m.getName(),
ex);
-        boolean weControlTx = preCallToken == null ? false : (Boolean)preCallToken;
-        if (weControlTx) {
-            safeRollback(emSupplier.get(), ex);
-        }
-        try {
-            emSupplier.postCall();
-        } catch (Exception e) {
-            LOG.warn("Exception from EmSupplier.postCall", e);
-        }
+        ((Coordination) preCallToken).fail(ex);
     }
 
     public void postCallWithReturn(ComponentMetadata cm, Method m, Object returnType, Object
preCallToken)
         throws Exception {
         LOG.debug("PostCallWithReturn for bean {}, method {}", cm.getId(), m.getName());
-        boolean weControlTx = preCallToken == null ? false : (Boolean)preCallToken;
-        if (weControlTx) {
-            emSupplier.get().getTransaction().commit();
-        }
-        emSupplier.postCall();
+        ((Coordination) preCallToken).end();
     }
 
-    private void safeRollback(EntityManager em, Throwable e) {
-        if (em != null) {
-            try {
-                em.getTransaction().rollback();
-            } catch (Exception e1) {
-            }
-        }
-    }
+
 
     private boolean isResourceLocal(EntityManager em) {
         if (cachedIsResourceLocal == null) {

Modified: aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/supplier/impl/ServiceProxy.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/supplier/impl/ServiceProxy.java?rev=1694015&r1=1694014&r2=1694015&view=diff
==============================================================================
--- aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/supplier/impl/ServiceProxy.java
(original)
+++ aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/supplier/impl/ServiceProxy.java
Tue Aug  4 08:52:02 2015
@@ -35,14 +35,16 @@ public class ServiceProxy implements Inv
     private ServiceTracker tracker;
 
     public ServiceProxy(BundleContext context, String filterS) {
-        Filter filter;
+        tracker = new ServiceTracker<>(context, createFilter(filterS), null);
+        tracker.open();
+    }
+
+    private Filter createFilter(String filterS) {
         try {
-            filter = FrameworkUtil.createFilter(filterS);
+            return filterS == null ? null : FrameworkUtil.createFilter(filterS);
         } catch (InvalidSyntaxException e) {
             throw new IllegalStateException(e);
         }
-        tracker = new ServiceTracker<>(context, filter, null);
-        tracker.open();
     }
 
     private Object getService() {
@@ -67,6 +69,14 @@ public class ServiceProxy implements Inv
             throw new IllegalStateException(e);
         }
     }
+    
+    public static <T> T create(BundleContext context, Class<T> iface) {
+        return  (T) create(context, iface, getFilter(iface));
+    }
+    
+    private static String getFilter(Class<?> clazz) {
+        return String.format("(objectClass=%s)", clazz.getName());
+    }
 
     @SuppressWarnings("unchecked")
     public static <T> T create(BundleContext context, Class<T> iface, String
filter) {

Modified: aries/trunk/jpa/jpa-support/src/main/java/org/apache/aries/jpa/support/impl/EMSupplierImpl.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/jpa-support/src/main/java/org/apache/aries/jpa/support/impl/EMSupplierImpl.java?rev=1694015&r1=1694014&r2=1694015&view=diff
==============================================================================
--- aries/trunk/jpa/jpa-support/src/main/java/org/apache/aries/jpa/support/impl/EMSupplierImpl.java
(original)
+++ aries/trunk/jpa/jpa-support/src/main/java/org/apache/aries/jpa/support/impl/EMSupplierImpl.java
Tue Aug  4 08:52:02 2015
@@ -141,17 +141,10 @@ public class EMSupplierImpl implements E
 
     @Override
     public void preCall() {
-        coordinator.begin("jpa." + unitName, 0);
     }
 
     @Override
     public void postCall() {
-        try {
-            Coordination coord = coordinator.pop();
-            coord.end();
-        } catch (Throwable t) {
-            LOG.warn("Error ending coord", t);
-        }
     }
 
     /**

Modified: aries/trunk/jpa/jpa-support/src/main/java/org/apache/aries/jpa/support/impl/XAJpaTemplate.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/jpa-support/src/main/java/org/apache/aries/jpa/support/impl/XAJpaTemplate.java?rev=1694015&r1=1694014&r2=1694015&view=diff
==============================================================================
--- aries/trunk/jpa/jpa-support/src/main/java/org/apache/aries/jpa/support/impl/XAJpaTemplate.java
(original)
+++ aries/trunk/jpa/jpa-support/src/main/java/org/apache/aries/jpa/support/impl/XAJpaTemplate.java
Tue Aug  4 08:52:02 2015
@@ -53,7 +53,6 @@ public class XAJpaTemplate extends Abstr
         try {
             tranToken = ta.begin(tm);
             coord = coordinator.begin(this.getClass().getName(), 0);
-            emSupplier.preCall();
             em = emSupplier.get();
             em.joinTransaction();
             R result = (R)code.apply(em);



Mime
View raw message