Search in sources :

Example 1 with InterfaceType

use of org.apache.openejb.InterfaceType in project tomee by apache.

the class EjbHomeProxyHandler method createProxy.

public Object createProxy(final Object primaryKey, final Class mainInterface) {
    try {
        final InterfaceType objectInterfaceType = this.interfaceType.getCounterpart();
        final BeanType type = getBeanContext().getComponentType();
        final EjbObjectProxyHandler handler = newEjbObjectHandler(getBeanContext(), primaryKey, objectInterfaceType, getInterfaces(), mainInterface);
        // TODO Is it correct for ManagedBean injection via managed bean class?
        if ((InterfaceType.LOCALBEAN.equals(objectInterfaceType) || getBeanContext().getComponentType().equals(BeanType.MANAGED)) && !getBeanContext().isDynamicallyImplemented()) {
            return LocalBeanProxyFactory.constructProxy(handler.getBeanContext().get(BeanContext.ProxyClass.class).getProxy(), handler);
        } else {
            final List<Class> proxyInterfaces = new ArrayList<Class>(handler.getInterfaces().size() + 2);
            proxyInterfaces.addAll(handler.getInterfaces());
            proxyInterfaces.add(Serializable.class);
            proxyInterfaces.add(IntraVmProxy.class);
            if (BeanType.STATEFUL.equals(type) || BeanType.MANAGED.equals(type)) {
                proxyInterfaces.add(BeanContext.Removable.class);
            }
            return ProxyManager.newProxyInstance(proxyInterfaces.toArray(new Class[proxyInterfaces.size()]), handler);
        }
    } catch (final IllegalAccessException iae) {
        throw new OpenEJBRuntimeException("Could not create IVM proxy for " + getInterfaces().get(0), iae);
    }
}
Also used : OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) BeanContext(org.apache.openejb.BeanContext) InterfaceType(org.apache.openejb.InterfaceType) BeanType(org.apache.openejb.BeanType) ArrayList(java.util.ArrayList)

Example 2 with InterfaceType

use of org.apache.openejb.InterfaceType in project tomee by apache.

the class BaseSessionContext method getInvokedBusinessInterface.

public Class getInvokedBusinessInterface() {
    doCheck(Call.getInvokedBusinessInterface);
    final ThreadContext threadContext = ThreadContext.getThreadContext();
    final Class invokedInterface = threadContext.getInvokedInterface();
    final InterfaceType type = threadContext.getBeanContext().getInterfaceType(invokedInterface);
    if (!type.isBusiness()) {
        throw new IllegalStateException("The EJB spec requires us to cripple the use of this method for anything but business interface proxy.  But FYI, your invoked interface is: " + invokedInterface.getName());
    }
    if (invokedInterface == null) {
        throw new IllegalStateException("Business interface not set into ThreadContext.");
    }
    return invokedInterface;
}
Also used : InterfaceType(org.apache.openejb.InterfaceType)

Example 3 with InterfaceType

use of org.apache.openejb.InterfaceType in project tomee by apache.

the class CdiPlugin method getSessionBeanProxy.

@Override
public Object getSessionBeanProxy(final Bean<?> inBean, final Class<?> interfce, final CreationalContext<?> creationalContext) {
    Object instance = cacheProxies.get(inBean);
    if (instance != null) {
        return instance;
    }
    synchronized (inBean) {
        // singleton for the app so safe to sync on it
        instance = cacheProxies.get(inBean);
        if (instance != null) {
            return instance;
        }
        final Class<? extends Annotation> scopeClass = inBean.getScope();
        final CdiEjbBean<Object> cdiEjbBean = (CdiEjbBean<Object>) inBean;
        final CreationalContext<Object> cc = (CreationalContext<Object>) creationalContext;
        if (scopeClass == null || Dependent.class == scopeClass) {
            // no need to add any layer, null = @New
            return cdiEjbBean.createEjb(cc);
        }
        // only stateful normally
        final InstanceBean<Object> bean = new InstanceBean<Object>(cdiEjbBean);
        if (webBeansContext.getBeanManagerImpl().isNormalScope(scopeClass)) {
            final BeanContext beanContext = cdiEjbBean.getBeanContext();
            final Provider provider = webBeansContext.getNormalScopeProxyFactory().getInstanceProvider(beanContext.getClassLoader(), cdiEjbBean);
            if (!beanContext.isLocalbean()) {
                final List<Class> interfaces = new ArrayList<Class>();
                final InterfaceType type = beanContext.getInterfaceType(interfce);
                if (type != null) {
                    interfaces.addAll(beanContext.getInterfaces(type));
                } else {
                    // can happen when looked up from impl instead of API in OWB -> default to business local
                    interfaces.addAll(beanContext.getInterfaces(InterfaceType.BUSINESS_LOCAL));
                }
                interfaces.add(Serializable.class);
                interfaces.add(IntraVmProxy.class);
                if (BeanType.STATEFUL.equals(beanContext.getComponentType()) || BeanType.MANAGED.equals(beanContext.getComponentType())) {
                    interfaces.add(BeanContext.Removable.class);
                }
                try {
                    instance = ProxyManager.newProxyInstance(interfaces.toArray(new Class<?>[interfaces.size()]), new InvocationHandler() {

                        @Override
                        public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
                            try {
                                return method.invoke(provider.get(), args);
                            } catch (final InvocationTargetException ite) {
                                throw ite.getCause();
                            }
                        }
                    });
                } catch (final IllegalAccessException e) {
                    throw new OpenEJBRuntimeException(e);
                }
            } else {
                final NormalScopeProxyFactory normalScopeProxyFactory = webBeansContext.getNormalScopeProxyFactory();
                final Class<?> proxyClass = normalScopeProxyFactory.createProxyClass(beanContext.getClassLoader(), beanContext.getBeanClass());
                instance = normalScopeProxyFactory.createProxyInstance(proxyClass, provider);
            }
            cacheProxies.put(inBean, instance);
        } else {
            final Context context = webBeansContext.getBeanManagerImpl().getContext(scopeClass);
            instance = context.get(bean, cc);
        }
        bean.setOwbProxy(instance);
        return instance;
    }
}
Also used : WebBeansContext(org.apache.webbeans.config.WebBeansContext) BeanContext(org.apache.openejb.BeanContext) Context(javax.enterprise.context.spi.Context) CreationalContext(javax.enterprise.context.spi.CreationalContext) NormalScopeProxyFactory(org.apache.webbeans.proxy.NormalScopeProxyFactory) ArrayList(java.util.ArrayList) Dependent(javax.enterprise.context.Dependent) ObserverMethod(javax.enterprise.inject.spi.ObserverMethod) Method(java.lang.reflect.Method) AnnotatedMethod(javax.enterprise.inject.spi.AnnotatedMethod) InvocationHandler(java.lang.reflect.InvocationHandler) InvocationTargetException(java.lang.reflect.InvocationTargetException) Provider(javax.inject.Provider) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) CreationalContext(javax.enterprise.context.spi.CreationalContext) BeanContext(org.apache.openejb.BeanContext) InterfaceType(org.apache.openejb.InterfaceType)

Example 4 with InterfaceType

use of org.apache.openejb.InterfaceType in project tomee by apache.

the class LazyEjbReference method getObject.

public Object getObject() throws NamingException {
    if (reference != null) {
        return reference.getObject();
    }
    final SystemInstance systemInstance = SystemInstance.get();
    final EjbResolver resolver = systemInstance.getComponent(EjbResolver.class);
    final String deploymentId = resolver.resolve(info, moduleUri);
    if (deploymentId == null) {
        String key = "lazyEjbRefNotResolved";
        if (info.getHome() != null) {
            key += ".home";
        }
        final String message = messages.format(key, info.getName(), info.getEjbLink(), info.getHome(), info.getInterface());
        throw new NameNotFoundException(message);
    }
    final ContainerSystem containerSystem = systemInstance.getComponent(ContainerSystem.class);
    final BeanContext beanContext = containerSystem.getBeanContext(deploymentId);
    if (beanContext == null) {
        final String message = messages.format("deploymentNotFound", info.getName(), deploymentId);
        throw new NameNotFoundException(message);
    }
    InterfaceType type = null;
    switch(info.getRefType()) {
        case LOCAL:
            type = InterfaceType.BUSINESS_LOCAL;
            break;
        case REMOTE:
            type = InterfaceType.BUSINESS_REMOTE;
            break;
    }
    final String jndiName = "openejb/Deployment/" + JndiBuilder.format(deploymentId, info.getInterface(), type);
    if (useCrossClassLoaderRef && isRemote(beanContext)) {
        reference = new CrossClassLoaderJndiReference(jndiName);
    } else {
        reference = new IntraVmJndiReference(jndiName);
    }
    return reference.getObject();
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) BeanContext(org.apache.openejb.BeanContext) IntraVmJndiReference(org.apache.openejb.core.ivm.naming.IntraVmJndiReference) InterfaceType(org.apache.openejb.InterfaceType) NameNotFoundException(javax.naming.NameNotFoundException) SystemInstance(org.apache.openejb.loader.SystemInstance) CrossClassLoaderJndiReference(org.apache.openejb.core.ivm.naming.CrossClassLoaderJndiReference)

Example 5 with InterfaceType

use of org.apache.openejb.InterfaceType in project tomee by apache.

the class EjbFactory method buildJndiName.

protected String buildJndiName(final Reference reference) throws NamingException {
    // get and verify deploymentId
    final String jndiName;
    final String deploymentId = NamingUtil.getProperty(reference, NamingUtil.DEPLOYMENT_ID);
    if (deploymentId == null) {
        throw new NamingException("ejb-ref deploymentId is null");
    }
    // get and verify interface type
    InterfaceType type = InterfaceType.BUSINESS_REMOTE;
    String interfaceType = NamingUtil.getProperty(reference, NamingUtil.REMOTE);
    if (interfaceType == null) {
        type = InterfaceType.LOCALBEAN;
        interfaceType = NamingUtil.getProperty(reference, NamingUtil.LOCALBEAN);
    }
    if (interfaceType == null) {
        type = InterfaceType.BUSINESS_LOCAL;
        interfaceType = NamingUtil.getProperty(reference, NamingUtil.LOCAL);
    }
    if (interfaceType == null) {
        throw new NamingException("ejb-ref interface type is null");
    }
    // build jndi name using the deploymentId and interface type
    jndiName = "java:openejb/Deployment/" + JndiBuilder.format(deploymentId, interfaceType, type);
    return jndiName;
}
Also used : InterfaceType(org.apache.openejb.InterfaceType) NamingException(javax.naming.NamingException)

Aggregations

InterfaceType (org.apache.openejb.InterfaceType)9 BeanContext (org.apache.openejb.BeanContext)7 ArrayList (java.util.ArrayList)6 ContainerSystem (org.apache.openejb.spi.ContainerSystem)3 Method (java.lang.reflect.Method)2 BeanType (org.apache.openejb.BeanType)2 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)2 EJBMetaDataImpl (org.apache.openejb.client.EJBMetaDataImpl)2 InvocationHandler (java.lang.reflect.InvocationHandler)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 PermissionCollection (java.security.PermissionCollection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Dependent (javax.enterprise.context.Dependent)1 Context (javax.enterprise.context.spi.Context)1 CreationalContext (javax.enterprise.context.spi.CreationalContext)1 AnnotatedMethod (javax.enterprise.inject.spi.AnnotatedMethod)1 ObserverMethod (javax.enterprise.inject.spi.ObserverMethod)1 Provider (javax.inject.Provider)1 NameNotFoundException (javax.naming.NameNotFoundException)1