Search in sources :

Example 51 with ThreadContext

use of org.apache.openejb.core.ThreadContext in project tomee by apache.

the class MdbPoolContainer method release.

public void release(final BeanContext deployInfo, final Object instance) {
    // get the mdb call context
    ThreadContext callContext = ThreadContext.getThreadContext();
    boolean contextExitRequired = false;
    if (callContext == null) {
        callContext = new ThreadContext(deployInfo, null);
        ThreadContext.enter(callContext);
        contextExitRequired = true;
    }
    try {
        // if we have an mdb call context we need to invoke the after invoke method
        final MdbCallContext mdbCallContext = callContext.get(MdbCallContext.class);
        if (mdbCallContext != null) {
            try {
                afterInvoke(mdbCallContext.txPolicy, callContext);
            } catch (final Exception e) {
                logger.error("error while releasing message endpoint", e);
            } finally {
                if (instance != null) {
                    try {
                        instanceManager.poolInstance(callContext, instance);
                    } catch (OpenEJBException e) {
                        logger.error("error while releasing message endpoint", e);
                    }
                }
            }
        }
    } finally {
        if (contextExitRequired) {
            ThreadContext.exit(callContext);
        }
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) ThreadContext(org.apache.openejb.core.ThreadContext) ResourceException(javax.resource.ResourceException) EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) EjbTransactionUtil.handleSystemException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException) NamingException(javax.naming.NamingException) ApplicationException(org.apache.openejb.ApplicationException) OpenEJBException(org.apache.openejb.OpenEJBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConstraintViolationException(javax.validation.ConstraintViolationException) SystemException(org.apache.openejb.SystemException)

Example 52 with ThreadContext

use of org.apache.openejb.core.ThreadContext in project tomee by apache.

the class MdbPoolContainer 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 ThreadContext callContext = new ThreadContext(beanContext, primKey);
    final Instance instance = this.instanceManager.getInstance(callContext);
    try {
        beforeDelivery(beanContext, instance, method, null);
        final Object value = invoke(instance, method, type, args);
        afterDelivery(instance);
        return value;
    } finally {
        if (instance != null) {
            if (callContext.isDiscardInstance()) {
                this.instanceManager.discardInstance(callContext, instance);
            } else {
                this.instanceManager.poolInstance(callContext, instance);
            }
        }
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) SystemInstance(org.apache.openejb.loader.SystemInstance) ThreadContext(org.apache.openejb.core.ThreadContext)

Example 53 with ThreadContext

use of org.apache.openejb.core.ThreadContext in project tomee by apache.

the class PoolEndpointHandler method beforeDelivery.

@Override
public void beforeDelivery(final Method method) throws ApplicationServerInternalException {
    // verify current state
    switch(state) {
        case RELEASED:
            throw new IllegalStateException("Message endpoint factory has been released");
        case BEFORE_CALLED:
            throw new IllegalStateException("beforeDelivery can not be called again until message is delivered and afterDelivery is called");
        case METHOD_CALLED:
        case SYSTEM_EXCEPTION:
            throw new IllegalStateException("The last message delivery must be completed with an afterDeliver before beforeDeliver can be called again");
    }
    // call beforeDelivery on the container
    try {
        instance = instanceManager.getInstance(new ThreadContext(deployment, null));
        container.beforeDelivery(deployment, instance, method, xaResource);
    } catch (final SystemException se) {
        final Throwable throwable = se.getRootCause() != null ? se.getRootCause() : se;
        throw new ApplicationServerInternalException(throwable);
    } catch (OpenEJBException oe) {
        throw new ApplicationServerInternalException(oe);
    }
    // before completed successfully we are now ready to invoke bean
    state = State.BEFORE_CALLED;
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) SystemException(org.apache.openejb.SystemException) ThreadContext(org.apache.openejb.core.ThreadContext) ApplicationServerInternalException(javax.resource.spi.ApplicationServerInternalException)

Example 54 with ThreadContext

use of org.apache.openejb.core.ThreadContext in project tomee by apache.

the class AbstractSecurityService method isCallerAuthorized.

@Override
public boolean isCallerAuthorized(final Method method, final InterfaceType type) {
    final ThreadContext threadContext = ThreadContext.getThreadContext();
    final BeanContext beanContext = threadContext.getBeanContext();
    try {
        final String ejbName = beanContext.getEjbName();
        String name = type == null ? null : type.getSpecName();
        if ("LocalBean".equals(name) || "LocalBeanHome".equals(name)) {
            name = null;
        }
        final Identity currentIdentity = clientIdentity.get();
        final SecurityContext securityContext;
        if (currentIdentity == null) {
            securityContext = threadContext.get(SecurityContext.class);
        } else {
            securityContext = new SecurityContext(currentIdentity.getSubject());
        }
        securityContext.acc.checkPermission(new EJBMethodPermission(ejbName, name, method));
    } catch (final AccessControlException e) {
        return false;
    }
    return true;
}
Also used : BeanContext(org.apache.openejb.BeanContext) ThreadContext(org.apache.openejb.core.ThreadContext) AccessControlException(java.security.AccessControlException) EJBMethodPermission(javax.security.jacc.EJBMethodPermission)

Example 55 with ThreadContext

use of org.apache.openejb.core.ThreadContext in project tomee by apache.

the class AbstractSecurityService method isCallerInRole.

@Override
public boolean isCallerInRole(final String role) {
    if (role == null) {
        throw new IllegalArgumentException("Role must not be null");
    }
    final ThreadContext threadContext = ThreadContext.getThreadContext();
    if (threadContext == null) {
        return false;
    }
    final SecurityContext securityContext = threadContext.get(SecurityContext.class);
    if ("**".equals(role)) {
        // ie logged in
        return securityContext != defaultContext;
    }
    final Set<Group> grps = securityContext.subject.getPrincipals(Group.class);
    for (final Group grp : grps) {
        if (grp.getName().equals(role)) {
            return true;
        }
    }
    final Set<GroupPrincipal> grpsp = securityContext.subject.getPrincipals(GroupPrincipal.class);
    for (final GroupPrincipal grp : grpsp) {
        if (grp.getName().equals(role)) {
            return true;
        }
    }
    return false;
}
Also used : GroupPrincipal(org.apache.openejb.core.security.jaas.GroupPrincipal) ThreadContext(org.apache.openejb.core.ThreadContext)

Aggregations

ThreadContext (org.apache.openejb.core.ThreadContext)68 BeanContext (org.apache.openejb.BeanContext)35 OpenEJBException (org.apache.openejb.OpenEJBException)23 Method (java.lang.reflect.Method)20 ApplicationException (org.apache.openejb.ApplicationException)14 EjbTransactionUtil.handleApplicationException (org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException)14 InterceptorStack (org.apache.openejb.core.interceptor.InterceptorStack)10 EjbTransactionUtil.handleSystemException (org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException)10 TransactionPolicy (org.apache.openejb.core.transaction.TransactionPolicy)10 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 EJBException (javax.ejb.EJBException)9 SystemException (org.apache.openejb.SystemException)9 InterceptorData (org.apache.openejb.core.interceptor.InterceptorData)9 RemoteException (java.rmi.RemoteException)7 NamingException (javax.naming.NamingException)6 BeanTransactionPolicy (org.apache.openejb.core.transaction.BeanTransactionPolicy)6 JtaTransactionPolicy (org.apache.openejb.core.transaction.JtaTransactionPolicy)6 SystemInstance (org.apache.openejb.loader.SystemInstance)6 ArrayList (java.util.ArrayList)5 EJBAccessException (javax.ejb.EJBAccessException)5