/** * Copy the info from the given sessiondata * * @param data the sessiondata to be copied */ public void copy (SessionData data) { if (data == null) return; //don't copy if no data if (data.getId() == null || !(getId().equals(data.getId()))) throw new IllegalStateException ("Can only copy data for same session id"); if (data == this) return; //don't copy ourself setLastNode(data.getLastNode()); setContextPath(data.getContextPath()); setVhost(data.getVhost()); setCookieSet(data.getCookieSet()); setCreated(data.getCreated()); setAccessed(data.getAccessed()); setLastAccessed(data.getLastAccessed()); setMaxInactiveMs(data.getMaxInactiveMs()); setExpiry(data.getExpiry()); setLastSaved(data.getLastSaved()); clearAllAttributes(); putAllAttributes(data.getAllAttributes()); }
public void deserializeAttributes() throws ClassNotFoundException, IOException { if (_serializedAttributes == null) return; try (ByteArrayInputStream bais = new ByteArrayInputStream(_serializedAttributes); ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(bais)) { SessionData.deserializeAttributes(this, ois); _serializedAttributes = null; } }
/** * Chech that the session data can be read. * * @throws IllegalStateException if the session is invalid */ protected void checkValidForRead () throws IllegalStateException { checkLocked(); if (_state == State.INVALID) throw new IllegalStateException("Invalid for read: id="+_sessionData.getId()+" created="+_sessionData.getCreated()+" accessed="+_sessionData.getAccessed()+" lastaccessed="+_sessionData.getLastAccessed()+" maxInactiveMs="+_sessionData.getMaxInactiveMs()+" expiry="+_sessionData.getExpiry()); if (_state == State.INVALIDATING) return; if (!isResident()) throw new IllegalStateException("Invalid for read: id="+_sessionData.getId()+" not resident"); }
@Override public void write(ObjectDataOutput out, SessionData data) throws IOException { out.writeUTF(data.getId()); out.writeUTF(data.getContextPath()); out.writeUTF(data.getVhost()); out.writeLong(data.getAccessed()); out.writeLong(data.getLastAccessed()); out.writeLong(data.getCreated()); out.writeLong(data.getCookieSet()); out.writeUTF(data.getLastNode()); out.writeLong(data.getExpiry()); out.writeLong(data.getMaxInactiveMs()); try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos)) { SessionData.serializeAttributes(data, oos); out.writeByteArray(baos.toByteArray()); } }
statement.setString(4, data.getLastNode());//my node id statement.setLong(5, data.getAccessed());//accessTime statement.setLong(6, data.getLastAccessed()); //lastAccessTime statement.setLong(7, data.getCreated()); //time created statement.setLong(8, data.getCookieSet());//time cookie was set statement.setLong(9, data.getLastSaved()); //last saved time statement.setLong(10, data.getExpiry()); statement.setLong(11, data.getMaxInactiveMs()); oos.writeObject(data.getAllAttributes()); oos.flush(); byte[] bytes = baos.toByteArray();
/** * @param os the output stream to save to * @param id identity of the session * @param data the info of the session * @throws IOException */ protected void save(OutputStream os, String id, SessionData data) throws IOException { DataOutputStream out = new DataOutputStream(os); out.writeUTF(id); out.writeUTF(_context.getCanonicalContextPath()); out.writeUTF(_context.getVhost()); out.writeUTF(data.getLastNode()); out.writeLong(data.getCreated()); out.writeLong(data.getAccessed()); out.writeLong(data.getLastAccessed()); out.writeLong(data.getCookieSet()); out.writeLong(data.getExpiry()); out.writeLong(data.getMaxInactiveMs()); List<String> keys = new ArrayList<String>(data.getKeys()); out.writeInt(keys.size()); ObjectOutputStream oos = new ObjectOutputStream(out); for (String name:keys) { oos.writeUTF(name); oos.writeObject(data.getAttribute(name)); } }
protected void doUpdate (String id, SessionData data) throws Exception { try (Connection connection = _dbAdaptor.getConnection()) { connection.setAutoCommit(true); try (PreparedStatement statement = _sessionTableSchema.getUpdateSessionStatement(connection, data.getId(), _context)) { statement.setString(1, data.getLastNode());//should be my node id statement.setLong(2, data.getAccessed());//accessTime statement.setLong(3, data.getLastAccessed()); //lastAccessTime statement.setLong(4, data.getLastSaved()); //last saved time statement.setLong(5, data.getExpiry()); statement.setLong(6, data.getMaxInactiveMs()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(data.getAllAttributes()); oos.flush(); byte[] bytes = baos.toByteArray(); ByteArrayInputStream bais = new ByteArrayInputStream(bytes); statement.setBinaryStream(7, bais, bytes.length);//attribute map as blob statement.executeUpdate(); if (LOG.isDebugEnabled()) LOG.debug("Updated session "+data); } } }
sets.put(__CREATED,data.getCreated()); sets.put(__VALID,true); sets.put(getContextSubfield(__VERSION),version); sets.put(getContextSubfield(__LASTSAVED), data.getLastSaved()); sets.put(getContextSubfield(__LASTNODE), data.getLastNode()); sets.put(__MAX_IDLE, data.getMaxInactiveMs()); sets.put(__EXPIRY, data.getExpiry()); ((NoSqlSessionData)data).setVersion(version); sets.put(getContextSubfield(__LASTSAVED), data.getLastSaved()); sets.put(getContextSubfield(__LASTNODE), data.getLastNode()); version = new Long(((Number)version).longValue() + 1); ((NoSqlSessionData)data).setVersion(version); long currentExpiry = (tmpLong == null? 0 : tmpLong.longValue()); if (currentMaxIdle != data.getMaxInactiveMs()) sets.put(__MAX_IDLE, data.getMaxInactiveMs()); if (currentExpiry != data.getExpiry()) sets.put(__EXPIRY, data.getExpiry()); sets.put(__ACCESSED, data.getAccessed()); sets.put(__LAST_ACCESSED, data.getLastAccessed()); SessionData.serializeAttributes(data, oos); sets.put(getContextSubfield(__ATTRIBUTES), baos.toByteArray());
session.setLastNode(lastNode); session.setContextPath(contextPath); session.setVhost(vhost); session.setCookieSet(cookieSet); session.setLastNode(lastNode); session.setLastSaved(lastSaved); session.setExpiry(expiry); try (ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(blob.asInputStream())) SessionData.deserializeAttributes(session, ois);
@Override public SessionData read(ObjectDataInput in) throws IOException { String id = in.readUTF(); String contextPath = in.readUTF(); String vhost = in.readUTF(); long accessed = in.readLong(); long lastAccessed = in.readLong(); long created = in.readLong(); long cookieSet = in.readLong(); String lastNode = in.readUTF(); long expiry = in.readLong(); long maxInactiveMs = in.readLong(); SessionData sd = new SessionData(id, contextPath, vhost, created, accessed, lastAccessed, maxInactiveMs); ByteArrayInputStream bais = new ByteArrayInputStream(in.readByteArray()); try (ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(bais)) { SessionData.deserializeAttributes(sd, ois); } catch(ClassNotFoundException e) { throw new IOException(e); } sd.setCookieSet(cookieSet); sd.setLastNode(lastNode); sd.setExpiry(expiry); return sd; }
public boolean isExpiredAt (long time) { if (LOG.isDebugEnabled()) LOG.debug("Testing expiry on session {}: expires at {} now {} maxIdle {}", _id, getExpiry(), time, getMaxInactiveMs()); if (getMaxInactiveMs() <= 0) return false; //never expires return (getExpiry() <= time); }
if (_context.getWorkerName().equals(sd.getLastNode())) if ((sd.getExpiry() > 0 ) && sd.getExpiry() <= now) if ((sd.getExpiry() > 0 ) && sd.getExpiry() < (now - (1000L * (3 * _gracePeriodSec)))) expired.add(candidate); if ((sd.getExpiry() > 0 ) && sd.getExpiry() < (now - (1000L * _gracePeriodSec))) expired.add(candidate);
/** * @see javax.servlet.http.HttpSession#getServletContext() */ @Override public ServletContext getServletContext() { if (_handler == null) throw new IllegalStateException ("No session manager for session "+ _sessionData.getId()); return _handler._context; }
/** * @see javax.servlet.http.HttpSession#setMaxInactiveInterval(int) */ @Override public void setMaxInactiveInterval(int secs) { try (Lock lock = _lock.lock()) { _sessionData.setMaxInactiveMs((long)secs*1000L); _sessionData.calcAndSetExpiry(); _sessionData.setDirty(true); updateInactivityTimer(); if (LOG.isDebugEnabled()) { if (secs <= 0) LOG.debug("Session {} is now immortal (maxInactiveInterval={})", _sessionData.getId(), secs); else LOG.debug("Session {} maxInactiveInterval={}", _sessionData.getId(), secs); } } }
protected void cookieSet() { try (Lock lock = _lock.lock()) { _sessionData.setCookieSet(_sessionData.getAccessed()); } } /* ------------------------------------------------------------ */
protected boolean access(long time) { try (Lock lock = _lock.lock()) { if (!isValid()) return false; _newSession=false; long lastAccessed = _sessionData.getAccessed(); _sessionData.setAccessed(time); _sessionData.setLastAccessed(lastAccessed); _sessionData.calcAndSetExpiry(time); if (isExpiredAt(time)) { invalidate(); return false; } _requests++; //temporarily stop the idle timer if (LOG.isDebugEnabled()) LOG.debug("Session {} accessed, stopping timer, active requests={}",getId(),_requests); _sessionInactivityTimer.cancel(); return true; } }
public long calcExpiry (long time) { return (getMaxInactiveMs() <= 0 ? 0 : (time + getMaxInactiveMs())); }
@Override public boolean exists( String id ) throws Exception { //TODO find way to do query without pulling in whole session data SessionData sd = load(id); if (sd == null) return false; if (sd.getExpiry() <= 0) return true; //never expires else return sd.getExpiry() > System.currentTimeMillis(); //not expired yet }
LOG.debug("invalidate {}",_sessionData.getId()); if (_state == State.VALID || _state == State.INVALIDATING) do keys = _sessionData.getKeys(); for (String key:keys) Object old=_sessionData.setAttribute(key,null); if (old == null) return; //if same as remove attribute but attribute was already removed, no change