/**
   * 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);
  }
  /**
   * Loads the session from the backing store.
   *
   * @param session the session to load.
   * @param now current 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.
   */
  protected boolean load(Env env, SessionArrayValue session, long now) {
    try {
      if (session.inUse()) {
        return true;
      } else if (now <= 0) {
        return false;
      }

      if (_persistentStore != null) {
        String encoded = (String) _persistentStore.get(session.getId());

        if (encoded != null) {
          session.decode(env, new StringBuilderValue(encoded));
        }
      }

      if (session.load()) {
        session.setAccess(now);
        return true;
      } else {
        session.reset(now);
      }
    } catch (Exception e) {
      log.log(Level.FINE, e.toString(), e);
      session.reset(now);
    }

    return false;
  }
  /**
   * Loads the session.
   *
   * @param in the input stream containing the serialized session
   * @param obj the session object to be deserialized
   */
  public void load(ObjectInputStream in, Object obj) throws IOException {
    SessionArrayValue session = (SessionArrayValue) obj;

    session.load(null, in);
  }