Search in sources :

Example 1 with ServletListener

use of org.apache.openejb.server.httpd.ServletListener in project tomee by apache.

the class HttpUtil method addServlet.

public static boolean addServlet(final String classname, final WebContext wc, final String mapping) {
    final HttpListenerRegistry registry = SystemInstance.get().getComponent(HttpListenerRegistry.class);
    if (registry == null || mapping == null) {
        return false;
    }
    final ServletListener listener;
    try {
        ServletContext servletContext = wc.getServletContext();
        if (servletContext == null) {
            servletContext = SystemInstance.get().getComponent(ServletContext.class);
        }
        if ("javax.faces.webapp.FacesServlet".equals(classname)) {
            try {
                // faking it to let the FacesServlet starting
                // NOTE: needs myfaces-impl + tomcat-jasper (JspFactory)
                // TODO: handle the whole lifecycle (cleanup mainly) + use myfaces SPI to make scanning really faster (take care should work in tomee were we already have it impl)
                final Class<?> mfListenerClass = wc.getClassLoader().loadClass("org.apache.myfaces.webapp.StartupServletContextListener");
                final ServletContextListener servletContextListener = ServletContextListener.class.cast(mfListenerClass.newInstance());
                servletContext.setAttribute("javax.enterprise.inject.spi.BeanManager", new InjectableBeanManager(wc.getWebBeansContext().getBeanManagerImpl()));
                final Thread thread = Thread.currentThread();
                final ClassLoader old = setClassLoader(wc, thread);
                try {
                    servletContextListener.contextInitialized(new ServletContextEvent(servletContext));
                } finally {
                    thread.setContextClassLoader(old);
                }
                servletContext.removeAttribute("javax.enterprise.inject.spi.BeanManager");
            } catch (final Exception e) {
            // no-op
            }
        }
        final Thread thread = Thread.currentThread();
        final ClassLoader old = setClassLoader(wc, thread);
        try {
            listener = new ServletListener((Servlet) wc.newInstance(wc.getClassLoader().loadClass(classname)), wc.getContextRoot());
            final ServletContext sc = servletContext;
            listener.getDelegate().init(new ServletConfig() {

                @Override
                public String getServletName() {
                    return classname;
                }

                @Override
                public ServletContext getServletContext() {
                    return sc;
                }

                @Override
                public String getInitParameter(final String s) {
                    return sc.getInitParameter(s);
                }

                @Override
                public Enumeration<String> getInitParameterNames() {
                    final Enumeration<String> parameterNames = sc.getInitParameterNames();
                    return parameterNames == null ? Collections.<String>emptyEnumeration() : parameterNames;
                }
            });
        } finally {
            thread.setContextClassLoader(old);
        }
    } catch (final Exception e) {
        throw new OpenEJBRuntimeException(e);
    }
    registry.addHttpListener(listener, pattern(wc.getContextRoot(), "/".equals(mapping) ? "/*" : mapping));
    return true;
}
Also used : Enumeration(java.util.Enumeration) ServletContextListener(javax.servlet.ServletContextListener) InjectableBeanManager(org.apache.webbeans.container.InjectableBeanManager) ServletConfig(javax.servlet.ServletConfig) HttpListenerRegistry(org.apache.openejb.server.httpd.HttpListenerRegistry) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) ServletListener(org.apache.openejb.server.httpd.ServletListener) ServletContext(javax.servlet.ServletContext) Servlet(javax.servlet.Servlet) ServletContextEvent(javax.servlet.ServletContextEvent)

Example 2 with ServletListener

use of org.apache.openejb.server.httpd.ServletListener in project tomee by apache.

the class HttpUtil method removeServlet.

public static void removeServlet(final String mapping, final WebContext wc) {
    final HttpListenerRegistry registry = SystemInstance.get().getComponent(HttpListenerRegistry.class);
    if (registry == null || mapping == null) {
        return;
    }
    final Servlet servlet = ((ServletListener) registry.removeHttpListener(pattern(wc.getContextRoot(), mapping))).getDelegate();
    servlet.destroy();
    wc.destroy(servlet);
    if (servlet.getClass().equals("org.apache.jasper.servlet.JspServlet")) {
        SystemInstance.get().getComponent(ServletContext.class).removeAttribute("org.apache.tomcat.InstanceManager");
    }
}
Also used : ServletListener(org.apache.openejb.server.httpd.ServletListener) Servlet(javax.servlet.Servlet) ServletContext(javax.servlet.ServletContext) HttpListenerRegistry(org.apache.openejb.server.httpd.HttpListenerRegistry)

Aggregations

Servlet (javax.servlet.Servlet)2 ServletContext (javax.servlet.ServletContext)2 HttpListenerRegistry (org.apache.openejb.server.httpd.HttpListenerRegistry)2 ServletListener (org.apache.openejb.server.httpd.ServletListener)2 Enumeration (java.util.Enumeration)1 ServletConfig (javax.servlet.ServletConfig)1 ServletContextEvent (javax.servlet.ServletContextEvent)1 ServletContextListener (javax.servlet.ServletContextListener)1 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)1 InjectableBeanManager (org.apache.webbeans.container.InjectableBeanManager)1