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/12 13:40:48 UTC

tomee git commit: TOMEE-1901 ensure CDI is activated if EJB are present

Repository: tomee
Updated Branches:
  refs/heads/master 112743174 -> 3de570e43


TOMEE-1901 ensure CDI is activated if EJB are present


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

Branch: refs/heads/master
Commit: 3de570e437898b0653c8bc91a8314945e973bcf3
Parents: 1127431
Author: Romain manni-Bucau <rm...@gmail.com>
Authored: Fri Aug 12 15:40:40 2016 +0200
Committer: Romain manni-Bucau <rm...@gmail.com>
Committed: Fri Aug 12 15:40:40 2016 +0200

----------------------------------------------------------------------
 .../openejb/assembler/classic/Assembler.java    |  2 +-
 .../openejb/config/EjbJarInfoBuilder.java       | 23 +++++++
 .../org/apache/openejb/config/EarCdiTest.java   | 71 ++++++++++++++++++++
 3 files changed, 95 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/3de570e4/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java b/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
index 4b34013..06dab32 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
@@ -1062,7 +1062,7 @@ public class Assembler extends AssemblerTool implements org.apache.openejb.spi.A
         if (!"true".equalsIgnoreCase(appInfo.properties.getProperty("openejb.cdi.activated", "true"))) {
             return false;
         }
-        for (final EjbJarInfo ejbJarInfo : appInfo.ejbJars) { // pretty trivial heuristic but fine for 99% of cases
+        for (final EjbJarInfo ejbJarInfo : appInfo.ejbJars) {
             if (ejbJarInfo.beans != null
                     && (!ejbJarInfo.beans.bdas.isEmpty() || !ejbJarInfo.beans.noDescriptorBdas.isEmpty())) {
                 return true;

http://git-wip-us.apache.org/repos/asf/tomee/blob/3de570e4/container/openejb-core/src/main/java/org/apache/openejb/config/EjbJarInfoBuilder.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/config/EjbJarInfoBuilder.java b/container/openejb-core/src/main/java/org/apache/openejb/config/EjbJarInfoBuilder.java
index 1d0ba80..bf3e7c0 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/config/EjbJarInfoBuilder.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/config/EjbJarInfoBuilder.java
@@ -102,6 +102,7 @@ import org.apache.openejb.util.Messages;
 import org.apache.webbeans.spi.BeanArchiveService;
 
 import java.net.MalformedURLException;
+import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
 import java.util.ArrayList;
@@ -313,6 +314,28 @@ public class EjbJarInfoBuilder {
                 bda.decorators.addAll(beans.getDecorators());
                 bda.stereotypeAlternatives.addAll(beans.getAlternativeStereotypes());
             }
+        } else if (!ejbJar.enterpriseBeans.isEmpty() && Boolean.parseBoolean(jar.getProperties().getProperty("openejb.cdi.activated", "true"))) {
+            // TOMEE-1901
+            ejbJar.beans = new BeansInfo();
+            ejbJar.beans.version = "1.1";
+            ejbJar.beans.discoveryMode = "ANNOTATED";
+
+
+            final BeansInfo.BDAInfo bdaInfo = new BeansInfo.BDAInfo();
+            try {
+                bdaInfo.uri = ejbJar.moduleUri == null ? DEFAULT_BEANS_XML_KEY.toURI() : ejbJar.moduleUri;
+                try {
+                    bdaInfo.uri.toURL();
+                } catch (final MalformedURLException | IllegalArgumentException iae) { // test? fake a URI
+                    bdaInfo.uri = URI.create("jar:file://!/" + bdaInfo.uri.toASCIIString() + "/META-INF/beans.xml");
+                }
+            } catch (final URISyntaxException e) {
+                logger.warning(e.getMessage(), e);
+            }
+            ejbJar.beans.noDescriptorBdas.add(bdaInfo);
+            for (final EnterpriseBeanInfo ebi : ejbJar.enterpriseBeans) {
+                bdaInfo.managedClasses.add(ebi.ejbClass);
+            }
         }
 
         return ejbJar;

http://git-wip-us.apache.org/repos/asf/tomee/blob/3de570e4/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
new file mode 100644
index 0000000..f7aade6
--- /dev/null
+++ b/container/openejb-core/src/test/java/org/apache/openejb/config/EarCdiTest.java
@@ -0,0 +1,71 @@
+/*
+ * 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.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.ejb.EJB;
+import javax.ejb.Stateless;
+import javax.inject.Inject;
+
+import static org.junit.Assert.assertEquals;
+
+@RunWith(ApplicationComposer.class)
+public class EarCdiTest {
+    @Module
+    public EjbJar ejb() {
+        return new EjbJar()
+                .enterpriseBean(new StatelessBean(B1.class))
+                .enterpriseBean(new StatelessBean(B2.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 B2 b2;
+
+    @Test
+    public void check() {
+        assertEquals("1", b2.val());
+    }
+
+    @Stateless
+    public static class B1 {
+        public String val() {
+            return "1";
+        }
+    }
+
+    @Stateless
+    public static class B2 {
+        @Inject
+        private B1 b1;
+
+        public String val() {
+            return b1.val();
+        }
+    }
+}