Search in sources :

Example 21 with WebAppInfo

use of org.apache.openejb.assembler.classic.WebAppInfo in project tomee by apache.

the class OpenEJBContextConfig method cleanUpRestServlets.

private void cleanUpRestServlets() {
    final WebAppInfo webAppInfo = info.get();
    final AppInfo appInfo = info.app();
    if (webAppInfo == null || appInfo == null || "false".equalsIgnoreCase(appInfo.properties.getProperty("openejb.jaxrs.on", "true"))) {
        return;
    }
    final Container[] children = context.findChildren();
    final Map<String, Container> mappedChildren = new HashMap<String, Container>();
    if (children != null) {
        // index potential rest containers by class to cleanup applications defined as servlet
        for (final Container c : children) {
            if (!(c instanceof StandardWrapper)) {
                continue;
            }
            final StandardWrapper wrapper = (StandardWrapper) c;
            final String appSpec = wrapper.getInitParameter("javax.ws.rs.Application");
            if (appSpec != null) {
                mappedChildren.put(appSpec, c);
            } else {
                final String app = wrapper.getInitParameter(Application.class.getName());
                if (app != null) {
                    mappedChildren.put(app, c);
                } else if (wrapper.getServletClass() == null) {
                    try {
                        if (Application.class.isAssignableFrom(context.getLoader().getClassLoader().loadClass(wrapper.getServletName()))) {
                            // remove directly since it is not in restApplications
                            context.removeChild(c);
                        }
                    } catch (final Exception e) {
                    // no-op
                    }
                }
            }
        }
        // cleanup
        for (final String clazz : webAppInfo.restApplications) {
            final Container child = mappedChildren.get(clazz);
            try {
                // remove only "fake" servlets to let users use their own stuff
                if (child != null) {
                    final String servletClass = StandardWrapper.class.cast(child).getServletClass();
                    if (servletClass == null || "org.apache.openejb.server.rest.OpenEJBRestServlet".equals(servletClass) || !HttpServlet.class.isAssignableFrom(info.loader().loadClass(servletClass))) {
                        context.removeChild(child);
                    }
                }
            } catch (final NoClassDefFoundError | ClassNotFoundException e) {
                context.removeChild(child);
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ClassFormatException(org.apache.tomcat.util.bcel.classfile.ClassFormatException) WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo) AppInfo(org.apache.openejb.assembler.classic.AppInfo) WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo) Container(org.apache.catalina.Container) Application(javax.ws.rs.core.Application) StandardWrapper(org.apache.catalina.core.StandardWrapper)

Example 22 with WebAppInfo

use of org.apache.openejb.assembler.classic.WebAppInfo in project tomee by apache.

the class OpenEJBContextConfig method processAnnotationsFile.

@Override
protected void processAnnotationsFile(final File file, final WebXml fragment, final boolean handlesTypesOnly, final Map<String, JavaClassCacheEntry> javaClassCache) {
    try {
        if (NewLoaderLogic.skip(file.toURI().toURL())) {
            return;
        }
    } catch (final MalformedURLException e) {
    // no-op: let it be
    }
    final WebAppInfo webAppInfo = info.get();
    if (webAppInfo == null) {
        super.processAnnotationsFile(file, fragment, handlesTypesOnly, javaClassCache);
        return;
    }
    internalProcessAnnotations(file, webAppInfo, fragment);
}
Also used : MalformedURLException(java.net.MalformedURLException) WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo)

Example 23 with WebAppInfo

use of org.apache.openejb.assembler.classic.WebAppInfo in project tomee by apache.

the class LightweightWebAppBuilder method deployWebApps.

@Override
public void deployWebApps(final AppInfo appInfo, final ClassLoader appClassLoader) throws Exception {
    final CoreContainerSystem cs = (CoreContainerSystem) SystemInstance.get().getComponent(ContainerSystem.class);
    final AppContext appContext = cs.getAppContext(appInfo.appId);
    if (appContext == null) {
        throw new OpenEJBRuntimeException("Can't find app context for " + appInfo.appId);
    }
    for (final WebAppInfo webAppInfo : appInfo.webApps) {
        ClassLoader classLoader = loaderByWebContext.get(webAppInfo.moduleId);
        if (classLoader == null) {
            classLoader = appClassLoader;
        }
        final Set<Injection> injections = new HashSet<Injection>(appContext.getInjections());
        injections.addAll(new InjectionBuilder(classLoader).buildInjections(webAppInfo.jndiEnc));
        final List<BeanContext> beanContexts;
        if (!appInfo.webAppAlone) {
            // add module bindings in app
            final Assembler assembler = SystemInstance.get().getComponent(Assembler.class);
            beanContexts = assembler.initEjbs(classLoader, appInfo, appContext, injections, new ArrayList<BeanContext>(), webAppInfo.moduleId);
            appContext.getBeanContexts().addAll(beanContexts);
        } else {
            beanContexts = null;
        }
        final Map<String, Object> bindings = new HashMap<>();
        bindings.putAll(appContext.getBindings());
        bindings.putAll(new JndiEncBuilder(webAppInfo.jndiEnc, injections, webAppInfo.moduleId, "Bean", null, webAppInfo.uniqueId, classLoader, appInfo.properties).buildBindings(JndiEncBuilder.JndiScope.comp));
        final WebContext webContext = new WebContext(appContext);
        webContext.setBindings(bindings);
        webContext.getBindings().putAll(new JndiEncBuilder(webAppInfo.jndiEnc, injections, webAppInfo.moduleId, "Bean", null, webAppInfo.uniqueId, classLoader, appInfo.properties).buildBindings(JndiEncBuilder.JndiScope.comp));
        webContext.setJndiEnc(WebInitialContext.create(bindings, appContext.getGlobalJndiContext()));
        webContext.setClassLoader(classLoader);
        webContext.setId(webAppInfo.moduleId);
        webContext.setContextRoot(webAppInfo.contextRoot);
        webContext.setHost(webAppInfo.host);
        webContext.getInjections().addAll(injections);
        webContext.setInitialContext(new EmbeddedInitialContext(webContext.getJndiEnc(), webContext.getBindings()));
        final ServletContext component = SystemInstance.get().getComponent(ServletContext.class);
        final ServletContextEvent sce = component == null ? new MockServletContextEvent() : new ServletContextEvent(new LightServletContext(component, webContext.getClassLoader()));
        servletContextEvents.put(webAppInfo, sce);
        webContext.setServletContext(sce.getServletContext());
        SystemInstance.get().fireEvent(new EmbeddedServletContextCreated(sce.getServletContext()));
        appContext.getWebContexts().add(webContext);
        cs.addWebContext(webContext);
        if (!appInfo.webAppAlone && hasCdi(appInfo)) {
            final Assembler assembler = SystemInstance.get().getComponent(Assembler.class);
            new CdiBuilder().build(appInfo, appContext, beanContexts, webContext);
            assembler.startEjbs(true, beanContexts);
        }
        // listeners
        for (final ListenerInfo listener : webAppInfo.listeners) {
            final Class<?> clazz = webContext.getClassLoader().loadClass(listener.classname);
            final Object instance = webContext.newInstance(clazz);
            if (ServletContextListener.class.isInstance(instance)) {
                switchServletContextIfNeeded(sce.getServletContext(), new Runnable() {

                    @Override
                    public void run() {
                        ((ServletContextListener) instance).contextInitialized(sce);
                    }
                });
            }
            List<Object> list = listeners.get(webAppInfo);
            if (list == null) {
                list = new ArrayList<Object>();
                listeners.put(webAppInfo, list);
            }
            list.add(instance);
        }
        for (final ClassListInfo info : webAppInfo.webAnnotatedClasses) {
            final String url = info.name;
            for (final String filterPath : info.list) {
                final Class<?> clazz = loadFromUrls(webContext.getClassLoader(), url, filterPath);
                final WebListener annotation = clazz.getAnnotation(WebListener.class);
                if (annotation != null) {
                    final Object instance = webContext.newInstance(clazz);
                    if (ServletContextListener.class.isInstance(instance)) {
                        switchServletContextIfNeeded(sce.getServletContext(), new Runnable() {

                            @Override
                            public void run() {
                                ((ServletContextListener) instance).contextInitialized(sce);
                            }
                        });
                    }
                    List<Object> list = listeners.get(webAppInfo);
                    if (list == null) {
                        list = new ArrayList<Object>();
                        listeners.put(webAppInfo, list);
                    }
                    list.add(instance);
                }
            }
        }
        final DeployedWebObjects deployedWebObjects = new DeployedWebObjects();
        deployedWebObjects.webContext = webContext;
        servletDeploymentInfo.put(webAppInfo, deployedWebObjects);
        if (webContext.getWebBeansContext() != null && webContext.getWebBeansContext().getBeanManagerImpl().isInUse()) {
            final Thread thread = Thread.currentThread();
            final ClassLoader old = thread.getContextClassLoader();
            thread.setContextClassLoader(webContext.getClassLoader());
            try {
                OpenEJBLifecycle.class.cast(webContext.getWebBeansContext().getService(ContainerLifecycle.class)).startServletContext(sce.getServletContext());
            } finally {
                thread.setContextClassLoader(old);
            }
        }
        if (addServletMethod == null) {
            // can't manage filter/servlets
            continue;
        }
        // register filters
        for (final FilterInfo info : webAppInfo.filters) {
            switchServletContextIfNeeded(sce.getServletContext(), new Runnable() {

                @Override
                public void run() {
                    for (final String mapping : info.mappings) {
                        final FilterConfig config = new SimpleFilterConfig(sce.getServletContext(), info.name, info.initParams);
                        try {
                            addFilterMethod.invoke(null, info.classname, webContext, mapping, config);
                            deployedWebObjects.filterMappings.add(mapping);
                        } catch (final Exception e) {
                            LOGGER.warning(e.getMessage(), e);
                        }
                    }
                }
            });
        }
        for (final ClassListInfo info : webAppInfo.webAnnotatedClasses) {
            final String url = info.name;
            for (final String filterPath : info.list) {
                final Class<?> clazz = loadFromUrls(webContext.getClassLoader(), url, filterPath);
                final WebFilter annotation = clazz.getAnnotation(WebFilter.class);
                if (annotation != null) {
                    final Properties initParams = new Properties();
                    for (final WebInitParam param : annotation.initParams()) {
                        initParams.put(param.name(), param.value());
                    }
                    final FilterConfig config = new SimpleFilterConfig(sce.getServletContext(), info.name, initParams);
                    for (final String[] mappings : asList(annotation.urlPatterns(), annotation.value())) {
                        switchServletContextIfNeeded(sce.getServletContext(), new Runnable() {

                            @Override
                            public void run() {
                                for (final String mapping : mappings) {
                                    try {
                                        addFilterMethod.invoke(null, clazz.getName(), webContext, mapping, config);
                                        deployedWebObjects.filterMappings.add(mapping);
                                    } catch (final Exception e) {
                                        LOGGER.warning(e.getMessage(), e);
                                    }
                                }
                            }
                        });
                    }
                }
            }
        }
        final Map<String, PortInfo> ports = new TreeMap<String, PortInfo>();
        for (final PortInfo port : webAppInfo.portInfos) {
            ports.put(port.serviceLink, port);
        }
        // register servlets
        for (final ServletInfo info : webAppInfo.servlets) {
            if ("true".equalsIgnoreCase(appInfo.properties.getProperty("openejb.jaxrs.on", "true"))) {
                // skip jaxrs servlets
                boolean skip = false;
                for (final ParamValueInfo pvi : info.initParams) {
                    if ("javax.ws.rs.Application".equals(pvi.name) || Application.class.getName().equals(pvi.name)) {
                        skip = true;
                    }
                }
                if (skip) {
                    continue;
                }
                if (info.servletClass == null) {
                    try {
                        if (Application.class.isAssignableFrom(classLoader.loadClass(info.servletName))) {
                            continue;
                        }
                    } catch (final Exception e) {
                    // no-op
                    }
                }
            }
            // If POJO web services, it will be overriden with WsServlet
            if (ports.containsKey(info.servletName) || ports.containsKey(info.servletClass)) {
                continue;
            }
            // deploy
            for (final String mapping : info.mappings) {
                switchServletContextIfNeeded(sce.getServletContext(), new Runnable() {

                    @Override
                    public void run() {
                        try {
                            addServletMethod.invoke(null, info.servletClass, webContext, mapping);
                            deployedWebObjects.mappings.add(mapping);
                        } catch (final Exception e) {
                            LOGGER.warning(e.getMessage(), e);
                        }
                    }
                });
            }
        }
        for (final ClassListInfo info : webAppInfo.webAnnotatedClasses) {
            final String url = info.name;
            for (final String servletPath : info.list) {
                final Class<?> clazz = loadFromUrls(webContext.getClassLoader(), url, servletPath);
                final WebServlet annotation = clazz.getAnnotation(WebServlet.class);
                if (annotation != null) {
                    for (final String[] mappings : asList(annotation.urlPatterns(), annotation.value())) {
                        switchServletContextIfNeeded(sce.getServletContext(), new Runnable() {

                            @Override
                            public void run() {
                                for (final String mapping : mappings) {
                                    try {
                                        addServletMethod.invoke(null, clazz.getName(), webContext, mapping);
                                        deployedWebObjects.mappings.add(mapping);
                                    } catch (final Exception e) {
                                        LOGGER.warning(e.getMessage(), e);
                                    }
                                }
                            }
                        });
                    }
                }
            }
        }
        if (addDefaults != null && tryJsp()) {
            addDefaults.invoke(null, webContext);
            deployedWebObjects.mappings.add("*\\.jsp");
        }
    }
}
Also used : CoreContainerSystem(org.apache.openejb.core.CoreContainerSystem) ContainerSystem(org.apache.openejb.spi.ContainerSystem) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ServletInfo(org.apache.openejb.assembler.classic.ServletInfo) WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo) ParamValueInfo(org.apache.openejb.assembler.classic.ParamValueInfo) FilterConfig(javax.servlet.FilterConfig) HashSet(java.util.HashSet) InjectionBuilder(org.apache.openejb.assembler.classic.InjectionBuilder) CoreContainerSystem(org.apache.openejb.core.CoreContainerSystem) Injection(org.apache.openejb.Injection) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) BeanContext(org.apache.openejb.BeanContext) ListenerInfo(org.apache.openejb.assembler.classic.ListenerInfo) CdiBuilder(org.apache.openejb.cdi.CdiBuilder) Assembler(org.apache.openejb.assembler.classic.Assembler) OpenEJBLifecycle(org.apache.openejb.cdi.OpenEJBLifecycle) WebContext(org.apache.openejb.core.WebContext) MockServletContextEvent(org.apache.webbeans.web.lifecycle.test.MockServletContextEvent) Properties(java.util.Properties) ClassListInfo(org.apache.openejb.assembler.classic.ClassListInfo) PortInfo(org.apache.openejb.assembler.classic.PortInfo) WebServlet(javax.servlet.annotation.WebServlet) MockServletContext(org.apache.webbeans.web.lifecycle.test.MockServletContext) ServletContext(javax.servlet.ServletContext) FilterInfo(org.apache.openejb.assembler.classic.FilterInfo) WebFilter(javax.servlet.annotation.WebFilter) AppContext(org.apache.openejb.AppContext) TreeMap(java.util.TreeMap) NamingException(javax.naming.NamingException) NameNotFoundException(javax.naming.NameNotFoundException) MalformedURLException(java.net.MalformedURLException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) WebListener(javax.servlet.annotation.WebListener) JndiEncBuilder(org.apache.openejb.assembler.classic.JndiEncBuilder) WebInitParam(javax.servlet.annotation.WebInitParam) ServletContextEvent(javax.servlet.ServletContextEvent) MockServletContextEvent(org.apache.webbeans.web.lifecycle.test.MockServletContextEvent)

Example 24 with WebAppInfo

use of org.apache.openejb.assembler.classic.WebAppInfo in project tomee by apache.

the class LightweightWebAppBuilder method undeployWebApps.

@Override
public void undeployWebApps(final AppInfo appInfo) throws Exception {
    for (final WebAppInfo webAppInfo : appInfo.webApps) {
        final DeployedWebObjects context = servletDeploymentInfo.remove(webAppInfo);
        final ServletContextEvent sce = servletContextEvents.remove(webAppInfo);
        final List<Object> listenerInstances = listeners.remove(webAppInfo);
        if (addServletMethod != null) {
            switchServletContextIfNeeded(sce.getServletContext(), new Runnable() {

                @Override
                public void run() {
                    for (final String mapping : context.mappings) {
                        try {
                            removeServletMethod.invoke(null, mapping, context.webContext);
                        } catch (final Exception e) {
                        // no-op
                        }
                    }
                    for (final String mapping : context.filterMappings) {
                        try {
                            removeFilterMethod.invoke(null, mapping, context.webContext);
                        } catch (final Exception e) {
                        // no-op
                        }
                    }
                }
            });
        }
        if (listenerInstances != null) {
            for (final Object instance : listenerInstances) {
                if (ServletContextListener.class.isInstance(instance)) {
                    switchServletContextIfNeeded(sce.getServletContext(), new Runnable() {

                        @Override
                        public void run() {
                            ((ServletContextListener) instance).contextDestroyed(sce);
                        }
                    });
                }
            }
        }
    }
}
Also used : WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo) ServletContextEvent(javax.servlet.ServletContextEvent) MockServletContextEvent(org.apache.webbeans.web.lifecycle.test.MockServletContextEvent) NamingException(javax.naming.NamingException) NameNotFoundException(javax.naming.NameNotFoundException) MalformedURLException(java.net.MalformedURLException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException)

Example 25 with WebAppInfo

use of org.apache.openejb.assembler.classic.WebAppInfo in project tomee by apache.

the class ConfigurationFactoryTest method testConfigureApplicationWebModule.

@Test
public void testConfigureApplicationWebModule() throws OpenEJBException {
    SystemInstance.get().setProperty("openejb.environment.default", "false");
    final String moduleId = "testConfigureApplicationWebModule";
    final String fileSeparator = System.getProperty("file.separator");
    SystemInstance.get().setProperty(ConfigurationFactory.VALIDATION_SKIP_PROPERTY, "false");
    SystemInstance.get().setProperty(DeploymentsResolver.SEARCH_CLASSPATH_FOR_DEPLOYMENTS_PROPERTY, "false");
    final ConfigurationFactory factory = new ConfigurationFactory();
    final WebApp webApp = new WebApp();
    // no real classes engaged so disable metadata (annotation) processing
    webApp.setMetadataComplete(true);
    final WebModule webModule = new WebModule(webApp, null, null, fileSeparator + "some" + fileSeparator + "where.war", moduleId);
    final WebAppInfo info = factory.configureApplication(webModule);
    assertEquals(moduleId, info.moduleId);
    SystemInstance.get().getProperties().remove("openejb.environment.default");
}
Also used : WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo) WebApp(org.apache.openejb.jee.WebApp) Test(org.junit.Test)

Aggregations

WebAppInfo (org.apache.openejb.assembler.classic.WebAppInfo)27 AppInfo (org.apache.openejb.assembler.classic.AppInfo)9 BeanContext (org.apache.openejb.BeanContext)8 MalformedURLException (java.net.MalformedURLException)7 HashMap (java.util.HashMap)7 EjbJarInfo (org.apache.openejb.assembler.classic.EjbJarInfo)7 ServletInfo (org.apache.openejb.assembler.classic.ServletInfo)7 NamingException (javax.naming.NamingException)6 EnterpriseBeanInfo (org.apache.openejb.assembler.classic.EnterpriseBeanInfo)6 File (java.io.File)5 ArrayList (java.util.ArrayList)5 NameNotFoundException (javax.naming.NameNotFoundException)5 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)5 ClassListInfo (org.apache.openejb.assembler.classic.ClassListInfo)5 IOException (java.io.IOException)4 Map (java.util.Map)4 StandardContext (org.apache.catalina.core.StandardContext)4 AppContext (org.apache.openejb.AppContext)4 OpenEJBException (org.apache.openejb.OpenEJBException)4 URL (java.net.URL)3