Repository: tomee Updated Branches: refs/heads/master 64c901a63 -> c80b481b3
TOMEE-1985 SessionBean ejbCreate invoked potentially twice Project: http://git-wip-us.apache.org/repos/asf/tomee/repo Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/c80b481b Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/c80b481b Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/c80b481b Branch: refs/heads/master Commit: c80b481b3f41443157aeacc7985e682d73d7df99 Parents: 64c901a Author: dblevins <[email protected]> Authored: Fri Dec 9 02:32:57 2016 -0800 Committer: dblevins <[email protected]> Committed: Fri Dec 9 02:32:57 2016 -0800 ---------------------------------------------------------------------- .../classic/InterceptorBindingBuilder.java | 7 +- .../stateless/StatelessInstanceManager.java | 17 -- .../StatelessSessionBeanInterfaceTest.java | 181 +++++++++++++++++++ pom.xml | 1 + 4 files changed, 186 insertions(+), 20 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee/blob/c80b481b/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/InterceptorBindingBuilder.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/InterceptorBindingBuilder.java b/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/InterceptorBindingBuilder.java index 7ae4875..15cd4b3 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/InterceptorBindingBuilder.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/InterceptorBindingBuilder.java @@ -99,14 +99,15 @@ public class InterceptorBindingBuilder { if (beanInfo instanceof StatelessBeanInfo || beanInfo instanceof MessageDrivenBeanInfo) { /* * 4.3.10.2 and 4.5.8 - * If the stateless session bean or MDB instance has an ejbCreate method, + * If the stateless session bean or MDB instance has an ejbCreate method, * the container treats the ejbCreate method as the instanceâs PostConstruct method, * and, in this case, the PostConstruct annotation (or deployment descriptor metadata) * can only be applied to the beanâs ejbCreate method. */ final NamedMethodInfo info = new NamedMethodInfo(); info.className = clazz.getName(); - info.methodName = "ejbCreate"; + final Method createMethod = beanContext.getCreateMethod(); + info.methodName = (createMethod != null) ? createMethod.getName(): "ejbCreate"; info.methodParams = new ArrayList<String>(); try { @@ -114,7 +115,7 @@ public class InterceptorBindingBuilder { if (ejbcreate != null) { final CallbackInfo ejbcreateAsPostConstruct = new CallbackInfo(); ejbcreateAsPostConstruct.className = ejbcreate.getDeclaringClass().getName(); - ejbcreateAsPostConstruct.method = "ejbCreate"; + ejbcreateAsPostConstruct.method = ejbcreate.getName(); beanInfo.postConstruct.add(ejbcreateAsPostConstruct); } } catch (final IllegalStateException e) { http://git-wip-us.apache.org/repos/asf/tomee/blob/c80b481b/container/openejb-core/src/main/java/org/apache/openejb/core/stateless/StatelessInstanceManager.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/core/stateless/StatelessInstanceManager.java b/container/openejb-core/src/main/java/org/apache/openejb/core/stateless/StatelessInstanceManager.java index 255d123..aaa91d3 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/core/stateless/StatelessInstanceManager.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/core/stateless/StatelessInstanceManager.java @@ -58,7 +58,6 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.rmi.RemoteException; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -241,22 +240,6 @@ public class StatelessInstanceManager { private Instance createInstance(final ThreadContext callContext, final BeanContext beanContext) throws ApplicationException { try { final InstanceContext context = beanContext.newInstance(); - if (context.getBean() instanceof SessionBean) { - final Operation originalOperation = callContext.getCurrentOperation(); - try { - callContext.setCurrentOperation(Operation.CREATE); - final InterceptorStack ejbCreate = new InterceptorStack( - context.getBean(), - beanContext.getCreateMethod(), - Operation.CREATE, - new ArrayList<InterceptorData>(), - new HashMap<String, Object>() - ); - ejbCreate.invoke(); - } finally { - callContext.setCurrentOperation(originalOperation); - } - } return new Instance(context.getBean(), context.getInterceptors(), context.getCreationalContext()); } catch (Throwable e) { http://git-wip-us.apache.org/repos/asf/tomee/blob/c80b481b/container/openejb-core/src/test/java/org/apache/openejb/core/stateless/StatelessSessionBeanInterfaceTest.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/test/java/org/apache/openejb/core/stateless/StatelessSessionBeanInterfaceTest.java b/container/openejb-core/src/test/java/org/apache/openejb/core/stateless/StatelessSessionBeanInterfaceTest.java new file mode 100644 index 0000000..ad9f106 --- /dev/null +++ b/container/openejb-core/src/test/java/org/apache/openejb/core/stateless/StatelessSessionBeanInterfaceTest.java @@ -0,0 +1,181 @@ +/* + * 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.stateless; + +import org.apache.openejb.jee.EjbJar; +import org.apache.openejb.jee.StatelessBean; +import org.apache.openejb.junit.ApplicationComposer; +import org.apache.openejb.testing.Module; +import org.apache.openejb.util.Join; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; + +import javax.ejb.CreateException; +import javax.ejb.EJB; +import javax.ejb.EJBException; +import javax.ejb.EJBHome; +import javax.ejb.EJBObject; +import javax.ejb.SessionBean; +import javax.ejb.SessionContext; +import java.rmi.RemoteException; +import java.util.List; +import java.util.Stack; + +@RunWith(ApplicationComposer.class) +public class StatelessSessionBeanInterfaceTest extends Assert { + + @EJB + private GreenRemoteHome greenRemoteHome; + + @EJB + private BlueRemoteHome blueRemoteHome; + + @Module + public EjbJar orange() { + final EjbJar ejbJar = new EjbJar(); + { + final StatelessBean bean = new StatelessBean("green", GreenBean.class); + bean.setHomeAndRemote(GreenRemoteHome.class, GreenRemote.class); + ejbJar.addEnterpriseBean(bean); + } + { + final StatelessBean bean = new StatelessBean("blue", BlueBean.class); + bean.setHomeAndRemote(BlueRemoteHome.class, BlueRemote.class); + ejbJar.addEnterpriseBean(bean); + } + + return ejbJar; + } + + @Test + public void testRegularEjbCreate() throws Exception { + lifecycle.clear(); + + assertNotNull(greenRemoteHome); + final GreenRemote greenRemote = greenRemoteHome.create(); + assertEquals("", Join.join("\n", lifecycle)); + + final String s = greenRemote.businessMethod("one", "two"); + assertEquals("setSessionContext\n" + + "ejbCreate\n" + + "businessMethod", Join.join("\n", lifecycle)); + + } + + @Test + public void testNamedEjbCreateMethod() throws Exception { + lifecycle.clear(); + + assertNotNull(blueRemoteHome); + final BlueRemote greenRemote = blueRemoteHome.createObject(); + assertEquals("", Join.join("\n", lifecycle)); + + final String s = greenRemote.businessMethod("one", "two"); + assertEquals("setSessionContext\n" + + "ejbCreate\n" + + "businessMethod", Join.join("\n", lifecycle)); + + } + + private static final List<String> lifecycle = new Stack<String>(); + + + public static class GreenBean implements SessionBean { + + public void ejbCreate() { + lifecycle.add("ejbCreate"); + } + + @Override + public void ejbActivate() throws EJBException, RemoteException { + lifecycle.add("ejbActivate"); + } + + @Override + public void ejbPassivate() throws EJBException, RemoteException { + lifecycle.add("ejbPassivate"); + } + + @Override + public void ejbRemove() throws EJBException, RemoteException { + lifecycle.add("ejbRemove"); + } + + @Override + public void setSessionContext(SessionContext ctx) throws EJBException, RemoteException { + lifecycle.add("setSessionContext"); + } + + public String businessMethod(String x, String y) { + lifecycle.add("businessMethod"); + return Join.join("\n", x, y); + } + + } + + private interface GreenRemoteHome extends EJBHome { + GreenRemote create() throws RemoteException, CreateException; + } + + private interface GreenRemote extends EJBObject { + String businessMethod(String x, String y) throws RemoteException; + } + + + public static class BlueBean implements SessionBean { + + public void ejbCreateObject() { + lifecycle.add("ejbCreate"); + } + + @Override + public void ejbActivate() throws EJBException, RemoteException { + lifecycle.add("ejbActivate"); + } + + @Override + public void ejbPassivate() throws EJBException, RemoteException { + lifecycle.add("ejbPassivate"); + } + + @Override + public void ejbRemove() throws EJBException, RemoteException { + lifecycle.add("ejbRemove"); + } + + @Override + public void setSessionContext(SessionContext ctx) throws EJBException, RemoteException { + lifecycle.add("setSessionContext"); + } + + public String businessMethod(String x, String y) { + lifecycle.add("businessMethod"); + return Join.join("\n", x, y); + } + + } + + private interface BlueRemoteHome extends EJBHome { + BlueRemote createObject() throws RemoteException, CreateException; + } + + private interface BlueRemote extends EJBObject { + String businessMethod(String x, String y) throws RemoteException; + } + +} http://git-wip-us.apache.org/repos/asf/tomee/blob/c80b481b/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index 9e07ac5..479766b 100644 --- a/pom.xml +++ b/pom.xml @@ -30,6 +30,7 @@ <relativePath><!--Resolve on repository--></relativePath> </parent> + <groupId>org.apache.tomee</groupId> <artifactId>tomee-project</artifactId> <packaging>pom</packaging>
