class SessionHelpers {

  static void findListenersByIntercepting(ServletContext context, HttpServletRequest request) {
    if (context.getAttribute(INTROSPECTING_LISTENERS) == null) {
      // If we haven't started or completed introspecting listeners, let's do it
      logger.info("Started collecting servlet listeners.");
      // We put a Set that will contain all introspected listeners in servlet
      // context attribute
      context.setAttribute(INTROSPECTING_LISTENERS, new HashSet<Object>());
      // Then we create session inside container (not "our" session).
      // This should trigger all HttpSessionListeners
      HttpSession session = request.getSession();
      // Next we add attribute. This should trigger all
      // HttpSessionAttributeListeners
      session.setAttribute(DUMMY_ATTRIBUTE, DUMMY_ATTRIBUTE);
      session.removeAttribute(DUMMY_ATTRIBUTE);
      // Finally we remove this session.
      session.invalidate();
      // And we mark that introspecting was done.
      context.setAttribute(INTROSPECTING_LISTENERS, Boolean.TRUE);
      logger.info("Finished collecting listeners.");
    }
  }

}