javax.servlet.ServletContextEvent Java Examples

The following examples show how to use javax.servlet.ServletContextEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TesterEchoServer.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent sce) {
    super.contextInitialized(sce);
    ServerContainer sc =
            (ServerContainer) sce.getServletContext().getAttribute(
                    Constants.SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE);
    try {
        sc.addEndpoint(Async.class);
        sc.addEndpoint(Basic.class);
        sc.addEndpoint(BasicLimitLow.class);
        sc.addEndpoint(BasicLimitHigh.class);
        sc.addEndpoint(WriterError.class);
        sc.addEndpoint(RootEcho.class);
    } catch (DeploymentException e) {
        throw new IllegalStateException(e);
    }
}
 
Example #2
Source File: Spr8510Tests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * If setConfigLocation has not been called explicitly against the application context,
 * then fall back to the ContextLoaderListener init-param if present.
 */
@Test
public void abstractRefreshableWAC_fallsBackToInitParam() {
	XmlWebApplicationContext ctx = new XmlWebApplicationContext();
	//ctx.setConfigLocation("programmatic.xml"); // nothing set programmatically
	ContextLoaderListener cll = new ContextLoaderListener(ctx);

	MockServletContext sc = new MockServletContext();
	sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "from-init-param.xml");

	try {
		cll.contextInitialized(new ServletContextEvent(sc));
		fail("expected exception");
	}
	catch (Throwable t) {
		// assert that an attempt was made to load the correct XML
		assertTrue(t.getMessage().endsWith(
				"Could not open ServletContext resource [/from-init-param.xml]"));
	}
}
 
Example #3
Source File: BirtMessagesPropertiesContextListener.java    From openemm with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
   public void contextInitialized(ServletContextEvent event) {
       try {
       	// Birt must generate messages in here, because it has no struts like EMM
       	// Messages are then available via I18nString
       	DBMessagesResource dbMessagesResource = new DBMessagesResource();
   		dbMessagesResource.init();
   		
   		ServletContext servletContext = event.getServletContext();
   		if (ConfigService.getInstance().getBooleanValue(ConfigValue.IsLiveInstance)) {
   			createMessagesPropertiesFiles(servletContext);
   		}
	} catch (Exception e) {
		logger.error("MessagesPropertiesGeneratorContextListener init: " + e.getMessage(), e);
	}
}
 
Example #4
Source File: ContextLoaderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testContextLoaderWithInvalidContext() throws Exception {
	MockServletContext sc = new MockServletContext("");
	sc.addInitParameter(ContextLoader.CONTEXT_CLASS_PARAM,
			"org.springframework.web.context.support.InvalidWebApplicationContext");
	ServletContextListener listener = new ContextLoaderListener();
	ServletContextEvent event = new ServletContextEvent(sc);
	try {
		listener.contextInitialized(event);
		fail("Should have thrown ApplicationContextException");
	}
	catch (ApplicationContextException ex) {
		// expected
		assertTrue(ex.getCause() instanceof ClassNotFoundException);
	}
}
 
Example #5
Source File: ContextLoaderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testContextLoaderListenerWithDefaultContext() {
	MockServletContext sc = new MockServletContext("");
	sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
			"/org/springframework/web/context/WEB-INF/applicationContext.xml " +
			"/org/springframework/web/context/WEB-INF/context-addition.xml");
	ServletContextListener listener = new ContextLoaderListener();
	ServletContextEvent event = new ServletContextEvent(sc);
	listener.contextInitialized(event);
	String contextAttr = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
	WebApplicationContext context = (WebApplicationContext) sc.getAttribute(contextAttr);
	assertTrue("Correct WebApplicationContext exposed in ServletContext", context instanceof XmlWebApplicationContext);
	assertTrue(WebApplicationContextUtils.getRequiredWebApplicationContext(sc) instanceof XmlWebApplicationContext);
	LifecycleBean lb = (LifecycleBean) context.getBean("lifecycle");
	assertTrue("Has father", context.containsBean("father"));
	assertTrue("Has rod", context.containsBean("rod"));
	assertTrue("Has kerry", context.containsBean("kerry"));
	assertTrue("Not destroyed", !lb.isDestroyed());
	assertFalse(context.containsBean("beans1.bean1"));
	assertFalse(context.containsBean("beans1.bean2"));
	listener.contextDestroyed(event);
	assertTrue("Destroyed", lb.isDestroyed());
	assertNull(sc.getAttribute(contextAttr));
	assertNull(WebApplicationContextUtils.getWebApplicationContext(sc));
}
 
Example #6
Source File: ExtensionSystemInitializationContextListener.java    From openemm with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent event) {
	ServletContext servletContext = event.getServletContext();
	WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
	configService = webApplicationContext.getBean("ConfigService", ConfigService.class);
	
	
	if (servletContext.getAttribute(ExtensionConstants.EXTENSION_SYSTEM_APPLICATION_SCOPE_ATTRIBUTE) != null) {
		logger.fatal ("Extension system already initialized for the application context");
	} else {
		try {
			ExtensionSystemImpl extensionSystem = createExtensionSystem( servletContext);
			servletContext.setAttribute(ExtensionConstants.EXTENSION_SYSTEM_APPLICATION_SCOPE_ATTRIBUTE, extensionSystem);
			
			extensionSystem.startUp();
		} catch (Exception e) {
			logger.fatal("Error initializing extension system", e);				
		}
	}
}
 
Example #7
Source File: Spr8510Tests.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * If a contextConfigLocation init-param has been specified for the ContextLoaderListener,
 * then it should take precedence. This is generally not a recommended practice, but
 * when it does happen, the init-param should be considered more specific than the
 * programmatic configuration, given that it still quite possibly externalized in
 * hybrid web.xml + WebApplicationInitializer cases.
 */
@Test
public void abstractRefreshableWAC_respectsInitParam_overProgrammaticConfigLocations() {
	XmlWebApplicationContext ctx = new XmlWebApplicationContext();
	ctx.setConfigLocation("programmatic.xml");
	ContextLoaderListener cll = new ContextLoaderListener(ctx);

	MockServletContext sc = new MockServletContext();
	sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "from-init-param.xml");

	try {
		cll.contextInitialized(new ServletContextEvent(sc));
		fail("expected exception");
	}
	catch (Throwable t) {
		// assert that an attempt was made to load the correct XML
		assertTrue(t.getMessage(), t.getMessage().endsWith(
				"Could not open ServletContext resource [/from-init-param.xml]"));
	}
}
 
Example #8
Source File: ContextLoaderListener.java    From myspring with MIT License 6 votes vote down vote up
/**
     * context初始化.
     */
    @Override
    public void contextInitialized(ServletContextEvent event) {
    	System.out.println("contextInitialized");
    	
    	ServletContext servletContext = event.getServletContext();
    	
    	if (null == this.context) {
    		
    		//配置的application文件路径
    		String cfgFile = servletContext.getInitParameter("contextConfigLocation");
    		
			this.context = new WebApplicationContext(cfgFile);
			
			//存入上下文,后面servlet会取用
			servletContext.setAttribute(ROOT_CXT_ATTR, this.context);
		}
    	
    	//源码中是在这里初始化context,调用refresh方法的,我们在上面new的时候先做了
//        initWebApplicationContext(this.context);
    }
 
Example #9
Source File: StartUpExecutionListener.java    From openemm with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public final void contextInitialized(final ServletContextEvent servletContextEvent) {
	try {
		final WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContextEvent.getServletContext());
		final StartupJobExecutionService executionService = webApplicationContext.getBean("StartupJobExecutionService", StartupJobExecutionService.class);
		
		if(LOGGER.isInfoEnabled()) {
			LOGGER.info("Starting execution of startup jobs");
		}
		
		final ExecutionResult result = executionService.executeAllPendingJobs();
		
		logResult(result);
	} catch (final Exception e) {
		LOGGER.error("Error processing startup jobs", e);
	}
}
 
Example #10
Source File: TesterTldListener.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent sce) {

    ServletContext sc = sce.getServletContext();
    servletContext = sc;

    // Try and use one of the Servlet 3.0 methods that should be blocked
    try {
        sc.getEffectiveMajorVersion();
        log.append("FAIL-01");
    } catch (UnsupportedOperationException uoe) {
        log.append("PASS-01");
    } catch (Exception e) {
        log.append("FAIL-02");
    }
}
 
Example #11
Source File: DeploymentManagerImpl.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
@Override
public void undeploy() {
    try {
        deployment.createThreadSetupAction(new ThreadSetupHandler.Action<Void, Object>() {
            @Override
            public Void call(HttpServerExchange exchange, Object ignore) throws ServletException {
                for(ServletContextListener listener : deployment.getDeploymentInfo().getDeploymentCompleteListeners()) {
                    try {
                        listener.contextDestroyed(new ServletContextEvent(deployment.getServletContext()));
                    } catch (Throwable t) {
                        UndertowServletLogger.REQUEST_LOGGER.failedToDestroy(listener, t);
                    }
                }
                deployment.destroy();
                deployment = null;
                state = State.UNDEPLOYED;
                return null;
            }
        }).call(null, null);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}
 
Example #12
Source File: ContextLoaderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testContextLoaderWithCustomContext() throws Exception {
	MockServletContext sc = new MockServletContext("");
	sc.addInitParameter(ContextLoader.CONTEXT_CLASS_PARAM,
			"org.springframework.web.servlet.SimpleWebApplicationContext");
	ServletContextListener listener = new ContextLoaderListener();
	ServletContextEvent event = new ServletContextEvent(sc);
	listener.contextInitialized(event);
	String contextAttr = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
	WebApplicationContext wc = (WebApplicationContext) sc.getAttribute(contextAttr);
	assertTrue("Correct WebApplicationContext exposed in ServletContext",
			wc instanceof SimpleWebApplicationContext);
}
 
Example #13
Source File: ServerShutdownListener.java    From singleton with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Behaviors after servlet context is initialized.
 *
 * @param servletContextEvent
 */
/*
@Override
public void contextInitialized(ServletContextEvent sce) {
	LOGGER.info("ServletContex initialized!");
	LOGGER.info("Server info : {}", sce.getServletContext().getServerInfo());
	try {
		long diskStoreSize = TranslationCache3
				.getDiskStoreSize(CacheName.SOURCE);
		if (diskStoreSize == 0) {
			LOGGER.info("No data stored on Disk!");
		} else {
			LOGGER.info("Disk cache size: {}", diskStoreSize);
			List<String> ehcachekeyList = TranslationCacheDES
					.getKeys(CacheName.SOURCE);
			LOGGER.info("restore cache key list: {}", ehcachekeyList);
			LOGGER.info("restore cache content: ");
			for (String ehcachekey : ehcachekeyList) {
				ComponentSourceDTO cachedComponentSourceDTO = (ComponentSourceDTO) TranslationCacheDES
						.getCachedObject(CacheName.SOURCE, ehcachekey);
				if (!StringUtils.isEmpty(cachedComponentSourceDTO)) {
					LOGGER.info(cachedComponentSourceDTO.toJSONString());
				}else {
					LOGGER.info("ehcachkey :{} no content", ehcachekey);
				}
			}
		}
	} catch (VIPCacheException e) {
		LOGGER.error("Error occurs when perform method contextInitialized.", e);
	}
}
 */
@Override
public void contextInitialized(ServletContextEvent sce) {
	// TODO Auto-generated method stub
	LOGGER.info("ServletContex initialized!");
	LOGGER.info("Server info : {}", sce.getServletContext().getServerInfo());
}
 
Example #14
Source File: ContextLoaderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testContextLoaderListenerWithGlobalContextInitializers() {
	MockServletContext sc = new MockServletContext("");
	sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
			"org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml");
	sc.addInitParameter(ContextLoader.GLOBAL_INITIALIZER_CLASSES_PARAM, StringUtils.arrayToCommaDelimitedString(
			new Object[] {TestContextInitializer.class.getName(), TestWebContextInitializer.class.getName()}));
	ContextLoaderListener listener = new ContextLoaderListener();
	listener.contextInitialized(new ServletContextEvent(sc));
	WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
	TestBean testBean = wac.getBean(TestBean.class);
	assertThat(testBean.getName(), equalTo("testName"));
	assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue());
}
 
Example #15
Source File: ContextLoaderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testContextLoaderListenerWithProgrammaticInitializers() {
	MockServletContext sc = new MockServletContext("");
	sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
			"org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml");
	ContextLoaderListener listener = new ContextLoaderListener();
	listener.setContextInitializers(new TestContextInitializer(), new TestWebContextInitializer());
	listener.contextInitialized(new ServletContextEvent(sc));
	WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
	TestBean testBean = wac.getBean(TestBean.class);
	assertThat(testBean.getName(), equalTo("testName"));
	assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue());
}
 
Example #16
Source File: ContextLoaderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testContextLoaderWithCustomContext() throws Exception {
	MockServletContext sc = new MockServletContext("");
	sc.addInitParameter(ContextLoader.CONTEXT_CLASS_PARAM,
			"org.springframework.web.servlet.SimpleWebApplicationContext");
	ServletContextListener listener = new ContextLoaderListener();
	ServletContextEvent event = new ServletContextEvent(sc);
	listener.contextInitialized(event);
	String contextAttr = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
	WebApplicationContext wc = (WebApplicationContext) sc.getAttribute(contextAttr);
	assertTrue("Correct WebApplicationContext exposed in ServletContext",
			wc instanceof SimpleWebApplicationContext);
}
 
Example #17
Source File: Spr8510Tests.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Ensure that ContextLoaderListener and GenericWebApplicationContext interact nicely.
 */
@Test
public void genericWAC() {
	GenericWebApplicationContext ctx = new GenericWebApplicationContext();
	ContextLoaderListener cll = new ContextLoaderListener(ctx);

	ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(ctx);
	scanner.scan("bogus.pkg");

	cll.contextInitialized(new ServletContextEvent(new MockServletContext()));
}
 
Example #18
Source File: UDPServer.java    From spring-boot-study with MIT License 5 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent sce) {
    try {
        logger.info("========启动一个线程,监听UDP数据报.PORT:" + UDP_PORT + "=========");
        // 启动一个线程,监听UDP数据报
        new Thread(new UDPProcess(UDP_PORT)).start();
    } catch (Exception e) {
        e.printStackTrace();
    }

}
 
Example #19
Source File: ContextLoaderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testContextLoaderListenerWithProgrammaticAndLocalInitializers() {
	MockServletContext sc = new MockServletContext("");
	sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
			"org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml");
	sc.addInitParameter(ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, TestContextInitializer.class.getName());
	ContextLoaderListener listener = new ContextLoaderListener();
	listener.setContextInitializers(new TestWebContextInitializer());
	listener.contextInitialized(new ServletContextEvent(sc));
	WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
	TestBean testBean = wac.getBean(TestBean.class);
	assertThat(testBean.getName(), equalTo("testName"));
	assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue());
}
 
Example #20
Source File: WebApplicationContextScopeTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testApplicationScope() {
	WebApplicationContext ac = initApplicationContext(WebApplicationContext.SCOPE_APPLICATION);
	assertNull(ac.getServletContext().getAttribute(NAME));
	DerivedTestBean bean = ac.getBean(NAME, DerivedTestBean.class);
	assertSame(bean, ac.getServletContext().getAttribute(NAME));
	assertSame(bean, ac.getBean(NAME));
	new ContextCleanupListener().contextDestroyed(new ServletContextEvent(ac.getServletContext()));
	assertTrue(bean.wasDestroyed());
}
 
Example #21
Source File: ContextLoaderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testContextLoaderListenerWithProgrammaticAndGlobalInitializers() {
	MockServletContext sc = new MockServletContext("");
	sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
			"org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml");
	sc.addInitParameter(ContextLoader.GLOBAL_INITIALIZER_CLASSES_PARAM, TestWebContextInitializer.class.getName());
	ContextLoaderListener listener = new ContextLoaderListener();
	listener.setContextInitializers(new TestContextInitializer());
	listener.contextInitialized(new ServletContextEvent(sc));
	WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
	TestBean testBean = wac.getBean(TestBean.class);
	assertThat(testBean.getName(), equalTo("testName"));
	assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue());
}
 
Example #22
Source File: FHIRServletContextListener.java    From FHIR with Apache License 2.0 5 votes vote down vote up
@Override
public void contextDestroyed(ServletContextEvent event) {
    if (log.isLoggable(Level.FINER)) {
        log.entering(FHIRServletContextListener.class.getName(), "contextDestroyed");
    }
    try {
        // Set our "initComplete" flag back to false.
        event.getServletContext().setAttribute(FHIR_SERVER_INIT_COMPLETE, Boolean.FALSE);

        // If we previously initialized the Kafka publisher, then shut it down now.
        if (kafkaPublisher != null) {
            kafkaPublisher.shutdown();
            kafkaPublisher = null;
        }

        // If we previously initialized the NATS publisher, then shut it down now.
        if (natsPublisher != null) {
            natsPublisher.shutdown();
            natsPublisher = null;
        }
    } catch (Exception e) {
    } finally {
        if (log.isLoggable(Level.FINER)) {
            log.exiting(FHIRServletContextListener.class.getName(), "contextDestroyed");
        }
    }
}
 
Example #23
Source File: I18NContextListenerForWebservice.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
   public void contextInitialized(ServletContextEvent event) {
       try {
       	if (I18nString.MESSAGE_RESOURCES == null) {
   			DBMessagesResource dbMessagesResource = new DBMessagesResource();
   			dbMessagesResource.init();
   		}
	} catch (Exception e) {
		logger.error("I18NContextListenerForWebservice init: " + e.getMessage(), e);
	}
}
 
Example #24
Source File: ExtensionSystemInitializationContextListener.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void contextDestroyed(ServletContextEvent event) {
	ServletContext servletContext = event.getServletContext();
	ExtensionSystemImpl extensionSystem = (ExtensionSystemImpl) servletContext.getAttribute(ExtensionConstants.EXTENSION_SYSTEM_APPLICATION_SCOPE_ATTRIBUTE);
	
	if (extensionSystem != null) {
		logger.info( "Shutting down ExtensionSystem");
		extensionSystem.shutdown();
		logger.info( "ExtensionSystem is shut down");
	}
}
 
Example #25
Source File: BackendDatasourceInitializationContextListener.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent event) {
	try {
		ServletContext servletContext = event.getServletContext();
		ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		DBase.DATASOURCE = (DataSource) applicationContext.getBean("dataSource");
		if (DBase.DATASOURCE == null) {
			logger.error("Datasource in DBase for Backend was empty. Backend will try to create its own datasource from emm.properties data");
		}
	} catch (Exception e) {
		logger.error("Cannot set Datasource in DBase for Backend. Backend will try to create its own datasource from emm.properties data", e);
	}
}
 
Example #26
Source File: ContextLoaderInitializerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void register() throws ServletException {
	initializer.onStartup(servletContext);

	assertTrue(eventListener instanceof ContextLoaderListener);
	ContextLoaderListener cll = (ContextLoaderListener) eventListener;
	cll.contextInitialized(new ServletContextEvent(servletContext));

	WebApplicationContext applicationContext = WebApplicationContextUtils
			.getRequiredWebApplicationContext(servletContext);

	assertTrue(applicationContext.containsBean(BEAN_NAME));
	assertTrue(applicationContext.getBean(BEAN_NAME) instanceof MyBean);
}
 
Example #27
Source File: MetricsInitializer.java    From metrics with Apache License 2.0 5 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent sce) {
  // Create a reporter and add it to the registry.
  // For demo purposes we use SLF4JReporter which should not be used for production systems
  SLF4JReporter reporter = SLF4JReporter.builder().withName("example").withStepSize(10).build();
  MyApp.metricRegistry.addReporter(reporter);
  sce.getServletContext().setAttribute(REPORTER_ATTRIBUTE, reporter);
}
 
Example #28
Source File: Bootstrap.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent sce) {
    container = (ServerWebSocketContainer) sce.getServletContext().getAttribute(ServerContainer.class.getName());
    FilterRegistration.Dynamic filter = sce.getServletContext().addFilter(FILTER_NAME, JsrWebSocketFilter.class);
    sce.getServletContext().addListener(JsrWebSocketFilter.LogoutListener.class);
    filter.setAsyncSupported(true);
    if(!container.getConfiguredServerEndpoints().isEmpty()){
        filter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");
    } else {
        container.setContextToAddFilter((ServletContextImpl) sce.getServletContext());
    }
}
 
Example #29
Source File: TestWsWebSocketContainer.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent sce) {
    super.contextInitialized(sce);
    ServerContainer sc =
            (ServerContainer) sce.getServletContext().getAttribute(
                    Constants.SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE);
    try {
        // Reset blocking state
        BlockingPojo.resetBlock();
        sc.addEndpoint(BlockingPojo.class);
    } catch (DeploymentException e) {
        throw new IllegalStateException(e);
    }
}
 
Example #30
Source File: ContextLoaderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testContextLoaderListenerWithLocalContextInitializers() {
	MockServletContext sc = new MockServletContext("");
	sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
			"org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml");
	sc.addInitParameter(ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, StringUtils.arrayToCommaDelimitedString(
			new Object[] {TestContextInitializer.class.getName(), TestWebContextInitializer.class.getName()}));
	ContextLoaderListener listener = new ContextLoaderListener();
	listener.contextInitialized(new ServletContextEvent(sc));
	WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
	TestBean testBean = wac.getBean(TestBean.class);
	assertThat(testBean.getName(), equalTo("testName"));
	assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue());
}