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 2015/01/29 09:12:46 UTC

[1/2] tomee git commit: test for cdi interceptor on ejb

Repository: tomee
Updated Branches:
  refs/heads/develop 21ad55b9f -> 76e3e9e23


test for cdi interceptor on ejb


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

Branch: refs/heads/develop
Commit: 76e3e9e2388a0912608dc33e67b290e91cc2e583
Parents: 7b57314
Author: Romain Manni-Bucau <rm...@apache.org>
Authored: Thu Jan 29 09:12:05 2015 +0100
Committer: Romain Manni-Bucau <rm...@apache.org>
Committed: Thu Jan 29 09:12:39 2015 +0100

----------------------------------------------------------------------
 .../openejb/core/mdb/MDBCdiInterceptorTest.java | 128 +++++++++++++++++++
 1 file changed, 128 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/76e3e9e2/container/openejb-core/src/test/java/org/apache/openejb/core/mdb/MDBCdiInterceptorTest.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/test/java/org/apache/openejb/core/mdb/MDBCdiInterceptorTest.java b/container/openejb-core/src/test/java/org/apache/openejb/core/mdb/MDBCdiInterceptorTest.java
new file mode 100644
index 0000000..b3ce73f
--- /dev/null
+++ b/container/openejb-core/src/test/java/org/apache/openejb/core/mdb/MDBCdiInterceptorTest.java
@@ -0,0 +1,128 @@
+/**
+ * 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.core.mdb;
+
+import org.apache.openejb.junit.ApplicationComposer;
+import org.apache.openejb.testing.Classes;
+import org.apache.openejb.testing.Configuration;
+import org.apache.openejb.testng.PropertiesBuilder;
+import org.apache.openejb.util.NetworkUtil;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.annotation.Resource;
+import javax.ejb.MessageDriven;
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.Interceptor;
+import javax.interceptor.InterceptorBinding;
+import javax.interceptor.InvocationContext;
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import javax.jms.Message;
+import javax.jms.MessageListener;
+import javax.jms.MessageProducer;
+import javax.jms.Queue;
+import javax.jms.Session;
+import javax.jms.TextMessage;
+import java.io.Serializable;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import java.util.Properties;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+@Classes(cdi = true, innerClassesAsBean = true, cdiInterceptors = MDBCdiInterceptorTest.InInterceptor.class)
+@RunWith(ApplicationComposer.class)
+public class MDBCdiInterceptorTest {
+    @Resource(name = "target")
+    private Queue queue;
+
+    @Resource
+    private ConnectionFactory connectionFactory;
+
+    @Test
+    public void sendMessage() throws Exception {
+        Connection connection = null;
+        Session session = null;
+        MessageProducer producer = null;
+        try {
+            connection = connectionFactory.createConnection();
+            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+            final TextMessage requestMessage = session.createTextMessage("sent");
+            producer = session.createProducer(queue);
+            producer.send(requestMessage);
+
+            // wait
+            MyInterceptedMdb.latch.await(1, TimeUnit.MINUTES);
+            assertNotNull(InInterceptor.called);
+            assertEquals("sent", TextMessage.class.cast(InInterceptor.called).getText());
+            assertTrue(MyInterceptedMdb.called);
+        } finally {
+            MdbUtil.close(producer);
+            MdbUtil.close(session);
+            MdbUtil.close(connection);
+        }
+    }
+
+    @Configuration
+    public Properties p() {
+        return new PropertiesBuilder()
+                .p("Default JMS Resource Adapter.BrokerXmlConfig", "broker:(tcp://localhost:" + NetworkUtil.getNextAvailablePort() + ")?useJmx=false")
+                .build();
+    }
+
+    @In
+    @MessageDriven(activationConfig = {
+            @javax.ejb.ActivationConfigProperty(propertyName = "maxSessions", propertyValue = "1"),
+            @javax.ejb.ActivationConfigProperty(propertyName = "maxMessagesPerSessions", propertyValue = "1"),
+            @javax.ejb.ActivationConfigProperty(propertyName = "destination", propertyValue = "target")
+    })
+    public static class MyInterceptedMdb implements MessageListener {
+        private static volatile boolean called = false;
+        private static final CountDownLatch latch = new CountDownLatch(1);
+
+        @Override
+        public void onMessage(final Message message) {
+            called = true;
+            latch.countDown();
+        }
+    }
+
+    @InterceptorBinding
+    @Target(ElementType.TYPE)
+    @Retention(RetentionPolicy.RUNTIME)
+    public static @interface In {
+    }
+
+    @Interceptor
+    @In
+    public static class InInterceptor implements Serializable {
+        private static volatile Message called;
+
+        @AroundInvoke
+        public Object aroundInvoke(final InvocationContext context) throws Exception {
+            called = Message.class.cast(context.getParameters()[0]);
+            return context.proceed();
+        }
+    }
+}


[2/2] tomee git commit: TOMEE-1502 cdi interceptor + MDB

Posted by rm...@apache.org.
TOMEE-1502 cdi interceptor + MDB


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

Branch: refs/heads/develop
Commit: 7b573149824853dc35da463cc10d08390d1b29ab
Parents: 21ad55b
Author: Romain Manni-Bucau <rm...@apache.org>
Authored: Thu Jan 29 09:10:38 2015 +0100
Committer: Romain Manni-Bucau <rm...@apache.org>
Committed: Thu Jan 29 09:12:39 2015 +0100

----------------------------------------------------------------------
 .../src/main/java/org/apache/openejb/cdi/CdiEjbBean.java  | 10 ++++++++--
 .../src/main/java/org/apache/openejb/cdi/CdiPlugin.java   |  4 ++--
 2 files changed, 10 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/7b573149/container/openejb-core/src/main/java/org/apache/openejb/cdi/CdiEjbBean.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/cdi/CdiEjbBean.java b/container/openejb-core/src/main/java/org/apache/openejb/cdi/CdiEjbBean.java
index fdd5019..34cd0b3 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/cdi/CdiEjbBean.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/cdi/CdiEjbBean.java
@@ -244,8 +244,10 @@ public class CdiEjbBean<T> extends BaseEjbBean<T> implements InterceptedMarker,
                 instance = (T) homeLocalBean.create();
             } else if (home != null) {
                 instance = (T) home.create();
-            } else /*if (remote != null)*/ {
+            } else if (remote != null) {
                 instance = (T) remote.create();
+            } else { // shouldn't be called for an MDB
+                throw new IllegalStateException("no interface to proxy for ejb " + beanContext.getEjbName() + ", is this is a MDB maybe you shouldn't use a scope?");
             }
 
             if (isDependentAndStateful) {
@@ -291,7 +293,11 @@ public class CdiEjbBean<T> extends BaseEjbBean<T> implements InterceptedMarker,
     public void initInternals() {
         final List<Class> classes = beanContext.getBusinessLocalInterfaces();
         final boolean noLocalInterface = classes.isEmpty();
-        if (noLocalInterface && beanContext.isLocalbean()) {
+        if (beanContext.getComponentType().isMessageDriven()) {
+            homeLocalBean = null;
+            home = null;
+            remote = null;
+        } else if (noLocalInterface && beanContext.isLocalbean()) {
             homeLocalBean = beanContext.getBusinessLocalBeanHome();
             home = null;
             remote = null;

http://git-wip-us.apache.org/repos/asf/tomee/blob/7b573149/container/openejb-core/src/main/java/org/apache/openejb/cdi/CdiPlugin.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/cdi/CdiPlugin.java b/container/openejb-core/src/main/java/org/apache/openejb/cdi/CdiPlugin.java
index 919bd9b..95bc6fe 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/cdi/CdiPlugin.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/cdi/CdiPlugin.java
@@ -142,9 +142,9 @@ public class CdiPlugin extends AbstractOwbPlugin implements OpenWebBeansJavaEEPl
     }
 
     public void configureDeployments(final List<BeanContext> ejbDeployments) {
-        beans = new WeakHashMap<Class<?>, BeanContext>();
+        beans = new WeakHashMap<>();
         for (final BeanContext deployment : ejbDeployments) {
-            if (deployment.getComponentType().isSession()) {
+            if (deployment.getComponentType().isCdiCompatible()) {
                 if (deployment.isLocalbean() && !deployment.isDynamicallyImplemented()) {
                     beans.put(deployment.get(BeanContext.ProxyClass.class).getProxy(), deployment);
                 }