/**
   * Load and return the Session associated with the specified session identifier from this Store,
   * without removing it. If there is no such stored Session, return <code>null</code>.
   *
   * @param id Session identifier of the session to load
   * @exception ClassNotFoundException if a deserialization error occurs
   * @exception IOException if an input/output error occurs
   */
  @Override
  public Session load(String id) throws ClassNotFoundException, IOException {

    // Open an input stream to the specified pathname, if any
    File file = file(id);
    if (file == null) {
      return (null);
    }

    if (!file.exists()) {
      return (null);
    }
    if (manager.getContext().getLogger().isDebugEnabled()) {
      manager
          .getContext()
          .getLogger()
          .debug(sm.getString(getStoreName() + ".loading", id, file.getAbsolutePath()));
    }

    ObjectInputStream ois = null;
    Loader loader = null;
    ClassLoader classLoader = null;
    ClassLoader oldThreadContextCL = Thread.currentThread().getContextClassLoader();
    try (FileInputStream fis = new FileInputStream(file.getAbsolutePath());
        BufferedInputStream bis = new BufferedInputStream(fis)) {
      Context context = manager.getContext();
      if (context != null) loader = context.getLoader();
      if (loader != null) classLoader = loader.getClassLoader();
      if (classLoader != null) {
        Thread.currentThread().setContextClassLoader(classLoader);
        ois = new CustomObjectInputStream(bis, classLoader);
      } else {
        ois = new ObjectInputStream(bis);
      }

      StandardSession session = (StandardSession) manager.createEmptySession();
      session.readObjectData(ois);
      session.setManager(manager);
      return (session);
    } catch (FileNotFoundException e) {
      if (manager.getContext().getLogger().isDebugEnabled())
        manager.getContext().getLogger().debug("No persisted data file found");
      return (null);
    } finally {
      if (ois != null) {
        // Close the input stream
        try {
          ois.close();
        } catch (IOException f) {
          // Ignore
        }
      }
      Thread.currentThread().setContextClassLoader(oldThreadContextCL);
    }
  }
  /**
   * Load any currently active sessions that were previously unloaded to the appropriate persistence
   * mechanism, if any. If persistence is not supported, this method returns without doing anything.
   *
   * @exception ClassNotFoundException if a serialized class cannot be found during the reload
   * @exception IOException if an input/output error occurs
   */
  public void load() throws ClassNotFoundException, IOException {

    if (debug >= 1) log("Start: Loading persisted sessions");

    // Initialize our internal data structures
    recycled.clear();
    sessions.clear();

    // Open an input stream to the specified pathname, if any
    File file = file();
    if (file == null) return;
    if (debug >= 1) log(sm.getString("standardManager.loading", pathname));
    FileInputStream fis = null;
    ObjectInputStream ois = null;
    Loader loader = null;
    ClassLoader classLoader = null;
    try {
      fis = new FileInputStream(file.getAbsolutePath());
      BufferedInputStream bis = new BufferedInputStream(fis);
      if (container != null) loader = container.getLoader();
      if (loader != null) classLoader = loader.getClassLoader();
      if (classLoader != null) {
        if (debug >= 1) log("Creating custom object input stream for class loader " + classLoader);
        ois = new CustomObjectInputStream(bis, classLoader);
      } else {
        if (debug >= 1) log("Creating standard object input stream");
        ois = new ObjectInputStream(bis);
      }
    } catch (FileNotFoundException e) {
      if (debug >= 1) log("No persisted data file found");
      return;
    } catch (IOException e) {
      log(sm.getString("standardManager.loading.ioe", e), e);
      if (ois != null) {
        try {
          ois.close();
        } catch (IOException f) {;
        }
        ois = null;
      }
      throw e;
    }

    // Load the previously unloaded active sessions
    synchronized (sessions) {
      try {
        Integer count = (Integer) ois.readObject();
        int n = count.intValue();
        if (debug >= 1) log("Loading " + n + " persisted sessions");
        for (int i = 0; i < n; i++) {
          StandardSession session = new StandardSession(this);
          session.readObjectData(ois);
          session.setManager(this);
          sessions.put(session.getId(), session);
          session.activate();
        }
      } catch (ClassNotFoundException e) {
        log(sm.getString("standardManager.loading.cnfe", e), e);
        if (ois != null) {
          try {
            ois.close();
          } catch (IOException f) {;
          }
          ois = null;
        }
        throw e;
      } catch (IOException e) {
        log(sm.getString("standardManager.loading.ioe", e), e);
        if (ois != null) {
          try {
            ois.close();
          } catch (IOException f) {;
          }
          ois = null;
        }
        throw e;
      } finally {
        // Close the input stream
        try {
          if (ois != null) ois.close();
        } catch (IOException f) {
          // ignored
        }

        // Delete the persistent storage file
        if (file != null && file.exists()) file.delete();
      }
    }

    if (debug >= 1) log("Finish: Loading persisted sessions");
  }
Example #3
0
  /**
   * Load the Session associated with the id <code>id</code>. If no such session is found <code>null
   * </code> is returned.
   *
   * @param id a value of type <code>String</code>
   * @return the stored <code>Session</code>
   * @exception ClassNotFoundException if an error occurs
   * @exception IOException if an input/output error occurred
   */
  public Session load(String id) throws ClassNotFoundException, IOException {
    ResultSet rst = null;
    StandardSession _session = null;
    Loader loader = null;
    ClassLoader classLoader = null;
    ObjectInputStream ois = null;
    BufferedInputStream bis = null;
    Container container = manager.getContainer();
    String loadSql =
        "SELECT "
            + sessionIdCol
            + ", "
            + sessionDataCol
            + " FROM "
            + sessionTable
            + " WHERE "
            + sessionIdCol
            + " = ? AND "
            + sessionAppCol
            + " = ?";

    synchronized (this) {
      Connection _conn = getConnection();
      if (_conn == null) {
        return (null);
      }

      try {
        if (preparedLoadSql == null) {
          preparedLoadSql = _conn.prepareStatement(loadSql);
        }

        preparedLoadSql.setString(1, id);
        preparedLoadSql.setString(2, getName());
        rst = preparedLoadSql.executeQuery();
        if (rst.next()) {
          bis = new BufferedInputStream(rst.getBinaryStream(2));

          if (container != null) {
            loader = container.getLoader();
          }
          if (loader != null) {
            classLoader = loader.getClassLoader();
          }
          if (classLoader != null) {
            ois = new CustomObjectInputStream(bis, classLoader);
          } else {
            ois = new ObjectInputStream(bis);
          }

          if (debug > 0) {
            log(sm.getString(getStoreName() + ".loading", id, sessionTable));
          }

          _session = (StandardSession) manager.createEmptySession();
          _session.readObjectData(ois);
          _session.setManager(manager);

        } else if (debug > 0) {
          log(getStoreName() + ": No persisted data object found");
        }
      } catch (SQLException e) {
        log(sm.getString(getStoreName() + ".SQLException", e));
      } finally {
        try {
          if (rst != null) {
            rst.close();
          }
        } catch (SQLException e) {;
        }
        if (ois != null) {
          try {
            ois.close();
          } catch (IOException e) {;
          }
        }
        release(_conn);
      }
    }

    return (_session);
  }