/**
   * Test for JBAS-5404
   *
   * @throws Exception
   */
  public void testMaxInactiveIntervalReplication() throws Exception {
    log.info("Enter testMaxInactiveIntervalReplication");

    ++testCount;

    JBossCacheManager jbcm0 =
        SessionTestUtil.createManager("test" + testCount, 5, false, null, true, true, null, caches);

    JBossWebMetaData webMetaData = SessionTestUtil.createWebMetaData(2);
    jbcm0.init("test.war", webMetaData);

    jbcm0.start();

    JBossCacheManager jbcm1 =
        SessionTestUtil.createManager("test" + testCount, 5, false, null, true, true, null, caches);

    jbcm1.init("test.war", webMetaData);

    jbcm1.start();

    // Set up a session
    String id = "1";
    Session sess = jbcm0.findSession(id);
    assertNull("session does not exist", sess);

    sess = jbcm0.createSession(id);
    sess.access();
    sess.getSession().setAttribute("test", "test");
    jbcm0.storeSession(sess);
    sess.endAccess();

    assertEquals("Session count correct", 1, jbcm0.getActiveSessionCount());
    assertEquals("Local session count correct", 1, jbcm0.getLocalActiveSessionCount());
    assertEquals("Session count correct", 1, jbcm1.getActiveSessionCount());
    assertEquals("Local session count correct", 0, jbcm1.getLocalActiveSessionCount());

    // Confirm a session timeout clears space
    sess = jbcm0.findSession(id);
    sess.access();
    sess.setMaxInactiveInterval(1);
    jbcm0.storeSession(sess);
    sess.endAccess();

    SessionTestUtil.sleepThread(1005);

    jbcm1.backgroundProcess();

    assertEquals("Session count correct", 1, jbcm0.getActiveSessionCount());
    assertEquals("Local session count correct", 1, jbcm0.getLocalActiveSessionCount());
    assertEquals("Session count correct", 0, jbcm1.getActiveSessionCount());
    assertEquals("Local session count correct", 0, jbcm1.getLocalActiveSessionCount());

    jbcm0.backgroundProcess();

    assertEquals("Session count correct", 0, jbcm0.getActiveSessionCount());
    assertEquals("Local session count correct", 0, jbcm0.getLocalActiveSessionCount());
    assertEquals("Session count correct", 0, jbcm1.getActiveSessionCount());
    assertEquals("Local session count correct", 0, jbcm1.getLocalActiveSessionCount());
  }
Example #2
0
 /**
  * For debugging: get a session attribute
  *
  * @param sessionId
  * @param key
  * @return The attribute value, if found, null otherwise
  */
 public String getSessionAttribute(String sessionId, String key) {
   Session s = (Session) sessions.get(sessionId);
   if (s == null) {
     return null;
   }
   Object o = s.getSession().getAttribute(key);
   if (o == null) return null;
   return o.toString();
 }
  /**
   * Return the session associated with this Request, creating one if necessary and requested.
   *
   * @param create Create a new session if one does not exist
   */
  public HttpSession getSession(boolean create) {

    if (crossContext) {

      // There cannot be a session if no context has been assigned yet
      if (context == null) return (null);

      // Return the current session if it exists and is valid
      if (session != null && session.isValid()) {
        return (session.getSession());
      }

      HttpSession other = super.getSession(false);
      if (create && (other == null)) {
        // First create a session in the first context: the problem is
        // that the top level request is the only one which can
        // create the cookie safely
        other = super.getSession(true);
      }
      if (other != null) {
        Session localSession = null;
        try {
          localSession = context.getManager().findSession(other.getId());
          if (localSession != null && !localSession.isValid()) {
            localSession = null;
          }
        } catch (IOException e) {
          // Ignore
        }
        if (localSession == null && create) {
          localSession = context.getManager().createSession(other.getId());
        }
        if (localSession != null) {
          localSession.access();
          session = localSession;
          return session.getSession();
        }
      }
      return null;

    } else {
      return super.getSession(create);
    }
  }
 /**
  * For debugging: get a session attribute
  *
  * @param sessionId
  * @param key
  * @return The attribute value, if found, null otherwise
  */
 public String getSessionAttribute(String sessionId, String key) {
   Session s = (Session) sessions.get(sessionId);
   if (s == null) {
     if (log.isInfoEnabled()) log.info("Session not found " + sessionId);
     return null;
   }
   Object o = s.getSession().getAttribute(key);
   if (o == null) return null;
   return o.toString();
 }
Example #5
0
  /**
   * Returns information about the session with the given session id.
   *
   * <p>The session information is organized as a HashMap, mapping session attribute names to the
   * String representation of their values.
   *
   * @param sessionId Session id
   * @return HashMap mapping session attribute names to the String representation of their values,
   *     or null if no session with the specified id exists, or if the session does not have any
   *     attributes
   */
  public HashMap getSession(String sessionId) {
    Session s = (Session) sessions.get(sessionId);
    if (s == null) {
      return null;
    }

    Enumeration ee = s.getSession().getAttributeNames();
    if (ee == null || !ee.hasMoreElements()) {
      return null;
    }

    HashMap map = new HashMap();
    while (ee.hasMoreElements()) {
      String attrName = (String) ee.nextElement();
      map.put(attrName, getSessionAttribute(sessionId, attrName));
    }

    return map;
  }
  /**
   * Returns information about the session with the given session id.
   *
   * <p>The session information is organized as a HashMap, mapping session attribute names to the
   * String representation of their values.
   *
   * @param sessionId Session id
   * @return HashMap mapping session attribute names to the String representation of their values,
   *     or null if no session with the specified id exists, or if the session does not have any
   *     attributes
   */
  public HashMap<String, String> getSession(String sessionId) {
    Session s = sessions.get(sessionId);
    if (s == null) {
      if (log.isInfoEnabled()) {
        log.info("Session not found " + sessionId);
      }
      return null;
    }

    Enumeration<String> ee = s.getSession().getAttributeNames();
    if (ee == null || !ee.hasMoreElements()) {
      return null;
    }

    HashMap<String, String> map = new HashMap<String, String>();
    while (ee.hasMoreElements()) {
      String attrName = ee.nextElement();
      map.put(attrName, getSessionAttribute(sessionId, attrName));
    }

    return map;
  }
  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;
  }