Example #1
0
 public String getLastAccessedTime(String sessionId) {
   Session s = (Session) sessions.get(sessionId);
   if (s == null) {
     return "";
   }
   return new Date(s.getLastAccessedTime()).toString();
 }
 public String getLastAccessedTime(String sessionId) {
   Session s = (Session) sessions.get(sessionId);
   if (s == null) {
     if (log.isInfoEnabled()) log.info("Session not found " + sessionId);
     return "";
   }
   return new Date(s.getLastAccessedTime()).toString();
 }
  public static ApplicationSession getApplicationSession(
      Session session, boolean calcSize, boolean addAttributes) {

    ApplicationSession sbean = null;
    if (session != null && session.isValid()) {
      sbean = new ApplicationSession();

      sbean.setId(session.getId());
      sbean.setCreationTime(new Date(session.getCreationTime()));
      sbean.setLastAccessTime(new Date(session.getLastAccessedTime()));
      sbean.setMaxIdleTime(session.getMaxInactiveInterval() * 1000);
      sbean.setManagerType(session.getManager().getClass().getName());
      // sbean.setInfo(session.getInfo());
      // TODO:fixmee

      boolean sessionSerializable = true;
      int attributeCount = 0;
      long size = 0;

      HttpSession httpSession = session.getSession();
      Set processedObjects = new HashSet(1000);

      // Exclude references back to the session itself
      processedObjects.add(httpSession);
      try {
        for (Enumeration e = httpSession.getAttributeNames(); e.hasMoreElements(); ) {
          String name = (String) e.nextElement();
          Object obj = httpSession.getAttribute(name);
          sessionSerializable = sessionSerializable && obj instanceof Serializable;

          long objSize = 0;
          if (calcSize) {
            try {
              objSize += Instruments.sizeOf(name, processedObjects);
              objSize += Instruments.sizeOf(obj, processedObjects);
            } catch (Throwable th) {
              logger.error("Cannot estimate size of attribute \"" + name + "\"", th);
              //
              // make sure we always re-throw ThreadDeath
              //
              if (e instanceof ThreadDeath) {
                throw (ThreadDeath) e;
              }
            }
          }

          if (addAttributes) {
            Attribute saBean = new Attribute();
            saBean.setName(name);
            saBean.setType(ClassUtils.getQualifiedName(obj.getClass()));
            saBean.setValue(obj);
            saBean.setSize(objSize);
            saBean.setSerializable(obj instanceof Serializable);
            sbean.addAttribute(saBean);
          }
          attributeCount++;
          size += objSize;
        }
        String lastAccessedIp =
            (String) httpSession.getAttribute(ApplicationSession.LAST_ACCESSED_BY_IP);
        if (lastAccessedIp != null) {
          sbean.setLastAccessedIp(lastAccessedIp);
        }
        try {
          sbean.setLastAccessedIpLocale(
              InetAddressLocator.getLocale(InetAddress.getByName(lastAccessedIp).getAddress()));
        } catch (Throwable e) {
          logger.error("Cannot determine Locale of " + lastAccessedIp);
          //
          // make sure we always re-throw ThreadDeath
          //
          if (e instanceof ThreadDeath) {
            throw (ThreadDeath) e;
          }
        }

      } catch (IllegalStateException e) {
        logger.info("Session appears to be invalidated, ignore");
      }

      sbean.setObjectCount(attributeCount);
      sbean.setSize(size);
      sbean.setSerializable(sessionSerializable);
    }

    return sbean;
  }
Example #4
0
  /**
   * Save a session to the Store.
   *
   * @param session the session to be stored
   * @exception IOException if an input/output error occurs
   */
  public void save(Session session) throws IOException {
    String saveSql =
        "INSERT INTO "
            + sessionTable
            + " ("
            + sessionIdCol
            + ", "
            + sessionAppCol
            + ", "
            + sessionDataCol
            + ", "
            + sessionValidCol
            + ", "
            + sessionMaxInactiveCol
            + ", "
            + sessionLastAccessedCol
            + ") VALUES (?, ?, ?, ?, ?, ?)";
    ObjectOutputStream oos = null;
    ByteArrayOutputStream bos = null;
    ByteArrayInputStream bis = null;
    InputStream in = null;

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

      // If sessions already exist in DB, remove and insert again.
      // TODO:
      // * Check if ID exists in database and if so use UPDATE.
      remove(session.getId());

      try {
        bos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(new BufferedOutputStream(bos));

        ((StandardSession) session).writeObjectData(oos);
        oos.close();

        byte[] obs = bos.toByteArray();
        int size = obs.length;
        bis = new ByteArrayInputStream(obs, 0, size);
        in = new BufferedInputStream(bis, size);

        if (preparedSaveSql == null) {
          preparedSaveSql = _conn.prepareStatement(saveSql);
        }

        preparedSaveSql.setString(1, session.getId());
        preparedSaveSql.setString(2, getName());
        preparedSaveSql.setBinaryStream(3, in, size);
        preparedSaveSql.setString(4, session.isValid() ? "1" : "0");
        preparedSaveSql.setInt(5, session.getMaxInactiveInterval());
        preparedSaveSql.setLong(6, session.getLastAccessedTime());
        preparedSaveSql.execute();
      } catch (SQLException e) {
        log(sm.getString(getStoreName() + ".SQLException", e));
      } catch (IOException e) {;
      } finally {
        if (bis != null) {
          bis.close();
        }
        if (in != null) {
          in.close();
        }

        release(_conn);
      }
    }

    if (debug > 0) {
      log(sm.getString(getStoreName() + ".saving", session.getId(), sessionTable));
    }
  }
 public long getLastAccessedTimestamp(String sessionId) {
   Session s = sessions.get(sessionId);
   if (s == null) return -1;
   return s.getLastAccessedTime();
 }
 public long getLastAccessedTime() {
   return tomcatSession.getLastAccessedTime();
 }