deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From johndam...@apache.org
Subject deltaspike git commit: DELTASPIKE-940 - First step in merging support for qualifiers and entity manager resolvers in data and JPA modules.
Date Sun, 25 Jun 2017 01:04:16 GMT
Repository: deltaspike
Updated Branches:
  refs/heads/master 764545c2d -> 256431539


DELTASPIKE-940 - First step in merging support for qualifiers and entity manager resolvers in data and JPA modules.


Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/25643153
Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/25643153
Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/25643153

Branch: refs/heads/master
Commit: 256431539ca8b01aa237d33f5bcb723e292f8893
Parents: 764545c
Author: John D. Ament <johndament@apache.org>
Authored: Sat Jun 24 21:03:10 2017 -0400
Committer: John D. Ament <johndament@apache.org>
Committed: Sat Jun 24 21:03:10 2017 -0400

----------------------------------------------------------------------
 deltaspike/modules/data/api/pom.xml             |   5 +
 .../data/api/EntityManagerConfig.java           |   4 +-
 .../data/api/EntityManagerResolver.java         |  10 +-
 .../data/impl/handler/EntityManagerRef.java     |  98 -------------
 .../impl/handler/EntityManagerRefLookup.java    | 136 -------------------
 .../data/impl/handler/QueryHandler.java         |   2 +
 .../data/impl/meta/RepositoryMetadata.java      |  39 +-----
 .../meta/RepositoryMetadataInitializer.java     |  29 ++--
 .../data/impl/handler/EntityManagerTest.java    |  21 ++-
 .../service/BasicEntityManagerResolver.java     |  38 ++++++
 ...gacyRepositoryWithEntityManagerResolver.java |  36 +++++
 ...mpleRepositoryWithEntityManagerResolver.java |   2 +-
 .../SimplisticEntityManagerResolver.java        |   4 +-
 .../api/entitymanager/EntityManagerConfig.java  |  58 ++++++++
 .../entitymanager/EntityManagerResolver.java    |  36 +++++
 .../jpa/api/transaction/Transactional.java      |   5 +
 .../entitymanager/EntityManagerMetadata.java    | 114 ++++++++++++++++
 .../impl/entitymanager/EntityManagerRef.java    |  99 ++++++++++++++
 .../entitymanager/EntityManagerRefLookup.java   | 136 +++++++++++++++++++
 19 files changed, 572 insertions(+), 300 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/25643153/deltaspike/modules/data/api/pom.xml
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/api/pom.xml b/deltaspike/modules/data/api/pom.xml
index 31e2fa1..9b4e920 100755
--- a/deltaspike/modules/data/api/pom.xml
+++ b/deltaspike/modules/data/api/pom.xml
@@ -59,6 +59,11 @@
             <artifactId>deltaspike-partial-bean-module-api</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>org.apache.deltaspike.modules</groupId>
+            <artifactId>deltaspike-jpa-module-api</artifactId>
+        </dependency>
+
     </dependencies>
 
 </project>

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/25643153/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/EntityManagerConfig.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/EntityManagerConfig.java b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/EntityManagerConfig.java
index e0bfb96..8a1a5f4 100644
--- a/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/EntityManagerConfig.java
+++ b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/EntityManagerConfig.java
@@ -18,6 +18,7 @@
  */
 package org.apache.deltaspike.data.api;
 
+import javax.persistence.FlushModeType;
 import java.lang.annotation.Documented;
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Inherited;
@@ -25,8 +26,6 @@ import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
 
-import javax.persistence.FlushModeType;
-
 /**
  * Configure the EntityManager for a specific repository.
  */
@@ -34,6 +33,7 @@ import javax.persistence.FlushModeType;
 @Retention(RetentionPolicy.RUNTIME)
 @Documented
 @Inherited
+@Deprecated
 public @interface EntityManagerConfig
 {
     /**

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/25643153/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/EntityManagerResolver.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/EntityManagerResolver.java b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/EntityManagerResolver.java
index de10da3..388d1d8 100644
--- a/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/EntityManagerResolver.java
+++ b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/EntityManagerResolver.java
@@ -18,15 +18,13 @@
  */
 package org.apache.deltaspike.data.api;
 
-import javax.persistence.EntityManager;
-
 /**
  * Resolve the EntityManager used for a specific repository.
  * Only necessary if there is more than one persistence unit.
+ *
+ * This interface is deprecated and instead you should use the version from JPA module
  */
-public interface EntityManagerResolver
+@Deprecated
+public interface EntityManagerResolver extends org.apache.deltaspike.jpa.api.entitymanager.EntityManagerResolver
 {
-
-    EntityManager resolveEntityManager();
-
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/25643153/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityManagerRef.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityManagerRef.java b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityManagerRef.java
deleted file mode 100644
index 5039003..0000000
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityManagerRef.java
+++ /dev/null
@@ -1,98 +0,0 @@
-/*
- * 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.deltaspike.data.impl.handler;
-
-import javax.persistence.EntityManager;
-import org.apache.deltaspike.core.api.provider.DependentProvider;
-import org.apache.deltaspike.data.api.EntityManagerResolver;
-
-public class EntityManagerRef
-{
-    private EntityManager entityManager;
-    private DependentProvider<? extends EntityManager> entityManagerDependentProvider;
-    
-    private Class<? extends EntityManagerResolver> entityManagerResolverClass;
-    private EntityManagerResolver entityManagerResolver;
-    private DependentProvider<? extends EntityManagerResolver> entityManagerResolverDependentProvider;
-        
-    public void release()
-    {
-        if (entityManagerDependentProvider != null)
-        {
-            entityManagerDependentProvider.destroy();
-        }
-        
-        if (entityManagerResolverDependentProvider != null)
-        {
-            entityManagerResolverDependentProvider.destroy();
-        }
-    }
-
-    public Class<? extends EntityManagerResolver> getEntityManagerResolverClass()
-    {
-        return entityManagerResolverClass;
-    }
-
-    public void setEntityManagerResolverClass(Class<? extends EntityManagerResolver> entityManagerResolverClass)
-    {
-        this.entityManagerResolverClass = entityManagerResolverClass;
-    }
-
-    public DependentProvider<? extends EntityManagerResolver> getEntityManagerResolverDependentProvider()
-    {
-        return entityManagerResolverDependentProvider;
-    }
-
-    public void setEntityManagerResolverDependentProvider(
-            DependentProvider<? extends EntityManagerResolver> entityManagerResolverDependentProvider)
-    {
-        this.entityManagerResolverDependentProvider = entityManagerResolverDependentProvider;
-    }
-
-    public EntityManager getEntityManager()
-    {
-        return entityManager;
-    }
-
-    public void setEntityManager(EntityManager entityManager)
-    {
-        this.entityManager = entityManager;
-    }
-
-    public EntityManagerResolver getEntityManagerResolver()
-    {
-        return entityManagerResolver;
-    }
-
-    public void setEntityManagerResolver(EntityManagerResolver entityManagerResolver)
-    {
-        this.entityManagerResolver = entityManagerResolver;
-    }
-
-    public DependentProvider<? extends EntityManager> getEntityManagerDependentProvider()
-    {
-        return entityManagerDependentProvider;
-    }
-
-    public void setEntityManagerDependentProvider(
-            DependentProvider<? extends EntityManager> entityManagerDependentProvider)
-    {
-        this.entityManagerDependentProvider = entityManagerDependentProvider;
-    }
-}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/25643153/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityManagerRefLookup.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityManagerRefLookup.java b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityManagerRefLookup.java
deleted file mode 100644
index 3bec649..0000000
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityManagerRefLookup.java
+++ /dev/null
@@ -1,136 +0,0 @@
-/*
- * 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.deltaspike.data.impl.handler;
-
-import java.util.Set;
-import javax.enterprise.context.ApplicationScoped;
-import javax.enterprise.inject.spi.Bean;
-import javax.enterprise.inject.spi.BeanManager;
-import javax.inject.Inject;
-import javax.persistence.EntityManager;
-import org.apache.deltaspike.core.api.provider.BeanManagerProvider;
-
-import org.apache.deltaspike.core.api.provider.BeanProvider;
-import org.apache.deltaspike.data.impl.meta.RepositoryMetadata;
-import org.apache.deltaspike.jpa.spi.entitymanager.ActiveEntityManagerHolder;
-
-@ApplicationScoped
-public class EntityManagerRefLookup
-{
-    @Inject
-    private ActiveEntityManagerHolder activeEntityManagerHolder;
-
-    private volatile Boolean globalEntityManagerInitialized;
-    private boolean globalEntityManagerIsNormalScope;
-    private EntityManager globalEntityManager;
-    
-    private void lazyInitGlobalEntityManager()
-    {
-        if (this.globalEntityManagerInitialized == null)
-        {
-            initGlobalEntityManager();
-        }
-    }
-
-    private synchronized void initGlobalEntityManager()
-    {
-        // switch into paranoia mode
-        if (this.globalEntityManagerInitialized == null)
-        {
-            this.globalEntityManagerInitialized = true;
-            
-            BeanManager beanManager = BeanManagerProvider.getInstance().getBeanManager();
-            Set<Bean<?>> beans = beanManager.getBeans(EntityManager.class);
-            Bean<?> bean = beanManager.resolve(beans);
-
-            if (bean == null)
-            {
-                throw new IllegalStateException("Could not find EntityManager with default qualifier.");
-            }
-            
-            globalEntityManagerIsNormalScope = beanManager.isNormalScope(bean.getScope());
-            if (globalEntityManagerIsNormalScope)
-            {
-                globalEntityManager = (EntityManager) beanManager.getReference(bean,
-                        EntityManager.class,
-                        beanManager.createCreationalContext(bean));       
-            }
-        }
-    }
-    
-    public EntityManagerRef lookupReference(final RepositoryMetadata repositoryMetadata)
-    {
-        EntityManagerRef ref = new EntityManagerRef();
-
-        if (repositoryMetadata.getEntityManagerResolverClass() != null)
-        {
-            ref.setEntityManagerResolverClass(repositoryMetadata.getEntityManagerResolverClass());
-            
-            if (repositoryMetadata.isEntityManagerResolverIsNormalScope())
-            {
-                ref.setEntityManagerResolver(
-                        BeanProvider.getContextualReference(ref.getEntityManagerResolverClass()));
-            }
-            else
-            {
-                ref.setEntityManagerResolverDependentProvider(
-                        BeanProvider.getDependent(ref.getEntityManagerResolverClass()));
-
-                ref.setEntityManagerResolver(
-                        ref.getEntityManagerResolverDependentProvider().get());
-            }
-            
-            ref.setEntityManager(
-                    ref.getEntityManagerResolver().resolveEntityManager());
-        }
-        else
-        {
-            if (activeEntityManagerHolder.isSet())
-            {
-                ref.setEntityManager(
-                        activeEntityManagerHolder.get());
-                
-                // TODO should we really not apply the FlushMode on the active EntityManager?
-                return ref;
-            }
-            else
-            {
-                lazyInitGlobalEntityManager();
-                if (globalEntityManagerIsNormalScope)
-                {
-                    ref.setEntityManager(globalEntityManager);
-                }
-                else
-                {
-                    ref.setEntityManagerDependentProvider(
-                            BeanProvider.getDependent(EntityManager.class));
-                    ref.setEntityManager(
-                            ref.getEntityManagerDependentProvider().get());
-                }
-            }
-        }
-
-        if (repositoryMetadata.getEntityManagerFlushMode() != null)
-        {
-            ref.getEntityManager().setFlushMode(repositoryMetadata.getEntityManagerFlushMode());
-        }
-
-        return ref;
-    }
-}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/25643153/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/QueryHandler.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/QueryHandler.java b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/QueryHandler.java
index 9767e1d..e17ec2e 100755
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/QueryHandler.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/QueryHandler.java
@@ -29,6 +29,8 @@ import org.apache.deltaspike.data.impl.builder.QueryBuilder;
 import org.apache.deltaspike.data.impl.builder.QueryBuilderFactory;
 import org.apache.deltaspike.data.impl.meta.RepositoryMetadataHandler;
 import org.apache.deltaspike.jpa.api.transaction.Transactional;
+import org.apache.deltaspike.jpa.impl.entitymanager.EntityManagerRef;
+import org.apache.deltaspike.jpa.impl.entitymanager.EntityManagerRefLookup;
 import org.apache.deltaspike.jpa.spi.entitymanager.ActiveEntityManagerHolder;
 import org.apache.deltaspike.jpa.spi.transaction.TransactionStrategy;
 

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/25643153/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/RepositoryMetadata.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/RepositoryMetadata.java b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/RepositoryMetadata.java
index e690128..4610e7a 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/RepositoryMetadata.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/RepositoryMetadata.java
@@ -20,20 +20,15 @@ package org.apache.deltaspike.data.impl.meta;
 
 import java.lang.reflect.Method;
 import java.util.Map;
-import javax.persistence.FlushModeType;
-import org.apache.deltaspike.data.api.EntityManagerResolver;
+import org.apache.deltaspike.jpa.impl.entitymanager.EntityManagerMetadata;
 
-public class RepositoryMetadata
+public class RepositoryMetadata extends EntityManagerMetadata
 {
     private Class<?> repositoryClass;
 
     private EntityMetadata entityMetadata;
     private Map<Method, RepositoryMethodMetadata> methodsMetadata;
 
-    private Class<? extends EntityManagerResolver> entityManagerResolverClass;
-    private boolean entityManagerResolverIsNormalScope;
-    private FlushModeType entityManagerFlushMode;
-    
     public RepositoryMetadata(Class<?> repositoryClass)
     {
         this.repositoryClass = repositoryClass;
@@ -65,36 +60,6 @@ public class RepositoryMetadata
         this.entityMetadata = entityMetadata;
     }
 
-    public Class<? extends EntityManagerResolver> getEntityManagerResolverClass()
-    {
-        return entityManagerResolverClass;
-    }
-
-    public void setEntityManagerResolverClass(Class<? extends EntityManagerResolver> entityManagerResolverClass)
-    {
-        this.entityManagerResolverClass = entityManagerResolverClass;
-    }
-
-    public FlushModeType getEntityManagerFlushMode()
-    {
-        return entityManagerFlushMode;
-    }
-
-    public void setEntityManagerFlushMode(FlushModeType entityManagerFlushMode)
-    {
-        this.entityManagerFlushMode = entityManagerFlushMode;
-    }
-
-    public boolean isEntityManagerResolverIsNormalScope()
-    {
-        return entityManagerResolverIsNormalScope;
-    }
-
-    public void setEntityManagerResolverIsNormalScope(boolean entityManagerResolverIsNormalScope)
-    {
-        this.entityManagerResolverIsNormalScope = entityManagerResolverIsNormalScope;
-    }
-
     public Map<Method, RepositoryMethodMetadata> getMethodsMetadata()
     {
         return methodsMetadata;

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/25643153/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/RepositoryMetadataInitializer.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/RepositoryMetadataInitializer.java b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/RepositoryMetadataInitializer.java
index 2ddcc6d..6ecda3e 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/RepositoryMetadataInitializer.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/RepositoryMetadataInitializer.java
@@ -49,24 +49,25 @@ public class RepositoryMetadataInitializer
     public RepositoryMetadata init(Class<?> repositoryClass, BeanManager beanManager)
     {
         RepositoryMetadata repositoryMetadata = new RepositoryMetadata(repositoryClass);
-        
-        repositoryMetadata.setEntityManagerResolverClass(extractEntityManagerResolver(repositoryClass));
-        repositoryMetadata.setEntityManagerFlushMode(extractEntityManagerFlushMode(repositoryClass));
-
-        if (repositoryMetadata.getEntityManagerResolverClass() != null)
+        // read from looks for JPA Transactional and EntityManagerConfig to determine attributes
+        // if those are set, don't process old annotations
+        if (!repositoryMetadata.readFrom(repositoryClass, beanManager))
         {
-            Set<Bean<?>> beans = beanManager.getBeans(repositoryMetadata.getEntityManagerResolverClass());
-            Class<? extends Annotation> scope = beanManager.resolve(beans).getScope();
+            repositoryMetadata.setEntityManagerResolverClass(extractEntityManagerResolver(repositoryClass));
+            repositoryMetadata.setEntityManagerFlushMode(extractEntityManagerFlushMode(repositoryClass));
 
-            repositoryMetadata.setEntityManagerResolverIsNormalScope(beanManager.isNormalScope(scope));
-        }
-        else
-        {
-            repositoryMetadata.setEntityManagerResolverIsNormalScope(false);
+            if (repositoryMetadata.getEntityManagerResolverClass() != null)
+            {
+                Set<Bean<?>> beans = beanManager.getBeans(repositoryMetadata.getEntityManagerResolverClass());
+                Class<? extends Annotation> scope = beanManager.resolve(beans).getScope();
+                repositoryMetadata.setEntityManagerResolverIsNormalScope(beanManager.isNormalScope(scope));
+            }
+            else
+            {
+                repositoryMetadata.setEntityManagerResolverIsNormalScope(false);
+            }
         }
-        
         repositoryMetadata.setEntityMetadata(entityMetadataInitializer.init(repositoryMetadata));
-
         initializeMethodsMetadata(repositoryMetadata, beanManager);
         
         return repositoryMetadata;

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/25643153/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/EntityManagerTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/EntityManagerTest.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/EntityManagerTest.java
index 608052a..a38c307 100644
--- a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/EntityManagerTest.java
+++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/EntityManagerTest.java
@@ -29,10 +29,7 @@ import javax.inject.Inject;
 
 import org.apache.deltaspike.data.api.QueryInvocationException;
 import org.apache.deltaspike.data.test.domain.Simple;
-import org.apache.deltaspike.data.test.service.SimpleRepositoryWithEntityManager;
-import org.apache.deltaspike.data.test.service.SimpleRepositoryWithEntityManagerResolver;
-import org.apache.deltaspike.data.test.service.Simplistic;
-import org.apache.deltaspike.data.test.service.SimplisticEntityManagerResolver;
+import org.apache.deltaspike.data.test.service.*;
 import org.apache.deltaspike.test.category.WebProfileCategory;
 import org.jboss.arquillian.container.test.api.Deployment;
 import org.jboss.arquillian.junit.Arquillian;
@@ -55,10 +52,15 @@ public class EntityManagerTest
                         SimpleRepositoryWithEntityManagerResolver.class,
                         QualifiedEntityManagerTestProducer.class,
                         NonQualifiedEntityManagerTestProducer.class,
+                        BasicEntityManagerResolver.class,
+                        LegacyRepositoryWithEntityManagerResolver.class,
                         Simplistic.class, SimplisticEntityManagerResolver.class);
     }
 
     @Inject
+    private LegacyRepositoryWithEntityManagerResolver legacyRepository;
+
+    @Inject
     private SimpleRepositoryWithEntityManager repoWithDefaultEm;
 
     @Inject
@@ -89,4 +91,15 @@ public class EntityManagerTest
         fail("Fake EM should have thrown Exception");
     }
 
+    @Test
+    public void shouldWorkWithLegacyConfigAsWell()
+    {
+        // when
+        List<Simple> result = repoWithDefaultEm.findByName("testUseQualifiedEntityManager");
+
+        // then
+        assertNotNull(result);
+        assertEquals(0, result.size());
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/25643153/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/BasicEntityManagerResolver.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/BasicEntityManagerResolver.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/BasicEntityManagerResolver.java
new file mode 100644
index 0000000..7b75b37
--- /dev/null
+++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/BasicEntityManagerResolver.java
@@ -0,0 +1,38 @@
+/*
+ * 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.deltaspike.data.test.service;
+
+import org.apache.deltaspike.data.api.EntityManagerResolver;
+
+import javax.inject.Inject;
+import javax.persistence.EntityManager;
+
+public class BasicEntityManagerResolver implements EntityManagerResolver
+{
+
+    @Inject
+    private EntityManager entityManager;
+
+    @Override
+    public EntityManager resolveEntityManager()
+    {
+        return entityManager;
+    }
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/25643153/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/LegacyRepositoryWithEntityManagerResolver.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/LegacyRepositoryWithEntityManagerResolver.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/LegacyRepositoryWithEntityManagerResolver.java
new file mode 100644
index 0000000..c619173
--- /dev/null
+++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/LegacyRepositoryWithEntityManagerResolver.java
@@ -0,0 +1,36 @@
+/*
+ * 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.deltaspike.data.test.service;
+
+import org.apache.deltaspike.data.api.AbstractEntityRepository;
+import org.apache.deltaspike.data.api.EntityManagerConfig;
+import org.apache.deltaspike.data.api.Repository;
+import org.apache.deltaspike.data.test.domain.Simple;
+
+import java.util.List;
+
+@Repository
+@EntityManagerConfig(entityManagerResolver = BasicEntityManagerResolver.class)
+public abstract class LegacyRepositoryWithEntityManagerResolver extends AbstractEntityRepository<Simple, Long>
+{
+
+    public abstract List<Simple> findByName(String name);
+
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/25643153/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleRepositoryWithEntityManagerResolver.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleRepositoryWithEntityManagerResolver.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleRepositoryWithEntityManagerResolver.java
index dc82e2d..118ae91 100644
--- a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleRepositoryWithEntityManagerResolver.java
+++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleRepositoryWithEntityManagerResolver.java
@@ -21,9 +21,9 @@ package org.apache.deltaspike.data.test.service;
 import java.util.List;
 
 import org.apache.deltaspike.data.api.AbstractEntityRepository;
-import org.apache.deltaspike.data.api.EntityManagerConfig;
 import org.apache.deltaspike.data.api.Repository;
 import org.apache.deltaspike.data.test.domain.Simple;
+import org.apache.deltaspike.jpa.api.entitymanager.EntityManagerConfig;
 
 @Repository
 @EntityManagerConfig(entityManagerResolver = SimplisticEntityManagerResolver.class)

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/25643153/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimplisticEntityManagerResolver.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimplisticEntityManagerResolver.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimplisticEntityManagerResolver.java
index 14f6511..851d0f4 100644
--- a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimplisticEntityManagerResolver.java
+++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimplisticEntityManagerResolver.java
@@ -18,11 +18,11 @@
  */
 package org.apache.deltaspike.data.test.service;
 
+import org.apache.deltaspike.data.api.EntityManagerResolver;
+
 import javax.inject.Inject;
 import javax.persistence.EntityManager;
 
-import org.apache.deltaspike.data.api.EntityManagerResolver;
-
 public class SimplisticEntityManagerResolver implements EntityManagerResolver
 {
 

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/25643153/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/entitymanager/EntityManagerConfig.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/entitymanager/EntityManagerConfig.java b/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/entitymanager/EntityManagerConfig.java
new file mode 100644
index 0000000..0155916
--- /dev/null
+++ b/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/entitymanager/EntityManagerConfig.java
@@ -0,0 +1,58 @@
+/*
+ * 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.deltaspike.jpa.api.entitymanager;
+
+import javax.enterprise.inject.Any;
+import javax.persistence.FlushModeType;
+import java.lang.annotation.Annotation;
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Configure the EntityManager for a specific repository.
+ */
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+@Inherited
+public @interface EntityManagerConfig
+{
+    /**
+     * References the type which provides the EntityManager for a specific repository.
+     * Must be resolvable over the BeanManager.
+     */
+    Class<? extends EntityManagerResolver> entityManagerResolver() default EntityManagerResolver.class;
+
+    /**
+     * If no entityManagerResolver is specified, then these qualifiers will be used to look up an entity manager
+     * @return
+     */
+    Class<? extends Annotation>[] qualifier() default Any.class;
+
+    /**
+     * Set the flush mode for the repository EntityManager.
+     */
+    FlushModeType flushMode() default FlushModeType.AUTO;
+
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/25643153/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/entitymanager/EntityManagerResolver.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/entitymanager/EntityManagerResolver.java b/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/entitymanager/EntityManagerResolver.java
new file mode 100644
index 0000000..b0c5321
--- /dev/null
+++ b/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/entitymanager/EntityManagerResolver.java
@@ -0,0 +1,36 @@
+/*
+ * 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.deltaspike.jpa.api.entitymanager;
+
+import javax.persistence.EntityManager;
+
+/**
+ * Resolve the EntityManager used for a specific repository.
+ * Only necessary if there is more than one persistence unit.
+ *
+ * The container will look in the following order:
+ * - A configured {@link EntityManagerConfig}
+ * - A bean of type <code>EntityManagerResolver</code>
+ * - Otherwise we'll assume there's a single {@link EntityManager}.
+ */
+public interface EntityManagerResolver
+{
+    EntityManager resolveEntityManager();
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/25643153/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/transaction/Transactional.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/transaction/Transactional.java b/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/transaction/Transactional.java
index ba5fec7..00bca2f 100644
--- a/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/transaction/Transactional.java
+++ b/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/transaction/Transactional.java
@@ -58,8 +58,13 @@ public @interface Transactional
      * {@link javax.persistence.EntityManager}s with qualifiers in a called method (of a different bean)
      * which isn't {@link Transactional} itself.
      *
+     * This method is now deprecated, and if you have multiple {@link javax.persistence.EntityManager}s you should use
+     * {@link org.apache.deltaspike.jpa.api.entitymanager.EntityManagerResolver}.
+     * If you want to use qualifiers only, use {@link org.apache.deltaspike.jpa.api.entitymanager.EntityManagerConfig}.
+     *
      * @return target persistence-unit identifier
      */
+    @Deprecated
     @Nonbinding Class<? extends Annotation>[] qualifier() default Any.class;
 
     /**

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/25643153/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/entitymanager/EntityManagerMetadata.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/entitymanager/EntityManagerMetadata.java b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/entitymanager/EntityManagerMetadata.java
new file mode 100644
index 0000000..1e9b902
--- /dev/null
+++ b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/entitymanager/EntityManagerMetadata.java
@@ -0,0 +1,114 @@
+/*
+ * 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.deltaspike.jpa.impl.entitymanager;
+
+import org.apache.deltaspike.jpa.api.entitymanager.EntityManagerConfig;
+import org.apache.deltaspike.jpa.api.entitymanager.EntityManagerResolver;
+import org.apache.deltaspike.jpa.api.transaction.Transactional;
+
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.persistence.FlushModeType;
+import java.lang.annotation.Annotation;
+import java.util.Set;
+
+public class EntityManagerMetadata
+{
+    private Class<? extends EntityManagerResolver> entityManagerResolverClass;
+    private Class<? extends Annotation>[] qualifiers;
+    private boolean entityManagerResolverIsNormalScope;
+    private FlushModeType entityManagerFlushMode;
+
+    public Class<? extends EntityManagerResolver> getEntityManagerResolverClass()
+    {
+        return entityManagerResolverClass;
+    }
+
+    public void setEntityManagerResolverClass(Class<? extends EntityManagerResolver> entityManagerResolverClass)
+    {
+        this.entityManagerResolverClass = entityManagerResolverClass;
+    }
+
+    public FlushModeType getEntityManagerFlushMode()
+    {
+        return entityManagerFlushMode;
+    }
+
+    public void setEntityManagerFlushMode(FlushModeType entityManagerFlushMode)
+    {
+        this.entityManagerFlushMode = entityManagerFlushMode;
+    }
+
+    public boolean isEntityManagerResolverIsNormalScope()
+    {
+        return entityManagerResolverIsNormalScope;
+    }
+
+    public void setEntityManagerResolverIsNormalScope(boolean entityManagerResolverIsNormalScope)
+    {
+        this.entityManagerResolverIsNormalScope = entityManagerResolverIsNormalScope;
+    }
+
+    public Class<? extends Annotation>[] getQualifiers()
+    {
+        return qualifiers;
+    }
+
+    public void setQualifiers(Class<? extends Annotation>[] qualifiers)
+    {
+        this.qualifiers = qualifiers;
+    }
+
+    public boolean readFrom(Class<?> componentClass, BeanManager beanManager)
+    {
+        EntityManagerConfig entityManagerConfig = componentClass.getAnnotation(EntityManagerConfig.class);
+        boolean processed = false;
+        if (entityManagerConfig != null)
+        {
+            processed = true;
+            this.setEntityManagerFlushMode(entityManagerConfig.flushMode());
+            this.setQualifiers(entityManagerConfig.qualifier());
+            Class<? extends EntityManagerResolver> resolverClass = entityManagerConfig.entityManagerResolver();
+            if (!resolverClass.equals(EntityManagerResolver.class))
+            {
+                this.setEntityManagerResolverClass(resolverClass);
+                Set<Bean<?>> beans = beanManager.getBeans(resolverClass);
+                Class<? extends Annotation> scope = beanManager.resolve(beans).getScope();
+                this.setEntityManagerResolverIsNormalScope(beanManager.isNormalScope(scope));
+            }
+            else
+            {
+                this.setEntityManagerResolverIsNormalScope(false);
+            }
+        }
+
+        Transactional transactional = componentClass.getAnnotation(Transactional.class);
+
+        if (transactional != null && this.qualifiers == null)
+        {
+            processed = true;
+            this.setQualifiers(transactional.qualifier());
+        }
+
+        return processed;
+    }
+
+}
+

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/25643153/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/entitymanager/EntityManagerRef.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/entitymanager/EntityManagerRef.java b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/entitymanager/EntityManagerRef.java
new file mode 100644
index 0000000..c28e866
--- /dev/null
+++ b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/entitymanager/EntityManagerRef.java
@@ -0,0 +1,99 @@
+/*
+ * 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.deltaspike.jpa.impl.entitymanager;
+
+import javax.persistence.EntityManager;
+import org.apache.deltaspike.core.api.provider.DependentProvider;
+import org.apache.deltaspike.jpa.api.entitymanager.EntityManagerResolver;
+
+public class EntityManagerRef
+{
+    private EntityManager entityManager;
+    private DependentProvider<? extends EntityManager> entityManagerDependentProvider;
+    
+    private Class<? extends EntityManagerResolver> entityManagerResolverClass;
+    private EntityManagerResolver entityManagerResolver;
+    private DependentProvider<? extends EntityManagerResolver> entityManagerResolverDependentProvider;
+        
+    public void release()
+    {
+        if (entityManagerDependentProvider != null)
+        {
+            entityManagerDependentProvider.destroy();
+        }
+        
+        if (entityManagerResolverDependentProvider != null)
+        {
+            entityManagerResolverDependentProvider.destroy();
+        }
+    }
+
+    public Class<? extends EntityManagerResolver> getEntityManagerResolverClass()
+    {
+        return entityManagerResolverClass;
+    }
+
+    public void setEntityManagerResolverClass(Class<? extends EntityManagerResolver> entityManagerResolverClass)
+    {
+        this.entityManagerResolverClass = entityManagerResolverClass;
+    }
+
+    public DependentProvider<? extends EntityManagerResolver> getEntityManagerResolverDependentProvider()
+    {
+        return entityManagerResolverDependentProvider;
+    }
+
+    public void setEntityManagerResolverDependentProvider(
+            DependentProvider<? extends EntityManagerResolver> entityManagerResolverDependentProvider)
+    {
+        this.entityManagerResolverDependentProvider = entityManagerResolverDependentProvider;
+    }
+
+    public EntityManager getEntityManager()
+    {
+        return entityManager;
+    }
+
+    public void setEntityManager(EntityManager entityManager)
+    {
+        this.entityManager = entityManager;
+    }
+
+    public EntityManagerResolver getEntityManagerResolver()
+    {
+        return entityManagerResolver;
+    }
+
+    public void setEntityManagerResolver(EntityManagerResolver entityManagerResolver)
+    {
+        this.entityManagerResolver = entityManagerResolver;
+    }
+
+    public DependentProvider<? extends EntityManager> getEntityManagerDependentProvider()
+    {
+        return entityManagerDependentProvider;
+    }
+
+    public void setEntityManagerDependentProvider(
+            DependentProvider<? extends EntityManager> entityManagerDependentProvider)
+    {
+        this.entityManagerDependentProvider = entityManagerDependentProvider;
+    }
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/25643153/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/entitymanager/EntityManagerRefLookup.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/entitymanager/EntityManagerRefLookup.java b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/entitymanager/EntityManagerRefLookup.java
new file mode 100644
index 0000000..4566102
--- /dev/null
+++ b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/entitymanager/EntityManagerRefLookup.java
@@ -0,0 +1,136 @@
+/*
+ * 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.deltaspike.jpa.impl.entitymanager;
+
+import java.util.Set;
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.inject.Inject;
+import javax.persistence.EntityManager;
+import org.apache.deltaspike.core.api.provider.BeanManagerProvider;
+
+import org.apache.deltaspike.core.api.provider.BeanProvider;
+import org.apache.deltaspike.jpa.spi.entitymanager.ActiveEntityManagerHolder;
+
+@ApplicationScoped
+public class EntityManagerRefLookup
+{
+    @Inject
+    private ActiveEntityManagerHolder activeEntityManagerHolder;
+
+    private volatile Boolean globalEntityManagerInitialized;
+    private boolean globalEntityManagerIsNormalScope;
+    private EntityManager globalEntityManager;
+    
+    private void lazyInitGlobalEntityManager()
+    {
+        if (this.globalEntityManagerInitialized == null)
+        {
+            initGlobalEntityManager();
+        }
+    }
+
+    private synchronized void initGlobalEntityManager()
+    {
+        // switch into paranoia mode
+        if (this.globalEntityManagerInitialized == null)
+        {
+            this.globalEntityManagerInitialized = true;
+            
+            BeanManager beanManager = BeanManagerProvider.getInstance().getBeanManager();
+            Set<Bean<?>> beans = beanManager.getBeans(EntityManager.class);
+            Bean<?> bean = beanManager.resolve(beans);
+
+            if (bean == null)
+            {
+                throw new IllegalStateException("Could not find EntityManager with default qualifier.");
+            }
+            
+            globalEntityManagerIsNormalScope = beanManager.isNormalScope(bean.getScope());
+            if (globalEntityManagerIsNormalScope)
+            {
+                globalEntityManager = (EntityManager) beanManager.getReference(bean,
+                        EntityManager.class,
+                        beanManager.createCreationalContext(bean));       
+            }
+        }
+    }
+    
+    public EntityManagerRef lookupReference(final EntityManagerMetadata entityManagerMetadata)
+    {
+        EntityManagerRef ref = new EntityManagerRef();
+
+        if (entityManagerMetadata.getEntityManagerResolverClass() != null)
+        {
+            ref.setEntityManagerResolverClass(entityManagerMetadata.getEntityManagerResolverClass());
+            
+            if (entityManagerMetadata.isEntityManagerResolverIsNormalScope())
+            {
+                ref.setEntityManagerResolver(
+                        BeanProvider.getContextualReference(ref.getEntityManagerResolverClass()));
+            }
+            else
+            {
+                ref.setEntityManagerResolverDependentProvider(
+                        BeanProvider.getDependent(ref.getEntityManagerResolverClass()));
+
+                ref.setEntityManagerResolver(
+                        ref.getEntityManagerResolverDependentProvider().get());
+            }
+            
+            ref.setEntityManager(
+                    ref.getEntityManagerResolver().resolveEntityManager());
+        }
+        else
+        {
+            if (activeEntityManagerHolder.isSet())
+            {
+                ref.setEntityManager(
+                        activeEntityManagerHolder.get());
+                
+                // TODO should we really not apply the FlushMode on the active EntityManager?
+                return ref;
+            }
+            else
+            {
+                lazyInitGlobalEntityManager();
+                if (globalEntityManagerIsNormalScope)
+                {
+                    ref.setEntityManager(globalEntityManager);
+                }
+                else
+                {
+                    ref.setEntityManagerDependentProvider(
+                            BeanProvider.getDependent(EntityManager.class));
+                    ref.setEntityManager(
+                            ref.getEntityManagerDependentProvider().get());
+                }
+            }
+        }
+
+        if (entityManagerMetadata.getEntityManagerFlushMode() != null)
+        {
+            ref.getEntityManager().setFlushMode(entityManagerMetadata.getEntityManagerFlushMode());
+        }
+
+        return ref;
+    }
+}


Mime
View raw message