From 159bef19b6cb193d744eb194fc41846c59e292ed Mon Sep 17 00:00:00 2001 From: ksirigeri Date: Tue, 30 Dec 2025 21:27:59 +0530 Subject: [PATCH 1/2] fix for TOMEE-4560 --- .../core/ivm/EjbObjectProxyHandler.java | 19 +++- .../StatefulBeanRegistryCleanupTest.java | 104 ++++++++++++++++++ 2 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 container/openejb-core/src/test/java/org/apache/openejb/core/stateful/StatefulBeanRegistryCleanupTest.java diff --git a/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/EjbObjectProxyHandler.java b/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/EjbObjectProxyHandler.java index 282e4f8b1be..46e08590939 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/EjbObjectProxyHandler.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/EjbObjectProxyHandler.java @@ -19,6 +19,7 @@ import org.apache.openejb.ApplicationException; import org.apache.openejb.BeanContext; +import org.apache.openejb.ContainerType; import org.apache.openejb.InterfaceType; import org.apache.openejb.InvalidateReferenceException; import org.apache.openejb.OpenEJBException; @@ -35,6 +36,7 @@ import javax.ejb.EJBAccessException; import javax.ejb.EJBLocalObject; import javax.ejb.EJBObject; +import javax.ejb.Remove; import java.io.ObjectStreamException; import java.lang.reflect.Method; import java.rmi.AccessException; @@ -75,7 +77,7 @@ public Object _invoke(final Object p, final Class interfce, final Method m, fina if (logger.isDebugEnabled()) { logger.debug("EjbObjectProxyHandler: invoking method " + methodName + " on " + deploymentID + " with identity " + primaryKey); } - Integer operation = dispatchTable.get(methodName); + Integer operation = getMappedOperation(m); if (operation != null) { if (operation == 3) { if (m.getParameterTypes()[0] != EJBObject.class && m.getParameterTypes()[0] != EJBLocalObject.class) { @@ -85,7 +87,12 @@ public Object _invoke(final Object p, final Class interfce, final Method m, fina operation = m.getParameterTypes().length == 0 ? operation : null; } } - if (operation == null || !interfaceType.isComponent()) { + + + boolean isComponentOperation = operation != null && interfaceType.isComponent(); + boolean isRemoveOperation = dispatchTable.get("remove").equals(operation) && ContainerType.STATEFUL.equals(this.container.getContainerType()); + + if (!isComponentOperation && !isRemoveOperation) { retValue = businessMethod(interfce, m, a, p); } else { switch (operation) { @@ -174,6 +181,14 @@ public Object _invoke(final Object p, final Class interfce, final Method m, fina } } + private static Integer getMappedOperation(final Method m) { + Integer opCode = dispatchTable.get(m.getName()); + if (opCode == null && m.getDeclaredAnnotation(Remove.class) != null) { + opCode = dispatchTable.get("remove"); + } + return opCode; + } + protected Object getEJBHome(final Method method, final Object[] args, final Object proxy) throws Throwable { checkAuthorization(method); return getBeanContext().getEJBHome(); diff --git a/container/openejb-core/src/test/java/org/apache/openejb/core/stateful/StatefulBeanRegistryCleanupTest.java b/container/openejb-core/src/test/java/org/apache/openejb/core/stateful/StatefulBeanRegistryCleanupTest.java new file mode 100644 index 00000000000..c69a4b6ac43 --- /dev/null +++ b/container/openejb-core/src/test/java/org/apache/openejb/core/stateful/StatefulBeanRegistryCleanupTest.java @@ -0,0 +1,104 @@ +/** + * 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.stateful; + +import junit.framework.TestCase; +import org.apache.openejb.OpenEJB; +import org.apache.openejb.assembler.classic.*; +import org.apache.openejb.config.ConfigurationFactory; +import org.apache.openejb.core.OpenEJBInitialContextFactory; +import org.apache.openejb.jee.EjbJar; +import org.apache.openejb.jee.StatefulBean; + +import javax.ejb.Remote; +import javax.ejb.Remove; +import javax.ejb.Stateful; +import javax.naming.Context; +import javax.naming.InitialContext; +import java.util.concurrent.ConcurrentMap; + +import static org.junit.Assert.assertTrue; + +/** + * + * @version $Rev$ $Date$ + */ +public class StatefulBeanRegistryCleanupTest extends TestCase { + + @Override + protected void setUp() throws Exception { + System.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, OpenEJBInitialContextFactory.class.getName()); +// System.setProperty("openejb.validation.output.level" , "VERBOSE"); + + final ConfigurationFactory config = new ConfigurationFactory(); + final Assembler assembler = new Assembler(); + + assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class)); + assembler.createSecurityService(config.configureService(SecurityServiceInfo.class)); + + // containers + final StatefulSessionContainerInfo statefulContainerInfo = config.configureService(StatefulSessionContainerInfo.class); + assembler.createContainer(statefulContainerInfo); + + final EjbJar ejbJar = new EjbJar(); + ejbJar.addEnterpriseBean(new StatefulBean(MyBean.class)); + + assembler.createApplication(config.configureApplication(ejbJar)); + } + + @Override + protected void tearDown() throws Exception { + OpenEJB.destroy(); + } + + public void test() throws Exception { + final Context context = new InitialContext(); + final MyBeanInterface myBean = (MyBeanInterface) context.lookup("MyBeanRemote"); + java.lang.reflect.Field hField = myBean.getClass().getSuperclass().getDeclaredField("h"); + hField.setAccessible(true); + Object hValue = hField.get(myBean); + ConcurrentMap reg = ((StatefulEjbObjectHandler) hValue).getLiveHandleRegistry(); + + myBean.cleanup(); + assertTrue("Live handle registry should be empty after removal", reg.isEmpty()); + } + + public interface MyBeanInterface { + String echo(String string); + + @Remove + void cleanup(); + } + + + @Stateful + @Remote + public static class MyBean implements MyBeanInterface { + + @Override + public String echo(final String string) { + final StringBuilder sb = new StringBuilder(string); + return sb.reverse().toString(); + } + + @Override + public void cleanup() { + System.out.println("cleaning up MyBean instance"); + } + + } +} From 3bf6491b5642396d5319e546443185aec84cd8f9 Mon Sep 17 00:00:00 2001 From: ksirigeri Date: Mon, 5 Jan 2026 19:01:55 +0530 Subject: [PATCH 2/2] removed reflection --- .../core/stateful/StatefulBeanRegistryCleanupTest.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/container/openejb-core/src/test/java/org/apache/openejb/core/stateful/StatefulBeanRegistryCleanupTest.java b/container/openejb-core/src/test/java/org/apache/openejb/core/stateful/StatefulBeanRegistryCleanupTest.java index c69a4b6ac43..364364b09e3 100644 --- a/container/openejb-core/src/test/java/org/apache/openejb/core/stateful/StatefulBeanRegistryCleanupTest.java +++ b/container/openejb-core/src/test/java/org/apache/openejb/core/stateful/StatefulBeanRegistryCleanupTest.java @@ -29,6 +29,7 @@ import javax.ejb.Stateful; import javax.naming.Context; import javax.naming.InitialContext; +import java.lang.reflect.Proxy; import java.util.concurrent.ConcurrentMap; import static org.junit.Assert.assertTrue; @@ -68,10 +69,8 @@ protected void tearDown() throws Exception { public void test() throws Exception { final Context context = new InitialContext(); final MyBeanInterface myBean = (MyBeanInterface) context.lookup("MyBeanRemote"); - java.lang.reflect.Field hField = myBean.getClass().getSuperclass().getDeclaredField("h"); - hField.setAccessible(true); - Object hValue = hField.get(myBean); - ConcurrentMap reg = ((StatefulEjbObjectHandler) hValue).getLiveHandleRegistry(); + StatefulEjbObjectHandler handler = (StatefulEjbObjectHandler) Proxy.getInvocationHandler(myBean); + ConcurrentMap reg = handler.getLiveHandleRegistry(); myBean.cleanup(); assertTrue("Live handle registry should be empty after removal", reg.isEmpty());