[seam-commits] Seam SVN: r13686 - in modules/persistence/trunk: impl/src/main/java/org/jboss/seam/persistence and 2 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Wed Sep 1 01:12:45 EDT 2010


Author: swd847
Date: 2010-09-01 01:12:45 -0400 (Wed, 01 Sep 2010)
New Revision: 13686

Added:
   modules/persistence/trunk/api/src/main/java/org/jboss/seam/persistence/SeamManagedPersistenceContextCreated.java
   modules/persistence/trunk/impl/src/test/java/org/jboss/seam/persistence/test/ManagedPersistenceContextCreatedEventTest.java
   modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/util/ManagedPersistenceContextObserver.java
Modified:
   modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextBeanLifecycle.java
Log:
fire event when SMPC is created



Added: modules/persistence/trunk/api/src/main/java/org/jboss/seam/persistence/SeamManagedPersistenceContextCreated.java
===================================================================
--- modules/persistence/trunk/api/src/main/java/org/jboss/seam/persistence/SeamManagedPersistenceContextCreated.java	                        (rev 0)
+++ modules/persistence/trunk/api/src/main/java/org/jboss/seam/persistence/SeamManagedPersistenceContextCreated.java	2010-09-01 05:12:45 UTC (rev 13686)
@@ -0,0 +1,44 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.seam.persistence;
+
+import javax.persistence.EntityManager;
+/**
+ * event that is fired when the SMPC is created
+ * 
+ * @author Stuart Douglas <stuart at baileyroberts.com.au>
+ *
+ */
+public class SeamManagedPersistenceContextCreated
+{
+   private final EntityManager entityManager;
+
+   public SeamManagedPersistenceContextCreated(EntityManager entityManager)
+   {
+      this.entityManager = entityManager;
+   }
+
+   public EntityManager getEntityManager()
+   {
+      return entityManager;
+   }
+}

Modified: modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextBeanLifecycle.java
===================================================================
--- modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextBeanLifecycle.java	2010-09-01 04:13:37 UTC (rev 13685)
+++ modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextBeanLifecycle.java	2010-09-01 05:12:45 UTC (rev 13686)
@@ -97,7 +97,6 @@
       {
          this.qualifiers[i++] = a;
       }
-
    }
 
    /**
@@ -113,6 +112,8 @@
          ManagedPersistenceContextProxyHandler handler = new ManagedPersistenceContextProxyHandler(entityManager, manager, bean.getQualifiers(), getPersistenceContexts(), getPersistenceProvider(entityManager));
          EntityManager proxy = (EntityManager) proxyConstructor.newInstance(handler);
          getPersistenceProvider(entityManager).setFlushMode(proxy, getPersistenceContexts().getFlushMode());
+         manager.fireEvent(new SeamManagedPersistenceContextCreated(proxy), qualifiers);
+         
          return proxy;
       }
       catch (Exception e)

Added: modules/persistence/trunk/impl/src/test/java/org/jboss/seam/persistence/test/ManagedPersistenceContextCreatedEventTest.java
===================================================================
--- modules/persistence/trunk/impl/src/test/java/org/jboss/seam/persistence/test/ManagedPersistenceContextCreatedEventTest.java	                        (rev 0)
+++ modules/persistence/trunk/impl/src/test/java/org/jboss/seam/persistence/test/ManagedPersistenceContextCreatedEventTest.java	2010-09-01 05:12:45 UTC (rev 13686)
@@ -0,0 +1,95 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.seam.persistence.test;
+
+import javax.inject.Inject;
+import javax.persistence.EntityManager;
+import javax.persistence.FlushModeType;
+import javax.transaction.HeuristicMixedException;
+import javax.transaction.HeuristicRollbackException;
+import javax.transaction.NotSupportedException;
+import javax.transaction.RollbackException;
+import javax.transaction.SystemException;
+
+import junit.framework.Assert;
+
+import org.jboss.arquillian.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.seam.persistence.SePersistenceContextExtension;
+import org.jboss.seam.persistence.transaction.DefaultTransaction;
+import org.jboss.seam.persistence.transaction.SeamTransaction;
+import org.jboss.seam.persistence.transaction.TransactionExtension;
+import org.jboss.seam.persistence.transaction.scope.TransactionScopeExtension;
+import org.jboss.seam.persistence.util.NamingUtils;
+import org.jboss.seam.transactions.test.util.ArtifactNames;
+import org.jboss.seam.transactions.test.util.HelloService;
+import org.jboss.seam.transactions.test.util.Hotel;
+import org.jboss.seam.transactions.test.util.ManagedPersistenceContextObserver;
+import org.jboss.seam.transactions.test.util.ManagedPersistenceContextProvider;
+import org.jboss.seam.transactions.test.util.MavenArtifactResolver;
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.ByteArrayAsset;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+ at RunWith(Arquillian.class)
+public class ManagedPersistenceContextCreatedEventTest
+{
+   @Deployment
+   public static Archive<?> createTestArchive()
+   {
+      WebArchive war = ShrinkWrap.createDomain().getArchiveFactory().create(WebArchive.class, "test.war");
+      war.addLibraries(MavenArtifactResolver.resolve(ArtifactNames.WELD_EXTENSIONS));
+      war.addLibraries(MavenArtifactResolver.resolve(ArtifactNames.SEAM_PERSISTENCE_API));
+      war.addPackage(TransactionExtension.class.getPackage());
+      war.addPackage(SePersistenceContextExtension.class.getPackage());
+      war.addPackage(TransactionScopeExtension.class.getPackage());
+      war.addPackage(NamingUtils.class.getPackage());
+      war.addClasses(ManagedPersistenceContextCreatedEventTest.class, ManagedPersistenceContextObserver.class, Hotel.class, ManagedPersistenceContextProvider.class, HelloService.class);
+      war.addWebResource("META-INF/persistence.xml", "classes/META-INF/persistence.xml");
+      war.addWebResource(new ByteArrayAsset(new byte[0]), "beans.xml");
+      war.addWebResource("META-INF/services/javax.enterprise.inject.spi.Extension", "classes/META-INF/services/javax.enterprise.inject.spi.Extension");
+      return war;
+   }
+
+   @Inject
+   @DefaultTransaction
+   SeamTransaction transaction;
+
+   @Inject
+   EntityManager em;
+   
+   @Inject
+   ManagedPersistenceContextObserver observer;
+
+   @Test
+   public void testSMPCCreationObserved() throws NotSupportedException, SystemException, SecurityException, IllegalStateException, RollbackException, HeuristicMixedException, HeuristicRollbackException
+   {
+      em.isOpen(); //need to make a call on the EM to force creation
+      
+      Assert.assertTrue(observer.isObserverRun());
+      Assert.assertEquals(FlushModeType.COMMIT, em.getFlushMode());
+   }
+
+}

Added: modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/util/ManagedPersistenceContextObserver.java
===================================================================
--- modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/util/ManagedPersistenceContextObserver.java	                        (rev 0)
+++ modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/util/ManagedPersistenceContextObserver.java	2010-09-01 05:12:45 UTC (rev 13686)
@@ -0,0 +1,46 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.seam.transactions.test.util;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.Default;
+import javax.persistence.FlushModeType;
+
+import org.jboss.seam.persistence.SeamManagedPersistenceContextCreated;
+
+ at ApplicationScoped
+public class ManagedPersistenceContextObserver
+{
+   private boolean observerRun = false;
+
+   public void observe(@Observes @Default SeamManagedPersistenceContextCreated event)
+   {
+      observerRun = true;
+      event.getEntityManager().setFlushMode(FlushModeType.COMMIT);
+   }
+   
+   public boolean isObserverRun()
+   {
+      return observerRun;
+   }
+}



More information about the seam-commits mailing list