aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cschnei...@apache.org
Subject svn commit: r1690676 - in /aries/trunk/jpa: examples/tasklist-blueprint/src/test/java/org/apache/aries/jpa/tasklist/blueprint/impl/ examples/tasklist-ds/src/test/java/org/apache/aries/jpa/tasklist/closure/impl/ itests/jpa-container-itest/ itests/jpa-co...
Date Mon, 13 Jul 2015 14:20:06 GMT
Author: cschneider
Date: Mon Jul 13 14:20:06 2015
New Revision: 1690676

URL: http://svn.apache.org/r1690676
Log:
[ARIES-1346] Use Coordinator to coordinate EM lifecycle

Added:
    aries/trunk/jpa/jpa-support/src/main/java/org/apache/aries/jpa/support/osgi/impl/CoordinatorTracker.java
      - copied, changed from r1690610, aries/trunk/jpa/jpa-support/src/main/java/org/apache/aries/jpa/support/osgi/impl/Activator.java
    aries/trunk/jpa/jpa-support/src/test/java/org/apache/aries/jpa/impl/DummyCoordination.java
    aries/trunk/jpa/jpa-support/src/test/java/org/apache/aries/jpa/impl/DummyCoordinator.java
Modified:
    aries/trunk/jpa/examples/tasklist-blueprint/src/test/java/org/apache/aries/jpa/tasklist/blueprint/impl/TaskServiceImplTest.java
    aries/trunk/jpa/examples/tasklist-ds/src/test/java/org/apache/aries/jpa/tasklist/closure/impl/TaskServiceImplTest.java
    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/EclipseLinkStartupTest.java
    aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/itest/AbstractJPAItest.java
    aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/supplier/impl/EmSupplierProxy.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/osgi/impl/Activator.java
    aries/trunk/jpa/jpa-support/src/main/java/org/apache/aries/jpa/support/osgi/impl/EMFTracker.java
    aries/trunk/jpa/jpa-support/src/test/java/org/apache/aries/jpa/impl/EmSupplierTest.java

Modified: aries/trunk/jpa/examples/tasklist-blueprint/src/test/java/org/apache/aries/jpa/tasklist/blueprint/impl/TaskServiceImplTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/examples/tasklist-blueprint/src/test/java/org/apache/aries/jpa/tasklist/blueprint/impl/TaskServiceImplTest.java?rev=1690676&r1=1690675&r2=1690676&view=diff
==============================================================================
--- aries/trunk/jpa/examples/tasklist-blueprint/src/test/java/org/apache/aries/jpa/tasklist/blueprint/impl/TaskServiceImplTest.java
(original)
+++ aries/trunk/jpa/examples/tasklist-blueprint/src/test/java/org/apache/aries/jpa/tasklist/blueprint/impl/TaskServiceImplTest.java
Mon Jul 13 14:20:06 2015
@@ -21,14 +21,13 @@ package org.apache.aries.jpa.tasklist.bl
 import java.util.HashMap;
 import java.util.Map;
 
+import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.Persistence;
 
 import org.apache.aries.jpa.example.tasklist.blueprint.impl.TaskServiceImpl;
 import org.apache.aries.jpa.example.tasklist.model.Task;
 import org.apache.aries.jpa.example.tasklist.model.TaskService;
-import org.apache.aries.jpa.supplier.EmSupplier;
-import org.apache.aries.jpa.support.impl.EMSupplierImpl;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -37,10 +36,9 @@ public class TaskServiceImplTest {
     public void testPersistence() {
         TaskServiceImpl taskServiceImpl = new TaskServiceImpl();
         EntityManagerFactory emf = createTestEMF();
-        EmSupplier emSupplier = new EMSupplierImpl(emf);
-        emSupplier.preCall();
-        emSupplier.get().getTransaction().begin();
-        taskServiceImpl.setEm(emSupplier.get());
+        final EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        taskServiceImpl.setEm(em);
 
         TaskService taskService = taskServiceImpl;
 
@@ -51,8 +49,8 @@ public class TaskServiceImplTest {
 
         Task task2 = taskService.getTask(1);
         Assert.assertEquals(task.getTitle(), task2.getTitle());
-        emSupplier.get().getTransaction().commit();
-        emSupplier.postCall();
+        em.getTransaction().commit();
+        em.close();
     }
 
     private EntityManagerFactory createTestEMF() {

Modified: aries/trunk/jpa/examples/tasklist-ds/src/test/java/org/apache/aries/jpa/tasklist/closure/impl/TaskServiceImplTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/examples/tasklist-ds/src/test/java/org/apache/aries/jpa/tasklist/closure/impl/TaskServiceImplTest.java?rev=1690676&r1=1690675&r2=1690676&view=diff
==============================================================================
--- aries/trunk/jpa/examples/tasklist-ds/src/test/java/org/apache/aries/jpa/tasklist/closure/impl/TaskServiceImplTest.java
(original)
+++ aries/trunk/jpa/examples/tasklist-ds/src/test/java/org/apache/aries/jpa/tasklist/closure/impl/TaskServiceImplTest.java
Mon Jul 13 14:20:06 2015
@@ -21,12 +21,13 @@ package org.apache.aries.jpa.tasklist.cl
 import java.util.HashMap;
 import java.util.Map;
 
+import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.Persistence;
 
 import org.apache.aries.jpa.example.tasklist.ds.impl.TaskServiceImpl;
 import org.apache.aries.jpa.example.tasklist.model.Task;
-import org.apache.aries.jpa.support.impl.EMSupplierImpl;
+import org.apache.aries.jpa.supplier.EmSupplier;
 import org.apache.aries.jpa.support.impl.ResourceLocalJpaTemplate;
 import org.apache.aries.jpa.template.JpaTemplate;
 import org.junit.Assert;
@@ -37,7 +38,7 @@ public class TaskServiceImplTest {
     public void testPersistence() {
         TaskServiceImpl taskService = new TaskServiceImpl();
         EntityManagerFactory emf = createTestEMF();
-        EMSupplierImpl emSupplier = new EMSupplierImpl(emf);
+        EmSupplier emSupplier = createEmSupplier(emf);
         JpaTemplate txManager = new ResourceLocalJpaTemplate(emSupplier);
         taskService.setJpaTemplate(txManager);
 
@@ -50,6 +51,22 @@ public class TaskServiceImplTest {
         Assert.assertEquals(task.getTitle(), task2.getTitle());
     }
 
+    private EmSupplier createEmSupplier(EntityManagerFactory emf) {
+        final EntityManager em = emf.createEntityManager();
+        EmSupplier emSupplier = new EmSupplier() {
+            public void preCall() {
+            }
+
+            public EntityManager get() {
+                return em;
+            }
+
+            public void postCall() {
+            }
+        };
+        return emSupplier;
+    }
+
     private EntityManagerFactory createTestEMF() {
         Map<String, String> properties = new HashMap<>();
         properties.put("javax.persistence.jdbc.driver", "org.apache.derby.jdbc.EmbeddedDriver");

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=1690676&r1=1690675&r2=1690676&view=diff
==============================================================================
--- aries/trunk/jpa/itests/jpa-container-itest/pom.xml (original)
+++ aries/trunk/jpa/itests/jpa-container-itest/pom.xml Mon Jul 13 14:20:06 2015
@@ -50,6 +50,12 @@
         </dependency>
         <dependency>
             <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.coordinator</artifactId>
+            <version>1.0.0</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
             <artifactId>org.apache.felix.configadmin</artifactId>
             <version>1.8.4</version>
             <scope>test</scope>

Modified: aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/container/itest/EclipseLinkStartupTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/container/itest/EclipseLinkStartupTest.java?rev=1690676&r1=1690675&r2=1690676&view=diff
==============================================================================
--- aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/container/itest/EclipseLinkStartupTest.java
(original)
+++ aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/container/itest/EclipseLinkStartupTest.java
Mon Jul 13 14:20:06 2015
@@ -19,8 +19,6 @@ import org.apache.aries.jpa.itest.Abstra
 import org.junit.Test;
 import org.ops4j.pax.exam.Configuration;
 import org.ops4j.pax.exam.Option;
-import org.ops4j.pax.exam.options.AbstractProvisionControl;
-import org.ops4j.pax.exam.options.MavenArtifactProvisionOption;
 
 public class EclipseLinkStartupTest extends AbstractJPAItest {
 

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=1690676&r1=1690675&r2=1690676&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
Mon Jul 13 14:20:06 2015
@@ -77,26 +77,26 @@ public abstract class AbstractJPAItest {
         }
     }
 
-	public String sanitizeFilter(String filter) {
-		return filter.startsWith("(") ? filter : "(" + filter + ")";
-	}	
-		
-    
-	/**
-	 * Helps to diagnose bundles that are not resolved as it will throw a detailed exception
-	 * 
-	 * @throws BundleException
-	 */
-	public void resolveBundles() throws BundleException {
-		Bundle[] bundles = bundleContext.getBundles();
-		for (Bundle bundle : bundles) {
-			if (bundle.getState() == Bundle.INSTALLED) {
-				System.out.println("Found non resolved bundle " + bundle.getBundleId() + ":" + bundle.getSymbolicName()
+ ":" + bundle.getVersion());
-				bundle.start();
-			}
-		}
-	}
-	
+    public String sanitizeFilter(String filter) {
+        return filter.startsWith("(") ? filter : "(" + filter + ")";
+    }
+
+    /**
+     * Helps to diagnose bundles that are not resolved as it will throw a detailed exception
+     * 
+     * @throws BundleException
+     */
+    public void resolveBundles() throws BundleException {
+        Bundle[] bundles = bundleContext.getBundles();
+        for (Bundle bundle : bundles) {
+            if (bundle.getState() == Bundle.INSTALLED) {
+                System.out.println("Found non resolved bundle " + bundle.getBundleId() +
":"
+                                   + bundle.getSymbolicName() + ":" + bundle.getVersion());
+                bundle.start();
+            }
+        }
+    }
+
     public Bundle getBundleByName(String symbolicName) {
         for (Bundle b : bundleContext.getBundles()) {
             if (b.getSymbolicName().equals(symbolicName)) {
@@ -145,6 +145,7 @@ public abstract class AbstractJPAItest {
                 
                 mvnBundle("org.ow2.asm", "asm-all"),
                 mvnBundle("org.apache.felix", "org.apache.felix.configadmin"),
+                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-blueprint/src/main/java/org/apache/aries/jpa/blueprint/supplier/impl/EmSupplierProxy.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/supplier/impl/EmSupplierProxy.java?rev=1690676&r1=1690675&r2=1690676&view=diff
==============================================================================
--- aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/supplier/impl/EmSupplierProxy.java
(original)
+++ aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/supplier/impl/EmSupplierProxy.java
Mon Jul 13 14:20:06 2015
@@ -33,12 +33,12 @@ import org.osgi.util.tracker.ServiceTrac
 
 public class EmSupplierProxy implements EmSupplier, Closeable {
     private ServiceTracker<EmSupplier, EmSupplier> tracker;
+    private Filter filter;
 
     public EmSupplierProxy(BundleContext context, String unitName) {
         String filterS = String.format("(&(objectClass=%s)(%s=%s))", EmSupplier.class.getName(),
                                        JPA_UNIT_NAME,
                                        unitName);
-        Filter filter;
         try {
             filter = FrameworkUtil.createFilter(filterS);
         } catch (InvalidSyntaxException e) {
@@ -70,7 +70,11 @@ public class EmSupplierProxy implements
 
     private EmSupplier getEmSupplier() {
         try {
-            return tracker.waitForService(10000);
+            EmSupplier emSupplier = tracker.waitForService(10000);
+            if (emSupplier == null) {
+                throw new IllegalStateException("EmSupplier service not available with filter
" + filter);
+            }
+            return emSupplier;
         } catch (InterruptedException e) {
             throw new IllegalStateException(e);
         }

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=1690676&r1=1690675&r2=1690676&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
Mon Jul 13 14:20:06 2015
@@ -24,12 +24,14 @@ import java.util.concurrent.ConcurrentHa
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.atomic.AtomicInteger;
 
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
 
 import org.apache.aries.jpa.supplier.EmSupplier;
+import org.osgi.service.coordinator.Coordination;
+import org.osgi.service.coordinator.Coordinator;
+import org.osgi.service.coordinator.Participant;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -47,25 +49,15 @@ public class EMSupplierImpl implements E
     private long shutdownWaitTime = DEFAULT_SHUTDOWN_WAIT_SECS;
     private TimeUnit shutdownWaitTimeUnit = TimeUnit.SECONDS;
 
-    private final ThreadLocal<EntityManager> localEm;
-
-    // Counts how deeply nested the calls on this EM are
-    private final ThreadLocal<AtomicInteger> usageCount;
     private Set<EntityManager> emSet;
     private CountDownLatch emsToShutDown;
-    
+    private Coordinator coordinator;
 
-    public EMSupplierImpl(final EntityManagerFactory emf) {
+    public EMSupplierImpl(final EntityManagerFactory emf, Coordinator coordinator) {
         this.emf = emf;
+        this.coordinator = coordinator;
         this.shutdown = new AtomicBoolean(false);
-        this.localEm = new ThreadLocal<EntityManager>();
         this.emSet = Collections.newSetFromMap(new ConcurrentHashMap<EntityManager, Boolean>());
-        this.usageCount = new ThreadLocal<AtomicInteger>() {
-            @Override
-            protected AtomicInteger initialValue() {
-                return new AtomicInteger(0);
-            }
-        };
     }
 
     private EntityManager createEm(EntityManagerFactory emf) {
@@ -81,46 +73,62 @@ public class EMSupplierImpl implements E
      */
     @Override
     public EntityManager get() {
-        EntityManager em = this.localEm.get();
-        if (em == null) {
-            LOG.warn("No EntityManager present on this thread. Remember to call preCall()
first");
+        Coordination coordination = getTopCoordination();
+        EntityManager em = getEm(coordination);
+        if (coordination != null && em == null) {
+            em = createEm(emf);
+            emSet.add(em);
+            coordination.getVariables().put(EntityManager.class, em);
+            coordination.addParticipant(new Participant() {
+                
+                @Override
+                public void failed(Coordination coordination) throws Exception {
+                    ended(coordination);
+                }
+                
+                @Override
+                public void ended(Coordination coordination) throws Exception {
+                    EntityManager em = getEm(coordination);
+                    em.close();
+                    emSet.remove(em);
+                    if (shutdown.get()) {
+                        emsToShutDown.countDown();
+                    }
+                }
+            });
         }
         return em;
     }
+    
+    Coordination getTopCoordination() {
+        Coordination coordination = coordinator.peek();
+        while (coordination != null && coordination.getEnclosingCoordination() !=
null) {
+            coordination = coordination.getEnclosingCoordination();
+        }
+        return coordination;
+    }
 
+    /**
+     * Get EntityManager from outer most Coordination that holds an EM
+     * @param coordination
+     * @return
+     */
+    private EntityManager getEm(Coordination coordination) {
+        if (coordination == null) {
+            return null;
+        } else {
+            return (EntityManager)coordination.getVariables().get(EntityManager.class);
+        }
+    }
 
     @Override
     public void preCall() {
-        if (shutdown.get()) {
-            throw new IllegalStateException("This EntityManagerFactory is being shut down.
Can not enter a new EM enabled method");
-        }
-        int count = this.usageCount.get().incrementAndGet();
-        if (count == 1) {
-            EntityManager em = createEm(emf);
-            emSet.add(em);
-            localEm.set(em);
-        }
+        coordinator.begin("jpa", 0);
     }
 
     @Override
     public void postCall() {
-        int count = this.usageCount.get().decrementAndGet();
-        if (count == 0) {
-            // Outermost call finished
-            closeAndRemoveLocalEm();
-        } else if (count < 0) {
-            throw new IllegalStateException("postCall() called without corresponding preCall()");
-        }
-    }
-
-    private synchronized void closeAndRemoveLocalEm() {
-        EntityManager em = localEm.get();
-        em.close();
-        emSet.remove(em);
-        localEm.remove();
-        if (shutdown.get()) {
-            emsToShutDown.countDown();
-        }
+        coordinator.pop().end();
     }
 
     /**
@@ -167,4 +175,5 @@ public class EMSupplierImpl implements E
         this.shutdownWaitTime = shutdownWaitTime;
         this.shutdownWaitTimeUnit = shutdownWaitTimeUnit;
     }
+
 }

Modified: aries/trunk/jpa/jpa-support/src/main/java/org/apache/aries/jpa/support/osgi/impl/Activator.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/jpa-support/src/main/java/org/apache/aries/jpa/support/osgi/impl/Activator.java?rev=1690676&r1=1690675&r2=1690676&view=diff
==============================================================================
--- aries/trunk/jpa/jpa-support/src/main/java/org/apache/aries/jpa/support/osgi/impl/Activator.java
(original)
+++ aries/trunk/jpa/jpa-support/src/main/java/org/apache/aries/jpa/support/osgi/impl/Activator.java
Mon Jul 13 14:20:06 2015
@@ -23,17 +23,17 @@ import org.osgi.framework.BundleContext;
 
 public class Activator implements BundleActivator {
 
-    private EMFTracker emfTracker;
+    private CoordinatorTracker coordinatorTracker;
 
     @Override
     public void start(BundleContext context) throws Exception {
-        emfTracker = new EMFTracker(context);
-        emfTracker.open();
+        coordinatorTracker = new CoordinatorTracker(context);
+        coordinatorTracker.open();
     }
 
     @Override
     public void stop(BundleContext context) throws Exception {
-        emfTracker.close();
+        coordinatorTracker.close();
     }
 
 }

Copied: aries/trunk/jpa/jpa-support/src/main/java/org/apache/aries/jpa/support/osgi/impl/CoordinatorTracker.java
(from r1690610, aries/trunk/jpa/jpa-support/src/main/java/org/apache/aries/jpa/support/osgi/impl/Activator.java)
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/jpa-support/src/main/java/org/apache/aries/jpa/support/osgi/impl/CoordinatorTracker.java?p2=aries/trunk/jpa/jpa-support/src/main/java/org/apache/aries/jpa/support/osgi/impl/CoordinatorTracker.java&p1=aries/trunk/jpa/jpa-support/src/main/java/org/apache/aries/jpa/support/osgi/impl/Activator.java&r1=1690610&r2=1690676&rev=1690676&view=diff
==============================================================================
--- aries/trunk/jpa/jpa-support/src/main/java/org/apache/aries/jpa/support/osgi/impl/Activator.java
(original)
+++ aries/trunk/jpa/jpa-support/src/main/java/org/apache/aries/jpa/support/osgi/impl/CoordinatorTracker.java
Mon Jul 13 14:20:06 2015
@@ -18,22 +18,29 @@
  */
 package org.apache.aries.jpa.support.osgi.impl;
 
-import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.coordinator.Coordinator;
+import org.osgi.util.tracker.ServiceTracker;
 
-public class Activator implements BundleActivator {
+public class CoordinatorTracker extends ServiceTracker<Coordinator, EMFTracker> {
 
-    private EMFTracker emfTracker;
+    public CoordinatorTracker(BundleContext context) {
+        super(context, Coordinator.class, null);
+    }
 
     @Override
-    public void start(BundleContext context) throws Exception {
-        emfTracker = new EMFTracker(context);
+    public EMFTracker addingService(ServiceReference<Coordinator> ref) {
+        Coordinator coordinator = context.getService(ref);
+        EMFTracker emfTracker = new EMFTracker(context, coordinator);
         emfTracker.open();
+        return emfTracker;
     }
 
     @Override
-    public void stop(BundleContext context) throws Exception {
+    public void removedService(ServiceReference<Coordinator> ref, EMFTracker emfTracker)
{
         emfTracker.close();
+        super.removedService(ref, emfTracker);
     }
 
 }

Modified: aries/trunk/jpa/jpa-support/src/main/java/org/apache/aries/jpa/support/osgi/impl/EMFTracker.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/jpa-support/src/main/java/org/apache/aries/jpa/support/osgi/impl/EMFTracker.java?rev=1690676&r1=1690675&r2=1690676&view=diff
==============================================================================
--- aries/trunk/jpa/jpa-support/src/main/java/org/apache/aries/jpa/support/osgi/impl/EMFTracker.java
(original)
+++ aries/trunk/jpa/jpa-support/src/main/java/org/apache/aries/jpa/support/osgi/impl/EMFTracker.java
Mon Jul 13 14:20:06 2015
@@ -33,6 +33,7 @@ import org.apache.aries.jpa.template.Jpa
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.coordinator.Coordinator;
 import org.osgi.util.tracker.ServiceTracker;
 
 /**
@@ -43,9 +44,12 @@ import org.osgi.util.tracker.ServiceTrac
 @SuppressWarnings("rawtypes")
 public class EMFTracker extends ServiceTracker {
 
+    private Coordinator coordinator;
+
     @SuppressWarnings("unchecked")
-    public EMFTracker(BundleContext context) {
+    public EMFTracker(BundleContext context, Coordinator coordinator) {
         super(context, EntityManagerFactory.class, null);
+        this.coordinator = coordinator;
     }
 
     @SuppressWarnings("unchecked")
@@ -58,7 +62,7 @@ public class EMFTracker extends ServiceT
         BundleContext bContext = reference.getBundle().getBundleContext();
         TrackedEmf tracked = new TrackedEmf();
         tracked.emf = (EntityManagerFactory)bContext.getService(reference);
-        tracked.emSupplier = new EMSupplierImpl(tracked.emf);
+        tracked.emSupplier = new EMSupplierImpl(tracked.emf, coordinator);
         tracked.emSupplierReg = bContext.registerService(EmSupplier.class, tracked.emSupplier,
                                                          getEmSupplierProps(unitName));
 

Added: aries/trunk/jpa/jpa-support/src/test/java/org/apache/aries/jpa/impl/DummyCoordination.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/jpa-support/src/test/java/org/apache/aries/jpa/impl/DummyCoordination.java?rev=1690676&view=auto
==============================================================================
--- aries/trunk/jpa/jpa-support/src/test/java/org/apache/aries/jpa/impl/DummyCoordination.java
(added)
+++ aries/trunk/jpa/jpa-support/src/test/java/org/apache/aries/jpa/impl/DummyCoordination.java
Mon Jul 13 14:20:06 2015
@@ -0,0 +1,121 @@
+/*
+ * 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 WARRANTIESOR 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.impl;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.osgi.framework.Bundle;
+import org.osgi.service.coordinator.Coordination;
+import org.osgi.service.coordinator.Participant;
+
+public class DummyCoordination implements Coordination {
+    private Set<Participant> participants = new HashSet<>();
+    private Map<Class<?>, Object> vars = new HashMap<Class<?>, Object>();
+    private Coordination enclosing;
+
+    public DummyCoordination(Coordination enclosing) {
+        this.enclosing = enclosing;
+    }
+    
+    @Override
+    public long getId() {
+        return 0;
+    }
+
+    @Override
+    public String getName() {
+        return null;
+    }
+
+    @Override
+    public void end() {
+        Iterator<Participant> it = participants.iterator();
+        while (it.hasNext()) {
+            try {
+                it.next().ended(this);
+            } catch (Exception e) {
+            }
+        }
+    }
+
+    @Override
+    public boolean fail(Throwable cause) {
+        return false;
+    }
+
+    @Override
+    public Throwable getFailure() {
+        return null;
+    }
+
+    @Override
+    public boolean isTerminated() {
+        return false;
+    }
+
+    @Override
+    public void addParticipant(Participant participant) {
+        this.participants.add(participant);
+    }
+
+    @Override
+    public List<Participant> getParticipants() {
+        return null;
+    }
+
+    @Override
+    public Map<Class<?>, Object> getVariables() {
+        return vars ;
+    }
+
+    @Override
+    public long extendTimeout(long timeMillis) {
+        return 0;
+    }
+
+    @Override
+    public void join(long timeMillis) throws InterruptedException {
+    }
+
+    @Override
+    public Coordination push() {
+        return null;
+    }
+
+    @Override
+    public Thread getThread() {
+        return null;
+    }
+
+    @Override
+    public Bundle getBundle() {
+        return null;
+    }
+
+    @Override
+    public Coordination getEnclosingCoordination() {
+        return enclosing;
+    }
+
+}

Added: aries/trunk/jpa/jpa-support/src/test/java/org/apache/aries/jpa/impl/DummyCoordinator.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/jpa-support/src/test/java/org/apache/aries/jpa/impl/DummyCoordinator.java?rev=1690676&view=auto
==============================================================================
--- aries/trunk/jpa/jpa-support/src/test/java/org/apache/aries/jpa/impl/DummyCoordinator.java
(added)
+++ aries/trunk/jpa/jpa-support/src/test/java/org/apache/aries/jpa/impl/DummyCoordinator.java
Mon Jul 13 14:20:06 2015
@@ -0,0 +1,76 @@
+/*
+ * 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 WARRANTIESOR 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.impl;
+
+import java.util.ArrayDeque;
+import java.util.Collection;
+
+import org.osgi.service.coordinator.Coordination;
+import org.osgi.service.coordinator.Coordinator;
+import org.osgi.service.coordinator.Participant;
+
+public class DummyCoordinator implements Coordinator {
+
+    private java.util.Deque<Coordination> coordinations = new ArrayDeque<>();
+
+    @Override
+    public Coordination create(String name, long timeMillis) {
+        throw new IllegalStateException();
+    }
+    
+
+    @Override
+    public Coordination begin(String name, long timeMillis) {
+        Coordination oldCoordination = coordinations.peekLast();
+        Coordination coordination = new DummyCoordination(oldCoordination);
+        this.coordinations.push(coordination);
+        return coordination;
+    }
+
+    @Override
+    public Coordination peek() {
+        return coordinations.peek();
+    }
+
+    @Override
+    public Coordination pop() {
+        return coordinations.pop();
+    }
+
+    @Override
+    public boolean fail(Throwable cause) {
+        return false;
+    }
+
+    @Override
+    public boolean addParticipant(Participant participant) {
+        return false;
+    }
+
+    @Override
+    public Collection<Coordination> getCoordinations() {
+        return null;
+    }
+
+    @Override
+    public Coordination getCoordination(long id) {
+        return null;
+    }
+
+}

Modified: aries/trunk/jpa/jpa-support/src/test/java/org/apache/aries/jpa/impl/EmSupplierTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/jpa-support/src/test/java/org/apache/aries/jpa/impl/EmSupplierTest.java?rev=1690676&r1=1690675&r2=1690676&view=diff
==============================================================================
--- aries/trunk/jpa/jpa-support/src/test/java/org/apache/aries/jpa/impl/EmSupplierTest.java
(original)
+++ aries/trunk/jpa/jpa-support/src/test/java/org/apache/aries/jpa/impl/EmSupplierTest.java
Mon Jul 13 14:20:06 2015
@@ -31,13 +31,15 @@ import javax.persistence.EntityManagerFa
 import org.apache.aries.jpa.support.impl.EMSupplierImpl;
 import org.junit.Assert;
 import org.junit.Test;
+import org.osgi.service.coordinator.Coordinator;
 
 public class EmSupplierTest {
 
     @Test
     public void lifeCycleTest() {
         EntityManagerFactory emf = mockEmf();
-        EMSupplierImpl emSupplier = new EMSupplierImpl(emf);
+        Coordinator coordinator = new DummyCoordinator();
+        EMSupplierImpl emSupplier = new EMSupplierImpl(emf, coordinator );
         
         Assert.assertNull("No EM may be present at start", emSupplier.get());
 
@@ -57,19 +59,15 @@ public class EmSupplierTest {
         Assert.assertTrue("Shutdown should be clean", clean);
     }
 
-    private EntityManagerFactory mockEmf() {
-        EntityManagerFactory emf = mock(EntityManagerFactory.class);
-        EntityManager em = mock(EntityManager.class);
-        when(emf.createEntityManager()).thenReturn(em);
-        return emf;
-    }
     
     @Test
     public void uncleanLifeCycleTest() {
         EntityManagerFactory emf = mockEmf();
-        EMSupplierImpl emSupplier = new EMSupplierImpl(emf);
+        Coordinator coordinator = new DummyCoordinator();
+        EMSupplierImpl emSupplier = new EMSupplierImpl(emf, coordinator);
         emSupplier.setShutdownWait(100, MILLISECONDS);
         emSupplier.preCall();
+        emSupplier.get();
         boolean clean = emSupplier.close();
         Assert.assertFalse("Shutdown should be unclean", clean);
     }
@@ -77,7 +75,8 @@ public class EmSupplierTest {
     @Test
     public void asyncCleanLifeCycleTest() throws InterruptedException {
         EntityManagerFactory emf = mockEmf();
-        final EMSupplierImpl emSupplier = new EMSupplierImpl(emf);
+        Coordinator coordinator = new DummyCoordinator();
+        final EMSupplierImpl emSupplier = new EMSupplierImpl(emf,coordinator);
         final Semaphore preCallSem = new Semaphore(0);
         Runnable command = new Runnable() {
             
@@ -95,4 +94,10 @@ public class EmSupplierTest {
         Assert.assertTrue("Shutdown should be clean", clean);
     }
 
+    private EntityManagerFactory mockEmf() {
+        EntityManagerFactory emf = mock(EntityManagerFactory.class);
+        EntityManager em = mock(EntityManager.class);
+        when(emf.createEntityManager()).thenReturn(em);
+        return emf;
+    }
 }



Mime
View raw message