/**
   * Method addEntry, adds information to the SASLSession table to track what sessions have been
   * authenicated with what critera.
   */
  protected synchronized void addEntry(Session session) throws SASLException {
    boolean anonymous = false;

    if (session == null) throw new SASLException(ERR_INVALID_PARAMETERS);

    SessionCredential cred = session.getPeerCredential();
    if (cred == null) throw new SASLException(ERR_INVALID_PARAMETERS);

    anonymous = cred.getAuthenticatorType().equals(SASLAnonymousProfile.MECHANISM);

    // If this session is mapped to another user, get rid of it.
    String authenticator = (String) sessionToName.get(session);

    // If it's OTP, store it both ways.
    if (anonymous) {
      // This will be in sessionToName by session
      sessionToName.remove(session);
    } else if (authenticator != null) {
      sessionToName.remove(session);
      nameToSession.remove(authenticator);
    }

    // Get the new credential
    authenticator = session.getPeerCredential().getAuthenticator();
    if (authenticator == null) throw new SASLException(ERR_INVALID_PARAMETERS);
    session.addSessionListener(this);
    if (sessionToName.contains(session)) nameToSession.remove(authenticator);
    sessionToName.put(session, authenticator);
    if (!anonymous) nameToSession.put(authenticator, session);
    printContents();
  }
  /**
   * Method removeEntry removes SASL/Authenticator data from the SASLSession table. Called when
   * sessions terminate, or when credentials are 'cleared'.
   */
  protected synchronized void removeEntry(Session session) throws SASLException {
    if (session == null) throw new SASLException(ERR_INVALID_PARAMETERS);

    if (session.getPeerCredential() != null) {
      String authenticator = session.getPeerCredential().getAuthenticator();
      if (authenticator == null) throw new SASLException(ERR_INVALID_PARAMETERS);
      nameToSession.remove(authenticator);
    }
    sessionToName.remove(session);
    session.removeSessionListener(this);
    printContents();
  }
  /**
   * Method printContents does a simple dump of the SASLSessionTable for the purposes of monitoring
   * or debugging.
   */
  void printContents() {
    log.debug(MSG_SESSIONS_TABLE_HEADER);
    if (sessionToName.size() == 0) {
      log.debug(MSG_EMPTY);
    } else {
      Enumeration e = sessionToName.keys();
      while (e.hasMoreElements()) {
        Session s = (Session) e.nextElement();
        String user = (String) sessionToName.get(s);
        String mech;
        if (s.getPeerCredential() != null) mech = s.getPeerCredential().getAuthenticatorType();
        else mech = "UNKNOWN";

        if (log.isDebugEnabled()) {
          log.debug(MSG_USER_PREFIX + user + MSG_MECHANISM_PREFIX + mech);
        }
      }
    }
    log.debug(MSG_SESSIONS_TABLE_TRAILER);
  }