/**
   * Construct and return a new session object, based on the default settings specified by this
   * Manager's properties. The session id specified will be used as the session id. If a new session
   * cannot be created for any reason, return <code>null</code>.
   *
   * @param sessionId The session id which should be used to create the new session; if <code>null
   *     </code>, a new session id will be generated
   * @exception IllegalStateException if a new session cannot be instantiated for any reason
   */
  @Override
  public Session createSession(String sessionId) {

    if ((maxActiveSessions >= 0) && (getActiveSessions() >= maxActiveSessions)) {
      rejectedSessions++;
      throw new IllegalStateException(sm.getString("managerBase.createSession.ise"));
    }

    // Recycle or create a Session instance
    Session session = createEmptySession();

    // Initialize the properties of the new session and return it
    session.setNew(true);
    session.setValid(true);
    session.setCreationTime(System.currentTimeMillis());
    session.setMaxInactiveInterval(this.maxInactiveInterval);
    String id = sessionId;
    if (id == null) {
      id = generateSessionId();
    }
    session.setId(id);
    sessionCounter++;

    SessionTiming timing = new SessionTiming(session.getCreationTime(), 0);
    synchronized (sessionCreationTiming) {
      sessionCreationTiming.add(timing);
      sessionCreationTiming.poll();
    }
    return (session);
  }
Exemple #2
0
 public String getCreationTime(String sessionId) {
   Session s = (Session) sessions.get(sessionId);
   if (s == null) {
     return "";
   }
   return new Date(s.getCreationTime()).toString();
 }
 public String getCreationTime(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.getCreationTime()).toString();
 }
 /**
  * Expire sessions whose lifetime is greater than or equal to <code>maxLifetimeMillis</code>
  *
  * @param maxLifetimeMillis The maximum session lifetime in milliseconds
  */
 public void expireSessions(long maxLifetimeMillis) {
   Session[] sessions = context.getManager().findSessions();
   for (Session session : sessions) {
     if ((System.currentTimeMillis() - session.getCreationTime()) >= maxLifetimeMillis) {
       session.expire();
     }
   }
 }
  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;
  }
 public long getCreationTimestamp(String sessionId) {
   Session s = sessions.get(sessionId);
   if (s == null) return -1;
   return s.getCreationTime();
 }
 public long getCreationTime() {
   return tomcatSession.getCreationTime();
 }