Search in sources :

Example 16 with SystemException

use of org.apache.openejb.SystemException in project tomee by apache.

the class RAFPassivater method passivate.

public synchronized void passivate(final Map stateTable) throws SystemException {
    try (final RandomAccessFile ras = new RandomAccessFile(JavaSecurityManagers.getSystemProperty("java.io.tmpdir", File.separator + "tmp") + File.separator + "passivation" + fileID + ".ser", "rw")) {
        fileID++;
        final Iterator iterator = stateTable.keySet().iterator();
        Pointer lastPointer = null;
        while (iterator.hasNext()) {
            final Object id = iterator.next();
            final Object obj = stateTable.get(id);
            final byte[] bytes = Serializer.serialize(obj);
            final long filepointer = ras.getFilePointer();
            if (lastPointer == null) {
                lastPointer = new Pointer(fileID, filepointer, (int) filepointer);
            } else {
                lastPointer = new Pointer(fileID, filepointer, (int) (filepointer - lastPointer.filepointer));
            }
            masterTable.put(id, lastPointer);
            ras.write(bytes);
        }
    } catch (final Exception e) {
        throw new SystemException(e);
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) SystemException(org.apache.openejb.SystemException) Iterator(java.util.Iterator) SystemException(org.apache.openejb.SystemException)

Example 17 with SystemException

use of org.apache.openejb.SystemException in project tomee by apache.

the class MdbContainer method beforeDelivery.

public void beforeDelivery(final BeanContext deployInfo, final Object instance, final Method method, final XAResource xaResource) throws SystemException {
    // intialize call context
    final ThreadContext callContext = new ThreadContext(deployInfo, null);
    final ThreadContext oldContext = ThreadContext.enter(callContext);
    // create mdb context
    final MdbCallContext mdbCallContext = new MdbCallContext();
    callContext.set(MdbCallContext.class, mdbCallContext);
    mdbCallContext.deliveryMethod = method;
    mdbCallContext.oldCallContext = oldContext;
    // call the tx before method
    try {
        mdbCallContext.txPolicy = createTransactionPolicy(deployInfo.getTransactionType(method), callContext);
        // if we have an xaResource and a transaction was not imported from the adapter, enlist the xaResource
        if (xaResource != null && mdbCallContext.txPolicy.isNewTransaction()) {
            mdbCallContext.txPolicy.enlistResource(xaResource);
        }
    } catch (final ApplicationException e) {
        ThreadContext.exit(oldContext);
        throw new SystemException("Should never get an Application exception", e);
    } catch (final SystemException e) {
        ThreadContext.exit(oldContext);
        throw e;
    } catch (final Exception e) {
        ThreadContext.exit(oldContext);
        throw new SystemException("Unable to enlist xa resource in the transaction", e);
    }
}
Also used : EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) ApplicationException(org.apache.openejb.ApplicationException) SystemException(org.apache.openejb.SystemException) EjbTransactionUtil.handleSystemException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException) ThreadContext(org.apache.openejb.core.ThreadContext) ResourceException(javax.resource.ResourceException) NamingException(javax.naming.NamingException) OpenEJBException(org.apache.openejb.OpenEJBException) UnavailableException(javax.resource.spi.UnavailableException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SystemException(org.apache.openejb.SystemException) EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) EjbTransactionUtil.handleSystemException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException) ApplicationException(org.apache.openejb.ApplicationException) ConstraintViolationException(javax.validation.ConstraintViolationException)

Example 18 with SystemException

use of org.apache.openejb.SystemException in project tomee by apache.

the class MdbContainer method invoke.

public Object invoke(final Object deploymentId, final InterfaceType type, final Class callInterface, final Method method, final Object[] args, final Object primKey) throws OpenEJBException {
    final BeanContext beanContext = getBeanContext(deploymentId);
    final EndpointFactory endpointFactory = (EndpointFactory) beanContext.getContainerData();
    final MdbInstanceFactory instanceFactory = endpointFactory.getInstanceFactory();
    final Instance instance;
    try {
        instance = (Instance) instanceFactory.createInstance(true);
    } catch (final UnavailableException e) {
        throw new SystemException("Unable to create instance for invocation", e);
    }
    try {
        beforeDelivery(beanContext, instance, method, null);
        final Object value = invoke(instance, method, type, args);
        afterDelivery(instance);
        return value;
    } finally {
        instanceFactory.freeInstance(instance, true);
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) SystemException(org.apache.openejb.SystemException) EjbTransactionUtil.handleSystemException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException) SystemInstance(org.apache.openejb.loader.SystemInstance) UnavailableException(javax.resource.spi.UnavailableException)

Example 19 with SystemException

use of org.apache.openejb.SystemException in project tomee by apache.

the class EntityInstanceManager method getPooledInstance.

protected EntityBean getPooledInstance(final ThreadContext callContext) throws OpenEJBException {
    final BeanContext beanContext = callContext.getBeanContext();
    final Stack methodReadyPool = poolMap.get(beanContext.getDeploymentID());
    if (methodReadyPool == null) {
        throw new SystemException("Invalid deployment id " + beanContext.getDeploymentID() + " for this container");
    }
    EntityBean bean = (EntityBean) methodReadyPool.pop();
    if (bean == null) {
        try {
            bean = (EntityBean) beanContext.getBeanClass().newInstance();
        } catch (final Exception e) {
            logger.error("Bean instantiation failed for class " + beanContext.getBeanClass(), e);
            throw new SystemException(e);
        }
        final Operation currentOp = callContext.getCurrentOperation();
        callContext.setCurrentOperation(Operation.SET_CONTEXT);
        try {
            /*
                * setEntityContext executes in an unspecified transactional context. In this case we choose to
                * allow it to have what every transaction context is current. Better then suspending it
                * unnecessarily.
                *
                * We also chose not to invoke EntityContainer.invoke( ) method, which duplicate the exception handling
                * logic but also attempt to manage the begining and end of a transaction. It its a container managed transaciton
                * we don't want the TransactionScopeHandler commiting the transaction in afterInvoke() which is what it would attempt
                * to do.
                */
            bean.setEntityContext(createEntityContext());
        } catch (final Exception e) {
            /*
                * The EJB 1.1 specification does not specify how exceptions thrown by setEntityContext impact the
                * transaction, if there is one.  In this case we choose the least disruptive operation, throwing an
                * application exception and NOT automatically marking the transaciton for rollback.
                */
            logger.error("Bean callback method failed ", e);
            throw new ApplicationException(e);
        } finally {
            callContext.setCurrentOperation(currentOp);
        }
    } else {
        reusingBean(bean, callContext);
    }
    if (callContext.getCurrentOperation() == Operation.BUSINESS || callContext.getCurrentOperation() == Operation.REMOVE) {
        /*
            * When a bean is retrieved from the bean pool to service a client's business method request it must be
            * notified that its about to enter service by invoking its ejbActivate( ) method. A bean instance
            * does not have its ejbActivate() invoked when:
            * 1. Its being retreived to service an ejbCreate()/ejbPostCreate().
            * 2. Its being retrieved to service an ejbFind method.
            * 3. Its being retrieved to service an ejbRemove() method.
            * See section 9.1.4 of the EJB 1.1 specification.
            */
        final Operation currentOp = callContext.getCurrentOperation();
        callContext.setCurrentOperation(Operation.ACTIVATE);
        try {
            /*
                In the event of an exception, OpenEJB is required to log the exception, evict the instance,
                and mark the transaction for rollback.  If there is a transaction to rollback, then the a
                javax.transaction.TransactionRolledbackException must be throw to the client.
                See EJB 1.1 specification, section 12.3.2
                */
            bean.ejbActivate();
        } catch (final Throwable e) {
            logger.error("Encountered exception during call to ejbActivate()", e);
            final TransactionPolicy txPolicy = callContext.getTransactionPolicy();
            if (txPolicy != null && txPolicy.isTransactionActive()) {
                txPolicy.setRollbackOnly(e);
                throw new ApplicationException(new TransactionRolledbackException("Reflection exception thrown while attempting to call ejbActivate() on the instance", e));
            }
            throw new ApplicationException(new RemoteException("Exception thrown while attempting to call ejbActivate() on the instance. Exception message = " + e.getMessage(), e));
        } finally {
            callContext.setCurrentOperation(currentOp);
        }
    }
    return bean;
}
Also used : BeanContext(org.apache.openejb.BeanContext) ApplicationException(org.apache.openejb.ApplicationException) SystemException(org.apache.openejb.SystemException) EntityBean(javax.ejb.EntityBean) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) Operation(org.apache.openejb.core.Operation) TransactionRolledbackException(org.apache.openejb.core.transaction.TransactionRolledbackException) RemoteException(java.rmi.RemoteException) OpenEJBException(org.apache.openejb.OpenEJBException) NoSuchObjectException(org.apache.openejb.core.NoSuchObjectException) RemoteException(java.rmi.RemoteException) InvalidateReferenceException(org.apache.openejb.InvalidateReferenceException) SystemException(org.apache.openejb.SystemException) TransactionRolledbackException(org.apache.openejb.core.transaction.TransactionRolledbackException) ApplicationException(org.apache.openejb.ApplicationException) NoSuchEntityException(javax.ejb.NoSuchEntityException) LinkedListStack(org.apache.openejb.util.LinkedListStack) Stack(org.apache.openejb.util.Stack)

Example 20 with SystemException

use of org.apache.openejb.SystemException in project tomee by apache.

the class LocalClientRunner method createDeployment.

private BeanContext createDeployment(final Class<?> testClass) {
    try {
        final AppContext appContext = new AppContext("", SystemInstance.get(), testClass.getClassLoader(), new IvmContext(), new IvmContext(), false);
        final ModuleContext moduleContext = new ModuleContext("", null, "", appContext, new IvmContext(), null);
        return new BeanContext(null, new IvmContext(), moduleContext, testClass, null, null, null, null, null, null, null, null, null, BeanType.MANAGED, false, false);
    } catch (final SystemException e) {
        throw new IllegalStateException(e);
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) IvmContext(org.apache.openejb.core.ivm.naming.IvmContext) SystemException(org.apache.openejb.SystemException) AppContext(org.apache.openejb.AppContext) ModuleContext(org.apache.openejb.ModuleContext)

Aggregations

SystemException (org.apache.openejb.SystemException)29 ApplicationException (org.apache.openejb.ApplicationException)12 OpenEJBException (org.apache.openejb.OpenEJBException)8 RemoteException (java.rmi.RemoteException)7 BeanContext (org.apache.openejb.BeanContext)7 InvalidateReferenceException (org.apache.openejb.InvalidateReferenceException)6 EjbTransactionUtil.handleSystemException (org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException)6 Operation (org.apache.openejb.core.Operation)5 File (java.io.File)4 IOException (java.io.IOException)4 NotSerializableException (java.io.NotSerializableException)4 RandomAccessFile (java.io.RandomAccessFile)4 EJBAccessException (javax.ejb.EJBAccessException)4 EJBException (javax.ejb.EJBException)4 NamingException (javax.naming.NamingException)4 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)4 SuspendedTransaction (org.apache.openejb.core.transaction.BeanTransactionPolicy.SuspendedTransaction)4 EjbTransactionUtil.handleApplicationException (org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException)4 UnavailableException (javax.resource.spi.UnavailableException)3 Transaction (javax.transaction.Transaction)3