Search in sources :

Example 1 with HttpListenerRegistry

use of org.apache.openejb.server.httpd.HttpListenerRegistry 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 HttpListenerRegistry

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

the class HttpUtil method removeFilter.

public static void removeFilter(final String mapping, final WebContext wc) {
    final HttpListenerRegistry registry = SystemInstance.get().getComponent(HttpListenerRegistry.class);
    if (registry == null || mapping == null) {
        return;
    }
    final Collection<HttpListener> filters = registry.removeHttpFilter(pattern(wc.getContextRoot(), mapping));
    for (HttpListener listener : filters) {
        final Filter filter = ((FilterListener) listener).getDelegate();
        filter.destroy();
        wc.destroy(filter);
    }
    filters.clear();
}
Also used : FilterListener(org.apache.openejb.server.httpd.FilterListener) Filter(javax.servlet.Filter) HttpListener(org.apache.openejb.server.httpd.HttpListener) HttpListenerRegistry(org.apache.openejb.server.httpd.HttpListenerRegistry)

Example 3 with HttpListenerRegistry

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

the class HttpUtil method addFilter.

public static boolean addFilter(final String classname, final WebContext wc, final String mapping, final FilterConfig config) {
    final HttpListenerRegistry registry = SystemInstance.get().getComponent(HttpListenerRegistry.class);
    if (registry == null || mapping == null) {
        return false;
    }
    final FilterListener listener;
    try {
        listener = new FilterListener((Filter) wc.newInstance(wc.getClassLoader().loadClass(classname)), wc.getContextRoot());
        listener.getDelegate().init(config);
    } catch (Exception e) {
        throw new OpenEJBRuntimeException(e);
    }
    registry.addHttpFilter(listener, pattern(wc.getContextRoot(), mapping));
    return true;
}
Also used : OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) FilterListener(org.apache.openejb.server.httpd.FilterListener) Filter(javax.servlet.Filter) HttpListenerRegistry(org.apache.openejb.server.httpd.HttpListenerRegistry) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException)

Example 4 with HttpListenerRegistry

use of org.apache.openejb.server.httpd.HttpListenerRegistry 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

HttpListenerRegistry (org.apache.openejb.server.httpd.HttpListenerRegistry)4 Filter (javax.servlet.Filter)2 Servlet (javax.servlet.Servlet)2 ServletContext (javax.servlet.ServletContext)2 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)2 FilterListener (org.apache.openejb.server.httpd.FilterListener)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 HttpListener (org.apache.openejb.server.httpd.HttpListener)1 InjectableBeanManager (org.apache.webbeans.container.InjectableBeanManager)1