Dear all,

It appears that if I obtain the ServletContext object via Spring's
WebApplicationInitializer
mechanism<http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/WebApplicationInitializer.html>,
it is not yet seeded with the WebSocket ServerContainer object.
Alternatively, if I use straight up ServletContextListener, all's good.

More specifically, this works:

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class ServletBootstrapper implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext sc = sce.getServletContext();
        ServerContainer serverContainer = (ServerContainer)
sc.getAttribute("javax.websocket.server.ServerContainer");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        // NO-OP
    }
}

But this doesn't:

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.springframework.web.WebApplicationInitializer;

public class SpringBootstrapper implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext sc) throws ServletException {
        // Next line returns null
        ServerContainer serverContainer = (ServerContainer)
sc.getAttribute("javax.websocket.server.ServerContainer");
    }
}

I verified that the ServletContext object returned in both is the same, so
that part is good.

I imagine the root cause is that Spring uses an earlier container event to
post implementations of the WebApplicationInitializer interface, before the
ServerContainer attribute is set by the WebSocket code.  Given the ubiquity
of Spring, this may prove problematic.

Many thanks,
-Igor.

Reply via email to