@Override public void contextInitialized(ServletContextEvent sce) { ServerWebSocketContainer container = (ServerWebSocketContainer) sce.getServletContext().getAttribute(ServerContainer.class.getName()); FilterRegistration.Dynamic filter = sce.getServletContext().addFilter(FILTER_NAME, JsrWebSocketFilter.class); filter.setAsyncSupported(true); if (!container.getConfiguredServerEndpoints().isEmpty()) { filter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*"); } else { container.setContextToAddFilter((ServletContextImpl) sce.getServletContext()); } }
@Override public void handleDeployment(DeploymentInfo deploymentInfo, ServletContext servletContext) { WebSocketDeploymentInfo info = (WebSocketDeploymentInfo) deploymentInfo .getServletContextAttributes() .get(WebSocketDeploymentInfo.ATTRIBUTE_NAME); if (info == null) { return; } if (info.getWorker() == null) { JsrWebSocketLogger.ROOT_LOGGER.xnioWorkerWasNull(); } final List<ThreadSetupAction> setup = new ArrayList<ThreadSetupAction>(); setup.add(new ContextClassLoaderSetupAction(deploymentInfo.getClassLoader())); setup.addAll(deploymentInfo.getThreadSetupActions()); final CompositeThreadSetupAction threadSetupAction = new CompositeThreadSetupAction(setup); ServerWebSocketContainer container = new ServerWebSocketContainer( deploymentInfo.getClassIntrospecter(), servletContext.getClassLoader(), info.getWorker(), info.getBuffers(), threadSetupAction, info.isDispatchToWorkerThread()); try { for (Class<?> annotation : info.getAnnotatedEndpoints()) { container.addEndpoint(annotation); } for (ServerEndpointConfig programatic : info.getProgramaticEndpoints()) { container.addEndpoint(programatic); } } catch (DeploymentException e) { throw new RuntimeException(e); } servletContext.setAttribute(ServerContainer.class.getName(), container); info.containerReady(container); SecurityActions.addContainer(deploymentInfo.getClassLoader(), container); deploymentInfo.addListener(Servlets.listener(WebSocketListener.class)); }
@Override public void contextDestroyed(ServletContextEvent sce) { SecurityActions.removeContainer(sce.getServletContext().getClassLoader()); container.close(); }