Search in sources :

Example 1 with RemoveException

use of jakarta.ejb.RemoveException in project tomee by apache.

the class ManagedContainer method removeEJBObject.

protected Object removeEJBObject(final BeanContext beanContext, final Object primKey, final Class callInterface, final Method callMethod, Object[] args, final InterfaceType interfaceType) throws OpenEJBException {
    if (primKey == null) {
        throw new NullPointerException("primKey is null");
    }
    final ThreadContext callContext = new ThreadContext(beanContext, primKey);
    final ThreadContext oldCallContext = ThreadContext.enter(callContext);
    try {
        // Security check
        final boolean internalRemove = BeanContext.Removable.class == callMethod.getDeclaringClass();
        if (!internalRemove) {
            checkAuthorization(callMethod, interfaceType);
        }
        // If a bean managed transaction is active, the bean can not be removed
        if (interfaceType.isComponent()) {
            final Instance instance = checkedOutInstances.get(primKey);
            /**
             * According to EJB 3.0 "4.4.4 Restrictions for Transactions" any remove methods
             * from home or component interfaces must not be allowed if the bean instance is
             * in a transaction.  Unfortunately, the Java EE 5 TCK has tests that ignore the
             * restrictions in 4.4.4 and expect beans in transactions can be removed via their
             * home or component interface.   The test to see if the bean instance implements
             * jakarta.ejb.SessionBean is a workaround for passing the TCK while the tests in
             * question can be challenged or the spec can be changed/updated.
             */
            if (instance != null && instance.bean instanceof SessionBean) {
                throw new ApplicationException(new RemoveException("A stateful EJB enrolled in a transaction can not be removed"));
            }
        }
        // Start transaction
        final TransactionPolicy txPolicy = EjbTransactionUtil.createTransactionPolicy(callContext.getBeanContext().getTransactionType(callMethod, interfaceType), callContext);
        Object returnValue = null;
        boolean retain = false;
        Instance instance = null;
        Method runMethod = null;
        try {
            // Obtain instance
            instance = obtainInstance(primKey, callContext);
            // Resume previous Bean transaction if there was one
            if (txPolicy instanceof BeanTransactionPolicy) {
                // Resume previous Bean transaction if there was one
                final SuspendedTransaction suspendedTransaction = instance.getBeanTransaction();
                if (suspendedTransaction != null) {
                    instance.setBeanTransaction(null);
                    final BeanTransactionPolicy beanTxEnv = (BeanTransactionPolicy) txPolicy;
                    beanTxEnv.resumeUserTransaction(suspendedTransaction);
                }
            }
            if (!internalRemove) {
                // Register the entity managers
                registerEntityManagers(instance, callContext);
                // Register for synchronization callbacks
                registerSessionSynchronization(instance, callContext);
                // Setup for remove invocation
                callContext.setCurrentOperation(Operation.REMOVE);
                callContext.setCurrentAllowedStates(null);
                callContext.setInvokedInterface(callInterface);
                runMethod = beanContext.getMatchingBeanMethod(callMethod);
                callContext.set(Method.class, runMethod);
                // Do not pass arguments on home.remove(remote) calls
                final Class<?> declaringClass = callMethod.getDeclaringClass();
                if (declaringClass.equals(EJBHome.class) || declaringClass.equals(EJBLocalHome.class)) {
                    args = new Object[] {};
                }
                // Initialize interceptor stack
                final List<InterceptorData> interceptors = beanContext.getMethodInterceptors(runMethod);
                final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, runMethod, Operation.REMOVE, interceptors, instance.interceptors);
                // Invoke
                if (args == null) {
                    returnValue = interceptorStack.invoke();
                } else {
                    returnValue = interceptorStack.invoke(args);
                }
            }
        } catch (final InvalidateReferenceException e) {
            throw new ApplicationException(e.getRootCause());
        } catch (final Throwable e) {
            if (interfaceType.isBusiness()) {
                retain = beanContext.retainIfExeption(runMethod);
                handleException(callContext, txPolicy, e);
            } else {
                try {
                    handleException(callContext, txPolicy, e);
                } catch (final ApplicationException ae) {
                // Don't throw application exceptions for non-business interface removes
                }
            }
        } finally {
            if (!retain) {
                try {
                    callContext.setCurrentOperation(Operation.PRE_DESTROY);
                    final List<InterceptorData> callbackInterceptors = beanContext.getCallbackInterceptors();
                    final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, null, Operation.PRE_DESTROY, callbackInterceptors, instance.interceptors);
                    interceptorStack.invoke();
                } catch (final Throwable callbackException) {
                    final String logMessage = "An unexpected exception occured while invoking the preDestroy method on the removed Stateful SessionBean instance; " + callbackException.getClass().getName() + " " + callbackException.getMessage();
                    /* [1] Log the exception or error */
                    logger.error(logMessage);
                } finally {
                    callContext.setCurrentOperation(Operation.REMOVE);
                }
                // todo destroy extended persistence contexts
                discardInstance(callContext);
            }
            // Commit transaction
            afterInvoke(callContext, txPolicy, instance);
        }
        return returnValue;
    } finally {
        ThreadContext.exit(oldCallContext);
    }
}
Also used : BeanTransactionPolicy(org.apache.openejb.core.transaction.BeanTransactionPolicy) SystemInstance(org.apache.openejb.loader.SystemInstance) EJBHome(jakarta.ejb.EJBHome) ThreadContext(org.apache.openejb.core.ThreadContext) JtaTransactionPolicy(org.apache.openejb.core.transaction.JtaTransactionPolicy) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) BeanTransactionPolicy(org.apache.openejb.core.transaction.BeanTransactionPolicy) Method(java.lang.reflect.Method) SuspendedTransaction(org.apache.openejb.core.transaction.BeanTransactionPolicy.SuspendedTransaction) SessionBean(jakarta.ejb.SessionBean) EJBLocalHome(jakarta.ejb.EJBLocalHome) BeanContext(org.apache.openejb.BeanContext) InvalidateReferenceException(org.apache.openejb.InvalidateReferenceException) ApplicationException(org.apache.openejb.ApplicationException) RemoveException(jakarta.ejb.RemoveException) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) InterceptorStack(org.apache.openejb.core.interceptor.InterceptorStack)

Example 2 with RemoveException

use of jakarta.ejb.RemoveException in project tomee by apache.

the class BasicBmp2DataSourcesBean method ejbRemove.

/**
 * A container invokes this method before it removes the EJB object
 * that is currently associated with the instance. This method
 * is invoked when a client invokes a remove operation on the
 * enterprise Bean's home interface or the EJB object's remote interface.
 * This method transitions the instance from the ready state to the pool
 * of available instances.
 */
public void ejbRemove() throws RemoveException, EJBException, RemoteException {
    try {
        final InitialContext jndiContext = new InitialContext();
        final DataSource ds = (DataSource) jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabase");
        final Connection con = ds.getConnection();
        try {
            final PreparedStatement stmt = con.prepareStatement("delete from entity where id = ?");
            try {
                final Integer primaryKey = (Integer) ejbContext.getPrimaryKey();
                stmt.setInt(1, primaryKey.intValue());
                stmt.executeUpdate();
            } finally {
                stmt.close();
            }
        } finally {
            con.close();
        }
    } catch (final Exception e) {
        e.printStackTrace();
        throw new jakarta.ejb.EJBException(e);
    }
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) EJBException(jakarta.ejb.EJBException) InitialContext(javax.naming.InitialContext) FinderException(jakarta.ejb.FinderException) RemoteException(java.rmi.RemoteException) RemoveException(jakarta.ejb.RemoveException) EJBException(jakarta.ejb.EJBException) DataSource(javax.sql.DataSource)

Example 3 with RemoveException

use of jakarta.ejb.RemoveException in project tomee by apache.

the class CmpContainer method ejbRemove.

private void ejbRemove(final EntityBean entityBean) throws RemoveException {
    if (entityBean == null) {
        throw new NullPointerException("entityBean is null");
    }
    if (isDeleted(entityBean)) {
        return;
    }
    final ThreadContext callContext = createThreadContext(entityBean);
    callContext.setCurrentOperation(Operation.REMOVE);
    final ThreadContext oldCallContext = ThreadContext.enter(callContext);
    try {
        entityBean.ejbRemove();
    } catch (final RemoteException e) {
        throw new EJBException(e);
    } finally {
        // todo replace with interface call when CmpEntityBean interface is added
        try {
            entityBean.getClass().getMethod("OpenEJB_deleted").invoke(entityBean);
        } catch (final Exception ignored) {
        // no-op
        }
        cancelTimers(callContext);
        ThreadContext.exit(oldCallContext);
    }
}
Also used : ThreadContext(org.apache.openejb.core.ThreadContext) RemoteException(java.rmi.RemoteException) EJBException(jakarta.ejb.EJBException) OpenEJBException(org.apache.openejb.OpenEJBException) FinderException(jakarta.ejb.FinderException) EJBAccessException(jakarta.ejb.EJBAccessException) EJBException(jakarta.ejb.EJBException) OpenEJBException(org.apache.openejb.OpenEJBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RemoteException(java.rmi.RemoteException) NoSuchObjectException(java.rmi.NoSuchObjectException) EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) EjbTransactionUtil.handleSystemException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException) ObjectNotFoundException(jakarta.ejb.ObjectNotFoundException) ApplicationException(org.apache.openejb.ApplicationException) RemoveException(jakarta.ejb.RemoveException)

Example 4 with RemoveException

use of jakarta.ejb.RemoveException in project tomee by apache.

the class StatefulContainer method removeEJBObject.

protected Object removeEJBObject(final BeanContext beanContext, final Object primKey, final Class callInterface, final Method callMethod, Object[] args, final InterfaceType interfaceType) throws OpenEJBException {
    if (primKey == null) {
        throw new NullPointerException("primKey is null");
    }
    final CdiEjbBean cdiEjbBean = beanContext.get(CdiEjbBean.class);
    if (cdiEjbBean != null) {
        final Class scope = cdiEjbBean.getScope();
        if (callMethod.getDeclaringClass() != BeanContext.Removable.class && scope != Dependent.class) {
            throw new UnsupportedOperationException("Can not call EJB Stateful Bean Remove Method without scoped @Dependent.  Found scope: @" + scope.getSimpleName());
        }
    }
    final boolean internalRemove = BeanContext.Removable.class == callMethod.getDeclaringClass();
    final ThreadContext callContext = new ThreadContext(beanContext, primKey);
    final ThreadContext oldCallContext = ThreadContext.enter(callContext);
    Object runAs = null;
    try {
        if (oldCallContext != null) {
            final BeanContext oldBc = oldCallContext.getBeanContext();
            if (oldBc.getRunAsUser() != null || oldBc.getRunAs() != null) {
                runAs = AbstractSecurityService.class.cast(securityService).overrideWithRunAsContext(callContext, beanContext, oldBc);
            }
        }
        // Security check
        if (!internalRemove) {
            checkAuthorization(callMethod, interfaceType);
        }
        // If a bean managed transaction is active, the bean can not be removed
        if (interfaceType.isComponent()) {
            final Instance instance = checkedOutInstances.get(primKey);
            /**
             * According to EJB 3.0 "4.4.4 Restrictions for Transactions" any remove methods
             * from home or component interfaces must not be allowed if the bean instance is
             * in a transaction.  Unfortunately, the Java EE 5 TCK has tests that ignore the
             * restrictions in 4.4.4 and expect beans in transactions can be removed via their
             * home or component interface.   The test to see if the bean instance implements
             * jakarta.ejb.SessionBean is a workaround for passing the TCK while the tests in
             * question can be challenged or the spec can be changed/updated.
             */
            if (instance != null && instance.bean instanceof SessionBean) {
                throw new ApplicationException(new RemoveException("A stateful EJB enrolled in a transaction can not be removed"));
            }
        }
        // Start transaction
        final TransactionPolicy txPolicy = EjbTransactionUtil.createTransactionPolicy(callContext.getBeanContext().getTransactionType(callMethod, interfaceType), callContext);
        Object returnValue = null;
        boolean retain = false;
        Instance instance = null;
        Method runMethod = null;
        try {
            // Obtain instance
            instance = obtainInstance(primKey, callContext, callMethod, beanContext.isPassivatingScope());
            // Resume previous Bean transaction if there was one
            if (txPolicy instanceof BeanTransactionPolicy) {
                // Resume previous Bean transaction if there was one
                final SuspendedTransaction suspendedTransaction = instance.getBeanTransaction();
                if (suspendedTransaction != null) {
                    instance.setBeanTransaction(null);
                    final BeanTransactionPolicy beanTxEnv = (BeanTransactionPolicy) txPolicy;
                    beanTxEnv.resumeUserTransaction(suspendedTransaction);
                }
            }
            if (!internalRemove) {
                // Register the entity managers
                registerEntityManagers(instance, callContext);
                // Register for synchronization callbacks
                registerSessionSynchronization(instance, callContext);
                // Setup for remove invocation
                callContext.setCurrentOperation(Operation.REMOVE);
                callContext.setCurrentAllowedStates(null);
                callContext.setInvokedInterface(callInterface);
                runMethod = beanContext.getMatchingBeanMethod(callMethod);
                callContext.set(Method.class, runMethod);
                // Do not pass arguments on home.remove(remote) calls
                final Class<?> declaringClass = callMethod.getDeclaringClass();
                if (declaringClass.equals(EJBHome.class) || declaringClass.equals(EJBLocalHome.class)) {
                    args = new Object[] {};
                }
                // Initialize interceptor stack
                final List<InterceptorData> interceptors = beanContext.getMethodInterceptors(runMethod);
                final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, runMethod, Operation.REMOVE, interceptors, instance.interceptors);
                // Invoke
                final CdiEjbBean<Object> bean = beanContext.get(CdiEjbBean.class);
                if (bean != null) {
                    // TODO: see if it should be called before or after next call
                    bean.getInjectionTarget().preDestroy(instance.bean);
                }
                if (args == null) {
                    returnValue = interceptorStack.invoke();
                } else {
                    returnValue = interceptorStack.invoke(args);
                }
            }
        } catch (final InvalidateReferenceException e) {
            throw new ApplicationException(e.getRootCause());
        } catch (final Throwable e) {
            if (interfaceType.isBusiness()) {
                retain = beanContext.retainIfExeption(runMethod);
                handleException(callContext, txPolicy, e);
            } else {
                try {
                    handleException(callContext, txPolicy, e);
                } catch (final ApplicationException ae) {
                // Don't throw application exceptions for non-business interface removes
                }
            }
        } finally {
            if (runAs != null) {
                try {
                    securityService.associate(runAs);
                } catch (final LoginException e) {
                // no-op
                }
            }
            if (!retain) {
                try {
                    callContext.setCurrentOperation(Operation.PRE_DESTROY);
                    final List<InterceptorData> callbackInterceptors = beanContext.getCallbackInterceptors();
                    if (instance != null) {
                        final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, null, Operation.PRE_DESTROY, callbackInterceptors, instance.interceptors);
                        interceptorStack.invoke();
                    }
                } catch (final Throwable t) {
                    final String logMessage = "An unexpected exception occured while invoking the preDestroy method on the Stateful SessionBean instance: " + (null != instance ? instance.bean.getClass().getName() : beanContext.getBeanClass().getName());
                    logger.error(logMessage, t);
                } finally {
                    callContext.setCurrentOperation(Operation.REMOVE);
                }
                discardInstance(primKey, instance);
            }
            // un register EntityManager
            final Map<EntityManagerFactory, JtaEntityManagerRegistry.EntityManagerTracker> unregisteredEntityManagers = unregisterEntityManagers(instance, callContext);
            // Commit transaction
            afterInvoke(callContext, txPolicy, instance);
            // Un register and close extended persistence contexts
            /*
                7.6.2 Container-managed Extended Persistence Context
                A container-managed extended persistence context can only be initiated within the scope of a stateful
                session bean. It exists from the point at which the stateful session bean that declares a dependency on an
                entity manager of type PersistenceContextType.EXTENDED is created, and is said to be bound
                to the stateful session bean. The dependency on the extended persistence context is declared by means
                of the PersistenceContext annotation or persistence-context-ref deployment descriptor element.
                The persistence context is closed by the container when the @Remove method of the stateful session
                bean completes (or the stateful session bean instance is otherwise destroyed).
                */
            closeEntityManagers(unregisteredEntityManagers);
        }
        return returnValue;
    } finally {
        ThreadContext.exit(oldCallContext);
    }
}
Also used : BeanTransactionPolicy(org.apache.openejb.core.transaction.BeanTransactionPolicy) SystemInstance(org.apache.openejb.loader.SystemInstance) CdiEjbBean(org.apache.openejb.cdi.CdiEjbBean) Dependent(jakarta.enterprise.context.Dependent) SessionBean(jakarta.ejb.SessionBean) EJBHome(jakarta.ejb.EJBHome) ThreadContext(org.apache.openejb.core.ThreadContext) JtaTransactionPolicy(org.apache.openejb.core.transaction.JtaTransactionPolicy) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) BeanTransactionPolicy(org.apache.openejb.core.transaction.BeanTransactionPolicy) Method(java.lang.reflect.Method) SuspendedTransaction(org.apache.openejb.core.transaction.BeanTransactionPolicy.SuspendedTransaction) EJBLocalHome(jakarta.ejb.EJBLocalHome) BeanContext(org.apache.openejb.BeanContext) InvalidateReferenceException(org.apache.openejb.InvalidateReferenceException) ApplicationException(org.apache.openejb.ApplicationException) RemoveException(jakarta.ejb.RemoveException) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) InterceptorStack(org.apache.openejb.core.interceptor.InterceptorStack) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) LoginException(javax.security.auth.login.LoginException)

Example 5 with RemoveException

use of jakarta.ejb.RemoveException in project tomee by apache.

the class EntityEjbHomeHandler method removeByPrimaryKey.

protected Object removeByPrimaryKey(final Class interfce, final Method method, final Object[] args, final Object proxy) throws Throwable {
    final Object primKey = args[0];
    // Check for the common mistake of passing the ejbObject instead of ejbObject.getPrimaryKey()
    if (primKey instanceof EJBLocalObject) {
        final Class ejbObjectProxyClass = primKey.getClass();
        String ejbObjectName = null;
        for (final Class clazz : ejbObjectProxyClass.getInterfaces()) {
            if (EJBLocalObject.class.isAssignableFrom(clazz)) {
                ejbObjectName = clazz.getSimpleName();
                break;
            }
        }
        throw new RemoveException("Invalid argument '" + ejbObjectName + "', expected primary key.  Update to ejbLocalHome.remove(" + lcfirst(ejbObjectName) + ".getPrimaryKey())");
    } else if (primKey instanceof EJBObject) {
        final Class ejbObjectProxyClass = primKey.getClass();
        String ejbObjectName = null;
        for (final Class clazz : ejbObjectProxyClass.getInterfaces()) {
            if (EJBObject.class.isAssignableFrom(clazz)) {
                ejbObjectName = clazz.getSimpleName();
                break;
            }
        }
        throw new RemoveException("Invalid argument '" + ejbObjectName + "', expected primary key.  Update to ejbHome.remove(" + lcfirst(ejbObjectName) + ".getPrimaryKey())");
    }
    container.invoke(deploymentID, interfaceType, interfce, method, args, primKey);
    /* 
        * This operation takes care of invalidating all the EjbObjectProxyHandlers associated with
        * the same RegistryId. See this.createProxy().
        */
    invalidateAllHandlers(EntityEjbObjectHandler.getRegistryId(container, deploymentID, primKey));
    return null;
}
Also used : RemoveException(jakarta.ejb.RemoveException) EJBObject(jakarta.ejb.EJBObject) EJBObject(jakarta.ejb.EJBObject) EJBLocalObject(jakarta.ejb.EJBLocalObject) EJBLocalObject(jakarta.ejb.EJBLocalObject)

Aggregations

RemoveException (jakarta.ejb.RemoveException)6 EJBException (jakarta.ejb.EJBException)3 FinderException (jakarta.ejb.FinderException)3 RemoteException (java.rmi.RemoteException)3 ApplicationException (org.apache.openejb.ApplicationException)3 ThreadContext (org.apache.openejb.core.ThreadContext)3 EJBHome (jakarta.ejb.EJBHome)2 EJBLocalHome (jakarta.ejb.EJBLocalHome)2 SessionBean (jakarta.ejb.SessionBean)2 Method (java.lang.reflect.Method)2 NoSuchObjectException (java.rmi.NoSuchObjectException)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 InitialContext (javax.naming.InitialContext)2 DataSource (javax.sql.DataSource)2 BeanContext (org.apache.openejb.BeanContext)2 InvalidateReferenceException (org.apache.openejb.InvalidateReferenceException)2 InterceptorData (org.apache.openejb.core.interceptor.InterceptorData)2 InterceptorStack (org.apache.openejb.core.interceptor.InterceptorStack)2 BeanTransactionPolicy (org.apache.openejb.core.transaction.BeanTransactionPolicy)2