Search in sources :

Example 61 with BeanContext

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

the class MdbContainer method undeploy.

public void undeploy(final BeanContext beanContext) throws OpenEJBException {
    if (!(beanContext instanceof BeanContext)) {
        return;
    }
    try {
        final EndpointFactory endpointFactory = (EndpointFactory) beanContext.getContainerData();
        if (endpointFactory != null) {
            CURRENT.set(beanContext);
            try {
                final ObjectName jmxBeanToRemove = mbeanNames.remove(beanContext);
                if (jmxBeanToRemove != null) {
                    LocalMBeanServer.unregisterSilently(jmxBeanToRemove);
                    logger.info("Undeployed MDB control for " + beanContext.getDeploymentID());
                }
                final MdbActivationContext activationContext = activationContexts.remove(beanContext);
                if (activationContext != null && activationContext.isStarted()) {
                    resourceAdapter.endpointDeactivation(endpointFactory, endpointFactory.getActivationSpec());
                }
            } finally {
                CURRENT.remove();
            }
            final MBeanServer server = LocalMBeanServer.get();
            for (final ObjectName objectName : endpointFactory.jmxNames) {
                try {
                    server.unregisterMBean(objectName);
                } catch (final Exception e) {
                    logger.error("Unable to unregister MBean " + objectName);
                }
            }
        }
    } finally {
        beanContext.setContainer(null);
        beanContext.setContainerData(null);
        deployments.remove(beanContext.getDeploymentID());
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) ResourceException(javax.resource.ResourceException) NamingException(javax.naming.NamingException) ReflectionException(javax.management.ReflectionException) OpenEJBException(org.apache.openejb.OpenEJBException) UnavailableException(javax.resource.spi.UnavailableException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MalformedObjectNameException(javax.management.MalformedObjectNameException) SystemException(org.apache.openejb.SystemException) AttributeNotFoundException(javax.management.AttributeNotFoundException) EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) EjbTransactionUtil.handleSystemException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException) ApplicationException(org.apache.openejb.ApplicationException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) ConstraintViolationException(javax.validation.ConstraintViolationException) ObjectName(javax.management.ObjectName) MBeanServer(javax.management.MBeanServer)

Example 62 with BeanContext

use of org.apache.openejb.BeanContext 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 63 with BeanContext

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

the class SingletonContainer method invoke.

@Override
public Object invoke(final Object deployID, InterfaceType type, final Class callInterface, final Method callMethod, final Object[] args, final Object primKey) throws OpenEJBException {
    final BeanContext beanContext = this.getBeanContext(deployID);
    if (beanContext == null) {
        throw new OpenEJBException("Deployment does not exist in this container. Deployment(id='" + deployID + "'), Container(id='" + containerID + "')");
    }
    // Use the backup way to determine call type if null was supplied.
    if (type == null) {
        type = beanContext.getInterfaceType(callInterface);
    }
    final Method runMethod = beanContext.getMatchingBeanMethod(callMethod);
    final ThreadContext callContext = new ThreadContext(beanContext, primKey);
    final ThreadContext oldCallContext = ThreadContext.enter(callContext);
    final CurrentCreationalContext currentCreationalContext = beanContext.get(CurrentCreationalContext.class);
    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);
            }
        }
        final boolean authorized = type == InterfaceType.TIMEOUT || getSecurityService().isCallerAuthorized(callMethod, type);
        if (!authorized) {
            throw new org.apache.openejb.ApplicationException(new EJBAccessException("Unauthorized Access by Principal Denied"));
        }
        final Class declaringClass = callMethod.getDeclaringClass();
        if (EJBHome.class.isAssignableFrom(declaringClass) || EJBLocalHome.class.isAssignableFrom(declaringClass)) {
            if (callMethod.getName().startsWith("create")) {
                return createEJBObject(beanContext, callMethod);
            } else {
                // EJBHome.remove( ) and other EJBHome methods are not process by the container
                return null;
            }
        } else if (EJBObject.class == declaringClass || EJBLocalObject.class == declaringClass) {
            // EJBObject.remove( ) and other EJBObject methods are not process by the container
            return null;
        }
        final Instance instance = instanceManager.getInstance(callContext);
        callContext.setCurrentOperation(type == InterfaceType.TIMEOUT ? Operation.TIMEOUT : Operation.BUSINESS);
        callContext.setCurrentAllowedStates(null);
        callContext.set(Method.class, runMethod);
        callContext.setInvokedInterface(callInterface);
        if (currentCreationalContext != null) {
            // noinspection unchecked
            currentCreationalContext.set(instance.creationalContext);
        }
        return _invoke(callMethod, runMethod, args, instance, callContext, type);
    } finally {
        if (runAs != null) {
            try {
                securityService.associate(runAs);
            } catch (final LoginException e) {
            // no-op
            }
        }
        ThreadContext.exit(oldCallContext);
        if (currentCreationalContext != null) {
            currentCreationalContext.remove();
        }
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) EJBHome(javax.ejb.EJBHome) ThreadContext(org.apache.openejb.core.ThreadContext) Method(java.lang.reflect.Method) EJBAccessException(javax.ejb.EJBAccessException) EJBLocalHome(javax.ejb.EJBLocalHome) CurrentCreationalContext(org.apache.openejb.cdi.CurrentCreationalContext) BeanContext(org.apache.openejb.BeanContext) EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) LoginException(javax.security.auth.login.LoginException) EJBObject(javax.ejb.EJBObject) EJBLocalObject(javax.ejb.EJBLocalObject)

Example 64 with BeanContext

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

the class SingletonContainer method _invoke.

protected Object _invoke(final Method callMethod, final Method runMethod, final Object[] args, final Instance instance, final ThreadContext callContext, final InterfaceType callType) throws OpenEJBException {
    final BeanContext beanContext = callContext.getBeanContext();
    final Duration accessTimeout = getAccessTimeout(beanContext, runMethod);
    final boolean read = javax.ejb.LockType.READ.equals(beanContext.getConcurrencyAttribute(runMethod));
    final Lock lock = aquireLock(read, accessTimeout, instance, runMethod);
    Object returnValue;
    try {
        final TransactionPolicy txPolicy = createTransactionPolicy(beanContext.getTransactionType(callMethod, callType), callContext);
        returnValue = null;
        try {
            if (callType == InterfaceType.SERVICE_ENDPOINT) {
                callContext.setCurrentOperation(Operation.BUSINESS_WS);
                returnValue = invokeWebService(args, beanContext, runMethod, instance);
            } else {
                final List<InterceptorData> interceptors = beanContext.getMethodInterceptors(runMethod);
                final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, runMethod, callType == InterfaceType.TIMEOUT ? Operation.TIMEOUT : Operation.BUSINESS, interceptors, instance.interceptors);
                returnValue = interceptorStack.invoke(args);
            }
        } catch (final Throwable e) {
            // handle reflection exception
            final ExceptionType type = beanContext.getExceptionType(e);
            if (type == ExceptionType.SYSTEM) {
                /* System Exception ****************************/
                // The bean instance is not put into the pool via instanceManager.poolInstance
                // and therefore the instance will be garbage collected and destroyed.
                // For this reason the discardInstance method of the StatelessInstanceManager
                // does nothing.
                handleSystemException(txPolicy, e, callContext);
            } else {
                /* Application Exception ***********************/
                handleApplicationException(txPolicy, e, type == ExceptionType.APPLICATION_ROLLBACK);
            }
        } finally {
            afterInvoke(txPolicy, callContext);
        }
    } finally {
        lock.unlock();
    }
    return returnValue;
}
Also used : BeanContext(org.apache.openejb.BeanContext) ExceptionType(org.apache.openejb.core.ExceptionType) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) InterceptorStack(org.apache.openejb.core.interceptor.InterceptorStack) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) EjbTransactionUtil.createTransactionPolicy(org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy) Duration(org.apache.openejb.util.Duration) EJBObject(javax.ejb.EJBObject) EJBLocalObject(javax.ejb.EJBLocalObject) Lock(java.util.concurrent.locks.Lock)

Example 65 with BeanContext

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

the class StatelessInstanceManager method discardInstance.

/**
 * This method is called to release the semaphore in case of the business method
 * throwing a system exception
 *
 * @param callContext ThreadContext
 * @param bean        Object
 */
public void discardInstance(final ThreadContext callContext, final Object bean) throws SystemException {
    if (bean == null) {
        throw new SystemException("Invalid arguments");
    }
    final Instance instance = Instance.class.cast(bean);
    final BeanContext beanContext = callContext.getBeanContext();
    final Data data = (Data) beanContext.getContainerData();
    if (null != data) {
        final Pool<Instance> pool = data.getPool();
        pool.discard(instance.getPoolEntry());
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) SystemException(org.apache.openejb.SystemException) InterceptorInstance(org.apache.openejb.core.interceptor.InterceptorInstance) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData)

Aggregations

BeanContext (org.apache.openejb.BeanContext)198 OpenEJBException (org.apache.openejb.OpenEJBException)40 ThreadContext (org.apache.openejb.core.ThreadContext)40 Method (java.lang.reflect.Method)38 ContainerSystem (org.apache.openejb.spi.ContainerSystem)28 ArrayList (java.util.ArrayList)27 AppContext (org.apache.openejb.AppContext)26 TransactionPolicy (org.apache.openejb.core.transaction.TransactionPolicy)26 NamingException (javax.naming.NamingException)24 InterceptorData (org.apache.openejb.core.interceptor.InterceptorData)23 Context (javax.naming.Context)22 ApplicationException (org.apache.openejb.ApplicationException)20 HashMap (java.util.HashMap)19 EJBLocalObject (javax.ejb.EJBLocalObject)18 EJBObject (javax.ejb.EJBObject)18 SystemException (org.apache.openejb.SystemException)18 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)17 ModuleContext (org.apache.openejb.ModuleContext)16 EjbTransactionUtil.createTransactionPolicy (org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy)16 FinderException (javax.ejb.FinderException)14