Search in sources :

Example 1 with EJBHome

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

the class BeanContext method mapHomeInterface.

private void mapHomeInterface(final Class intrface) {
    final Method[] homeMethods = intrface.getMethods();
    for (final Method method : homeMethods) {
        final Class owner = method.getDeclaringClass();
        if (owner == EJBHome.class || owner == EJBLocalHome.class) {
            continue;
        }
        try {
            Method beanMethod = null;
            if (method.getName().startsWith("create")) {
                final StringBuilder ejbCreateName = new StringBuilder(method.getName());
                ejbCreateName.replace(0, 1, "ejbC");
                beanMethod = beanClass.getMethod(ejbCreateName.toString(), method.getParameterTypes());
                getLegacyView().createMethod = beanMethod;
                /*
                    Entity beans have a ejbCreate and ejbPostCreate methods with matching
                    parameters. This code maps that relationship.
                    */
                if (this.componentType == BeanType.BMP_ENTITY || this.componentType == BeanType.CMP_ENTITY) {
                    ejbCreateName.insert(3, "Post");
                    Class clazz = beanClass;
                    if (getCmp().cmpImplClass != null) {
                        clazz = getCmp().cmpImplClass;
                    }
                    final Method postCreateMethod = clazz.getMethod(ejbCreateName.toString(), method.getParameterTypes());
                    getCmp().postCreateMethodMap.put(getLegacyView().createMethod, postCreateMethod);
                }
            /*
                     * Stateless session beans only have one create method. The getCreateMethod is
                     * used by instance manager of the core.stateless.StatelessContainer as a convenience
                     * method for obtaining the ejbCreate method.
                    */
            } else if (method.getName().startsWith("find")) {
                if (this.componentType == BeanType.BMP_ENTITY) {
                    final String beanMethodName = "ejbF" + method.getName().substring(1);
                    beanMethod = beanClass.getMethod(beanMethodName, method.getParameterTypes());
                }
            } else {
                final String beanMethodName = "ejbHome" + method.getName().substring(0, 1).toUpperCase() + method.getName().substring(1);
                beanMethod = beanClass.getMethod(beanMethodName, method.getParameterTypes());
            }
            if (beanMethod != null) {
                mapMethods(method, beanMethod);
            }
        } catch (final NoSuchMethodException nsme) {
        // throw new OpenEJBRuntimeException("Invalid method [" + method + "] Not declared by " + beanClass.getName() + " class");
        }
    }
}
Also used : EJBHome(jakarta.ejb.EJBHome) Method(java.lang.reflect.Method) AnnotatedMethod(jakarta.enterprise.inject.spi.AnnotatedMethod) EJBLocalHome(jakarta.ejb.EJBLocalHome)

Example 2 with EJBHome

use of jakarta.ejb.EJBHome 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 3 with EJBHome

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

the class CrossClassLoaderProxyTest method testCrossClassLoaderRemoteInterface.

public void testCrossClassLoaderRemoteInterface() throws Exception {
    final HackClassLoader loader = new HackClassLoader(getClass().getClassLoader());
    final ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(loader);
    try {
        final Class testObjectClass = loader.loadClass(CrossClassLoaderProxyTestObject.class.getName());
        assertFalse(CrossClassLoaderProxyTestObject.class.equals(testObjectClass));
        final Class widgetClass = (Class) testObjectClass.getField("widgetClass").get(null);
        assertEquals(Widget.class, widgetClass);
        final Class widgetHomeClass = (Class) testObjectClass.getField("widgetHomeClass").get(null);
        assertFalse(WidgetHome.class.equals(widgetHomeClass));
        final Class widgetRemoteClass = (Class) testObjectClass.getField("widgetRemoteClass").get(null);
        assertFalse(WidgetRemote.class.equals(widgetRemoteClass));
        final Object testObject = testObjectClass.newInstance();
        final InitialContext ctx = new InitialContext();
        final EJBHome rawHome = (EJBHome) ctx.lookup("WidgetBeanRemoteHome");
        final EJBHome home = (EJBHome) copy(rawHome);
        assertNotNull("home", home);
        assertEquals(widgetHomeClass.getClassLoader(), home.getClass().getClassLoader());
        assertTrue(widgetHomeClass.isAssignableFrom(home.getClass()));
        testObjectClass.getField("widgetHome").set(testObject, home);
        testObjectClass.getMethod("testRemoteInterface").invoke(testObject);
    } finally {
        Thread.currentThread().setContextClassLoader(oldClassLoader);
    }
}
Also used : EJBHome(jakarta.ejb.EJBHome) EJBObject(jakarta.ejb.EJBObject) InitialContext(javax.naming.InitialContext)

Example 4 with EJBHome

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

the class Cmp2EjbMetaDataTests method test01_getEJBHome.

// =================================
// Test meta data methods
// 
public void test01_getEJBHome() {
    try {
        final EJBHome home = ejbMetaData.getEJBHome();
        assertNotNull("The EJBHome is null", home);
    } catch (final Exception e) {
        fail("Received Exception " + e.getClass() + " : " + e.getMessage());
    }
}
Also used : EJBHome(jakarta.ejb.EJBHome)

Example 5 with EJBHome

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

the class Cmp2EjbObjectTests method test04_getEjbHome.

public void test04_getEjbHome() {
    try {
        final EJBHome home = ejbObject.getEJBHome();
        assertNotNull("The EJBHome is null", home);
    } catch (final Exception e) {
        fail("Received Exception " + e.getClass() + " : " + e.getMessage());
    }
}
Also used : EJBHome(jakarta.ejb.EJBHome) ObjectNotFoundException(jakarta.ejb.ObjectNotFoundException)

Aggregations

EJBHome (jakarta.ejb.EJBHome)60 RemoteException (java.rmi.RemoteException)14 EJBObject (jakarta.ejb.EJBObject)8 InitialContext (javax.naming.InitialContext)8 ObjectNotFoundException (jakarta.ejb.ObjectNotFoundException)7 EJBException (jakarta.ejb.EJBException)6 HomeHandle (jakarta.ejb.HomeHandle)6 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ObjectInputStream (java.io.ObjectInputStream)3 ObjectOutputStream (java.io.ObjectOutputStream)3 MarshalledObject (java.rmi.MarshalledObject)3 EJBLocalHome (jakarta.ejb.EJBLocalHome)2 RemoveException (jakarta.ejb.RemoveException)2 Method (java.lang.reflect.Method)2 CreateException (jakarta.ejb.CreateException)1 EJBAccessException (jakarta.ejb.EJBAccessException)1 EJBLocalObject (jakarta.ejb.EJBLocalObject)1 AnnotatedMethod (jakarta.enterprise.inject.spi.AnnotatedMethod)1 LoginException (javax.security.auth.login.LoginException)1