[seam-commits] Seam SVN: r13769 - 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
Tue Sep 21 06:06:34 EDT 2010


Author: swd847
Date: 2010-09-21 06:06:33 -0400 (Tue, 21 Sep 2010)
New Revision: 13769

Modified:
   modules/persistence/trunk/impl/pom.xml
   modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextBeanLifecycle.java
   modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextExtension.java
   modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextProxyHandler.java
   modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/transaction/EjbSynchronizations.java
   modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/transaction/EntityTransaction.java
   modules/persistence/trunk/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
Log:
work on making seam-persistence work in SE environments


Modified: modules/persistence/trunk/impl/pom.xml
===================================================================
--- modules/persistence/trunk/impl/pom.xml	2010-09-20 11:29:33 UTC (rev 13768)
+++ modules/persistence/trunk/impl/pom.xml	2010-09-21 10:06:33 UTC (rev 13769)
@@ -116,6 +116,7 @@
          <artifactId>hibernate-search</artifactId>
          <version>3.2.1.Final</version>
          <optional>true</optional>
+         <scope>provided</scope>
       </dependency>
       
       <dependency>

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-20 11:29:33 UTC (rev 13768)
+++ modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextBeanLifecycle.java	2010-09-21 10:06:33 UTC (rev 13769)
@@ -27,6 +27,7 @@
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Proxy;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 import java.util.Set;
 
@@ -111,9 +112,10 @@
          entityManager = getPersistenceProvider(entityManager).proxyEntityManager(entityManager);
          ManagedPersistenceContextProxyHandler handler = new ManagedPersistenceContextProxyHandler(entityManager, manager, bean.getQualifiers(), getPersistenceContexts(), getPersistenceProvider(entityManager));
          EntityManager proxy = (EntityManager) proxyConstructor.newInstance(handler);
+         arg0.push(proxy);
          getPersistenceProvider(entityManager).setFlushMode(proxy, getPersistenceContexts().getFlushMode());
          manager.fireEvent(new SeamManagedPersistenceContextCreated(proxy), qualifiers);
-         
+
          return proxy;
       }
       catch (Exception e)
@@ -124,7 +126,7 @@
 
    public void destroy(Bean<EntityManager> bean, EntityManager em, CreationalContext<EntityManager> arg1)
    {
-      ((ManagedPersistenceContext)em).closeAfterTransaction();
+      ((ManagedPersistenceContext) em).closeAfterTransaction();
       arg1.release();
       try
       {
@@ -177,7 +179,7 @@
          Bean<EntityManagerFactory> bean = (Bean) manager.resolve(manager.getBeans(EntityManagerFactory.class, qualifiers));
          if (bean == null)
          {
-            throw new RuntimeException("Could not find EntityManagerFactory bean with qualifiers" + qualifiers);
+            throw new RuntimeException("Could not find EntityManagerFactory bean with qualifiers" + Arrays.toString(qualifiers));
          }
          CreationalContext<EntityManagerFactory> ctx = manager.createCreationalContext(bean);
          emf = (EntityManagerFactory) manager.getReference(bean, EntityManagerFactory.class, ctx);

Modified: modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextExtension.java
===================================================================
--- modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextExtension.java	2010-09-20 11:29:33 UTC (rev 13768)
+++ modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextExtension.java	2010-09-21 10:06:33 UTC (rev 13769)
@@ -22,13 +22,19 @@
 package org.jboss.seam.persistence;
 
 import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 import javax.annotation.Resource;
+import javax.enterprise.context.ApplicationScoped;
 import javax.enterprise.context.Dependent;
+import javax.enterprise.context.spi.CreationalContext;
 import javax.enterprise.event.Observes;
 import javax.enterprise.inject.Alternative;
 import javax.enterprise.inject.Produces;
@@ -36,17 +42,22 @@
 import javax.enterprise.inject.spi.AnnotatedField;
 import javax.enterprise.inject.spi.AnnotatedMember;
 import javax.enterprise.inject.spi.AnnotatedMethod;
+import javax.enterprise.inject.spi.AnnotatedType;
 import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.BeanManager;
 import javax.enterprise.inject.spi.BeforeBeanDiscovery;
 import javax.enterprise.inject.spi.Extension;
+import javax.enterprise.inject.spi.InjectionPoint;
 import javax.enterprise.inject.spi.ProcessAnnotatedType;
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
+import javax.persistence.Persistence;
 import javax.persistence.PersistenceUnit;
 
+import org.jboss.seam.persistence.util.EnvironmentUtils;
 import org.jboss.weld.extensions.annotated.AnnotatedTypeBuilder;
 import org.jboss.weld.extensions.bean.BeanBuilder;
+import org.jboss.weld.extensions.bean.Beans;
 import org.jboss.weld.extensions.literal.AnyLiteral;
 import org.jboss.weld.extensions.literal.ApplicationScopedLiteral;
 import org.jboss.weld.extensions.literal.DefaultLiteral;
@@ -71,6 +82,8 @@
 
    private static final Logger log = LoggerFactory.getLogger(ManagedPersistenceContextExtension.class);
 
+   private Map<String, Bean<EntityManagerFactory>> emfBeans = new HashMap<String, Bean<EntityManagerFactory>>();
+
    public void beforeBeanDiscovery(@Observes BeforeBeanDiscovery event)
    {
       ServiceLoader<SeamPersistenceProvider> providers = ServiceLoader.load(SeamPersistenceProvider.class);
@@ -88,12 +101,28 @@
     * smpc bean is created and registered. Any scope declaration on the producer
     * are removed as this is not supported by the spec
     * 
+    * For non-ee environments this extension also bootstraps @PersistenceUnit
+    * producer fields
+    * 
     */
-   public <T> void processAnnotatedType(@Observes ProcessAnnotatedType<T> event, BeanManager manager)
+   public <T> void processAnnotatedType(@Observes final ProcessAnnotatedType<T> event, BeanManager manager)
    {
       AnnotatedTypeBuilder<T> modifiedType = null;
+      boolean bootstrapped = false;
       for (AnnotatedField<? super T> f : event.getAnnotatedType().getFields())
       {
+         if (f.isAnnotationPresent(PersistenceUnit.class) && f.isAnnotationPresent(Produces.class) && EnvironmentUtils.isEEEnvironment())
+         {
+            bootstrapped = true;
+            final String unitName = f.getAnnotation(PersistenceUnit.class).unitName();
+            final Set<Annotation> qualifiers = Beans.getQualifiers(manager, f.getAnnotations());
+            if (qualifiers.isEmpty())
+            {
+               qualifiers.add(DefaultLiteral.INSTANCE);
+            }
+            qualifiers.add(AnyLiteral.INSTANCE);
+            beans.add(createEMFBean(unitName, qualifiers, event.getAnnotatedType()));
+         }
          // look for a seam managed persistence unit declaration on EE resource
          // producer fields
          if (f.isAnnotationPresent(SeamManaged.class) && (f.isAnnotationPresent(PersistenceUnit.class) || f.isAnnotationPresent(Resource.class)) && f.isAnnotationPresent(Produces.class) && EntityManagerFactory.class.isAssignableFrom(f.getJavaMember().getType()))
@@ -127,6 +156,10 @@
             {
                modifiedType.removeFromField(f.getJavaMember(), scope);
             }
+            if (bootstrapped)
+            {
+               modifiedType.removeFromField(f.getJavaMember(), Produces.class);
+            }
             registerManagedPersistenceContext(qualifiers, scope, f.isAnnotationPresent(Alternative.class), manager, event.getAnnotatedType().getJavaClass().getClassLoader(), f);
             log.info("Configuring Seam Managed Persistence Context from producer field " + f.getDeclaringType().getJavaClass().getName() + "." + f.getJavaMember().getName() + " with qualifiers " + qualifiers);
          }
@@ -179,6 +212,72 @@
       }
    }
 
+   private Bean<?> createEMFBean(final String unitName, final Set<Annotation> qualifiers, final AnnotatedType<?> type)
+   {
+      return new Bean<EntityManagerFactory>()
+      {
+         public Set<Type> getTypes()
+         {
+            Set<Type> types = new HashSet<Type>();
+            types.add(Object.class);
+            types.add(EntityManagerFactory.class);
+            return types;
+         }
+
+         public Class<? extends Annotation> getScope()
+         {
+            return ApplicationScoped.class;
+         }
+
+         public EntityManagerFactory create(CreationalContext<EntityManagerFactory> ctx)
+         {
+            return Persistence.createEntityManagerFactory(unitName);
+         }
+
+         public void destroy(EntityManagerFactory emf, CreationalContext<EntityManagerFactory> ctx)
+         {
+            emf.close();
+            ctx.release();
+         }
+
+         public Class<?> getBeanClass()
+         {
+            return type.getJavaClass();
+         }
+
+         public Set<InjectionPoint> getInjectionPoints()
+         {
+            return Collections.emptySet();
+         }
+
+         public String getName()
+         {
+            return null;
+         }
+
+         public Set<Annotation> getQualifiers()
+         {
+            return qualifiers;
+         }
+
+         public Set<Class<? extends Annotation>> getStereotypes()
+         {
+            return Collections.emptySet();
+
+         }
+
+         public boolean isAlternative()
+         {
+            return false;
+         }
+
+         public boolean isNullable()
+         {
+            return false;
+         }
+      };
+   }
+
    private void registerManagedPersistenceContext(Set<Annotation> qualifiers, Class<? extends Annotation> scope, boolean alternative, BeanManager manager, ClassLoader loader, AnnotatedMember<?> member)
    {
       // we need to add all additional interfaces from our
@@ -214,6 +313,6 @@
       {
          event.addBean(i);
       }
+
    }
-
 }

Modified: modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextProxyHandler.java
===================================================================
--- modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextProxyHandler.java	2010-09-20 11:29:33 UTC (rev 13768)
+++ modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextProxyHandler.java	2010-09-21 10:06:33 UTC (rev 13769)
@@ -84,11 +84,6 @@
 
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
    {
-      if (!synchronizationRegistered)
-      {
-         joinTransaction();
-      }
-      touch((ManagedPersistenceContext) proxy);
       if ("changeFlushMode".equals(method.getName()) && method.getParameterTypes().length == 1 && method.getParameterTypes()[0].equals(FlushModeType.class))
       {
          changeFushMode((FlushModeType) args[0]);
@@ -111,11 +106,25 @@
          closeAfterTransaction();
          return null;
       }
+      // we do not join the transaction for setFlushMode calls, as this may
+      // result in an infinite loop, as this is called during SMPC
+      // initialisation
+      if (!"setFlushMode".equals(method.getName()))
+      {
+         if (!synchronizationRegistered)
+         {
+            joinTransaction();
+         }
+      }
+
+      touch((ManagedPersistenceContext) proxy);
+
       return super.invoke(proxy, method, args);
    }
 
    private void joinTransaction() throws SystemException
    {
+      synchronizationRegistered = true;
       SeamTransaction transaction = userTransactionInstance.get();
       if (transaction.isActive())
       {
@@ -123,13 +132,13 @@
          try
          {
             transaction.registerSynchronization(this);
-            synchronizationRegistered = true;
          }
          catch (Exception e)
          {
             // synchronizationRegistered =
             // PersistenceProvider.instance().registerSynchronization(this,
             // entityManager);
+            synchronizationRegistered = false;
             throw new RuntimeException(e);
          }
       }

Modified: modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/transaction/EjbSynchronizations.java
===================================================================
--- modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/transaction/EjbSynchronizations.java	2010-09-20 11:29:33 UTC (rev 13768)
+++ modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/transaction/EjbSynchronizations.java	2010-09-21 10:06:33 UTC (rev 13769)
@@ -35,6 +35,7 @@
 import javax.inject.Inject;
 import javax.transaction.Synchronization;
 
+import org.jboss.weld.extensions.defaultbean.DefaultBean;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -50,6 +51,7 @@
 @Stateful
 @RequestScoped
 @TransactionAttribute(TransactionAttributeType.SUPPORTS)
+ at DefaultBean(type = Synchronizations.class)
 public class EjbSynchronizations implements LocalEjbSynchronizations, SessionSynchronization
 {
    private static final Logger log = LoggerFactory.getLogger(EjbSynchronizations.class);

Modified: modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/transaction/EntityTransaction.java
===================================================================
--- modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/transaction/EntityTransaction.java	2010-09-20 11:29:33 UTC (rev 13768)
+++ modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/transaction/EntityTransaction.java	2010-09-21 10:06:33 UTC (rev 13769)
@@ -138,12 +138,12 @@
 
    public int getStatus() throws SystemException
    {
-      if (getDelegate().getRollbackOnly())
+      if (getDelegate().isActive())
       {
-         return Status.STATUS_MARKED_ROLLBACK;
-      }
-      else if (getDelegate().isActive())
-      {
+         if (getDelegate().getRollbackOnly())
+         {
+            return Status.STATUS_MARKED_ROLLBACK;
+         }
          return Status.STATUS_ACTIVE;
       }
       else

Modified: modules/persistence/trunk/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
===================================================================
--- modules/persistence/trunk/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension	2010-09-20 11:29:33 UTC (rev 13768)
+++ modules/persistence/trunk/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension	2010-09-21 10:06:33 UTC (rev 13769)
@@ -1,4 +1,3 @@
-org.jboss.seam.persistence.SePersistenceContextExtension
 org.jboss.seam.persistence.transaction.TransactionExtension
 org.jboss.seam.persistence.transaction.scope.TransactionScopeExtension
 org.jboss.seam.persistence.ManagedPersistenceContextExtension
\ No newline at end of file



More information about the seam-commits mailing list