private void addCacheSessionListener(final String uuid) {

    final ExternalContext.Session session =
        NetUtils.getSession(XFormsStateManager.FORCE_SESSION_CREATION);

    assert session != null;

    final Map<String, Object> sessionAttributes =
        session.getAttributesMap(ExternalContext.Session.APPLICATION_SCOPE);
    final String listenerSessionKey = getListenerSessionKey(uuid);
    if (sessionAttributes.get(listenerSessionKey) == null) {

      // Remove from cache when session expires
      final ExternalContext.Session.SessionListener listener =
          new ExternalContext.Session.SessionListener() {
            public void sessionDestroyed() {
              indentedLogger.logDebug(
                  LOG_TYPE, "Removing document from cache following session expiration.");
              // NOTE: This will call onRemoved() on the document, and onRemovedFromCache() on
              // XFormsStateManager
              XFormsDocumentCache.instance().removeDocument(uuid);
            }
          };

      // Add listener
      session.addListener(listener);
      // Remember, in session, mapping (UUID -> session listener)
      sessionAttributes.put(listenerSessionKey, listener);
    }
  }
 /**
  * Add listener to fileItem which is going to be automatically destroyed on session destruction
  *
  * @param pipelineContext PipelineContext
  * @param fileItem FileItem
  */
 public static void deleteFileOnSessionTermination(
     PipelineContext pipelineContext, final FileItem fileItem) {
   // Try to delete the file on exit and on session termination
   final ExternalContext externalContext =
       (ExternalContext) pipelineContext.getAttribute(PipelineContext.EXTERNAL_CONTEXT);
   final ExternalContext.Session session = externalContext.getSession(false);
   if (session != null) {
     session.addListener(
         new ExternalContext.Session.SessionListener() {
           public void sessionDestroyed() {
             deleteFileItem(fileItem, SESSION_SCOPE);
           }
         });
   } else {
     logger.debug(
         "No existing session found so cannot register temporary file deletion upon session destruction: "
             + fileItem.getName());
   }
 }