You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2016/08/15 13:51:36 UTC

tomee git commit: try to guess if we need to activate cdi or allow to force it for modules with only EJB, this is not 100% spec compliant but save a lot of mem and keep bck compat but still works for most cases

Repository: tomee
Updated Branches:
  refs/heads/master e2c2ef1f4 -> c75c9f8ee


try to guess if we need to activate cdi or allow to force it for modules with only EJB, this is not 100% spec compliant but save a lot of mem and keep bck compat but still works for most cases


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

Branch: refs/heads/master
Commit: c75c9f8eeb02b450261c785763b2235f4d4f944b
Parents: e2c2ef1
Author: Romain manni-Bucau <rm...@gmail.com>
Authored: Mon Aug 15 15:51:20 2016 +0200
Committer: Romain manni-Bucau <rm...@gmail.com>
Committed: Mon Aug 15 15:51:20 2016 +0200

----------------------------------------------------------------------
 .../openejb/config/AnnotationDeployer.java      | 19 +++++-
 .../org/apache/openejb/config/EarCdiTest.java   |  3 +
 .../openejb/config/EarEjbButNoCdiTest.java      | 65 ++++++++++++++++++++
 3 files changed, 85 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/c75c9f8e/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java b/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java
index 35521c3..a151887 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java
@@ -200,6 +200,7 @@ import javax.enterprise.inject.Produces;
 import javax.enterprise.inject.Stereotype;
 import javax.enterprise.inject.spi.DefinitionException;
 import javax.enterprise.inject.spi.Extension;
+import javax.inject.Inject;
 import javax.interceptor.ExcludeClassInterceptors;
 import javax.interceptor.ExcludeDefaultInterceptors;
 import javax.interceptor.Interceptors;
@@ -1504,8 +1505,7 @@ public class AnnotationDeployer implements DynamicDeployer {
 
                 final boolean deployComp;
                 if (beans == null && !ejbModule.getEjbJar().getEnterpriseBeansByEjbName().isEmpty()
-                        && Boolean.parseBoolean(ejbModule.getProperties().getProperty("openejb.cdi.activated", "true"))
-                        && Boolean.parseBoolean(SystemInstance.get().getProperty("openejb.cdi.activated-on-ejb", "true"))) {
+                        && isActivateCdiForEjbOnlyModules(ejbModule)) {
                     logger.info("Activating CDI in ACTIVATED mode in module '" + ejbModule.getModuleUri() + "' cause EJB were found\n" +
                             "  add openejb.cdi.activated=false in application.properties to switch it off or\n" +
                             "  openejb.cdi.activated-on-ejb=false in conf/system.properties" +
@@ -1567,6 +1567,21 @@ public class AnnotationDeployer implements DynamicDeployer {
             return ejbModule;
         }
 
+        private boolean isActivateCdiForEjbOnlyModules(final EjbModule ejbModule) {
+            final String activated = ejbModule.getProperties().getProperty("openejb.cdi.activated");
+            return (activated == null && hasAtInject(ejbModule)) || (activated != null && Boolean.parseBoolean(activated))
+                    || Boolean.parseBoolean(SystemInstance.get().getProperty("openejb.cdi.activated-on-ejb", "false" /*spec should be true but mem + bck compat*/));
+        }
+
+        // quick heuristic to guess if cdi is there, avoid to need more mem when useless
+        private boolean hasAtInject(final EjbModule ejbModule) {
+            final IAnnotationFinder finder = ejbModule.getFinder();
+            return finder != null &&
+                    (!finder.findAnnotatedFields(Inject.class).isEmpty()
+                    || !finder.findAnnotatedConstructors(Inject.class).isEmpty()
+                    || !finder.findAnnotatedMethods(Inject.class).isEmpty());
+        }
+
         private SessionType getSessionType(final Class<?> clazz) {
             if (clazz.isAnnotationPresent(Stateful.class)) {
                 return SessionType.STATEFUL;

http://git-wip-us.apache.org/repos/asf/tomee/blob/c75c9f8e/container/openejb-core/src/test/java/org/apache/openejb/config/EarCdiTest.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/test/java/org/apache/openejb/config/EarCdiTest.java b/container/openejb-core/src/test/java/org/apache/openejb/config/EarCdiTest.java
index f7aade6..3249eb4 100644
--- a/container/openejb-core/src/test/java/org/apache/openejb/config/EarCdiTest.java
+++ b/container/openejb-core/src/test/java/org/apache/openejb/config/EarCdiTest.java
@@ -21,6 +21,7 @@ import org.apache.openejb.jee.StatelessBean;
 import org.apache.openejb.jee.WebApp;
 import org.apache.openejb.junit.ApplicationComposer;
 import org.apache.openejb.testing.Module;
+import org.apache.webbeans.config.WebBeansContext;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
@@ -29,6 +30,7 @@ import javax.ejb.Stateless;
 import javax.inject.Inject;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 
 @RunWith(ApplicationComposer.class)
 public class EarCdiTest {
@@ -50,6 +52,7 @@ public class EarCdiTest {
     @Test
     public void check() {
         assertEquals("1", b2.val());
+        assertNotNull(WebBeansContext.currentInstance());
     }
 
     @Stateless

http://git-wip-us.apache.org/repos/asf/tomee/blob/c75c9f8e/container/openejb-core/src/test/java/org/apache/openejb/config/EarEjbButNoCdiTest.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/test/java/org/apache/openejb/config/EarEjbButNoCdiTest.java b/container/openejb-core/src/test/java/org/apache/openejb/config/EarEjbButNoCdiTest.java
new file mode 100644
index 0000000..efd7e7d
--- /dev/null
+++ b/container/openejb-core/src/test/java/org/apache/openejb/config/EarEjbButNoCdiTest.java
@@ -0,0 +1,65 @@
+/*
+ * 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.openejb.config;
+
+import org.apache.openejb.jee.EjbJar;
+import org.apache.openejb.jee.StatelessBean;
+import org.apache.openejb.jee.WebApp;
+import org.apache.openejb.junit.ApplicationComposer;
+import org.apache.openejb.testing.Module;
+import org.apache.webbeans.config.WebBeansContext;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.ejb.EJB;
+import javax.ejb.Stateless;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+// not fully spec compliant but save a bunch of mem for legacy apps
+// + avoid to breaks existing ones so better than the opposite
+// we support config for that anyway
+@RunWith(ApplicationComposer.class)
+public class EarEjbButNoCdiTest {
+    @Module
+    public EjbJar ejb() {
+        return new EjbJar()
+                .enterpriseBean(new StatelessBean(B1.class));
+    }
+
+    @Module
+    public WebApp web() { // we have shortcut when we have a single module so adding another one ensure we test an "ear"
+        return new WebApp();
+    }
+
+    @EJB
+    private B1 b1;
+
+    @Test
+    public void check() {
+        assertEquals("1", b1.val());
+        assertNull(WebBeansContext.currentInstance());
+    }
+
+    @Stateless
+    public static class B1 {
+        public String val() {
+            return "1";
+        }
+    }
+}