Search in sources :

Example 6 with WebContext

use of org.apache.openejb.core.WebContext in project tomee by apache.

the class WsService method afterApplicationCreated.

public void afterApplicationCreated(final AppInfo appInfo, final WebAppInfo webApp) {
    final WebContext webContext = containerSystem.getWebContextByHost(webApp.moduleId, webApp.host != null ? webApp.host : virtualHost);
    if (webContext == null)
        return;
    // if already deployed skip this webapp
    if (!deployedWebApps.add(webApp))
        return;
    final Map<String, PortInfo> ports = new TreeMap<>();
    for (final PortInfo port : webApp.portInfos) {
        ports.put(port.serviceLink, port);
    }
    URL moduleBaseUrl = null;
    try {
        moduleBaseUrl = new File(webApp.path).toURI().toURL();
    } catch (final MalformedURLException e) {
        LOGGER.error("Invalid ejb jar location " + webApp.path, e);
    }
    // lazy init
    Collection<IdPropertiesInfo> pojoConfiguration = null;
    for (final ServletInfo servlet : webApp.servlets) {
        if (servlet.servletName == null) {
            continue;
        }
        final PortInfo portInfo = ports.get(servlet.servletName);
        if (portInfo == null) {
            continue;
        }
        final ClassLoader old = Thread.currentThread().getContextClassLoader();
        final ClassLoader classLoader = webContext.getClassLoader();
        Thread.currentThread().setContextClassLoader(classLoader);
        try {
            final Collection<Injection> injections = webContext.getInjections();
            final Context context = webContext.getJndiEnc();
            final Class target = classLoader.loadClass(servlet.servletClass);
            final Map<String, Object> bindings = webContext.getBindings();
            final PortData port = WsBuilder.toPortData(portInfo, injections, moduleBaseUrl, classLoader);
            pojoConfiguration = PojoUtil.findPojoConfig(pojoConfiguration, appInfo, webApp);
            final HttpListener container = createPojoWsContainer(classLoader, moduleBaseUrl, port, portInfo.serviceLink, target, context, webApp.contextRoot, bindings, new ServiceConfiguration(PojoUtil.findConfiguration(pojoConfiguration, target.getName()), appInfo.services));
            if (wsRegistry != null) {
                String auth = authMethod;
                String realm = realmName;
                String transport = transportGuarantee;
                if ("BASIC".equals(portInfo.authMethod) || "DIGEST".equals(portInfo.authMethod) || "CLIENT-CERT".equals(portInfo.authMethod)) {
                    auth = portInfo.authMethod;
                    realm = portInfo.realmName;
                    transport = portInfo.transportGuarantee;
                }
                // give servlet a reference to the webservice container
                final List<String> addresses = wsRegistry.setWsContainer(container, classLoader, webApp.contextRoot, host(webApp), servlet, realm, transport, auth, webApp.moduleId);
                // one of the registered addresses to be the connonical address
                final String address = HttpUtil.selectSingleAddress(addresses);
                if (address != null) {
                    // add address to global registry
                    portAddressRegistry.addPort(portInfo.serviceId, portInfo.wsdlService, portInfo.portId, portInfo.wsdlPort, portInfo.seiInterfaceName, address);
                    setWsdl(container, address);
                    LOGGER.info("Webservice(wsdl=" + address + ", qname=" + port.getWsdlService() + ") --> Pojo(id=" + portInfo.portId + ")");
                    /*
                        In POJO webservices it is very common to have the same JAW-WS endpoint published under different URLs
                        using url-pattern for the servlet. We only deploy one instance of the WsServlet and keep the same
                        set of url-pattern the user defined. At the moment we can't add additional ports, with different
                        addresses. So at least we should list other URLs so the user knows they are successfully deployed
                        and is aware of the different URLs
                         */
                    for (String urlAddress : addresses) {
                        if (address.equals(urlAddress)) {
                            continue;
                        }
                        LOGGER.info("Webservice(wsdl=" + urlAddress + ", qname=" + port.getWsdlService() + ") --> Pojo(id=" + portInfo.portId + ")");
                    }
                    servletAddresses.put(webApp.moduleId + "." + servlet.servletName, address);
                    addressesForApp(webApp.moduleId).add(new EndpointInfo(address, port.getWsdlService(), target.getName()));
                }
            }
        } catch (final Throwable e) {
            LOGGER.error("Error deploying CXF webservice for servlet " + portInfo.serviceLink, e);
        } finally {
            Thread.currentThread().setContextClassLoader(old);
        }
    }
}
Also used : WebContext(org.apache.openejb.core.WebContext) AppContext(org.apache.openejb.AppContext) BeanContext(org.apache.openejb.BeanContext) Context(javax.naming.Context) MalformedURLException(java.net.MalformedURLException) WebContext(org.apache.openejb.core.WebContext) Injection(org.apache.openejb.Injection) TreeMap(java.util.TreeMap) URL(java.net.URL) PortInfo(org.apache.openejb.assembler.classic.PortInfo) ServletInfo(org.apache.openejb.assembler.classic.ServletInfo) ServiceConfiguration(org.apache.openejb.assembler.classic.util.ServiceConfiguration) PortData(org.apache.openejb.core.webservices.PortData) IdPropertiesInfo(org.apache.openejb.assembler.classic.IdPropertiesInfo) HttpListener(org.apache.openejb.server.httpd.HttpListener) File(java.io.File)

Example 7 with WebContext

use of org.apache.openejb.core.WebContext in project tomee by apache.

the class RESTService method afterApplicationCreated.

/**
 * Deployment of JAX-RS services starts in response to a AfterApplicationCreated event
 * after normal deployment is done
 * @param appInfo the ear (real or auto-created) in which the webapp is contained
 * @param webApp the webapp containing EJB or Pojo rest services to deploy
 */
public void afterApplicationCreated(final AppInfo appInfo, final WebAppInfo webApp) {
    if ("false".equalsIgnoreCase(appInfo.properties.getProperty("openejb.jaxrs.on", "true"))) {
        return;
    }
    final WebContext webContext = containerSystem.getWebContextByHost(webApp.moduleId, webApp.host != null ? webApp.host : virtualHost);
    if (webContext == null) {
        return;
    }
    if (!deployedWebApps.add(webApp)) {
        return;
    }
    final Map<String, EJBRestServiceInfo> restEjbs = getRestEjbs(appInfo, webApp.moduleId);
    final ClassLoader classLoader = getClassLoader(webContext.getClassLoader());
    final Collection<Injection> injections = webContext.getInjections();
    final WebBeansContext owbCtx;
    if (webContext.getWebbeansContext() != null) {
        owbCtx = webContext.getWebbeansContext();
    } else {
        owbCtx = webContext.getAppContext().getWebBeansContext();
    }
    Context context = webContext.getJndiEnc();
    if (context == null) {
        // usually true since it is set in org.apache.tomee.catalina.TomcatWebAppBuilder.afterStart() and lookup(comp) fails
        context = webContext.getAppContext().getAppJndiContext();
    }
    final ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(classLoader);
    final Collection<Object> additionalProviders = new HashSet<>();
    addAppProvidersIfNeeded(appInfo, webApp, classLoader, additionalProviders);
    // done lazily
    Collection<IdPropertiesInfo> pojoConfigurations = null;
    try {
        boolean deploymentWithApplication = "true".equalsIgnoreCase(appInfo.properties.getProperty(OPENEJB_USE_APPLICATION_PROPERTY, APPLICATION_DEPLOYMENT));
        if (deploymentWithApplication) {
            Class<?> appClazz;
            for (final String app : webApp.restApplications) {
                Application application;
                boolean appSkipped = false;
                String prefix = "/";
                try {
                    appClazz = classLoader.loadClass(app);
                    application = Application.class.cast(appClazz.newInstance());
                    if (owbCtx != null && owbCtx.getBeanManagerImpl().isInUse()) {
                        try {
                            webContext.inject(application);
                        } catch (final Exception e) {
                        // not important since not required by the spec
                        }
                    }
                } catch (final Exception e) {
                    throw new OpenEJBRestRuntimeException("can't create class " + app, e);
                }
                application = wrapApplication(appInfo, application);
                final Set<Class<?>> classes = new HashSet<>(application.getClasses());
                final Set<Object> singletons = application.getSingletons();
                if (classes.size() + singletons.size() == 0) {
                    appSkipped = true;
                } else {
                    for (final Class<?> clazz : classes) {
                        if (isProvider(clazz)) {
                            additionalProviders.add(clazz);
                        } else if (!hasEjbAndIsNotAManagedBean(restEjbs, clazz.getName())) {
                            pojoConfigurations = PojoUtil.findPojoConfig(pojoConfigurations, appInfo, webApp);
                            if (PojoUtil.findConfiguration(pojoConfigurations, clazz.getName()) != null) {
                                deploymentWithApplication = false;
                                logOldDeploymentUsage(clazz.getName());
                            }
                        }
                    }
                    if (deploymentWithApplication) {
                        // don't do it if we detected we should use old deployment
                        for (final Object o : singletons) {
                            final Class<?> clazz = o.getClass();
                            if (isProvider(clazz)) {
                                additionalProviders.add(o);
                            } else if (!hasEjbAndIsNotAManagedBean(restEjbs, clazz.getName())) {
                                pojoConfigurations = PojoUtil.findPojoConfig(pojoConfigurations, appInfo, webApp);
                                if (PojoUtil.findConfiguration(pojoConfigurations, clazz.getName()) != null) {
                                    deploymentWithApplication = false;
                                    logOldDeploymentUsage(clazz.getName());
                                }
                            }
                        }
                    }
                }
                if (deploymentWithApplication) {
                    // don't do it if we detected we should use old deployment
                    final String path = appPrefix(webApp, appClazz);
                    if (path != null) {
                        prefix += path + wildcard;
                    } else {
                        prefix += wildcard;
                    }
                }
                if (deploymentWithApplication) {
                    // don't do it if we detected we should use old deployment
                    if (appSkipped || application == null) {
                        application = !InternalApplication.class.isInstance(application) ? new InternalApplication(application) : application;
                        for (final String clazz : webApp.restClass) {
                            try {
                                final Class<?> loaded = classLoader.loadClass(clazz);
                                if (!isProvider(loaded)) {
                                    pojoConfigurations = PojoUtil.findPojoConfig(pojoConfigurations, appInfo, webApp);
                                    if (PojoUtil.findConfiguration(pojoConfigurations, loaded.getName()) != null) {
                                        deploymentWithApplication = false;
                                        logOldDeploymentUsage(loaded.getName());
                                        break;
                                    }
                                    application.getClasses().add(loaded);
                                } else {
                                    additionalProviders.add(loaded);
                                }
                            } catch (final Exception e) {
                                throw new OpenEJBRestRuntimeException("can't load class " + clazz, e);
                            }
                        }
                        if (deploymentWithApplication) {
                            addEjbToApplication(application, restEjbs);
                            if (!prefix.endsWith(wildcard)) {
                                prefix += wildcard;
                            }
                        }
                    }
                    if (!application.getClasses().isEmpty() || !application.getSingletons().isEmpty()) {
                        pojoConfigurations = PojoUtil.findPojoConfig(pojoConfigurations, appInfo, webApp);
                        deployApplication(appInfo, webApp.contextRoot, restEjbs, classLoader, injections, owbCtx, context, additionalProviders, pojoConfigurations, application, prefix);
                    }
                }
                if (!deploymentWithApplication) {
                    fullServletDeployment(appInfo, webApp, webContext, restEjbs, classLoader, injections, owbCtx, context, additionalProviders, pojoConfigurations);
                }
            }
            if (webApp.restApplications.isEmpty()) {
                final Application application = new InternalApplication(null);
                for (final String clazz : webApp.restClass) {
                    try {
                        final Class<?> loaded = classLoader.loadClass(clazz);
                        if (!isProvider(loaded)) {
                            pojoConfigurations = PojoUtil.findPojoConfig(pojoConfigurations, appInfo, webApp);
                            if (PojoUtil.findConfiguration(pojoConfigurations, loaded.getName()) != null) {
                                deploymentWithApplication = false;
                                logOldDeploymentUsage(loaded.getName());
                                break;
                            }
                            application.getClasses().add(loaded);
                        } else {
                            additionalProviders.add(loaded);
                        }
                    } catch (final Exception e) {
                        throw new OpenEJBRestRuntimeException("can't load class " + clazz, e);
                    }
                }
                addEjbToApplication(application, restEjbs);
                if (deploymentWithApplication) {
                    if (!application.getClasses().isEmpty() || !application.getSingletons().isEmpty()) {
                        final String path = appPrefix(webApp, application.getClass());
                        final String prefix;
                        if (path != null) {
                            prefix = "/" + path + wildcard;
                        } else {
                            prefix = "/" + wildcard;
                        }
                        pojoConfigurations = PojoUtil.findPojoConfig(pojoConfigurations, appInfo, webApp);
                        deployApplication(appInfo, webApp.contextRoot, restEjbs, classLoader, injections, owbCtx, context, additionalProviders, pojoConfigurations, application, prefix);
                    }
                } else {
                    fullServletDeployment(appInfo, webApp, webContext, restEjbs, classLoader, injections, owbCtx, context, additionalProviders, pojoConfigurations);
                }
            }
        } else {
            fullServletDeployment(appInfo, webApp, webContext, restEjbs, classLoader, injections, owbCtx, context, additionalProviders, pojoConfigurations);
        }
    } finally {
        Thread.currentThread().setContextClassLoader(oldLoader);
    }
}
Also used : WebContext(org.apache.openejb.core.WebContext) WebBeansContext(org.apache.webbeans.config.WebBeansContext) AppContext(org.apache.openejb.AppContext) BeanContext(org.apache.openejb.BeanContext) Context(javax.naming.Context) WebContext(org.apache.openejb.core.WebContext) Injection(org.apache.openejb.Injection) URISyntaxException(java.net.URISyntaxException) UncheckedIOException(java.io.UncheckedIOException) ServiceException(org.apache.openejb.server.ServiceException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) WebBeansContext(org.apache.webbeans.config.WebBeansContext) IdPropertiesInfo(org.apache.openejb.assembler.classic.IdPropertiesInfo) MetaAnnotatedClass(org.apache.xbean.finder.MetaAnnotatedClass) Application(javax.ws.rs.core.Application) HashSet(java.util.HashSet)

Example 8 with WebContext

use of org.apache.openejb.core.WebContext in project tomee by apache.

the class BeginWebBeansListener method contextDestroyed.

@Override
public void contextDestroyed(final ServletContextEvent servletContextEvent) {
    WebBeansListenerHelper.destroyFakedRequest(this);
    final WebContext wc = WebContext.class.cast(servletContextEvent.getServletContext().getAttribute("openejb.web.context"));
    if (wc != null) {
        wc.release();
    }
}
Also used : WebContext(org.apache.openejb.core.WebContext)

Example 9 with WebContext

use of org.apache.openejb.core.WebContext in project tomee by apache.

the class AppFinder method findAppContextOrWeb.

public static <T> T findAppContextOrWeb(final ClassLoader cl, final Transformer<T> transformer) {
    final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
    for (final AppContext appContext : containerSystem.getAppContexts()) {
        final ClassLoader appContextClassLoader = appContext.getClassLoader();
        boolean found = false;
        if (appContextClassLoader.equals(cl) || (cl != null && cl.equals(appContextClassLoader))) {
            // CxfContainerLoader is not symmetric
            final T from = transformer.from(appContext);
            found = true;
            if (from != null) {
                return from;
            }
        }
        for (final WebContext web : appContext.getWebContexts()) {
            final ClassLoader webClassLoader = web.getClassLoader();
            if (webClassLoader.equals(cl) || (cl != null && cl.equals(webClassLoader))) {
                return transformer.from(web);
            }
        }
        if (found) {
            // for cases where app and webapp share the same classloader
            break;
        }
    }
    return null;
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) WebContext(org.apache.openejb.core.WebContext) AppContext(org.apache.openejb.AppContext)

Example 10 with WebContext

use of org.apache.openejb.core.WebContext in project tomee by apache.

the class OpenEJBEnricher method resolve.

public static Object[] resolve(final AppContext appContext, final TestClass ignored, final Method method) {
    // suppose all is a CDI bean...
    final Object[] values = new Object[method.getParameterTypes().length];
    if (appContext == null) {
        return values;
    }
    final List<BeanManager> beanManagers = new ArrayList<>();
    final BeanManager bm = findBeanManager(appContext);
    if (bm != null) {
        // then add web bean manager first, TODO: selection of the webapp containing the test?
        for (final WebContext web : appContext.getWebContexts()) {
            final WebBeansContext webBeansContext = web.getWebBeansContext();
            if (webBeansContext == null) {
                continue;
            }
            final BeanManagerImpl webAppBm = webBeansContext.getBeanManagerImpl();
            if (bm != webAppBm) {
                beanManagers.add(webAppBm);
            }
        }
        beanManagers.add(bm);
    }
    if (beanManagers.isEmpty()) {
        return values;
    }
    final Class<?>[] parameterTypes = method.getParameterTypes();
    for (int i = 0; i < parameterTypes.length; i++) {
        Exception ex = null;
        for (final BeanManager beanManager : beanManagers) {
            try {
                values[i] = getParamInstance(beanManager, i, method);
                break;
            } catch (final Exception e) {
                ex = e;
            }
        }
        if (ex != null) {
            LOGGER.info(ex.getMessage());
        }
    }
    return values;
}
Also used : WebContext(org.apache.openejb.core.WebContext) WebBeansContext(org.apache.webbeans.config.WebBeansContext) BeanManagerImpl(org.apache.webbeans.container.BeanManagerImpl) ArrayList(java.util.ArrayList) TestClass(org.jboss.arquillian.test.spi.TestClass) BeanManager(javax.enterprise.inject.spi.BeanManager) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) OpenEJBException(org.apache.openejb.OpenEJBException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException)

Aggregations

WebContext (org.apache.openejb.core.WebContext)25 AppContext (org.apache.openejb.AppContext)17 BeanContext (org.apache.openejb.BeanContext)11 ContainerSystem (org.apache.openejb.spi.ContainerSystem)11 WebBeansContext (org.apache.webbeans.config.WebBeansContext)9 NamingException (javax.naming.NamingException)8 IOException (java.io.IOException)7 Context (javax.naming.Context)7 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)7 ArrayList (java.util.ArrayList)6 ServletContext (javax.servlet.ServletContext)6 OpenEJBException (org.apache.openejb.OpenEJBException)6 MalformedURLException (java.net.MalformedURLException)5 NameNotFoundException (javax.naming.NameNotFoundException)5 Injection (org.apache.openejb.Injection)5 WebAppInfo (org.apache.openejb.assembler.classic.WebAppInfo)5 InitialContext (javax.naming.InitialContext)4 ThreadContext (org.apache.openejb.core.ThreadContext)4 URL (java.net.URL)3 HashSet (java.util.HashSet)3