@Test
  public void basicsNoFactory() throws Exception {
    SessionManager<Integer> s = new SessionManager<Integer>();
    assertEquals(SessionManager.INFINITE_SESSION_LIFESPAN, s.getLifespan());
    SessionHolder<Integer> h = new SessionHolder<Integer>(TEST_USER, TEST_CONTEXT);
    s.put(TEST_SESSION_ID, h);
    assertSame(h, s.get(TEST_SESSION_ID));
    s.remove(TEST_SESSION_ID);
    assertNull(s.get(TEST_SESSION_ID));

    // Removal of nonexistent session ID.

    s.remove(TEST_SESSION_ID);
  }
  @Test
  public void basics() throws Exception {
    TestFactory f = new TestFactory();
    SessionManager<Integer> s = new SessionManager<Integer>(f);
    assertSame(f, s.getSessionFactory());
    assertEquals(SessionManager.INFINITE_SESSION_LIFESPAN, s.getLifespan());

    assertNull(s.getServerId());
    s.setServerId(TEST_SERVER_ID);
    assertEquals(TEST_SERVER_ID, s.getServerId());
    s.setServerId(null);
    assertNull(s.getServerId());

    assertNull(s.get(TEST_SESSION_ID));

    SessionHolder<Integer> hD = new SessionHolder<Integer>(TEST_USER, TEST_CONTEXT);
    long time = hD.getLastAccess();
    Thread.sleep(100);
    s.put(TEST_SESSION_ID, hD);
    assertTrue(hD.getLastAccess() > time);
    assertEquals(TEST_SESSION_ID, f.getLastSessionId());

    time = hD.getLastAccess();
    Thread.sleep(100);
    assertSame(hD, s.get(TEST_SESSION_ID));
    assertTrue(hD.getLastAccess() > time);

    SessionHolder<Integer> h = new SessionHolder<Integer>(TEST_USER, TEST_CONTEXT);
    s.put(TEST_SESSION_ID, h);
    assertSame(h, s.get(TEST_SESSION_ID));

    assertNull(s.get(TEST_SESSION_ID_D));
    s.put(TEST_SESSION_ID_D, hD);
    assertSame(hD, s.get(TEST_SESSION_ID_D));
    assertSame(h, s.get(TEST_SESSION_ID));
    assertEquals(TEST_SESSION_ID_D, f.getLastSessionId());

    s.remove(TEST_SESSION_ID);
    assertNull(s.get(TEST_SESSION_ID));
    assertSame(hD, s.get(TEST_SESSION_ID_D));
    assertEquals(TEST_SESSION, f.getLastRemovedSession());

    // Removal of nonexistent session ID.

    s.remove(TEST_SESSION_ID);
  }
예제 #3
0
  /** Bucle principal */
  public void run() {
    while (true) {
      try {
        Thread.sleep(THREAD_SLEEP);
      } catch (InterruptedException e) {
      }
      logger.debug("Inicio limpiado de sesiones inactivas");

      Long sessionId = null;
      List removed = new ArrayList();
      Iterator it = null;
      synchronized (sessions) {

        // Recorre las sesiones cerrando las inactivas
        it = sessions.keySet().iterator();

        while (it.hasNext()) {
          sessionId = (Long) it.next();
          Session session = (Session) sessions.get(sessionId);

          if (session != null && session.inactive()) {
            // Cerrar sesion
            logger.info("Session inactiva. Cerrando: " + session);
            session.close();
            // Se añade la sesion a la lista de sesiones a borrar
            removed.add(sessionId);
          }
        }
      }
      // Recorrer la lista de sesiones a borrar
      it = removed.iterator();
      while (it.hasNext()) {
        sessionId = (Long) it.next();
        manager.remove(sessionId.longValue());
      }
    }
  }