private WebApplicationContext springSetup() {
    if (logger.isDebugEnabled()) {
      String msg = String.format(" '%s'", "spring setup");
      logger.debug(msg);
    }
    StringBuilder sb = new StringBuilder();

    // in web.xml this is loaded through the contextloaderlistener
    // here since we're mocking we need to load it ourselves
    // String s = String.format("classpath:%s classpath:%s classpath:%s",
    // "spring-service-layer.xml", "spring-db-testing.xml",
    // "spring-test.xml");
    // sb.append(s);

    // TODO make this configurable
    sb.append("classpath:spring-service-layer.xml");

    // spring class in spring-test
    MockServletContext sc = new MockServletContext();

    // spring's ContextLoader.createWebApplicationContext looks for this key
    sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, sb.toString());

    ServletContextListener contextListener = new ContextLoaderListener();
    ServletContextEvent event = new ServletContextEvent(sc);
    contextListener.contextInitialized(event);
    WebApplicationContext wac =
        (WebApplicationContext)
            sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

    if (logger.isDebugEnabled()) {
      String msg = String.format("just created '%s'", wac);
      logger.debug(msg);
    }
    getSession()
        .getServletContext()
        .setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    getActionServlet()
        .getServletContext()
        .setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

    // if (applicationContext == null) {
    // // this is the first time. Initialize the Spring context.
    // applicationContext = (new ContextLoader())
    // .initWebApplicationContext(getRequest().getSession()
    // .getServletContext());
    // } else {
    // // Spring context is already initialized. Set it in servlet context
    // // so that Spring's ContextLoaderPlugIn will not initialize it again
    // getRequest()
    // .getSession()
    // .getServletContext()
    // .setAttribute(
    // WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
    // applicationContext);
    // }

    return WebApplicationContextUtils.getRequiredWebApplicationContext(
        getSession().getServletContext());
  }
 @Override
 public void contextDestroyed(final ServletContextEvent servletContextEvent) {
   synchronized (servletContextListeners) {
     for (int i = servletContextListeners.size() - 1; i >= 0; --i) {
       final ServletContextListener listener =
           (ServletContextListener) servletContextListeners.get(i);
       listener.contextDestroyed(servletContextEvent);
     }
   }
 }
 @Override
 public void contextInitialized(final ServletContextEvent servletContextEvent) {
   servletContextEvent.getServletContext().setAttribute(MVCEventAdapter.ATTR_NAME, this);
   synchronized (servletContextListeners) {
     for (int i = 0; i < servletContextListeners.size(); ++i) {
       final ServletContextListener listener =
           (ServletContextListener) servletContextListeners.get(i);
       listener.contextInitialized(servletContextEvent);
     }
   }
 }
 @Override
 public void contextDestroyed(final ServletContextEvent servletContextEvent) {
   super.contextDestroyed(servletContextEvent);
   if (servletContextListeners != null) {
     Iterator<ServletContextListener> it = servletContextListeners.iterator();
     while (it.hasNext()) {
       ServletContextListener listener = it.next();
       logger.finer("Destroy ServletContextListener: " + listener.getClass().getName());
       listener.contextDestroyed(servletContextEvent);
     }
   }
   logger.finer("Destroyed ServletContextListener");
 }
  public void contextDestroyed(ServletContextEvent sce) {
    if (ctx == null) {
      logger.warn("cannot find applicationContext");

      return;
    }

    Collection<ServletContextListener> servletContextListeners =
        ctx.getBeansOfType(ServletContextListener.class).values();

    for (ServletContextListener servletContextListener : servletContextListeners) {
      servletContextListener.contextDestroyed(sce);
    }
  }
  public void testContextInitialized() {
    listener.contextInitialized(new ServletContextEvent(sc));

    assertTrue(
        sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null);
    assertTrue(sc.getAttribute(Constants.AVAILABLE_ROLES) != null);
  }
  public void contextInitialized(ServletContextEvent sce) {
    ctx = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());

    if (ctx == null) {
      logger.warn("cannot find applicationContext");

      return;
    }

    Collection<ServletContextListener> servletContextListeners =
        ctx.getBeansOfType(ServletContextListener.class).values();

    for (ServletContextListener servletContextListener : servletContextListeners) {
      servletContextListener.contextInitialized(sce);
    }
  }
 @Override
 public void contextInitialized(final ServletContextEvent servletContextEvent) {
   BootstrapUtil.restoreSystemProperties();
   if (servletContextListeners == null) {
     servletContextListeners = ServiceLoader.load(ServletContextListener.class);
   }
   if (servletContextListeners != null) {
     Iterator<ServletContextListener> it = servletContextListeners.iterator();
     while (it.hasNext()) {
       ServletContextListener listener = it.next();
       logger.finer("Initialize ServletContextListener: " + listener.getClass().getName());
       listener.contextInitialized(servletContextEvent);
     }
   }
   super.contextInitialized(servletContextEvent);
   logger.finer("Initialized ServletContextListener");
 }
  public void testContextInitialized() {
    listener.contextInitialized(new ServletContextEvent(sc));

    assertTrue(sc.getAttribute(Constants.CONFIG) != null);
    Map config = (Map) sc.getAttribute(Constants.CONFIG);
    assertEquals(config.get(Constants.CSS_THEME), "simplicity");

    assertTrue(
        sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null);
    assertTrue(sc.getAttribute(Constants.AVAILABLE_ROLES) != null);
  }
  public void contextInitialized(ServletContextEvent sce) {
    final IConfig scConfig = new ServletContextConfig(sce.getServletContext());
    final String prefix = "RubyListener." + getClass().getSimpleName() + ".";
    listenerConfig = new PrefixedConfig(prefix, scConfig);
    final IConfig config = new ChainedConfig(listenerConfig, scConfig);

    this.sce = sce;
    factory = Util.getFactory(config, Util.RESTARTABLE_RUBY_FACTORY);
    factory.reference(this);
    child = factory.makeListener(listenerConfig);

    child.contextInitialized(sce);
  }
  @Override
  protected void fireUndeployEvent() {
    if (_servletContextListeners != null) {
      ServletContextEvent servletContextEvent = new ServletContextEvent(servletContext);

      for (ServletContextListener servletContextListener : _servletContextListeners) {

        try {
          servletContextListener.contextDestroyed(servletContextEvent);
        } catch (Throwable t) {
          String className = ClassUtil.getClassName(servletContextListener.getClass());

          _log.error(
              className
                  + " is unable to process a context "
                  + "destroyed event for "
                  + servletContext.getServletContextName(),
              t);
        }
      }
    }

    super.fireUndeployEvent();
  }
  protected void instantiatingListener(String listenerClassName) throws Exception {

    if (_log.isDebugEnabled()) {
      _log.debug("Instantiating " + listenerClassName);
    }

    Object listener = InstanceFactory.newInstance(pluginClassLoader, listenerClassName);

    if (listener instanceof HttpSessionActivationListener) {
      if (_httpSessionActivationListeners == null) {
        _httpSessionActivationListeners = new CopyOnWriteArrayList<HttpSessionActivationListener>();
      }

      _httpSessionActivationListeners.add((HttpSessionActivationListener) listener);
    }

    if (listener instanceof HttpSessionAttributeListener) {
      if (_httpSessionAttributeListeners == null) {
        _httpSessionAttributeListeners = new CopyOnWriteArrayList<HttpSessionAttributeListener>();
      }

      _httpSessionAttributeListeners.add((HttpSessionAttributeListener) listener);
    }

    if (listener instanceof HttpSessionBindingListener) {
      if (_httpSessionBindingListeners == null) {
        _httpSessionBindingListeners = new CopyOnWriteArrayList<HttpSessionBindingListener>();
      }

      _httpSessionBindingListeners.add((HttpSessionBindingListener) listener);
    }

    if (listener instanceof HttpSessionListener) {
      if (_httpSessionListeners == null) {
        _httpSessionListeners = new CopyOnWriteArrayList<HttpSessionListener>();
      }

      _httpSessionListeners.add((HttpSessionListener) listener);
    }

    if (listener instanceof ServletContextListener) {
      if (_servletContextListeners == null) {
        _servletContextListeners = new CopyOnWriteArrayList<ServletContextListener>();
      }

      ServletContextListener servletContextListener = (ServletContextListener) listener;

      _servletContextListeners.add(servletContextListener);

      ServletContextEvent servletContextEvent = new ServletContextEvent(servletContext);

      servletContextListener.contextInitialized(servletContextEvent);
    }

    if (listener instanceof ServletRequestAttributeListener) {
      if (_servletRequestAttributeListeners == null) {
        _servletRequestAttributeListeners =
            new CopyOnWriteArrayList<ServletRequestAttributeListener>();
      }

      _servletRequestAttributeListeners.add((ServletRequestAttributeListener) listener);
    }

    if (listener instanceof ServletRequestListener) {
      if (_servletRequestListeners == null) {
        _servletRequestListeners = new CopyOnWriteArrayList<ServletRequestListener>();
      }

      _servletRequestListeners.add((ServletRequestListener) listener);
    }
  }
 public void destroy() {
   delegate.destroy();
   listener.contextDestroyed(new ServletContextEvent(config.getServletContext()));
   config = null;
 }
 public void init(ServletConfig config) throws ServletException {
   this.config = config;
   listener.contextInitialized(new ServletContextEvent(config.getServletContext()));
   delegate.init(config);
 }
 public void restart() {
   assertNotNull(this, child).contextDestroyed(assertNotNull(sce));
   child = factory.makeListener(getListenerConfig());
   child.contextInitialized(assertNotNull(sce));
 }