Search in sources :

Example 81 with BeanContext

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

the class JndiRequestHandler method doLookup.

private void doLookup(final JNDIRequest req, final JNDIResponse res, final String prefix) {
    Object object;
    final String name = req.getRequestString();
    try {
        if (name.equals("info/injections")) {
            // noinspection unchecked
            final List<Injection> injections = (List<Injection>) rootContext.lookup(prefix + name);
            final InjectionMetaData metaData = new InjectionMetaData();
            for (final Injection injection : injections) {
                if (injection.getTarget() == null) {
                    continue;
                }
                metaData.addInjection(injection.getTarget().getName(), injection.getName(), injection.getJndiName());
            }
            res.setResponseCode(ResponseCodes.JNDI_INJECTIONS);
            res.setResult(metaData);
            return;
        } else {
            try {
                object = rootContext.lookup(prefix + name);
            } catch (NameNotFoundException nnfe) {
                // fallback to resources
                object = rootContext.lookup("openejb/Resource/" + name);
            }
        }
        if (object instanceof Context) {
            res.setResponseCode(ResponseCodes.JNDI_CONTEXT);
            return;
        } else if (object == null) {
            throw new NullPointerException("lookup of '" + name + "' returned null");
        } else if (object instanceof DataSource) {
            if (DataSourceFactory.knows(object)) {
                try {
                    final DbcpDataSource cf = new DbcpDataSource(object);
                    final DataSourceMetaData dataSourceMetaData = new DataSourceMetaData(cf.getDriverClassName(), cf.getUrl(), cf.getUsername(), cf.getPassword());
                    res.setResponseCode(ResponseCodes.JNDI_DATA_SOURCE);
                    res.setResult(dataSourceMetaData);
                } catch (Exception e) {
                    res.setResponseCode(ResponseCodes.JNDI_ERROR);
                    res.setResult(new ThrowableArtifact(e));
                }
                return;
            } else if (object instanceof Referenceable) {
                res.setResponseCode(ResponseCodes.JNDI_REFERENCE);
                res.setResult(((Referenceable) object).getReference());
                return;
            }
        } else if (object instanceof ConnectionFactory) {
            res.setResponseCode(ResponseCodes.JNDI_RESOURCE);
            res.setResult(ConnectionFactory.class.getName());
            return;
        } else if (ORB_CLASS != null && ORB_CLASS.isInstance(object)) {
            res.setResponseCode(ResponseCodes.JNDI_RESOURCE);
            res.setResult(ORB_CLASS.getName());
            return;
        } else if (object instanceof ValidatorFactory) {
            res.setResponseCode(ResponseCodes.JNDI_RESOURCE);
            res.setResult(ValidatorFactory.class.getName());
            return;
        } else if (object instanceof Validator) {
            res.setResponseCode(ResponseCodes.JNDI_RESOURCE);
            res.setResult(Validator.class.getName());
            return;
        } else if (object instanceof Queue) {
            res.setResponseCode(ResponseCodes.JNDI_RESOURCE);
            res.setResult(Queue.class.getName());
            return;
        } else if (object instanceof Topic) {
            res.setResponseCode(ResponseCodes.JNDI_RESOURCE);
            res.setResult(Topic.class.getName());
            return;
        }
        final ServiceRefData serviceRef;
        if (object instanceof ServiceRefData) {
            serviceRef = (ServiceRefData) object;
        } else {
            serviceRef = ServiceRefData.getServiceRefData(object);
        }
        if (serviceRef != null) {
            final WsMetaData serviceMetaData = new WsMetaData();
            // service class
            String serviceClassName = null;
            if (serviceRef.getServiceClass() != null) {
                serviceClassName = serviceRef.getServiceClass().getName();
            }
            serviceMetaData.setServiceClassName(serviceClassName);
            // reference class
            String referenceClassName = null;
            if (serviceRef.getReferenceClass() != null) {
                referenceClassName = serviceRef.getReferenceClass().getName();
            }
            serviceMetaData.setReferenceClassName(referenceClassName);
            // set service qname
            if (serviceRef.getServiceQName() != null) {
                serviceMetaData.setServiceQName(serviceRef.getServiceQName().toString());
            }
            // get the port addresses for this service
            final PortAddressRegistry portAddressRegistry = SystemInstance.get().getComponent(PortAddressRegistry.class);
            Set<PortAddress> portAddresses = null;
            if (portAddressRegistry != null) {
                portAddresses = portAddressRegistry.getPorts(serviceRef.getId(), serviceRef.getServiceQName(), referenceClassName);
            }
            // resolve the wsdl url
            if (serviceRef.getWsdlURL() != null) {
                serviceMetaData.setWsdlUrl(serviceRef.getWsdlURL().toExternalForm());
            }
            if (portAddresses.size() == 1) {
                final PortAddress portAddress = portAddresses.iterator().next();
                serviceMetaData.setWsdlUrl(portAddress.getAddress() + "?wsdl");
            }
            // add handler chains
            for (final HandlerChainData handlerChain : serviceRef.getHandlerChains()) {
                final HandlerChainMetaData handlerChainMetaData = new HandlerChainMetaData();
                handlerChainMetaData.setServiceNamePattern(handlerChain.getServiceNamePattern());
                handlerChainMetaData.setPortNamePattern(handlerChain.getPortNamePattern());
                handlerChainMetaData.getProtocolBindings().addAll(handlerChain.getProtocolBindings());
                for (final HandlerData handler : handlerChain.getHandlers()) {
                    final HandlerMetaData handlerMetaData = new HandlerMetaData();
                    handlerMetaData.setHandlerClass(handler.getHandlerClass().getName());
                    for (final Method method : handler.getPostConstruct()) {
                        final CallbackMetaData callbackMetaData = new CallbackMetaData();
                        callbackMetaData.setClassName(method.getDeclaringClass().getName());
                        callbackMetaData.setMethod(method.getName());
                        handlerMetaData.getPostConstruct().add(callbackMetaData);
                    }
                    for (final Method method : handler.getPreDestroy()) {
                        final CallbackMetaData callbackMetaData = new CallbackMetaData();
                        callbackMetaData.setClassName(method.getDeclaringClass().getName());
                        callbackMetaData.setMethod(method.getName());
                        handlerMetaData.getPreDestroy().add(callbackMetaData);
                    }
                    handlerChainMetaData.getHandlers().add(handlerMetaData);
                }
                serviceMetaData.getHandlerChains().add(handlerChainMetaData);
            }
            // add port refs
            final Map<QName, PortRefMetaData> portsByQName = new HashMap<>();
            for (final PortRefData portRef : serviceRef.getPortRefs()) {
                final PortRefMetaData portRefMetaData = new PortRefMetaData();
                portRefMetaData.setQName(portRef.getQName());
                portRefMetaData.setServiceEndpointInterface(portRef.getServiceEndpointInterface());
                portRefMetaData.setEnableMtom(portRef.isEnableMtom());
                portRefMetaData.getProperties().putAll(portRef.getProperties());
                portRefMetaData.getAddresses().addAll(portRef.getAddresses());
                if (portRef.getQName() != null) {
                    portsByQName.put(portRef.getQName(), portRefMetaData);
                }
                serviceMetaData.getPortRefs().add(portRefMetaData);
            }
            // add PortRefMetaData for any portAddress not added above
            for (final PortAddress portAddress : portAddresses) {
                PortRefMetaData portRefMetaData = portsByQName.get(portAddress.getPortQName());
                if (portRefMetaData == null) {
                    portRefMetaData = new PortRefMetaData();
                    portRefMetaData.setQName(portAddress.getPortQName());
                    portRefMetaData.setServiceEndpointInterface(portAddress.getServiceEndpointInterface());
                    portRefMetaData.getAddresses().add(portAddress.getAddress());
                    serviceMetaData.getPortRefs().add(portRefMetaData);
                } else {
                    portRefMetaData.getAddresses().add(portAddress.getAddress());
                    if (portRefMetaData.getServiceEndpointInterface() == null) {
                        portRefMetaData.setServiceEndpointInterface(portAddress.getServiceEndpointInterface());
                    }
                }
            }
            res.setResponseCode(ResponseCodes.JNDI_WEBSERVICE);
            res.setResult(serviceMetaData);
            return;
        }
    } catch (NameNotFoundException e) {
        res.setResponseCode(ResponseCodes.JNDI_NOT_FOUND);
        return;
    } catch (NamingException e) {
        res.setResponseCode(ResponseCodes.JNDI_NAMING_EXCEPTION);
        res.setResult(new ThrowableArtifact(e));
        return;
    }
    BaseEjbProxyHandler handler;
    try {
        handler = (BaseEjbProxyHandler) ProxyManager.getInvocationHandler(object);
    } catch (Exception e) {
        try {
            final Field field = object.getClass().getDeclaredField("invocationHandler");
            field.setAccessible(true);
            handler = (BaseEjbProxyHandler) field.get(object);
        } catch (Exception e1) {
            // Not a proxy.  See if it's serializable and send it
            if (object instanceof java.io.Serializable) {
                res.setResponseCode(ResponseCodes.JNDI_OK);
                res.setResult(object);
                return;
            } else {
                res.setResponseCode(ResponseCodes.JNDI_NAMING_EXCEPTION);
                final NamingException namingException = new NamingException("Expected an ejb proxy, found unknown object: type=" + object.getClass().getName() + ", toString=" + object);
                res.setResult(new ThrowableArtifact(namingException));
                return;
            }
        }
    }
    final ProxyInfo proxyInfo = handler.getProxyInfo();
    final BeanContext beanContext = proxyInfo.getBeanContext();
    final String deploymentID = beanContext.getDeploymentID().toString();
    updateServer(req, res, proxyInfo);
    switch(proxyInfo.getInterfaceType()) {
        case EJB_HOME:
            {
                res.setResponseCode(ResponseCodes.JNDI_EJBHOME);
                final EJBMetaDataImpl metaData = new EJBMetaDataImpl(beanContext.getHomeInterface(), beanContext.getRemoteInterface(), beanContext.getPrimaryKeyClass(), beanContext.getComponentType().toString(), deploymentID, -1, convert(proxyInfo.getInterfaceType()), null, beanContext.getAsynchronousMethodSignatures());
                metaData.loadProperties(beanContext.getProperties());
                log(metaData);
                res.setResult(metaData);
                break;
            }
        case EJB_LOCAL_HOME:
            {
                res.setResponseCode(ResponseCodes.JNDI_NAMING_EXCEPTION);
                final NamingException namingException = new NamingException("Not remotable: '" + name + "'. EJBLocalHome interfaces are not remotable as per the EJB specification.");
                res.setResult(new ThrowableArtifact(namingException));
                break;
            }
        case BUSINESS_REMOTE:
            {
                res.setResponseCode(ResponseCodes.JNDI_BUSINESS_OBJECT);
                final EJBMetaDataImpl metaData = new EJBMetaDataImpl(null, null, beanContext.getPrimaryKeyClass(), beanContext.getComponentType().toString(), deploymentID, -1, convert(proxyInfo.getInterfaceType()), proxyInfo.getInterfaces(), beanContext.getAsynchronousMethodSignatures());
                metaData.setPrimaryKey(proxyInfo.getPrimaryKey());
                metaData.loadProperties(beanContext.getProperties());
                log(metaData);
                res.setResult(metaData);
                break;
            }
        case BUSINESS_LOCAL:
            {
                res.setResponseCode(ResponseCodes.JNDI_NAMING_EXCEPTION);
                final NamingException namingException = new NamingException("Not remotable: '" + name + "'. Business Local interfaces are not remotable as per the EJB specification.  To disable this restriction, set the system property 'openejb.remotable.businessLocals=true' in the server.");
                res.setResult(new ThrowableArtifact(namingException));
                break;
            }
        case LOCALBEAN:
            {
                res.setResponseCode(ResponseCodes.JNDI_NAMING_EXCEPTION);
                final NamingException namingException = new NamingException("Not remotable: '" + name + "'. LocalBean classes are not remotable as per the EJB specification.");
                res.setResult(new ThrowableArtifact(namingException));
                break;
            }
        default:
            {
                res.setResponseCode(ResponseCodes.JNDI_NAMING_EXCEPTION);
                final NamingException namingException = new NamingException("Not remotable: '" + name + "'.");
                res.setResult(new ThrowableArtifact(namingException));
            }
    }
}
Also used : HandlerChainData(org.apache.openejb.core.webservices.HandlerChainData) HandlerMetaData(org.apache.openejb.client.HandlerMetaData) HashMap(java.util.HashMap) WsMetaData(org.apache.openejb.client.WsMetaData) DataSourceMetaData(org.apache.openejb.client.DataSourceMetaData) InjectionMetaData(org.apache.openejb.client.InjectionMetaData) HandlerData(org.apache.openejb.core.webservices.HandlerData) Field(java.lang.reflect.Field) ProxyInfo(org.apache.openejb.ProxyInfo) ConnectionFactory(javax.jms.ConnectionFactory) Referenceable(javax.resource.Referenceable) List(java.util.List) ArrayList(java.util.ArrayList) NamingException(javax.naming.NamingException) PortRefData(org.apache.openejb.core.webservices.PortRefData) Topic(javax.jms.Topic) Queue(java.util.Queue) CallbackMetaData(org.apache.openejb.client.CallbackMetaData) IvmContext(org.apache.openejb.core.ivm.naming.IvmContext) BeanContext(org.apache.openejb.BeanContext) Context(javax.naming.Context) BaseEjbProxyHandler(org.apache.openejb.core.ivm.BaseEjbProxyHandler) ValidatorFactory(javax.validation.ValidatorFactory) NameNotFoundException(javax.naming.NameNotFoundException) QName(javax.xml.namespace.QName) ThrowableArtifact(org.apache.openejb.client.ThrowableArtifact) PortRefMetaData(org.apache.openejb.client.PortRefMetaData) Injection(org.apache.openejb.Injection) ServiceRefData(org.apache.openejb.core.webservices.ServiceRefData) Method(java.lang.reflect.Method) NamingException(javax.naming.NamingException) NameNotFoundException(javax.naming.NameNotFoundException) DataSource(javax.sql.DataSource) BeanContext(org.apache.openejb.BeanContext) PortAddress(org.apache.openejb.core.webservices.PortAddress) EJBMetaDataImpl(org.apache.openejb.client.EJBMetaDataImpl) PortAddressRegistry(org.apache.openejb.core.webservices.PortAddressRegistry) Validator(javax.validation.Validator) HandlerChainMetaData(org.apache.openejb.client.HandlerChainMetaData)

Example 82 with BeanContext

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

the class UberInterfaceTest method test.

public void test() throws Exception {
    final EjbServer ejbServer = new EjbServer();
    final Properties initProps = new Properties();
    initProps.setProperty("openejb.deployments.classpath.include", "");
    initProps.setProperty("openejb.deployments.classpath.filter.descriptors", "true");
    OpenEJB.init(initProps, new ServerFederation());
    ejbServer.init(new Properties());
    final ServicePool pool = new ServicePool(ejbServer, 10);
    final ServiceDaemon serviceDaemon = new ServiceDaemon(pool, 0, "localhost");
    serviceDaemon.start();
    final int port = serviceDaemon.getPort();
    final Assembler assembler = SystemInstance.get().getComponent(Assembler.class);
    final ConfigurationFactory config = new ConfigurationFactory();
    final EjbJar ejbJar = new EjbJar();
    final StatelessBean bean = ejbJar.addEnterpriseBean(new StatelessBean(SuperBean.class));
    final EjbJarInfo ejbJarInfo = config.configureApplication(ejbJar);
    final EnterpriseBeanInfo beanInfo = ejbJarInfo.enterpriseBeans.get(0);
    assertEquals(asList(Everything.class.getName()), beanInfo.businessLocal);
    assertEquals(asList(Everything.class.getName()), beanInfo.businessRemote);
    assembler.createApplication(ejbJarInfo);
    final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
    final BeanContext deployment = containerSystem.getBeanContext(beanInfo.ejbDeploymentId);
    assertEquals(asList(Everything.class), deployment.getBusinessLocalInterfaces());
    assertEquals(asList(Everything.class), deployment.getBusinessRemoteInterfaces());
    {
        // remote invoke
        final Properties props = new Properties();
        props.put("java.naming.factory.initial", "org.apache.openejb.client.RemoteInitialContextFactory");
        props.put("java.naming.provider.url", "ejbd://127.0.0.1:" + port);
        final Context context = new InitialContext(props);
        final Everything remote = (Everything) context.lookup("SuperBeanRemote");
        final Reference reference = new Reference("test");
        assertEquals(reference, remote.echo(reference));
        // pass by value
        assertNotSame(reference, remote.echo(reference));
    }
    {
        // local invoke
        final Properties props = new Properties();
        props.put("java.naming.factory.initial", "org.apache.openejb.core.LocalInitialContextFactory");
        final Context context = new InitialContext(props);
        final Everything local = (Everything) context.lookup("SuperBeanLocal");
        final Reference reference = new Reference("test");
        assertEquals(reference, local.echo(reference));
        // pass by reference
        assertSame(reference, local.echo(reference));
    }
    serviceDaemon.stop();
    OpenEJB.destroy();
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) InitialContext(javax.naming.InitialContext) BeanContext(org.apache.openejb.BeanContext) Context(javax.naming.Context) ServerFederation(org.apache.openejb.core.ServerFederation) ServicePool(org.apache.openejb.server.ServicePool) Properties(java.util.Properties) InitialContext(javax.naming.InitialContext) EnterpriseBeanInfo(org.apache.openejb.assembler.classic.EnterpriseBeanInfo) BeanContext(org.apache.openejb.BeanContext) StatelessBean(org.apache.openejb.jee.StatelessBean) ServiceDaemon(org.apache.openejb.server.ServiceDaemon) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) Assembler(org.apache.openejb.assembler.classic.Assembler) EjbJarInfo(org.apache.openejb.assembler.classic.EjbJarInfo) EjbJar(org.apache.openejb.jee.EjbJar)

Example 83 with BeanContext

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

the class BasicClusterableRequestHandlerTest method setUp.

@Override
protected void setUp() throws Exception {
    requestHandler = new BasicClusterableRequestHandler();
    request = (ClusterableRequest) mock(ClusterableRequest.class);
    response = (ClusterableResponse) mock(ClusterableResponse.class);
    clusteredContainer = (ClusteredRPCContainer) mock(ClusteredRPCContainer.class);
    beanContext = new BeanContext("aDeploymentId", null, new ModuleContext("", null, "", new AppContext("", SystemInstance.get(), null, null, null, false), null, null), BasicClusterableRequestHandlerTest.class, null, null, null, null, null, null, null, null, null, null, false, false);
}
Also used : BeanContext(org.apache.openejb.BeanContext) AppContext(org.apache.openejb.AppContext) ModuleContext(org.apache.openejb.ModuleContext)

Example 84 with BeanContext

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

the class ClientObjectFactory method getHomeHandle.

@Override
public javax.ejb.HomeHandle getHomeHandle(final ProxyInfo info) {
    final CallContext call = CallContext.getCallContext();
    final BeanContext beanContext = info.getBeanContext();
    final int idCode = -1;
    Object securityIdentity = null;
    try {
        securityIdentity = call.getEJBRequest().getClientIdentity();
    } catch (Exception e) {
        e.printStackTrace();
    }
    final ClientMetaData cMetaData = new ClientMetaData(securityIdentity);
    final EJBMetaDataImpl eMetaData = buildEjbMetaData(info, beanContext, idCode);
    final EJBHomeHandler hanlder = EJBHomeHandler.createEJBHomeHandler(JNDIContext.globalExecutor(), eMetaData, getServerMetaData(), cMetaData, call.get(JNDIContext.AuthenticationInfo.class));
    return new EJBHomeHandle(hanlder.createEJBHomeProxy());
}
Also used : BeanContext(org.apache.openejb.BeanContext) EJBMetaDataImpl(org.apache.openejb.client.EJBMetaDataImpl) EJBHomeHandler(org.apache.openejb.client.EJBHomeHandler) EJBHomeHandle(org.apache.openejb.client.EJBHomeHandle) ClientMetaData(org.apache.openejb.client.ClientMetaData)

Example 85 with BeanContext

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

the class DeploymentIndex method getDeployment.

public BeanContext getDeployment(final EJBRequest req) throws RemoteException {
    final BeanContext info;
    final int deploymentCode = req.getDeploymentCode();
    if (deploymentCode > 0 && deploymentCode < deployments.length) {
        info = deployments[deploymentCode];
        req.setDeploymentId((String) info.getDeploymentID());
        return info;
    }
    if (req.getDeploymentId() == null) {
        throw new RemoteException(messages.format("invalidDeploymentIdAndCode", req.getDeploymentId(), req.getDeploymentCode()));
    }
    final int idCode = getDeploymentIndex(req.getDeploymentId());
    if (idCode == -1) {
        throw new RemoteException(messages.format("noSuchDeploymentIdAndCode", req.getDeploymentId(), req.getDeploymentCode()));
    }
    req.setDeploymentCode(idCode);
    if (req.getDeploymentCode() < 0 || req.getDeploymentCode() >= deployments.length) {
        throw new RemoteException(messages.format("invalidDeploymentIdAndCode", req.getDeploymentId(), req.getDeploymentCode()));
    }
    return deployments[req.getDeploymentCode()];
}
Also used : BeanContext(org.apache.openejb.BeanContext) RemoteException(java.rmi.RemoteException)

Aggregations

BeanContext (org.apache.openejb.BeanContext)198 OpenEJBException (org.apache.openejb.OpenEJBException)40 ThreadContext (org.apache.openejb.core.ThreadContext)40 Method (java.lang.reflect.Method)38 ContainerSystem (org.apache.openejb.spi.ContainerSystem)28 ArrayList (java.util.ArrayList)27 AppContext (org.apache.openejb.AppContext)26 TransactionPolicy (org.apache.openejb.core.transaction.TransactionPolicy)26 NamingException (javax.naming.NamingException)24 InterceptorData (org.apache.openejb.core.interceptor.InterceptorData)23 Context (javax.naming.Context)22 ApplicationException (org.apache.openejb.ApplicationException)20 HashMap (java.util.HashMap)19 EJBLocalObject (javax.ejb.EJBLocalObject)18 EJBObject (javax.ejb.EJBObject)18 SystemException (org.apache.openejb.SystemException)18 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)17 ModuleContext (org.apache.openejb.ModuleContext)16 EjbTransactionUtil.createTransactionPolicy (org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy)16 FinderException (javax.ejb.FinderException)14