Search in sources :

Example 6 with BeanType

use of org.apache.openejb.BeanType 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

BeanType (org.apache.openejb.BeanType)6 ArrayList (java.util.ArrayList)4 BeanContext (org.apache.openejb.BeanContext)4 InterfaceType (org.apache.openejb.InterfaceType)2 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)2 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 TreeMap (java.util.TreeMap)1 Dependent (javax.enterprise.context.Dependent)1 SessionBeanType (javax.enterprise.inject.spi.SessionBeanType)1 NameNotFoundException (javax.naming.NameNotFoundException)1 NamingException (javax.naming.NamingException)1 LifecycleException (org.apache.catalina.LifecycleException)1 InternalErrorException (org.apache.openejb.InternalErrorException)1 MethodContext (org.apache.openejb.MethodContext)1 OpenEJBException (org.apache.openejb.OpenEJBException)1 EjbObjectProxyHandler (org.apache.openejb.core.ivm.EjbObjectProxyHandler)1