Search in sources :

Example 6 with EjbObjectProxyHandler

use of org.apache.openejb.core.ivm.EjbObjectProxyHandler in project tomee by apache.

the class Cmp2Util method getEntityBean.

public static <Bean extends EntityBean> Bean getEntityBean(final EJBLocalObject proxy) {
    if (proxy == null) {
        return null;
    }
    final EjbObjectProxyHandler handler = (EjbObjectProxyHandler) ProxyManager.getInvocationHandler(proxy);
    if (handler.container == null) {
        return null;
    }
    if (!(handler.container instanceof CmpContainer)) {
        throw new IllegalArgumentException("Proxy is not connected to a CMP container but is conect to " + handler.container.getClass().getName());
    }
    final CmpContainer container = (CmpContainer) handler.container;
    final Bean entity = (Bean) container.getEjbInstance(handler.getBeanContext(), handler.primaryKey);
    return entity;
}
Also used : CmpContainer(org.apache.openejb.core.cmp.CmpContainer) EjbObjectProxyHandler(org.apache.openejb.core.ivm.EjbObjectProxyHandler) EntityBean(javax.ejb.EntityBean)

Example 7 with EjbObjectProxyHandler

use of org.apache.openejb.core.ivm.EjbObjectProxyHandler in project tomee by apache.

the class ManagedHomeHandler method createProxy.

public Object createProxy(final Object primaryKey, final Class mainInterface) {
    final Object proxy = super.createProxy(primaryKey, mainInterface);
    EjbObjectProxyHandler handler = null;
    try {
        handler = (EjbObjectProxyHandler) ProxyManager.getInvocationHandler(proxy);
    } catch (final Exception e) {
        // try getting the invocation handler from the localbean
        try {
            final Field field = proxy.getClass().getDeclaredField("invocationHandler");
            field.setAccessible(true);
            handler = (EjbObjectProxyHandler) field.get(proxy);
        } catch (final Exception e1) {
        // no-op
        }
    }
    registerHandler(handler.getRegistryId(), handler);
    return proxy;
}
Also used : Field(java.lang.reflect.Field) EjbObjectProxyHandler(org.apache.openejb.core.ivm.EjbObjectProxyHandler) RemoveException(javax.ejb.RemoveException)

Example 8 with EjbObjectProxyHandler

use of org.apache.openejb.core.ivm.EjbObjectProxyHandler in project tomee by apache.

the class BaseSessionContext method getBusinessObject.

public Object getBusinessObject(final Class interfce) {
    doCheck(Call.getBusinessObject);
    if (interfce == null) {
        throw new IllegalStateException("Interface argument cannot me null.");
    }
    final ThreadContext threadContext = ThreadContext.getThreadContext();
    final BeanContext di = threadContext.getBeanContext();
    final InterfaceType interfaceType = di.getInterfaceType(interfce);
    final BeanType type = di.getComponentType();
    if (interfaceType == null) {
        throw new IllegalStateException("Component has no such interface: " + interfce.getName());
    }
    if (!interfaceType.isBusiness()) {
        throw new IllegalStateException("Interface is not a business interface for this bean: " + interfce.getName());
    }
    try {
        final EjbObjectProxyHandler handler;
        switch(di.getComponentType()) {
            case STATEFUL:
                {
                    handler = new StatefulEjbObjectHandler(di, threadContext.getPrimaryKey(), interfaceType, new ArrayList<Class>(), interfce);
                    break;
                }
            case STATELESS:
                {
                    handler = new StatelessEjbObjectHandler(di, threadContext.getPrimaryKey(), interfaceType, new ArrayList<Class>(), interfce);
                    break;
                }
            case SINGLETON:
                {
                    handler = new SingletonEjbObjectHandler(di, threadContext.getPrimaryKey(), interfaceType, new ArrayList<Class>(), interfce);
                    break;
                }
            case MANAGED:
                {
                    handler = new ManagedObjectHandler(di, threadContext.getPrimaryKey(), interfaceType, new ArrayList<Class>(), interfce);
                    break;
                }
            default:
                throw new IllegalStateException("Bean is not a session bean: " + di.getComponentType());
        }
        if (InterfaceType.LOCALBEAN.equals(interfaceType)) {
            return LocalBeanProxyFactory.constructProxy(di.get(BeanContext.ProxyClass.class).getProxy(), handler);
        } else {
            final List<Class> interfaces = new ArrayList<Class>();
            interfaces.addAll(di.getInterfaces(interfaceType));
            interfaces.add(Serializable.class);
            interfaces.add(IntraVmProxy.class);
            if (BeanType.STATEFUL.equals(type) || BeanType.MANAGED.equals(type)) {
                interfaces.add(BeanContext.Removable.class);
            }
            return ProxyManager.newProxyInstance(interfaces.toArray(new Class[interfaces.size()]), handler);
        }
    } catch (final IllegalAccessException iae) {
        throw new InternalErrorException("Could not create IVM proxy for " + interfce.getName() + " interface", iae);
    }
}
Also used : StatefulEjbObjectHandler(org.apache.openejb.core.stateful.StatefulEjbObjectHandler) ManagedObjectHandler(org.apache.openejb.core.managed.ManagedObjectHandler) ArrayList(java.util.ArrayList) InternalErrorException(org.apache.openejb.InternalErrorException) EjbObjectProxyHandler(org.apache.openejb.core.ivm.EjbObjectProxyHandler) BeanContext(org.apache.openejb.BeanContext) InterfaceType(org.apache.openejb.InterfaceType) BeanType(org.apache.openejb.BeanType) SingletonEjbObjectHandler(org.apache.openejb.core.singleton.SingletonEjbObjectHandler) StatelessEjbObjectHandler(org.apache.openejb.core.stateless.StatelessEjbObjectHandler)

Aggregations

EjbObjectProxyHandler (org.apache.openejb.core.ivm.EjbObjectProxyHandler)8 BeanContext (org.apache.openejb.BeanContext)3 InternalErrorException (org.apache.openejb.InternalErrorException)3 Field (java.lang.reflect.Field)2 EJBLocalObject (javax.ejb.EJBLocalObject)2 EJBObject (javax.ejb.EJBObject)2 EntityBean (javax.ejb.EntityBean)2 RemoveException (javax.ejb.RemoveException)2 ThreadContext (org.apache.openejb.core.ThreadContext)2 CmpContainer (org.apache.openejb.core.cmp.CmpContainer)2 IntraVmProxy (org.apache.openejb.core.ivm.IntraVmProxy)2 ArrayList (java.util.ArrayList)1 BeanType (org.apache.openejb.BeanType)1 InterfaceType (org.apache.openejb.InterfaceType)1 ManagedObjectHandler (org.apache.openejb.core.managed.ManagedObjectHandler)1 SingletonEjbObjectHandler (org.apache.openejb.core.singleton.SingletonEjbObjectHandler)1 StatefulEjbObjectHandler (org.apache.openejb.core.stateful.StatefulEjbObjectHandler)1 StatelessEjbObjectHandler (org.apache.openejb.core.stateless.StatelessEjbObjectHandler)1