Search in sources :

Example 1 with OSGiDispatcherServlet

use of org.motechproject.osgi.web.http.OSGiDispatcherServlet in project motech by motech.

the class HttpServiceTracker method register.

private void register(HttpService httpService) {
    if (contextPath == null && httpService != null) {
        DispatcherServlet dispatcherServlet = new OSGiDispatcherServlet(context, (ConfigurableWebApplicationContext) bundleContextWrapper.getBundleApplicationContext());
        contextPath = WebBundleUtil.getContextPath(context.getBundle());
        dispatcherServlet.setContextClass(MotechOSGiWebApplicationContext.class);
        dispatcherServlet.setContextConfigLocation(WebBundleUtil.getContextLocation(context.getBundle()));
        ClassLoader old = Thread.currentThread().getContextClassLoader();
        try {
            Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
            HttpContext httpContext = HttpContextFactory.getHttpContext(httpService.createDefaultHttpContext(), context.getBundle());
            httpService.unregister(contextPath);
            httpService.registerServlet(contextPath, dispatcherServlet, null, httpContext);
            if (resourceMapping != null) {
                for (String key : resourceMapping.keySet()) {
                    LOGGER.debug(String.format("Registering %s = %s for bundle %s ", key, resourceMapping.keySet(), bundleContextWrapper.getCurrentBundleSymbolicName()));
                    httpService.registerResources(key, resourceMapping.get(key), httpContext);
                }
            }
            LOGGER.info(String.format("servlet registered with context path %s for bundle %s", contextPath, OsgiStringUtils.nullSafeSymbolicName(context.getBundle())));
        } catch (ServletException e) {
            LOGGER.error("Unable to register dispatcher servlet for {}", bundleContextWrapper.getCurrentBundleSymbolicName(), e);
        } catch (NamespaceException e) {
            LOGGER.error("Unable to register dispatcher servlet for {}, namespace already taken", bundleContextWrapper.getCurrentBundleSymbolicName(), e);
        } finally {
            Thread.currentThread().setContextClassLoader(old);
        }
    }
}
Also used : ServletException(javax.servlet.ServletException) OSGiDispatcherServlet(org.motechproject.osgi.web.http.OSGiDispatcherServlet) OSGiDispatcherServlet(org.motechproject.osgi.web.http.OSGiDispatcherServlet) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) HttpContext(org.osgi.service.http.HttpContext) NamespaceException(org.osgi.service.http.NamespaceException)

Aggregations

ServletException (javax.servlet.ServletException)1 OSGiDispatcherServlet (org.motechproject.osgi.web.http.OSGiDispatcherServlet)1 HttpContext (org.osgi.service.http.HttpContext)1 NamespaceException (org.osgi.service.http.NamespaceException)1 DispatcherServlet (org.springframework.web.servlet.DispatcherServlet)1