2013/9/13 Igor Urisman <igor.uris...@gmail.com>: > 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. >
I see no issue here, as both WebSocket implementation and Spring's WebApplicationInitializer rely on use of "javax.servlet.ServletContainerInitializer" to initialize themselves. Reading the Servlet specification 3.1, I see no words in the specification on the ordering of ServletContainerInitializer instances. (It would be reasonable that they were covered by ch. "8.2.2 Ordering of web.xml and web-fragment.xml", but I see no explicit wording there). If 8.2.2 indeed implies the ordering of those, then you can specify absolute ordering in your web.xml or relative ordering in your web-fragment.xml. If it does not work with Tomcat then file an issue. If specification does not cover the ordering, or you do not want to configure it, then you have to fall back to using a ServletContextListener. (You should be able to use WebApplicationInitializer to install an instance of ServletContextListener for you). Best regards, Konstantin Kolinko --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org