openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1766412 - in /openwebbeans/microwave/trunk: microwave-core/src/main/java/org/apache/microwave/ microwave-core/src/main/java/org/apache/microwave/openwebbeans/ microwave-core/src/main/resources/META-INF/services/ microwave-jpa/ microwave-jp...
Date Mon, 24 Oct 2016 14:29:01 GMT
Author: rmannibucau
Date: Mon Oct 24 14:29:01 2016
New Revision: 1766412

URL: http://svn.apache.org/viewvc?rev=1766412&view=rev
Log:
making the unit info build optional if datasource is not ambiguous

Added:
    openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/openwebbeans/AnyLiteral.java
      - copied, changed from r1766367, openwebbeans/microwave/trunk/microwave-jpa/src/main/java/org/apache/microwave/jpa/internal/AnyLiteral.java
    openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/openwebbeans/DefaultLiteral.java
    openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/openwebbeans/MicrowaveBeansExtension.java
    openwebbeans/microwave/trunk/microwave-core/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
Modified:
    openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/Microwave.java
    openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/openwebbeans/KnowClassesFilter.java
    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
    openwebbeans/microwave/trunk/microwave-jpa/src/test/java/org/apache/microwave/jpa/internal/JpaExtensionTest.java
    openwebbeans/microwave/trunk/microwave-jpa/src/test/java/org/app/JPADao.java

Modified: openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/Microwave.java
URL: http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/Microwave.java?rev=1766412&r1=1766411&r2=1766412&view=diff
==============================================================================
--- openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/Microwave.java
(original)
+++ openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/Microwave.java
Mon Oct 24 14:29:01 2016
@@ -208,13 +208,10 @@ public class Microwave implements AutoCl
 
         ctx.addServletContainerInitializer((c, ctx1) -> {
             ctx.getServletContext().setAttribute("microwave.configuration", configuration);
-            try {
-                new OWBAutoSetup().onStartup(c, ctx1);
-                new CxfCdiAutoSetup().onStartup(c, ctx1);
-                new TomcatAutoInitializer().onStartup(c, ctx1);
-            } finally {
-                ctx.getServletContext().removeAttribute("microwave.configuration");
-            }
+
+            new OWBAutoSetup().onStartup(c, ctx1);
+            new CxfCdiAutoSetup().onStartup(c, ctx1);
+            new TomcatAutoInitializer().onStartup(c, ctx1);
         }, emptySet());
 
         ofNullable(meta.consumer).ifPresent(c -> c.accept(ctx));

Copied: openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/openwebbeans/AnyLiteral.java
(from r1766367, openwebbeans/microwave/trunk/microwave-jpa/src/main/java/org/apache/microwave/jpa/internal/AnyLiteral.java)
URL: http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/openwebbeans/AnyLiteral.java?p2=openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/openwebbeans/AnyLiteral.java&p1=openwebbeans/microwave/trunk/microwave-jpa/src/main/java/org/apache/microwave/jpa/internal/AnyLiteral.java&r1=1766367&r2=1766412&rev=1766412&view=diff
==============================================================================
--- openwebbeans/microwave/trunk/microwave-jpa/src/main/java/org/apache/microwave/jpa/internal/AnyLiteral.java
(original)
+++ openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/openwebbeans/AnyLiteral.java
Mon Oct 24 14:29:01 2016
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.microwave.jpa.internal;
+package org.apache.microwave.openwebbeans;
 
 import javax.enterprise.inject.Any;
 import javax.enterprise.util.AnnotationLiteral;

Added: openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/openwebbeans/DefaultLiteral.java
URL: http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/openwebbeans/DefaultLiteral.java?rev=1766412&view=auto
==============================================================================
--- openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/openwebbeans/DefaultLiteral.java
(added)
+++ openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/openwebbeans/DefaultLiteral.java
Mon Oct 24 14:29:01 2016
@@ -0,0 +1,51 @@
+/*
+ * 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 Default
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.microwave.openwebbeans;
+
+import javax.enterprise.inject.Default;
+import javax.enterprise.util.AnnotationLiteral;
+import java.lang.annotation.Annotation;
+
+public class DefaultLiteral extends AnnotationLiteral<Default> implements Default {
+    public static final DefaultLiteral INSTANCE = new DefaultLiteral();
+
+    private static final String TOSTRING = "@javax.enterprise.inject.Default()";
+    private static final long serialVersionUID = 8867272511520063730L;
+
+    @Override
+    public Class<? extends Annotation> annotationType() {
+        return Default.class;
+    }
+
+    @Override
+    public boolean equals(final Object other) {
+        return Default.class.isInstance(other)
+                || (AnnotationLiteral.class.isInstance(other) && AnnotationLiteral.class.cast(other).annotationType()
== annotationType());
+    }
+
+    @Override
+    public int hashCode() {
+        return 0;
+    }
+
+    @Override
+    public String toString() {
+        return TOSTRING;
+    }
+}

Modified: openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/openwebbeans/KnowClassesFilter.java
URL: http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/openwebbeans/KnowClassesFilter.java?rev=1766412&r1=1766411&r2=1766412&view=diff
==============================================================================
--- openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/openwebbeans/KnowClassesFilter.java
(original)
+++ openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/openwebbeans/KnowClassesFilter.java
Mon Oct 24 14:29:01 2016
@@ -147,7 +147,6 @@ public class KnowClassesFilter implement
         included.add("org.apache.myfaces.cdi");
         // included.add("org.apache.myfaces.application.cdi");
         included.add("org.apache.myfaces.flow.cdi");
-        included.add("org.apache.microwave.cxf.MicrowaveBus");
 
         delegateSkip = new OptimizedExclusionFilter(excluded);
         delegateAccept = new OptimizedExclusionFilter(included);

Added: openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/openwebbeans/MicrowaveBeansExtension.java
URL: http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/openwebbeans/MicrowaveBeansExtension.java?rev=1766412&view=auto
==============================================================================
--- openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/openwebbeans/MicrowaveBeansExtension.java
(added)
+++ openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/openwebbeans/MicrowaveBeansExtension.java
Mon Oct 24 14:29:01 2016
@@ -0,0 +1,34 @@
+/*
+ * 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.microwave.openwebbeans;
+
+import org.apache.microwave.cxf.MicrowaveBus;
+
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.BeforeBeanDiscovery;
+import javax.enterprise.inject.spi.Extension;
+import java.util.stream.Stream;
+
+public class MicrowaveBeansExtension implements Extension {
+    void addBeansFromJava(@Observes final BeforeBeanDiscovery bbd, final BeanManager bm)
{
+        // stream not really needed but here for the pattern in case we need other beans
+        Stream.of(MicrowaveBus.class).forEach(type -> bbd.addAnnotatedType(bm.createAnnotatedType(type)));
+    }
+}

Added: openwebbeans/microwave/trunk/microwave-core/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
URL: http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-core/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension?rev=1766412&view=auto
==============================================================================
--- openwebbeans/microwave/trunk/microwave-core/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
(added)
+++ openwebbeans/microwave/trunk/microwave-core/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
Mon Oct 24 14:29:01 2016
@@ -0,0 +1 @@
+org.apache.microwave.openwebbeans.MicrowaveBeansExtension

Modified: openwebbeans/microwave/trunk/microwave-jpa/README.adoc
URL: http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-jpa/README.adoc?rev=1766412&r1=1766411&r2=1766412&view=diff
==============================================================================
--- openwebbeans/microwave/trunk/microwave-jpa/README.adoc (original)
+++ openwebbeans/microwave/trunk/microwave-jpa/README.adoc Mon Oct 24 14:29:01 2016
@@ -23,13 +23,17 @@ public class JpaConfig {
 }
 ----
 
+TIP: if your application uses a single persistence unit this is optional and
+a default one will be created if a single DataSource bean is available as Bean<?>.
+
 The datasource can be produces as you wish using your own configuration mecanism:
 
 [source,java]
 ----
 @ApplicationScoped
 public class JpaConfig {
-    @Produces // dbcp2 datasource
+    @Produces // dbcp2 datasource for instance
+    @ApplicationScoped
     public DataSource dataSource() {
         final BasicDataSource source = new BasicDataSource();
         source.setDriver(new Driver());

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=1766412&r1=1766411&r2=1766412&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 14:29:01 2016
@@ -5,7 +5,6 @@ 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;
@@ -40,7 +39,7 @@ public class EntityManagerBean implement
         this.synchronization = synchronization;
     }
 
-    void init(final BeanManager bm, final PersistenceUnitInfo info) {
+    void init(final PersistenceUnitInfo info) {
         final PersistenceProvider provider;
         try {
             provider = PersistenceProvider.class.cast(

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=1766412&r1=1766411&r2=1766412&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 14:29:01 2016
@@ -18,8 +18,9 @@
  */
 package org.apache.microwave.jpa.internal;
 
-import org.apache.microwave.jpa.api.Unit;
+import org.apache.microwave.Microwave;
 import org.apache.microwave.jpa.api.PersistenceUnitInfoBuilder;
+import org.apache.microwave.jpa.api.Unit;
 
 import javax.enterprise.context.spi.CreationalContext;
 import javax.enterprise.event.Observes;
@@ -35,15 +36,20 @@ import javax.enterprise.inject.spi.WithA
 import javax.persistence.Embeddable;
 import javax.persistence.Entity;
 import javax.persistence.MappedSuperclass;
+import javax.persistence.SynchronizationType;
 import javax.persistence.spi.PersistenceUnitInfo;
+import javax.servlet.ServletContext;
+import javax.sql.DataSource;
 import java.lang.annotation.Annotation;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.stream.Stream;
 
+import static java.util.Optional.ofNullable;
 import static java.util.function.Function.identity;
 import static java.util.stream.Collectors.toMap;
 
@@ -56,7 +62,7 @@ import static java.util.stream.Collector
 public class JpaExtension implements Extension {
     private final EntityManagerContext entityManagerContext = new EntityManagerContext();
     private final List<String> jpaClasses = new ArrayList<>();
-    private final Map<String, EntityManagerBean> entityManagerBeans = new HashMap<>();
+    private final Map<UnitKey, EntityManagerBean> entityManagerBeans = new HashMap<>();
     private final Collection<Bean<?>> unitBuilders = new ArrayList<>(2);
 
     void addInternals(@Observes final BeforeBeanDiscovery bbd, final BeanManager bm) {
@@ -65,10 +71,10 @@ public class JpaExtension implements Ext
     }
 
     void collectEntityManagerInjections(@Observes final ProcessBean<?> bean) {
-        final Map<String, EntityManagerBean> beans = bean.getBean().getInjectionPoints().stream()
+        final Map<UnitKey, EntityManagerBean> beans = bean.getBean().getInjectionPoints().stream()
                 .filter(i -> i.getAnnotated().isAnnotationPresent(Unit.class))
                 .map(i -> i.getAnnotated().getAnnotation(Unit.class))
-                .collect(toMap(Unit::name, unit -> new EntityManagerBean(entityManagerContext,
unit.name(), unit.synchronization())));
+                .collect(toMap(u -> new UnitKey(u.name(), u.synchronization()), unit ->
new EntityManagerBean(entityManagerContext, unit.name(), unit.synchronization())));
         entityManagerBeans.putAll(beans);
     }
 
@@ -107,16 +113,77 @@ public class JpaExtension implements Ext
                 }).collect(toMap(PersistenceUnitInfo::getPersistenceUnitName, identity()));
 
         entityManagerBeans.forEach((k, e) -> {
-            final PersistenceUnitInfo info = infoIndex.get(k);
+            PersistenceUnitInfo info = infoIndex.get(k.unitName);
             if (info == null) {
+                info = tryCreateDefaultPersistenceUnit(k.unitName, bm);
+            }
+            if (info == null) { // not valid
                 adv.addDeploymentProblem(new IllegalArgumentException("Didn't find any PersistenceUnitInfoBuilder
for " + k));
             } else {
-                e.init(bm, info);
+                e.init(info);
             }
         });
     }
 
-    public EntityManagerContext getEntityManagerContext() {
+    private PersistenceUnitInfo tryCreateDefaultPersistenceUnit(final String unitName, final
BeanManager bm) {
+        final Set<Bean<?>> beans = bm.getBeans(DataSource.class);
+        final Bean<?> bean = bm.resolve(beans);
+        if (bean == null || !bm.isNormalScope(bean.getScope())) {
+            return null;
+        }
+
+        final DataSource ds = DataSource.class.cast(bm.getReference(bean, DataSource.class,
bm.createCreationalContext(null)));
+
+        final PersistenceUnitInfoBuilder builder = new PersistenceUnitInfoBuilder()
+                .setManagedClassNames(jpaClasses)
+                .setExcludeUnlistedClasses(true)
+                .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))));
+        }
+
+        return builder.toInfo();
+    }
+
+    EntityManagerContext getEntityManagerContext() {
         return entityManagerContext;
     }
+
+    private static class UnitKey {
+        private final String unitName;
+        private final SynchronizationType synchronizationType;
+        private final int hash;
+
+        private UnitKey(final String unitName, final SynchronizationType synchronizationType)
{
+            this.unitName = unitName;
+            this.synchronizationType = synchronizationType;
+            this.hash = 31 * unitName.hashCode() + synchronizationType.hashCode();
+        }
+
+        @Override
+        public boolean equals(final Object o) {
+            if (this == o) {
+                return true;
+            }
+            if (o == null || getClass() != o.getClass()) {
+                return false;
+            }
+
+            final UnitKey unitKey = UnitKey.class.cast(o);
+            return unitName.equals(unitKey.unitName) && synchronizationType == unitKey.synchronizationType;
+
+        }
+
+        @Override
+        public int hashCode() {
+            return hash;
+        }
+    }
 }

Modified: openwebbeans/microwave/trunk/microwave-jpa/src/test/java/org/apache/microwave/jpa/internal/JpaExtensionTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-jpa/src/test/java/org/apache/microwave/jpa/internal/JpaExtensionTest.java?rev=1766412&r1=1766411&r2=1766412&view=diff
==============================================================================
--- openwebbeans/microwave/trunk/microwave-jpa/src/test/java/org/apache/microwave/jpa/internal/JpaExtensionTest.java
(original)
+++ openwebbeans/microwave/trunk/microwave-jpa/src/test/java/org/apache/microwave/jpa/internal/JpaExtensionTest.java
Mon Oct 24 14:29:01 2016
@@ -18,6 +18,7 @@
  */
 package org.apache.microwave.jpa.internal;
 
+import org.apache.microwave.Microwave;
 import org.apache.microwave.junit.MicrowaveRule;
 import org.app.JPADao;
 import org.junit.Rule;
@@ -29,7 +30,12 @@ import static org.junit.Assert.assertEqu
 
 public class JpaExtensionTest {
     @Rule
-    public final MicrowaveRule rule = new MicrowaveRule().inject(this);
+    public final MicrowaveRule rule = new MicrowaveRule(
+            new Microwave.Builder().randomHttpPort()
+                    .property("jpa.property.openjpa.RuntimeUnenhancedClasses", "supported")
+                    .property("jpa.property.openjpa.jdbc.SynchronizeMappings", "buildSchema"),
+            "")
+            .inject(this);
 
     @Inject
     private JPADao service;

Modified: openwebbeans/microwave/trunk/microwave-jpa/src/test/java/org/app/JPADao.java
URL: http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-jpa/src/test/java/org/app/JPADao.java?rev=1766412&r1=1766411&r2=1766412&view=diff
==============================================================================
--- openwebbeans/microwave/trunk/microwave-jpa/src/test/java/org/app/JPADao.java (original)
+++ openwebbeans/microwave/trunk/microwave-jpa/src/test/java/org/app/JPADao.java Mon Oct 24
14:29:01 2016
@@ -20,7 +20,6 @@ package org.app;
 
 import org.apache.commons.dbcp2.BasicDataSource;
 import org.apache.microwave.jpa.api.Jpa;
-import org.apache.microwave.jpa.api.PersistenceUnitInfoBuilder;
 import org.apache.microwave.jpa.api.Unit;
 import org.h2.Driver;
 
@@ -55,23 +54,13 @@ public class JPADao {
     @ApplicationScoped
     public static class JpaConfig {
         @Produces
+        @ApplicationScoped
         public DataSource dataSource() {
             final BasicDataSource source = new BasicDataSource();
             source.setDriver(new Driver());
             source.setUrl("jdbc:h2:mem:jpaextensiontest");
             return source;
         }
-
-        @Produces
-        public PersistenceUnitInfoBuilder unit(final DataSource ds) {
-            return new PersistenceUnitInfoBuilder()
-                    .setUnitName("test")
-                    .setDataSource(ds)
-                    .setExcludeUnlistedClasses(true)
-                    .addManagedClazz(User.class)
-                    .addProperty("openjpa.RuntimeUnenhancedClasses", "supported")
-                    .addProperty("openjpa.jdbc.SynchronizeMappings", "buildSchema");
-        }
     }
 
     @Entity



Mime
View raw message