Search in sources :

Example 11 with ProxyInfo

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

the class EntityEjbHomeHandler method findX.

protected Object findX(final Class interfce, final Method method, final Object[] args, final Object proxy) throws Throwable {
    final Object retValue;
    try {
        retValue = container.invoke(deploymentID, interfaceType, interfce, method, args, null);
    } catch (final OpenEJBException e) {
        logger.debug("entityEjbHomeHandler.containerInvocationFailure", e, e.getMessage());
        throw e;
    }
    if (retValue instanceof Collection) {
        final Object[] proxyInfos = ((Collection) retValue).toArray();
        final Vector<Object> proxies = new Vector<>();
        for (Object proxyInfo1 : proxyInfos) {
            final ProxyInfo proxyInfo = (ProxyInfo) proxyInfo1;
            proxies.addElement(createProxy(proxyInfo.getPrimaryKey(), getMainInterface()));
        }
        return proxies;
    } else if (retValue instanceof ArrayEnumeration) {
        @SuppressWarnings("unchecked") final ArrayEnumeration<Object> enumeration = (ArrayEnumeration<Object>) retValue;
        for (int i = enumeration.size() - 1; i >= 0; --i) {
            final ProxyInfo proxyInfo = (ProxyInfo) enumeration.get(i);
            enumeration.set(i, createProxy(proxyInfo.getPrimaryKey(), getMainInterface()));
        }
        return enumeration;
    } else if (retValue instanceof Enumeration) {
        final Enumeration enumeration = (Enumeration) retValue;
        final List<Object> proxies = new ArrayList<>();
        while (enumeration.hasMoreElements()) {
            final ProxyInfo proxyInfo = (ProxyInfo) enumeration.nextElement();
            proxies.add(createProxy(proxyInfo.getPrimaryKey(), getMainInterface()));
        }
        return new ArrayEnumeration<>(proxies);
    } else {
        final ProxyInfo proxyInfo = (ProxyInfo) retValue;
        return createProxy(proxyInfo.getPrimaryKey(), getMainInterface());
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) ProxyInfo(org.apache.openejb.ProxyInfo) Enumeration(java.util.Enumeration) ArrayEnumeration(org.apache.openejb.util.ArrayEnumeration) ArrayList(java.util.ArrayList) Collection(java.util.Collection) EJBObject(javax.ejb.EJBObject) EJBLocalObject(javax.ejb.EJBLocalObject) Vector(java.util.Vector) ArrayEnumeration(org.apache.openejb.util.ArrayEnumeration)

Example 12 with ProxyInfo

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

the class StatelessContainer method invoke.

@SuppressWarnings("unchecked")
@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) {
        final String msg = "Deployment does not exist in this container. Deployment(id='" + deployID + "'), Container(id='" + containerID + "')";
        throw new OpenEJBException(msg);
    }
    // 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);
    Instance bean = null;
    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);
            }
        }
        // Check auth before overriding context
        final boolean authorized = type == InterfaceType.TIMEOUT || this.securityService.isCallerAuthorized(callMethod, type);
        if (!authorized) {
            throw new org.apache.openejb.ApplicationException(new javax.ejb.EJBAccessException("Unauthorized Access by Principal Denied"));
        }
        final Class declaringClass = callMethod.getDeclaringClass();
        if (javax.ejb.EJBHome.class.isAssignableFrom(declaringClass) || javax.ejb.EJBLocalHome.class.isAssignableFrom(declaringClass)) {
            if (callMethod.getName().startsWith("create")) {
                return new ProxyInfo(beanContext, null);
            } else {
                // EJBHome.remove( ) and other EJBHome methods are not process by the container
                return null;
            }
        } else if (javax.ejb.EJBObject.class == declaringClass || javax.ejb.EJBLocalObject.class == declaringClass) {
            // EJBObject.remove( ) and other EJBObject methods are not process by the container
            return null;
        }
        bean = this.instanceManager.getInstance(callContext);
        callContext.setCurrentOperation(type == InterfaceType.TIMEOUT ? Operation.TIMEOUT : Operation.BUSINESS);
        callContext.set(Method.class, runMethod);
        callContext.setInvokedInterface(callInterface);
        if (currentCreationalContext != null) {
            currentCreationalContext.set(bean.creationalContext);
        }
        return _invoke(callMethod, runMethod, args, bean, callContext, type);
    } finally {
        if (runAs != null) {
            try {
                securityService.associate(runAs);
            } catch (final LoginException e) {
            // no-op
            }
        }
        if (bean != null) {
            if (callContext.isDiscardInstance()) {
                this.instanceManager.discardInstance(callContext, bean);
            } else {
                this.instanceManager.poolInstance(callContext, bean);
            }
        }
        ThreadContext.exit(oldCallContext);
        if (currentCreationalContext != null) {
            currentCreationalContext.remove();
        }
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) ThreadContext(org.apache.openejb.core.ThreadContext) Method(java.lang.reflect.Method) CurrentCreationalContext(org.apache.openejb.cdi.CurrentCreationalContext) BeanContext(org.apache.openejb.BeanContext) ProxyInfo(org.apache.openejb.ProxyInfo) EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) LoginException(javax.security.auth.login.LoginException)

Example 13 with ProxyInfo

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

the class EjbRequestHandler method doEjbHome_FIND.

protected void doEjbHome_FIND(final EJBRequest req, final EJBResponse res) throws Exception {
    final CallContext call = CallContext.getCallContext();
    final RpcContainer c = (RpcContainer) call.getBeanContext().getContainer();
    Object result = c.invoke(req.getDeploymentId(), InterfaceType.EJB_HOME, req.getInterfaceClass(), req.getMethodInstance(), req.getMethodParameters(), req.getPrimaryKey());
    /* Multiple instances found */
    if (result instanceof Collection) {
        final Object[] primaryKeys = ((Collection) result).toArray();
        for (int i = 0; i < primaryKeys.length; i++) {
            final ProxyInfo proxyInfo = ((ProxyInfo) primaryKeys[i]);
            if (proxyInfo == null) {
                primaryKeys[i] = null;
            } else {
                primaryKeys[i] = proxyInfo.getPrimaryKey();
            }
        }
        res.setResponse(req.getVersion(), ResponseCodes.EJB_OK_FOUND_COLLECTION, primaryKeys);
    } else if (result instanceof java.util.Enumeration) {
        final java.util.Enumeration resultAsEnum = (java.util.Enumeration) result;
        final java.util.List<Object> listOfPKs = new ArrayList<>();
        while (resultAsEnum.hasMoreElements()) {
            final ProxyInfo proxyInfo = ((ProxyInfo) resultAsEnum.nextElement());
            if (proxyInfo == null) {
                listOfPKs.add(null);
            } else {
                listOfPKs.add(proxyInfo.getPrimaryKey());
            }
        }
        res.setResponse(req.getVersion(), ResponseCodes.EJB_OK_FOUND_ENUMERATION, listOfPKs.toArray(new Object[listOfPKs.size()]));
    /* Single instance found */
    } else if (result instanceof ProxyInfo) {
        final ProxyInfo proxyInfo = ((ProxyInfo) result);
        result = proxyInfo.getPrimaryKey();
        res.setResponse(req.getVersion(), ResponseCodes.EJB_OK_FOUND, result);
    } else if (result == null) {
        res.setResponse(req.getVersion(), ResponseCodes.EJB_OK_FOUND, null);
    } else {
        final String message = "The bean is not EJB compliant. " + "The finder method [" + req.getMethodInstance().getName() + "] is declared " + "to return neither Collection nor the Remote Interface, " + "but [" + result.getClass().getName() + "]";
        result = new RemoteException(message);
        LOGGER.error(req + " " + message);
        res.setResponse(req.getVersion(), ResponseCodes.EJB_SYS_EXCEPTION, result);
    }
}
Also used : ProxyInfo(org.apache.openejb.ProxyInfo) RpcContainer(org.apache.openejb.RpcContainer) Collection(java.util.Collection) ArrayList(java.util.ArrayList) RemoteException(java.rmi.RemoteException)

Aggregations

ProxyInfo (org.apache.openejb.ProxyInfo)13 BeanContext (org.apache.openejb.BeanContext)10 Method (java.lang.reflect.Method)7 EJBLocalObject (javax.ejb.EJBLocalObject)6 EJBObject (javax.ejb.EJBObject)6 TransactionPolicy (org.apache.openejb.core.transaction.TransactionPolicy)6 ThreadContext (org.apache.openejb.core.ThreadContext)5 ArrayList (java.util.ArrayList)4 Collection (java.util.Collection)4 EntityBean (javax.ejb.EntityBean)4 OpenEJBException (org.apache.openejb.OpenEJBException)4 EjbTransactionUtil.createTransactionPolicy (org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy)4 Enumeration (java.util.Enumeration)3 RemoteException (java.rmi.RemoteException)2 Vector (java.util.Vector)2 EJBException (javax.ejb.EJBException)2 FinderException (javax.ejb.FinderException)2 ObjectNotFoundException (javax.ejb.ObjectNotFoundException)2 EntityManagerFactory (javax.persistence.EntityManagerFactory)2 LoginException (javax.security.auth.login.LoginException)2