class SessionHelpers {

  public void onAddListener(ServletContext servletContext, Object listener) {
    String contextPath = servletContext.getContextPath();
    ServletContextDescriptor scd = getDescriptor(servletContext);
    logger.debug("Registering listener {} for context {}", listener, contextPath);
    // As theoretically one class can implement many listener interfaces we
    // check if it implements each of supported ones
    if (listener instanceof HttpSessionListener) {
      scd.addHttpSessionListener((HttpSessionListener)listener);
    }
    if (listener instanceof HttpSessionAttributeListener) {
      scd.addHttpSessionAttributeListener((HttpSessionAttributeListener)listener);
    }
    if (ServletLevel.isServlet31) {
      // Guard the code inside block to avoid use of classes
      // that are not available in versions before Servlet 3.1
      if (listener instanceof HttpSessionIdListener) { // NOSONAR
        scd.addHttpSessionIdListener((HttpSessionIdListener)listener);
      }
    }
  }

}