Repository: deltaspike
Updated Branches:
  refs/heads/master 28bcefc88 -> 56d56989d


DELTASPIKE-940 - Introduce an SPI based defualt resolver based on qualifiers.

Adding note to not use the deprecated classes.


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

Branch: refs/heads/master
Commit: 56d56989d4aa3f34e4353da42bca428f6a6751d2
Parents: 28bcefc
Author: John D. Ament <johndam...@apache.org>
Authored: Sat Aug 12 17:49:13 2017 -0400
Committer: John D. Ament <johndam...@apache.org>
Committed: Sat Aug 12 17:49:13 2017 -0400

----------------------------------------------------------------------
 .../data/api/EntityManagerConfig.java           |  4 +
 .../impl/meta/RepositoryMetadataHandler.java    | 19 -----
 .../meta/RepositoryMetadataInitializer.java     | 12 ++-
 .../QualifierBackedEntityManagerResolver.java   | 86 ++++++++++++++++++++
 .../entitymanager/EntityManagerMetadata.java    | 11 +++
 .../entitymanager/EntityManagerRefLookup.java   |  8 +-
 .../ResourceLocalTransactionStrategy.java       | 37 +--------
 .../NoEntityManagerProducerTest.java            |  4 +-
 8 files changed, 121 insertions(+), 60 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/56d56989/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 8a1a5f4..b3a032f 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
@@ -26,8 +26,12 @@ import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
 
+import org.apache.deltaspike.jpa.api.entitymanager.EntityManagerResolver;
+
 /**
  * Configure the EntityManager for a specific repository.
+ *
+ * This class is deprecated, instead use {@link 
org.apache.deltaspike.jpa.api.entitymanager.EntityManagerConfig}
  */
 @Target(ElementType.TYPE)
 @Retention(RetentionPolicy.RUNTIME)

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/56d56989/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/RepositoryMetadataHandler.java
----------------------------------------------------------------------
diff --git 
a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/RepositoryMetadataHandler.java
 
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/RepositoryMetadataHandler.java
index 2c03aa8..2e0622f 100644
--- 
a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/RepositoryMetadataHandler.java
+++ 
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/RepositoryMetadataHandler.java
@@ -71,21 +71,6 @@ public class RepositoryMetadataHandler
         }
         throw new RuntimeException("Unknown Repository classes " + 
candidateClasses);
     }
-
-    /**
-     * Lookup the {@link RepositoryMetadata} for a specific Repository class.
-     *
-     * @param repositoryClass The repository class to lookup the method for
-     * @return A {@link RepositoryMetadata}.
-     */
-    public RepositoryMetadata lookupMetadata(Class<?> repositoryClass)
-    {
-        if (repositoriesMetadata.containsKey(repositoryClass))
-        {
-            return repositoriesMetadata.get(repositoryClass);
-        }
-        throw new RuntimeException("Unknown Repository class " + 
repositoryClass.getName());
-    }
     
     /**
      * lookup the {@link RepositoryMethodMetadata} for a specific repository 
and method.
@@ -99,8 +84,4 @@ public class RepositoryMetadataHandler
         return repositoryMetadata.getMethodsMetadata().get(method);
     }
 
-    public Map<Class<?>, RepositoryMetadata> getRepositories()
-    {
-        return repositoriesMetadata;
-    }
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/56d56989/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 6ecda3e..8fc8ff8 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
@@ -33,7 +33,8 @@ import javax.inject.Inject;
 import javax.persistence.FlushModeType;
 
 import org.apache.deltaspike.data.api.EntityManagerConfig;
-import org.apache.deltaspike.data.api.EntityManagerResolver;
+import org.apache.deltaspike.jpa.api.entitymanager.EntityManagerResolver;
+import 
org.apache.deltaspike.jpa.spi.entitymanager.QualifierBackedEntityManagerResolver;
 
 @ApplicationScoped
 public class RepositoryMetadataInitializer
@@ -62,10 +63,17 @@ public class RepositoryMetadataInitializer
                 Class<? extends Annotation> scope = 
beanManager.resolve(beans).getScope();
                 
repositoryMetadata.setEntityManagerResolverIsNormalScope(beanManager.isNormalScope(scope));
             }
-            else
+            else if (repositoryMetadata.getQualifiers() != null)
             {
+                repositoryMetadata.setUnmanagedResolver(new 
QualifierBackedEntityManagerResolver(beanManager,
+                        repositoryMetadata.getQualifiers()));
                 
repositoryMetadata.setEntityManagerResolverIsNormalScope(false);
             }
+            else
+            {
+                throw new IllegalStateException("Unable to locate an 
EntityManagerResolver or Qualifiers for "
+                        + repositoryClass.getName());
+            }
         }
         
repositoryMetadata.setEntityMetadata(entityMetadataInitializer.init(repositoryMetadata));
         initializeMethodsMetadata(repositoryMetadata, beanManager);

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/56d56989/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/spi/entitymanager/QualifierBackedEntityManagerResolver.java
----------------------------------------------------------------------
diff --git 
a/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/spi/entitymanager/QualifierBackedEntityManagerResolver.java
 
b/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/spi/entitymanager/QualifierBackedEntityManagerResolver.java
new file mode 100644
index 0000000..c85aae6
--- /dev/null
+++ 
b/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/spi/entitymanager/QualifierBackedEntityManagerResolver.java
@@ -0,0 +1,86 @@
+/*
+ * 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.spi.entitymanager;
+
+import org.apache.deltaspike.core.api.literal.AnyLiteral;
+import org.apache.deltaspike.jpa.api.entitymanager.EntityManagerResolver;
+
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.persistence.EntityManager;
+import java.lang.annotation.Annotation;
+import java.util.HashSet;
+import java.util.Set;
+
+public class QualifierBackedEntityManagerResolver implements 
EntityManagerResolver
+{
+    private final Class<? extends Annotation>[] qualifiers;
+    private final BeanManager beanManager;
+
+    public QualifierBackedEntityManagerResolver(BeanManager beanManager, 
Class<? extends Annotation>... qualifiers)
+    {
+        this.beanManager = beanManager;
+        this.qualifiers = qualifiers;
+    }
+
+    @Override
+    public EntityManager resolveEntityManager()
+    {
+        Bean<EntityManager> entityManagerBean = resolveEntityManagerBeans();
+
+        if (entityManagerBean == null)
+        {
+            StringBuilder qualifierNames = new StringBuilder();
+            for (Class<?> c : qualifiers)
+            {
+                qualifierNames.append(c.getName()).append(" ");
+            }
+            throw new IllegalStateException("Cannot find an EntityManager 
qualified with [" + qualifierNames
+                    + "]. Did you add a corresponding producer?");
+        }
+
+        return (EntityManager) beanManager.getReference(entityManagerBean, 
EntityManager.class,
+                beanManager.createCreationalContext(entityManagerBean));
+    }
+    private Bean<EntityManager> resolveEntityManagerBeans()
+    {
+        Set<Bean<?>> entityManagerBeans = 
beanManager.getBeans(EntityManager.class, new AnyLiteral());
+        if (entityManagerBeans == null)
+        {
+            entityManagerBeans = new HashSet<Bean<?>>();
+        }
+        for (Class<? extends Annotation> qualifierClass : qualifiers)
+        {
+            for (Bean<?> currentEntityManagerBean : entityManagerBeans)
+            {
+                Set<Annotation> foundQualifierAnnotations = 
currentEntityManagerBean.getQualifiers();
+
+                for (Annotation currentQualifierAnnotation : 
foundQualifierAnnotations)
+                {
+                    if 
(currentQualifierAnnotation.annotationType().equals(qualifierClass))
+                    {
+                        return (Bean<EntityManager>) currentEntityManagerBean;
+                    }
+                }
+            }
+        }
+        return null;
+    }
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/56d56989/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
index 2827fd7..fafba1a 100644
--- 
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
@@ -33,6 +33,7 @@ import java.util.Set;
 public class EntityManagerMetadata
 {
     private Class<? extends EntityManagerResolver> entityManagerResolverClass;
+    private EntityManagerResolver unmanagedResolver;
     private Class<? extends Annotation>[] qualifiers;
     private boolean entityManagerResolverIsNormalScope;
     private FlushModeType entityManagerFlushMode;
@@ -78,6 +79,16 @@ public class EntityManagerMetadata
         this.qualifiers = qualifiers;
     }
 
+    public EntityManagerResolver getUnmanagedResolver()
+    {
+        return unmanagedResolver;
+    }
+
+    public void setUnmanagedResolver(EntityManagerResolver unmanagedResolver)
+    {
+        this.unmanagedResolver = unmanagedResolver;
+    }
+
     public boolean readFrom(AnnotatedElement method, BeanManager beanManager)
     {
         EntityManagerConfig entityManagerConfig = 
method.getAnnotation(EntityManagerConfig.class);

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/56d56989/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
index 4566102..e2e9700 100644
--- 
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
@@ -78,10 +78,14 @@ public class EntityManagerRefLookup
     {
         EntityManagerRef ref = new EntityManagerRef();
 
-        if (entityManagerMetadata.getEntityManagerResolverClass() != null)
+        if (entityManagerMetadata.getUnmanagedResolver() != null)
+        {
+            
ref.setEntityManagerResolver(entityManagerMetadata.getUnmanagedResolver());
+        }
+        else if (entityManagerMetadata.getEntityManagerResolverClass() != null)
         {
             
ref.setEntityManagerResolverClass(entityManagerMetadata.getEntityManagerResolverClass());
-            
+
             if (entityManagerMetadata.isEntityManagerResolverIsNormalScope())
             {
                 ref.setEntityManagerResolver(

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/56d56989/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/ResourceLocalTransactionStrategy.java
----------------------------------------------------------------------
diff --git 
a/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/ResourceLocalTransactionStrategy.java
 
b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/ResourceLocalTransactionStrategy.java
index defa418..4c9cb7b 100644
--- 
a/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/ResourceLocalTransactionStrategy.java
+++ 
b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/ResourceLocalTransactionStrategy.java
@@ -28,20 +28,19 @@ import java.util.logging.Logger;
 
 import javax.enterprise.context.Dependent;
 import javax.enterprise.inject.Default;
-import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.BeanManager;
 import javax.inject.Inject;
 import javax.interceptor.InvocationContext;
 import javax.persistence.EntityManager;
 import javax.persistence.EntityTransaction;
 
-import org.apache.deltaspike.core.api.literal.AnyLiteral;
 import org.apache.deltaspike.core.util.ProxyUtils;
 import org.apache.deltaspike.jpa.api.transaction.Transactional;
 import org.apache.deltaspike.jpa.impl.entitymanager.EntityManagerMetadata;
 import org.apache.deltaspike.jpa.impl.transaction.context.EntityManagerEntry;
 import 
org.apache.deltaspike.jpa.impl.transaction.context.TransactionBeanStorage;
 import org.apache.deltaspike.jpa.spi.entitymanager.ActiveEntityManagerHolder;
+import 
org.apache.deltaspike.jpa.spi.entitymanager.QualifierBackedEntityManagerResolver;
 import org.apache.deltaspike.jpa.spi.transaction.TransactionStrategy;
 
 /**
@@ -379,16 +378,7 @@ public class ResourceLocalTransactionStrategy implements 
TransactionStrategy
         {
             return emHolder.get();
         }
-        Bean<EntityManager> entityManagerBean = 
resolveEntityManagerBean(emQualifier);
-
-        if (entityManagerBean == null)
-        {
-            throw new IllegalStateException("Cannot find an EntityManager 
qualified with [" + emQualifier.getName()
-                    + "]. Did you add a corresponding producer?");
-        }
-
-        return (EntityManager) beanManager.getReference(entityManagerBean, 
EntityManager.class,
-                beanManager.createCreationalContext(entityManagerBean));
+        return new QualifierBackedEntityManagerResolver(beanManager, 
emQualifier).resolveEntityManager();
     }
 
     /**
@@ -406,27 +396,4 @@ public class ResourceLocalTransactionStrategy implements 
TransactionStrategy
     {
         TransactionBeanStorage.close();
     }
-
-    protected Bean<EntityManager> resolveEntityManagerBean(Class<? extends 
Annotation> qualifierClass)
-    {
-        Set<Bean<?>> entityManagerBeans = 
beanManager.getBeans(EntityManager.class, new AnyLiteral());
-        if (entityManagerBeans == null)
-        {
-            entityManagerBeans = new HashSet<Bean<?>>();
-        }
-
-        for (Bean<?> currentEntityManagerBean : entityManagerBeans)
-        {
-            Set<Annotation> foundQualifierAnnotations = 
currentEntityManagerBean.getQualifiers();
-
-            for (Annotation currentQualifierAnnotation : 
foundQualifierAnnotations)
-            {
-                if 
(currentQualifierAnnotation.annotationType().equals(qualifierClass))
-                {
-                    return (Bean<EntityManager>) currentEntityManagerBean;
-                }
-            }
-        }
-        return null;
-    }
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/56d56989/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/noentitymanager/NoEntityManagerProducerTest.java
----------------------------------------------------------------------
diff --git 
a/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/noentitymanager/NoEntityManagerProducerTest.java
 
b/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/noentitymanager/NoEntityManagerProducerTest.java
index 1540d06..6af0bf5 100644
--- 
a/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/noentitymanager/NoEntityManagerProducerTest.java
+++ 
b/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/noentitymanager/NoEntityManagerProducerTest.java
@@ -83,8 +83,8 @@ public class NoEntityManagerProducerTest
              * way to test this behavior but it will be sufficient to ensure 
the user gets all the information required
              * to fix the problem.
              */
-            assertTrue(e.getMessage().contains("EntityManager"));
-            assertTrue(e.getMessage().contains(Second.class.getName()));
+            assertTrue("Expected "+e.getMessage()+" to contain 
EntityManager",e.getMessage().contains("EntityManager"));
+            assertTrue("Expected "+e.getMessage()+" to contain 
Second",e.getMessage().contains(Second.class.getName()));
         }
 
     }

Reply via email to