Search in sources :

Example 1 with EJBObject

use of jakarta.ejb.EJBObject 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(jakarta.ejb.EJBHome) ThreadContext(org.apache.openejb.core.ThreadContext) Method(java.lang.reflect.Method) EJBAccessException(jakarta.ejb.EJBAccessException) EJBLocalHome(jakarta.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) EJBLocalObject(jakarta.ejb.EJBLocalObject) EJBObject(jakarta.ejb.EJBObject)

Example 2 with EJBObject

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

the class EntityContext method getEJBObject.

public EJBObject getEJBObject() throws IllegalStateException {
    doCheck(Call.getEJBObject);
    final ThreadContext threadContext = ThreadContext.getThreadContext();
    final BeanContext di = threadContext.getBeanContext();
    if (di.getRemoteInterface() == null) {
        throw new IllegalStateException("EJB " + di.getDeploymentID() + " does not have a remote interface");
    }
    final EjbObjectProxyHandler handler = new EntityEjbObjectHandler(di.getContainer().getBeanContext(di.getDeploymentID()), threadContext.getPrimaryKey(), InterfaceType.EJB_OBJECT, new ArrayList<>(), di.getRemoteInterface());
    try {
        final Class[] interfaces = new Class[] { di.getRemoteInterface(), IntraVmProxy.class };
        return (EJBObject) ProxyManager.newProxyInstance(interfaces, handler);
    } catch (final IllegalAccessException iae) {
        throw new InternalErrorException("Could not create IVM proxy for " + di.getRemoteInterface() + " interface", iae);
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) EJBObject(jakarta.ejb.EJBObject) ThreadContext(org.apache.openejb.core.ThreadContext) InternalErrorException(org.apache.openejb.InternalErrorException) IntraVmProxy(org.apache.openejb.core.ivm.IntraVmProxy) EjbObjectProxyHandler(org.apache.openejb.core.ivm.EjbObjectProxyHandler)

Example 3 with EJBObject

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

the class BaseSessionContext method getEJBObject.

public EJBObject getEJBObject() throws IllegalStateException {
    doCheck(Call.getEJBObject);
    final ThreadContext threadContext = ThreadContext.getThreadContext();
    final BeanContext di = threadContext.getBeanContext();
    if (di.getHomeInterface() == null) {
        throw new IllegalStateException("Bean does not have an EJBObject interface: " + di.getDeploymentID());
    }
    return (EJBObject) EjbObjectProxyHandler.createProxy(di, threadContext.getPrimaryKey(), InterfaceType.EJB_OBJECT, di.getRemoteInterface());
}
Also used : BeanContext(org.apache.openejb.BeanContext) EJBObject(jakarta.ejb.EJBObject)

Example 4 with EJBObject

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

the class CmpRmiIiopTests method test54_returnHandleArray.

public void test54_returnHandleArray() {
    try {
        final Object obj = initialContext.lookup("client/tests/entity/cmp/EncBean");
        final EncCmpHome home = (EncCmpHome) obj;
        assertNotNull("The EJBHome returned from JNDI is null", home);
        final EncCmpObject object = home.create("test_54 CmpBean");
        assertNotNull("The EJBObject created is null", object);
        final Handle[] expected = new Handle[3];
        for (int i = 0; i < expected.length; i++) {
            expected[i] = object.getHandle();
            assertNotNull("The EJBObject Handle returned is null", expected[i]);
        }
        final Handle[] actual = (Handle[]) ejbObject.returnHandleArray(expected);
        assertNotNull("The Handle array returned is null", actual);
        assertEquals(expected.length, actual.length);
        for (int i = 0; i < expected.length; i++) {
            assertNotNull("The EJBObject Handle returned is null", actual[i]);
            assertNotNull("The EJBObject in the Handle is null", actual[i].getEJBObject());
            assertTrue("The EJBObjects in the Handles are not equal", expected[i].getEJBObject().isIdentical(actual[i].getEJBObject()));
        }
    } catch (final Exception e) {
        fail("Received Exception " + e.getClass() + " : " + e.getMessage());
    }
}
Also used : EJBObject(jakarta.ejb.EJBObject) RemoteException(java.rmi.RemoteException) Handle(jakarta.ejb.Handle)

Example 5 with EJBObject

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

the class CmpRmiIiopTests method test44_returnEJBObjectArray.

public void test44_returnEJBObjectArray() {
    try {
        final Object obj = initialContext.lookup("client/tests/entity/cmp/EncBean");
        final EncCmpHome home = (EncCmpHome) obj;
        assertNotNull("The EJBHome returned from JNDI is null", home);
        final EncCmpObject[] expected = new EncCmpObject[3];
        for (int i = 0; i < expected.length; i++) {
            expected[i] = home.create("test_44 CmpBean");
            assertNotNull("The EJBObject created is null", expected[i]);
        }
        final EJBObject[] actual = ejbObject.returnEJBObjectArray(expected);
        assertNotNull("The EJBObject array returned is null", actual);
        assertEquals(expected.length, actual.length);
        for (int i = 0; i < actual.length; i++) {
            assertTrue("The EJBObejcts are not identical", expected[i].isIdentical(actual[i]));
        }
    } catch (final Exception e) {
        fail("Received Exception " + e.getClass() + " : " + e.getMessage());
    }
}
Also used : EJBObject(jakarta.ejb.EJBObject) EJBObject(jakarta.ejb.EJBObject) RemoteException(java.rmi.RemoteException)

Aggregations

EJBObject (jakarta.ejb.EJBObject)84 RemoteException (java.rmi.RemoteException)37 Handle (jakarta.ejb.Handle)25 ObjectGraph (org.apache.openejb.test.object.ObjectGraph)12 EncCmpObject (org.apache.openejb.test.entity.cmp.EncCmpObject)7 RmiIiopCmpObject (org.apache.openejb.test.entity.cmp.RmiIiopCmpObject)7 MarshalledObject (java.rmi.MarshalledObject)6 EncCmpHome (org.apache.openejb.test.entity.cmp.EncCmpHome)6 EJBLocalObject (jakarta.ejb.EJBLocalObject)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ObjectInputStream (java.io.ObjectInputStream)3 ObjectOutputStream (java.io.ObjectOutputStream)3 BeanContext (org.apache.openejb.BeanContext)3 ThreadContext (org.apache.openejb.core.ThreadContext)2 EJBAccessException (jakarta.ejb.EJBAccessException)1 EJBException (jakarta.ejb.EJBException)1 EJBHome (jakarta.ejb.EJBHome)1 EJBLocalHome (jakarta.ejb.EJBLocalHome)1 EntityBean (jakarta.ejb.EntityBean)1