List of usage examples for javax.servlet ServletContextAttributeListener ServletContextAttributeListener
ServletContextAttributeListener
From source file:org.brutusin.rpc.RpcWebInitializer.java
public void onStartup(final ServletContext ctx) throws ServletException { final RpcServlet rpcServlet = registerRpcServlet(ctx); final WebsocketFilter websocketFilter = new WebsocketFilter(); FilterRegistration.Dynamic dynamic = ctx.addFilter(WebsocketFilter.class.getName(), websocketFilter); dynamic.addMappingForUrlPatterns(null, false, RpcConfig.getInstance().getPath() + "/wskt"); JsonCodec.getInstance().registerStringFormat(MetaDataInputStream.class, "inputstream"); final Bean<RpcSpringContext> rpcCtxBean = new Bean<RpcSpringContext>(); ctx.addListener(new ServletRequestListener() { public void requestDestroyed(ServletRequestEvent sre) { GlobalThreadLocal.clear();//from ww w. jav a2 s . com } public void requestInitialized(ServletRequestEvent sre) { GlobalThreadLocal.set(new GlobalThreadLocal((HttpServletRequest) sre.getServletRequest(), null)); } }); ctx.addListener(new ServletContextListener() { public void contextInitialized(ServletContextEvent sce) { RpcSpringContext rpcCtx = createRpcSpringContext(ctx); rpcCtxBean.setValue(rpcCtx); rpcServlet.setRpcCtx(rpcCtx); initWebsocketRpcRuntime(ctx, rpcCtx); ctx.setAttribute(RpcSpringContext.class.getName(), rpcCtx); } public void contextDestroyed(ServletContextEvent sce) { LOGGER.info("Destroying RPC context"); if (rpcCtxBean.getValue() != null) { rpcCtxBean.getValue().destroy(); } } }); ctx.addListener(new ServletContextAttributeListener() { public void attributeAdded(ServletContextAttributeEvent event) { if (event.getName().equals(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)) { updateRootContext(); } } public void attributeRemoved(ServletContextAttributeEvent event) { if (event.getName().equals(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)) { updateRootContext(); } } public void attributeReplaced(ServletContextAttributeEvent event) { if (event.getName().equals(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)) { updateRootContext(); } } private void updateRootContext() { WebApplicationContext rootCtx = WebApplicationContextUtils.getWebApplicationContext(ctx); if (rootCtx != null) { if (rpcCtxBean.getValue() != null) { rpcCtxBean.getValue().setParent(rootCtx); } if (rootCtx.containsBean("springSecurityFilterChain")) { LOGGER.info("Moving WebsocketFilter behind springSecurityFilterChain"); websocketFilter.disable(); FilterChainProxy fcp = (FilterChainProxy) rootCtx.getBean("springSecurityFilterChain"); fcp.getFilterChains().get(0).getFilters().add(new WebsocketFilter()); } } } }); }