public void contextInitialized(ServletContextEvent arg0) { arg0.getServletContext().setAttribute(ATTR_NAME, this); synchronized (servletContextListeners) { int s = servletContextListeners.size(); for (int i = 0; i < s; ++i) { ((ServletContextListener) servletContextListeners.get(i)).contextInitialized(arg0); } } }
@Override public <T extends EventListener> void addListener(T t) { if (t instanceof ServletContextListener) { ((ServletContextListener) t).contextInitialized(new ServletContextEvent(this)); } }
/** * Addresses the issues raised in <a * href="http://opensource.atlassian.com/projects/spring/browse/SPR-4008" * target="_blank">SPR-4008</a>: <em>Supply an opportunity to customize * context before calling refresh in ContextLoaders</em>. */ @Test public void testContextLoaderListenerWithCustomizedContextLoader() { final StringBuffer buffer = new StringBuffer(); final String expectedContents = "customizeContext() was called"; final MockServletContext sc = new MockServletContext(""); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/org/springframework/web/context/WEB-INF/applicationContext.xml"); ServletContextListener listener = new ContextLoaderListener() { @Override protected void customizeContext(ServletContext sc, ConfigurableWebApplicationContext wac) { assertNotNull("The ServletContext should not be null.", sc); assertEquals("Verifying that we received the expected ServletContext.", sc, sc); assertFalse("The ApplicationContext should not yet have been refreshed.", wac.isActive()); buffer.append(expectedContents); } }; listener.contextInitialized(new ServletContextEvent(sc)); assertEquals("customizeContext() should have been called.", expectedContents, buffer.toString()); }
@Test public void testContextLoaderWithDefaultLocation() throws Exception { MockServletContext sc = new MockServletContext(""); ServletContextListener listener = new ContextLoaderListener(); ServletContextEvent event = new ServletContextEvent(sc); try { listener.contextInitialized(event); fail("Should have thrown BeanDefinitionStoreException"); } catch (BeanDefinitionStoreException ex) { // expected assertTrue(ex.getCause() instanceof IOException); assertTrue(ex.getCause().getMessage().contains("/WEB-INF/applicationContext.xml")); } }
@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); }
@Test public void testContextLoaderWithInvalidLocation() throws Exception { MockServletContext sc = new MockServletContext(""); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/WEB-INF/myContext.xml"); ServletContextListener listener = new ContextLoaderListener(); ServletContextEvent event = new ServletContextEvent(sc); try { listener.contextInitialized(event); fail("Should have thrown BeanDefinitionStoreException"); } catch (BeanDefinitionStoreException ex) { // expected assertTrue(ex.getCause() instanceof FileNotFoundException); } }
@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); } }
private void notifyStartListeners(ServletContext context) { for (ServletContextListener listener : contextListeners) { listener.contextInitialized(new ServletContextEvent(context)); } }
@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)); }
servletContext); servletContextListener.contextInitialized(servletContextEvent);
@Test public void testContextDestroyed() { ServletContext context = mock(ServletContext.class); ServletContextEvent destroyed = mock(ServletContextEvent.class); when(destroyed.getServletContext()).thenReturn(context); ServletContextListener listener = new IIOProviderContextListener(); listener.contextInitialized(mock(ServletContextEvent.class)); listener.contextDestroyed(destroyed); }
@Test public void testContextInitialized() { ServletContextListener listener = new IIOProviderContextListener(); listener.contextInitialized(mock(ServletContextEvent.class)); }
private void startListeners() { for (ServletContextListener listener : listeners) { listener.contextInitialized(event); } }
@Override public void contextInitialized(ServletContextEvent sce) { for (ServletContextListener listener : listeners) { listener.contextInitialized(sce); } }
public void contextInitialized(ServletContextEvent sce) { if (contextListeners != null && !contextListeners.isEmpty()) { for (ServletContextListener listener : contextListeners) { listener.contextInitialized(sce); } } }
@Test public void testDestroyConcurrentModRegression() { ServletContext context = mock(ServletContext.class); ServletContextEvent destroyed = mock(ServletContextEvent.class); when(destroyed.getServletContext()).thenReturn(context); ServletContextListener listener = new IIOProviderContextListener(); listener.contextInitialized(mock(ServletContextEvent.class)); ImageReaderSpi provider1 = new MockImageReaderSpiOne(); ImageReaderSpi provider2 = new MockImageReaderSpiToo(); // NOTE: Fake registering for simplicity, but it still exposes the original problem with de-registering IIORegistry registry = IIORegistry.getDefaultInstance(); registry.registerServiceProvider(provider1); registry.registerServiceProvider(provider2); assertTrue(registry.contains(provider1)); assertTrue(registry.contains(provider2)); listener.contextDestroyed(destroyed); assertFalse(registry.contains(provider1)); assertFalse(registry.contains(provider2)); }
public void contextInitialized(ServletContextEvent arg0) { arg0.getServletContext().setAttribute(ATTR_NAME, this); synchronized(servletContextListeners) { int s = servletContextListeners.size(); for(int i = 0; i < s; ++i) { ((ServletContextListener)servletContextListeners.get(i)).contextInitialized(arg0); } } }
protected void initWebServletContextListener(ServletContextEvent servletContextEvent, ServletContext sc) { String className = sc.getInitParameter("webServletContextListener"); if (!Strings.isNullOrEmpty(className)) { try { Class<?> clazz = this.getClass().getClassLoader().loadClass(className); webServletContextListener = (ServletContextListener) clazz.newInstance(); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { throw new RuntimeException("An error occurred while starting single WAR application", e); } webServletContextListener.contextInitialized(servletContextEvent); } }
protected void callContextInitialized (ServletContextListener l, ServletContextEvent e) { LOG.debug("contextInitialized: {}->{}",e,l); l.contextInitialized(e); }
@Override public ServletContextListener addingService(ServiceReference<ServletContextListener> reference) { ServletContextListener service = super.addingService(reference); service.contextInitialized(new ServletContextEvent(servletContext)); return service; }