long timeout = getTimeout();
/** * Returns the hashCode. If the {@link #getId() id} is not {@code null}, its hashcode is returned immediately. * If it is {@code null}, an attributes-based hashCode will be calculated and returned. * <p/> * Do your best to ensure {@code SimpleSession} instances receive an ID very early in their lifecycle to * avoid the more expensive attributes-based calculation. * * @return this object's hashCode * @since 1.0 */ @Override public int hashCode() { Serializable id = getId(); if (id != null) { return id.hashCode(); } int hashCode = getStartTimestamp() != null ? getStartTimestamp().hashCode() : 0; hashCode = 31 * hashCode + (getStopTimestamp() != null ? getStopTimestamp().hashCode() : 0); hashCode = 31 * hashCode + (getLastAccessTime() != null ? getLastAccessTime().hashCode() : 0); hashCode = 31 * hashCode + Long.valueOf(Math.max(getTimeout(), 0)).hashCode(); hashCode = 31 * hashCode + Boolean.valueOf(isExpired()).hashCode(); hashCode = 31 * hashCode + (getHost() != null ? getHost().hashCode() : 0); hashCode = 31 * hashCode + (getAttributes() != null ? getAttributes().hashCode() : 0); return hashCode; }
public void validate() throws InvalidSessionException { //check for stopped: if (isStopped()) { //timestamp is set, so the session is considered stopped: String msg = "Session with id [" + getId() + "] has been " + "explicitly stopped. No further interaction under this session is " + "allowed."; throw new StoppedSessionException(msg); } //check for expiration if (isTimedOut()) { expire(); //throw an exception explaining details of why it expired: Date lastAccessTime = getLastAccessTime(); long timeout = getTimeout(); Serializable sessionId = getId(); DateFormat df = DateFormat.getInstance(); String msg = "Session with id [" + sessionId + "] has expired. " + "Last access time: " + df.format(lastAccessTime) + ". Current time: " + df.format(new Date()) + ". Session timeout is set to " + timeout / MILLIS_PER_SECOND + " seconds (" + timeout / MILLIS_PER_MINUTE + " minutes)"; if (log.isTraceEnabled()) { log.trace(msg); } throw new ExpiredSessionException(msg); } }
/** * Provides an attribute-based comparison (no ID comparison) - incurred <em>only</em> when 'this' or the * session object being compared for equality do not have a session id. * * @param ss the SimpleSession instance to compare for equality. * @return true if all the attributes, except the id, are equal to this object's attributes. * @since 1.0 */ protected boolean onEquals(SimpleSession ss) { return (getStartTimestamp() != null ? getStartTimestamp().equals(ss.getStartTimestamp()) : ss.getStartTimestamp() == null) && (getStopTimestamp() != null ? getStopTimestamp().equals(ss.getStopTimestamp()) : ss.getStopTimestamp() == null) && (getLastAccessTime() != null ? getLastAccessTime().equals(ss.getLastAccessTime()) : ss.getLastAccessTime() == null) && (getTimeout() == ss.getTimeout()) && (isExpired() == ss.isExpired()) && (getHost() != null ? getHost().equals(ss.getHost()) : ss.getHost() == null) && (getAttributes() != null ? getAttributes().equals(ss.getAttributes()) : ss.getAttributes() == null); }
@Test(groups = "fast") public void testRoundTrip() throws Exception { final SimpleSession simpleSession = new SimpleSession(); simpleSession.setStartTimestamp(new Date(System.currentTimeMillis() - 5000)); simpleSession.setLastAccessTime(new Date(System.currentTimeMillis())); simpleSession.setTimeout(493934L); simpleSession.setHost(UUID.randomUUID().toString()); simpleSession.setAttribute(UUID.randomUUID(), Short.MIN_VALUE); simpleSession.setAttribute(UUID.randomUUID(), Integer.MIN_VALUE); simpleSession.setAttribute(UUID.randomUUID(), Long.MIN_VALUE); simpleSession.setAttribute(UUID.randomUUID().toString(), UUID.randomUUID().toString()); // Test with Serializable objects simpleSession.setAttribute(UUID.randomUUID().toString(), UUID.randomUUID()); simpleSession.setAttribute(UUID.randomUUID().toString(), new Date(1242)); final SessionModelDao sessionModelDao = new SessionModelDao(simpleSession); Assert.assertEquals(sessionModelDao.getTimeout(), simpleSession.getTimeout()); Assert.assertEquals(sessionModelDao.getHost(), simpleSession.getHost()); Assert.assertTrue(sessionModelDao.getSessionData().length > 0); final Session retrievedSession = sessionModelDao.toSimpleSession(); Assert.assertEquals(retrievedSession, simpleSession); } }
@Test public void testDefaultSerialization() throws Exception { SimpleSession session = new SimpleSession(); long timeout = session.getTimeout(); Date start = session.getStartTimestamp(); Date lastAccess = session.getLastAccessTime(); SimpleSession deserialized = serializeAndDeserialize(session); assertEquals(timeout, deserialized.getTimeout()); assertEquals(start, deserialized.getStartTimestamp()); assertEquals(lastAccess, deserialized.getLastAccessTime()); }
long timeout = getTimeout();
/** * Returns the hashCode. If the {@link #getId() id} is not {@code null}, its hashcode is returned immediately. * If it is {@code null}, an attributes-based hashCode will be calculated and returned. * <p/> * Do your best to ensure {@code SimpleSession} instances receive an ID very early in their lifecycle to * avoid the more expensive attributes-based calculation. * * @return this object's hashCode * @since 1.0 */ @Override public int hashCode() { Serializable id = getId(); if (id != null) { return id.hashCode(); } int hashCode = getStartTimestamp() != null ? getStartTimestamp().hashCode() : 0; hashCode = 31 * hashCode + (getStopTimestamp() != null ? getStopTimestamp().hashCode() : 0); hashCode = 31 * hashCode + (getLastAccessTime() != null ? getLastAccessTime().hashCode() : 0); hashCode = 31 * hashCode + Long.valueOf(Math.max(getTimeout(), 0)).hashCode(); hashCode = 31 * hashCode + Boolean.valueOf(isExpired()).hashCode(); hashCode = 31 * hashCode + (getHost() != null ? getHost().hashCode() : 0); hashCode = 31 * hashCode + (getAttributes() != null ? getAttributes().hashCode() : 0); return hashCode; }
public void validate() throws InvalidSessionException { //check for stopped: if (isStopped()) { //timestamp is set, so the session is considered stopped: String msg = "Session with id [" + getId() + "] has been " + "explicitly stopped. No further interaction under this session is " + "allowed."; throw new StoppedSessionException(msg); } //check for expiration if (isTimedOut()) { expire(); //throw an exception explaining details of why it expired: Date lastAccessTime = getLastAccessTime(); long timeout = getTimeout(); Serializable sessionId = getId(); DateFormat df = DateFormat.getInstance(); String msg = "Session with id [" + sessionId + "] has expired. " + "Last access time: " + df.format(lastAccessTime) + ". Current time: " + df.format(new Date()) + ". Session timeout is set to " + timeout / MILLIS_PER_SECOND + " seconds (" + timeout / MILLIS_PER_MINUTE + " minutes)"; if (log.isTraceEnabled()) { log.trace(msg); } throw new ExpiredSessionException(msg); } }
/** * Provides an attribute-based comparison (no ID comparison) - incurred <em>only</em> when 'this' or the * session object being compared for equality do not have a session id. * * @param ss the SimpleSession instance to compare for equality. * @return true if all the attributes, except the id, are equal to this object's attributes. * @since 1.0 */ protected boolean onEquals(SimpleSession ss) { return (getStartTimestamp() != null ? getStartTimestamp().equals(ss.getStartTimestamp()) : ss.getStartTimestamp() == null) && (getStopTimestamp() != null ? getStopTimestamp().equals(ss.getStopTimestamp()) : ss.getStopTimestamp() == null) && (getLastAccessTime() != null ? getLastAccessTime().equals(ss.getLastAccessTime()) : ss.getLastAccessTime() == null) && (getTimeout() == ss.getTimeout()) && (isExpired() == ss.isExpired()) && (getHost() != null ? getHost().equals(ss.getHost()) : ss.getHost() == null) && (getAttributes() != null ? getAttributes().equals(ss.getAttributes()) : ss.getAttributes() == null); }
long timeout = getTimeout();
/** * Returns the hashCode. If the {@link #getId() id} is not {@code null}, its hashcode is returned immediately. * If it is {@code null}, an attributes-based hashCode will be calculated and returned. * <p/> * Do your best to ensure {@code SimpleSession} instances receive an ID very early in their lifecycle to * avoid the more expensive attributes-based calculation. * * @return this object's hashCode * @since 1.0 */ @Override public int hashCode() { Serializable id = getId(); if (id != null) { return id.hashCode(); } int hashCode = getStartTimestamp() != null ? getStartTimestamp().hashCode() : 0; hashCode = 31 * hashCode + (getStopTimestamp() != null ? getStopTimestamp().hashCode() : 0); hashCode = 31 * hashCode + (getLastAccessTime() != null ? getLastAccessTime().hashCode() : 0); hashCode = 31 * hashCode + Long.valueOf(Math.max(getTimeout(), 0)).hashCode(); hashCode = 31 * hashCode + Boolean.valueOf(isExpired()).hashCode(); hashCode = 31 * hashCode + (getHost() != null ? getHost().hashCode() : 0); hashCode = 31 * hashCode + (getAttributes() != null ? getAttributes().hashCode() : 0); return hashCode; }
public void validate() throws InvalidSessionException { //check for stopped: if (isStopped()) { //timestamp is set, so the session is considered stopped: String msg = "Session with id [" + getId() + "] has been " + "explicitly stopped. No further interaction under this session is " + "allowed."; throw new StoppedSessionException(msg); } //check for expiration if (isTimedOut()) { expire(); //throw an exception explaining details of why it expired: Date lastAccessTime = getLastAccessTime(); long timeout = getTimeout(); Serializable sessionId = getId(); DateFormat df = DateFormat.getInstance(); String msg = "Session with id [" + sessionId + "] has expired. " + "Last access time: " + df.format(lastAccessTime) + ". Current time: " + df.format(new Date()) + ". Session timeout is set to " + timeout / MILLIS_PER_SECOND + " seconds (" + timeout / MILLIS_PER_MINUTE + " minutes)"; if (log.isTraceEnabled()) { log.trace(msg); } throw new ExpiredSessionException(msg); } }
/** * Provides an attribute-based comparison (no ID comparison) - incurred <em>only</em> when 'this' or the * session object being compared for equality do not have a session id. * * @param ss the SimpleSession instance to compare for equality. * @return true if all the attributes, except the id, are equal to this object's attributes. * @since 1.0 */ protected boolean onEquals(SimpleSession ss) { return (getStartTimestamp() != null ? getStartTimestamp().equals(ss.getStartTimestamp()) : ss.getStartTimestamp() == null) && (getStopTimestamp() != null ? getStopTimestamp().equals(ss.getStopTimestamp()) : ss.getStopTimestamp() == null) && (getLastAccessTime() != null ? getLastAccessTime().equals(ss.getLastAccessTime()) : ss.getLastAccessTime() == null) && (getTimeout() == ss.getTimeout()) && (isExpired() == ss.isExpired()) && (getHost() != null ? getHost().equals(ss.getHost()) : ss.getHost() == null) && (getAttributes() != null ? getAttributes().equals(ss.getAttributes()) : ss.getAttributes() == null); }
@Test(groups = "fast") public void testRoundTrip() throws Exception { final SimpleSession simpleSession = new SimpleSession(); simpleSession.setStartTimestamp(new Date(System.currentTimeMillis() - 5000)); simpleSession.setLastAccessTime(new Date(System.currentTimeMillis())); simpleSession.setTimeout(493934L); simpleSession.setHost(UUID.randomUUID().toString()); simpleSession.setAttribute(UUID.randomUUID(), Short.MIN_VALUE); simpleSession.setAttribute(UUID.randomUUID(), Integer.MIN_VALUE); simpleSession.setAttribute(UUID.randomUUID(), Long.MIN_VALUE); simpleSession.setAttribute(UUID.randomUUID().toString(), UUID.randomUUID().toString()); // Test with Serializable objects simpleSession.setAttribute(UUID.randomUUID().toString(), UUID.randomUUID()); simpleSession.setAttribute(UUID.randomUUID().toString(), new Date(1242)); final SessionModelDao sessionModelDao = new SessionModelDao(simpleSession); Assert.assertEquals(sessionModelDao.getTimeout(), simpleSession.getTimeout()); Assert.assertEquals(sessionModelDao.getHost(), simpleSession.getHost()); Assert.assertTrue(sessionModelDao.getSessionData().length > 0); final Session retrievedSession = sessionModelDao.toSimpleSession(); Assert.assertEquals(retrievedSession, simpleSession); } }
@Test(groups = "fast") public void testRoundTrip() throws Exception { final SimpleSession simpleSession = new SimpleSession(); simpleSession.setStartTimestamp(new Date(System.currentTimeMillis() - 5000)); simpleSession.setLastAccessTime(new Date(System.currentTimeMillis())); simpleSession.setTimeout(493934L); simpleSession.setHost(UUID.randomUUID().toString()); simpleSession.setAttribute(UUID.randomUUID(), Short.MIN_VALUE); simpleSession.setAttribute(UUID.randomUUID(), Integer.MIN_VALUE); simpleSession.setAttribute(UUID.randomUUID(), Long.MIN_VALUE); simpleSession.setAttribute(UUID.randomUUID().toString(), UUID.randomUUID().toString()); // Test with Serializable objects simpleSession.setAttribute(UUID.randomUUID().toString(), UUID.randomUUID()); simpleSession.setAttribute(UUID.randomUUID().toString(), new Date(1242)); final SessionModelDao sessionModelDao = new SessionModelDao(simpleSession); Assert.assertEquals(sessionModelDao.getTimeout(), simpleSession.getTimeout()); Assert.assertEquals(sessionModelDao.getHost(), simpleSession.getHost()); Assert.assertTrue(sessionModelDao.getSessionData().length > 0); final Session retrievedSession = sessionModelDao.toSimpleSession(); Assert.assertEquals(retrievedSession, simpleSession); } }