GrizzlySessionManager implements SessionManager { @Override public Session getSession(final Request request, final String requestedSessionId) { BigInteger sessionId = IdUtil.fromString(requestedSessionId); if (sessionId != null) { SessionFactory sessionFactory = sessionFactoryProvider.get(); org.hibernate.Session hibernateSession = sessionFactory.openSession(); hibernateSession.beginTransaction(); HttpSession entity = hibernateSession.get(HttpSession.class, sessionId); hibernateSession.getTransaction().commit(); if (entity != null) { Calendar now = Calendar.getInstance(TimeZone.getTimeZone("UTC")); entity.setModifiedDate(now); hibernateSession.beginTransaction(); hibernateSession.update(entity); hibernateSession.getTransaction().commit(); } hibernateSession.close(); return asSession(entity); } return null; } @Inject  GrizzlySessionManager(final SystemConfiguration c,
                                 final Provider<SessionFactory> sfProvider); @Override Session getSession(final Request request,
                              final String requestedSessionId); @Override Session createSession(final Request request); @Override String changeSessionId(final Request request,
                                  final Session session); @Override String getSessionCookieName(); @Override void setSessionCookieName(final String name); @Override void configureSessionCookie(final Request request,
                                       final Cookie cookie);  }
@Test public void testGetSession() { org.hibernate.Session hSession = getSession(); HttpSession s = new HttpSession(); s.setSessionTimeout(1000); Calendar zero = Calendar.getInstance(); zero.setTimeInMillis(0); hSession.beginTransaction(); hSession.save(s); Query q = hSession.createQuery("update HttpSession set " + "createdDate=?, modifiedDate=?"); q.setParameter(0, zero); q.setParameter(1, zero); q.executeUpdate(); hSession.evict(s); hSession.getTransaction().commit(); Session result = manager.getSession(null, IdUtil.toString(s.getId())); hSession.clear(); hSession.beginTransaction(); s = hSession.get(HttpSession.class, s.getId()); hSession.getTransaction().commit(); assertTrue(s.getModifiedDate().getTimeInMillis() > 0); ensureSessionsMatch(s, result); }
@Test public void testGetSessionNoId() { assertNull(manager.getSession(null, null)); }
@Test public void testNonexistentSession() { String testId = IdUtil.toString(IdUtil.next()); assertNull(manager.getSession(null, testId)); }