Example usage for javax.servlet ServletContextListener contextInitialized

List of usage examples for javax.servlet ServletContextListener contextInitialized

Introduction

In this page you can find the example usage for javax.servlet ServletContextListener contextInitialized.

Prototype

default public void contextInitialized(ServletContextEvent sce) 

Source Link

Document

Receives notification that the web application initialization process is starting.

Usage

From source file:com.amazonaws.serverless.proxy.spring.LambdaSpringApplicationInitializer.java

private void notifyStartListeners(ServletContext context) {
    for (ServletContextListener listener : contextListeners) {
        listener.contextInitialized(new ServletContextEvent(context));
    }//from   w ww.j a va2 s  .  co m
}

From source file:org.grails.plugin.batch.Launcher.java

public void init(String[] args) {
    logDebug(true, "init(): begin");
    synchronized (initLock) {

        logDebug(true, "init(): this classLoader ", this.getClass().getClassLoader());
        logDebug(true, "init(): thread classLoader ", Thread.currentThread().getContextClassLoader());

        logDebug(true, "init(): env ", Environment.getCurrent());

        String resourcePath = getSystemProperty("resourcePath", null);
        if (resourcePath == null) {
            resourcePath = "war";
        }//w w w.  j a v  a 2  s.  c o  m

        logDebug(true, "init(): resourcePath ", resourcePath);

        servletContext = resourcePath != null ? new MockServletContext(resourcePath) : new MockServletContext();
        servletContext.setAttribute("args", args);

        servletContextListeners = new ServletContextListener[] { new Log4jConfigListener(),
                new GrailsContextLoaderListener() };

        this.shutdownHook = new Thread() {

            public void run() {
                logDebug(true, "shutdown hook run():");
                Launcher.this.destroy();
            }
        };

        this.destroyed = false;

        Runtime.getRuntime().addShutdownHook(this.shutdownHook);
        logDebug(true, "init(): shutdown hook added");

        try {
            ServletContextEvent event = new ServletContextEvent(servletContext);
            for (ServletContextListener l : servletContextListeners) {
                l.contextInitialized(event);
            }
        } catch (RuntimeException e) {
            log.error("init()", e);
            throw e;
        }

        logDebug("init(): thread classLoader ", Thread.currentThread().getContextClassLoader());

        GrailsApplication grailsApplication = ApplicationHolder.getApplication();
        DefaultGrailsMainClass main = getMainClass(grailsApplication);

        if (main != null) {
            final Object instance = main.getReferenceInstance();

            WebApplicationContext webContext = (WebApplicationContext) grailsApplication.getMainContext();
            webContext.getAutowireCapableBeanFactory().autowireBeanProperties(instance,
                    AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
            main.callInit();
        }

        logDebug("init(): end");
    }
}

From source file:edu.cornell.mannlib.vitro.webapp.startup.StartupManager.java

/**
 * Call contextInitialized() on the listener.
 * /*w ww.j  a  v a2s  . c om*/
 * If there is an unexpected exception, set a fatal error.
 */
private void initialize(ServletContextListener listener, ServletContextEvent sce) {
    try {
        log.debug("Initializing '" + listener.getClass().getName() + "'");
        listener.contextInitialized(sce);
        ss.listenerExecuted(listener);
    } catch (Exception e) {
        ss.fatal(listener, "Threw unexpected exception", e);
    } catch (Throwable t) {
        log.fatal(listener + " Threw unexpected error", t);
        throw t;
    }
}

From source file:com.jsmartframework.web.manager.ServletControl.java

@Override
public void init(ServletConfig servletConfig) throws ServletException {
    super.init(servletConfig);
    WebContext.setServlet(this);

    // Call registered WebContextListeners
    for (ServletContextListener contextListener : HANDLER.contextListeners) {
        HANDLER.executeInjection(contextListener);
        contextListener.contextInitialized(new ServletContextEvent(servletConfig.getServletContext()));
    }//from w  w w  .j  a  v  a 2 s .c  o  m
}

From source file:org.jasig.springframework.web.portlet.context.PortletContextLoaderTests.java

@Test
public void testContextLoaderWithDefaultLocation() throws Exception {
    MockServletContext sc = new MockServletContext("");
    ServletContextListener listener = new PortletContextLoaderListener();
    ServletContextEvent event = new ServletContextEvent(sc);
    listener.contextInitialized(event);
    try {/*from  ww  w. j  a  v a2 s . co  m*/
        //initialize the portlet application context, needed because PortletContextLoaderListener.contextInitialized doesn't actually create
        //the portlet app context due to lack of PortletContext reference
        MockPortletContext pc = new MockPortletContext(sc);
        PortletApplicationContextUtils2.getPortletApplicationContext(pc);
        fail("Should have thrown BeanDefinitionStoreException");
    } catch (BeanDefinitionStoreException ex) {
        // expected
        assertTrue(ex.getCause() instanceof IOException);
        assertTrue(ex.getCause().getMessage().contains("/WEB-INF/portletApplicationContext.xml"));
    }
}

From source file:org.jasig.springframework.web.portlet.context.PortletContextLoaderTests.java

@Test
public void testContextLoaderWithInvalidLocation() throws Exception {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(PortletContextLoader.CONFIG_LOCATION_PARAM, "/WEB-INF/myContext.xml");
    ServletContextListener listener = new PortletContextLoaderListener();
    ServletContextEvent event = new ServletContextEvent(sc);
    listener.contextInitialized(event);
    try {//from  w  w  w  . ja  v  a 2s  .com
        //initialize the portlet application context, needed because PortletContextLoaderListener.contextInitialized doesn't actually create
        //the portlet app context due to lack of PortletContext reference
        MockPortletContext pc = new MockPortletContext(sc);
        PortletApplicationContextUtils2.getPortletApplicationContext(pc);

        fail("Should have thrown BeanDefinitionStoreException");
    } catch (BeanDefinitionStoreException ex) {
        // expected
        assertTrue(ex.getCause() instanceof FileNotFoundException);
    }
}

From source file:org.jasig.springframework.web.portlet.context.PortletContextLoaderTests.java

@Test
public void testContextLoaderWithInvalidContext() throws Exception {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(PortletContextLoader.CONTEXT_CLASS_PARAM,
            "org.springframework.web.context.support.InvalidWebApplicationContext");
    ServletContextListener listener = new PortletContextLoaderListener();
    ServletContextEvent event = new ServletContextEvent(sc);
    listener.contextInitialized(event);
    try {/*ww  w .j  a va2  s . c  o m*/
        //initialize the portlet application context, needed because PortletContextLoaderListener.contextInitialized doesn't actually create
        //the portlet app context due to lack of PortletContext reference
        MockPortletContext pc = new MockPortletContext(sc);
        PortletApplicationContextUtils2.getPortletApplicationContext(pc);
        fail("Should have thrown ApplicationContextException");
    } catch (ApplicationContextException ex) {
        // expected
        assertTrue(ex.getCause() instanceof ClassNotFoundException);
    }
}

From source file:org.jasig.springframework.web.portlet.context.PortletContextLoaderTests.java

@Test
public void testContextLoaderListenerWithDefaultContext() {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(PortletContextLoader.CONFIG_LOCATION_PARAM,
            "/org/springframework/web/context/WEB-INF/applicationContext.xml "
                    + "/org/springframework/web/context/WEB-INF/context-addition.xml");
    ServletContextListener listener = new PortletContextLoaderListener();
    ServletContextEvent event = new ServletContextEvent(sc);
    listener.contextInitialized(event);
    PortletContextLoader contextLoader = (PortletContextLoader) sc
            .getAttribute(PortletApplicationContextUtils2.ROOT_PORTLET_APPLICATION_CONTEXT_LOADER_ATTRIBUTE);
    assertNotNull(contextLoader);//from  w  w  w . j  ava  2  s .  c o  m

    //initialize the portlet application context, needed because PortletContextLoaderListener.contextInitialized doesn't actually create
    //the portlet app context due to lack of PortletContext reference
    MockPortletContext pc = new MockPortletContext(sc);
    PortletApplicationContext context = PortletApplicationContextUtils2.getPortletApplicationContext(pc);

    assertTrue("Correct PortletApplicationContext exposed in PortletContext",
            context instanceof ContribXmlPortletApplicationContext);
    assertTrue(PortletContextLoader
            .getCurrentPortletApplicationContext() instanceof ContribXmlPortletApplicationContext);
    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(PortletApplicationContext.ROOT_PORTLET_APPLICATION_CONTEXT_ATTRIBUTE));
    assertNull(PortletContextLoader.getCurrentPortletApplicationContext());
}

From source file:org.jasig.springframework.web.portlet.context.PortletContextLoaderTests.java

@Test
public void testContextLoaderWithCustomContextAndParent() throws Exception {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(ContextLoader.CONTEXT_CLASS_PARAM, StaticWebApplicationContext.class.getName());
    sc.addInitParameter(PortletContextLoader.CONTEXT_CLASS_PARAM,
            SimplePortletApplicationContext.class.getName());
    ContextLoaderListener servletListener = new ContextLoaderListener();
    ServletContextListener listener = new PortletContextLoaderListener();
    ServletContextEvent event = new ServletContextEvent(sc);

    servletListener.contextInitialized(event);
    listener.contextInitialized(event);

    //initialize the portlet application context, needed because PortletContextLoaderListener.contextInitialized doesn't actually create
    //the portlet app context due to lack of PortletContext reference
    MockPortletContext pc = new MockPortletContext(sc);
    PortletApplicationContextUtils2.getPortletApplicationContext(pc);

    PortletApplicationContext wc = (PortletApplicationContext) pc
            .getAttribute(PortletApplicationContext.ROOT_PORTLET_APPLICATION_CONTEXT_ATTRIBUTE);
    assertTrue("Correct PortletApplicationContext exposed in PortletContext",
            wc instanceof SimplePortletApplicationContext);
}

From source file:org.ireland.jnetty.webapp.WebApp.java

/**
 * ?publish ContextInitialized Event /*from   w w w  .  j  a  va2  s .c  o m*/
 */
protected void publishContextInitializedEvent() {
    log.debug("publish ContextInitialized Event");

    // ?ServletContextListener#contextInitialized
    ServletContextEvent event = new ServletContextEvent(this);

    for (int i = 0; i < _contextListeners.size(); i++) {
        ServletContextListener listener = _contextListeners.get(i);

        try {
            listener.contextInitialized(event);
        } catch (Exception e) {
            log.warn(e.toString(), e);
        }
    }
}