/**
   * Returns a session from the session store, returning null if there's no cached session.
   *
   * @param key the session id
   * @param now the time in milliseconds. now == 0 implies that we're just checking for the
   *     existence of such a session in the cache and do not intend actually to load it if it is
   *     not.
   * @return the cached session.
   */
  public SessionArrayValue getSession(Env env, String key, long now) {
    SessionArrayValue session;
    boolean isNew = false;
    boolean killSession = false;

    if (_sessions == null) return null;

    // Check the cache first
    session = _sessions.get(key);

    if (session != null && !session.getId().equals(key))
      throw new IllegalStateException(key + " != " + session.getId());

    if (session != null) {
      if (session.inUse()) {
        System.out.println("USE:" + isNew);
        return (SessionArrayValue) session.copy(env);
      }
    }

    if (session == null) return null;

    if (isNew) {
      isNew = !load(env, session, now);
      System.out.println("LOAD:" + isNew);
    } else if (!getSaveOnlyOnShutdown() && !session.load()) {
      // if the load failed, then the session died out from underneath
      session.reset(now);
      isNew = true;
    }

    if (!isNew) session.setAccess(now);

    return (SessionArrayValue) session.copy(env);
  }
  public void saveSession(Env env, SessionArrayValue session) {
    SessionArrayValue copy = (SessionArrayValue) session.copy(env);

    _sessions.put(session.getId(), copy);

    session.finish();

    if (_persistentStore != null) {
      _persistentStore.put(session.getId(), copy.encode(env));
    }
  }
  /**
   * Creates a session. It's already been established that the key does not currently have a
   * session.
   */
  protected SessionArrayValue create(Env env, String key, long now) {
    SessionArrayValue session = createSessionValue(key, now, _sessionTimeout);

    load(env, session, now);

    // If another thread has created and stored a new session,
    // putIfNew will return the old session
    session = _sessions.putIfNew(key, session);

    if (!key.equals(session.getId()))
      throw new IllegalStateException(key + " != " + session.getId());

    return (SessionArrayValue) session.copy(env);
  }