openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1766442 - in /openwebbeans/microwave/trunk/microwave-jpa: README.adoc src/main/java/org/apache/microwave/jpa/internal/EntityManagerBean.java src/main/java/org/apache/microwave/jpa/internal/JpaExtension.java
Date Mon, 24 Oct 2016 20:31:27 GMT
Author: rmannibucau
Date: Mon Oct 24 20:31:27 2016
New Revision: 1766442

URL: http://svn.apache.org/viewvc?rev=1766442&view=rev
Log:
global jpa properties configuration + cdi/bval link with jpa

Modified:
    openwebbeans/microwave/trunk/microwave-jpa/README.adoc
    openwebbeans/microwave/trunk/microwave-jpa/src/main/java/org/apache/microwave/jpa/internal/EntityManagerBean.java
    openwebbeans/microwave/trunk/microwave-jpa/src/main/java/org/apache/microwave/jpa/internal/JpaExtension.java

Modified: openwebbeans/microwave/trunk/microwave-jpa/README.adoc
URL: http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-jpa/README.adoc?rev=1766442&r1=1766441&r2=1766442&view=diff
==============================================================================
--- openwebbeans/microwave/trunk/microwave-jpa/README.adoc (original)
+++ openwebbeans/microwave/trunk/microwave-jpa/README.adoc Mon Oct 24 20:31:27 2016
@@ -43,6 +43,8 @@ public class JpaConfig {
 }
 ----
 
+NOTE: it is recommanded to ensure the `DataSource` is normal-scoped to not get surprises
in term of behavior.
+
 Finally you can inject your entity manager using `@Unit`. Ensure to
 decorate with `@Jpa` a class/method before using the entity manager to activate
 the jpa CDI context:
@@ -93,3 +95,8 @@ public class JPADao {
     }
 }
 ----
+
+== Integration with Bean Validation
+
+The extension will try to find a `ValidatorFactory` in CDI context and will provide ir to
the JPA provider
+if the `ValidationMode` is not `NONE` and a `Bean<ValidatorFactory>` exists.

Modified: openwebbeans/microwave/trunk/microwave-jpa/src/main/java/org/apache/microwave/jpa/internal/EntityManagerBean.java
URL: http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-jpa/src/main/java/org/apache/microwave/jpa/internal/EntityManagerBean.java?rev=1766442&r1=1766441&r2=1766442&view=diff
==============================================================================
--- openwebbeans/microwave/trunk/microwave-jpa/src/main/java/org/apache/microwave/jpa/internal/EntityManagerBean.java
(original)
+++ openwebbeans/microwave/trunk/microwave-jpa/src/main/java/org/apache/microwave/jpa/internal/EntityManagerBean.java
Mon Oct 24 20:31:27 2016
@@ -5,11 +5,13 @@ import org.apache.microwave.jpa.api.Enti
 import javax.enterprise.context.spi.CreationalContext;
 import javax.enterprise.inject.Vetoed;
 import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
 import javax.enterprise.inject.spi.InjectionPoint;
 import javax.enterprise.inject.spi.PassivationCapable;
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.SynchronizationType;
+import javax.persistence.ValidationMode;
 import javax.persistence.spi.PersistenceProvider;
 import javax.persistence.spi.PersistenceUnitInfo;
 import java.lang.annotation.Annotation;
@@ -22,6 +24,7 @@ import java.util.function.Supplier;
 import static java.util.Arrays.asList;
 import static java.util.Collections.emptySet;
 import static java.util.Collections.singletonList;
+import static java.util.Optional.ofNullable;
 
 @Vetoed
 public class EntityManagerBean implements Bean<EntityManager>, PassivationCapable {
@@ -39,7 +42,7 @@ public class EntityManagerBean implement
         this.synchronization = synchronization;
     }
 
-    void init(final PersistenceUnitInfo info) {
+    void init(final PersistenceUnitInfo info, final BeanManager bm) {
         final PersistenceProvider provider;
         try {
             provider = PersistenceProvider.class.cast(
@@ -48,11 +51,27 @@ public class EntityManagerBean implement
             throw new IllegalArgumentException("Bad provider: " + info.getPersistenceProviderClassName());
         }
         final EntityManagerFactory factory = provider.createContainerEntityManagerFactory(info,
new HashMap() {{
-            // TODO: bval + CDI integ
+            put("javax.persistence.bean.manager", bm);
+            if (ValidationMode.NONE != info.getValidationMode()) {
+                ofNullable(findValidatorFactory(bm)).ifPresent(factory -> put("javax.persistence.validation.factory",
factory));
+            }
         }});
         instanceFactory = synchronization == SynchronizationType.SYNCHRONIZED ? factory::createEntityManager
: () -> factory.createEntityManager(synchronization);
     }
 
+    private Object findValidatorFactory(final BeanManager bm) {
+        try {
+            final Class<?> type = Thread.currentThread().getContextClassLoader().loadClass("javax.validation.ValidatorFactory");
+            final Bean<?> bean = bm.resolve(bm.getBeans(type));
+            if (bean == null || !bm.isNormalScope(bean.getScope())) {
+                return null;
+            }
+            return bm.getReference(bean, type, bm.createCreationalContext(null));
+        } catch (final NoClassDefFoundError | ClassNotFoundException e) {
+            return null;
+        }
+    }
+
     @Override
     public EntityManager create(final CreationalContext<EntityManager> context) {
         final EntityManager entityManager = instanceFactory.get();

Modified: openwebbeans/microwave/trunk/microwave-jpa/src/main/java/org/apache/microwave/jpa/internal/JpaExtension.java
URL: http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-jpa/src/main/java/org/apache/microwave/jpa/internal/JpaExtension.java?rev=1766442&r1=1766441&r2=1766442&view=diff
==============================================================================
--- openwebbeans/microwave/trunk/microwave-jpa/src/main/java/org/apache/microwave/jpa/internal/JpaExtension.java
(original)
+++ openwebbeans/microwave/trunk/microwave-jpa/src/main/java/org/apache/microwave/jpa/internal/JpaExtension.java
Mon Oct 24 20:31:27 2016
@@ -102,6 +102,20 @@ public class JpaExtension implements Ext
     }
 
     void initBeans(@Observes final AfterDeploymentValidation adv, final BeanManager bm) {
+        if (entityManagerBeans.isEmpty()) {
+            return;
+        }
+
+        // only not portable part is this config read, could be optional
+        final ServletContext sc = ServletContext.class.cast(bm.getReference(bm.resolve(bm.getBeans(ServletContext.class)),
ServletContext.class, bm.createCreationalContext(null)));
+        final Microwave.Builder config = Microwave.Builder.class.cast(sc.getAttribute("microwave.configuration"));
+        final Map<String, String> props = new HashMap<>();
+        if (config != null) {
+            ofNullable(config.getProperties()).ifPresent(p -> p.stringPropertyNames().stream()
+                    .filter(k -> k.startsWith("jpa.property."))
+                    .forEach(k -> props.put(k.substring("jpa.property.".length()), p.getProperty(k))));
+        }
+
         final Map<String, PersistenceUnitInfo> infoIndex = unitBuilders.stream()
                 .map(bean -> {
                     final CreationalContext<?> cc = bm.createCreationalContext(null);
@@ -114,6 +128,7 @@ public class JpaExtension implements Ext
                         if (builder.getManagedClasses().isEmpty()) {
                             builder.setManagedClassNames(jpaClasses).setExcludeUnlistedClasses(true);
                         }
+                        props.forEach(builder::addProperty);
                         return builder.toInfo();
                     } finally {
                         cc.release();
@@ -123,17 +138,17 @@ public class JpaExtension implements Ext
         entityManagerBeans.forEach((k, e) -> {
             PersistenceUnitInfo info = infoIndex.get(k.unitName);
             if (info == null) {
-                info = tryCreateDefaultPersistenceUnit(k.unitName, bm);
+                info = tryCreateDefaultPersistenceUnit(k.unitName, bm, props);
             }
             if (info == null) { // not valid
                 adv.addDeploymentProblem(new IllegalArgumentException("Didn't find any PersistenceUnitInfoBuilder
for " + k));
             } else {
-                e.init(info);
+                e.init(info, bm);
             }
         });
     }
 
-    private PersistenceUnitInfo tryCreateDefaultPersistenceUnit(final String unitName, final
BeanManager bm) {
+    private PersistenceUnitInfo tryCreateDefaultPersistenceUnit(final String unitName, final
BeanManager bm, final Map<String, String> props) {
         final Set<Bean<?>> beans = bm.getBeans(DataSource.class);
         final Bean<?> bean = bm.resolve(beans);
         if (bean == null || !bm.isNormalScope(bean.getScope())) {
@@ -148,14 +163,7 @@ public class JpaExtension implements Ext
                 .setUnitName(unitName)
                 .setDataSource(ds);
 
-        // only not portable part, we could make it optional
-        final ServletContext sc = ServletContext.class.cast(bm.getReference(bm.resolve(bm.getBeans(ServletContext.class)),
ServletContext.class, bm.createCreationalContext(null)));
-        final Microwave.Builder config = Microwave.Builder.class.cast(sc.getAttribute("microwave.configuration"));
-        if (config != null) {
-            ofNullable(config.getProperties()).ifPresent(p -> p.stringPropertyNames().stream()
-                    .filter(k -> k.startsWith("jpa.property."))
-                    .forEach(k -> builder.addProperty(k.substring("jpa.property.".length()),
p.getProperty(k))));
-        }
+        props.forEach(builder::addProperty);
 
         return builder.toInfo();
     }



Mime
View raw message