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");
}
}
}
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();
}
}
}
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);
}
}
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());
}
}
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());
}
}
Aggregations