public long calcExpiry (long time) { return (getMaxInactiveMs() <= 0 ? 0 : (time + getMaxInactiveMs())); }
/** * @see javax.servlet.http.HttpSession#getMaxInactiveInterval() */ @Override public int getMaxInactiveInterval() { try (Lock lock = _lock.lock()) { long maxInactiveMs = _sessionData.getMaxInactiveMs(); return (int)(maxInactiveMs < 0 ? -1 : maxInactiveMs/1000); } }
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); }
/** * @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); } } }
statement.setLong(9, data.getLastSaved()); //last saved time statement.setLong(10, data.getExpiry()); statement.setLong(11, data.getMaxInactiveMs());
/** * Check that the session can be modified. * * @throws IllegalStateException if the session is invalid */ protected void checkValidForWrite() throws IllegalStateException { checkLocked(); if (_state == State.INVALID) throw new IllegalStateException("Not valid for write: id="+_sessionData.getId()+" created="+_sessionData.getCreated()+" accessed="+_sessionData.getAccessed()+" lastaccessed="+_sessionData.getLastAccessed()+" maxInactiveMs="+_sessionData.getMaxInactiveMs()+" expiry="+_sessionData.getExpiry()); if (_state == State.INVALIDATING) return; //in the process of being invalidated, listeners may try to remove attributes if (!isResident()) throw new IllegalStateException("Not valid for write: id="+_sessionData.getId()+" not resident"); }
/** * 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"); }
data.getAccessed(), data.getLastAccessed(), data.getMaxInactiveMs()); isd.putAllAttributes(data.getAllAttributes()); isd.setExpiry(data.getExpiry());
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); long currentExpiry = (tmpLong == null? 0 : tmpLong.longValue()); if (currentMaxIdle != data.getMaxInactiveMs()) sets.put(__MAX_IDLE, data.getMaxInactiveMs());
@Override public void doStore(String id, SessionData data, long lastSaveTime) throws Exception { //Put an idle timeout on the cache entry if the session is not immortal - //if no requests arrive at any node before this timeout occurs, or no node //scavenges the session before this timeout occurs, the session will be removed. //NOTE: that no session listeners can be called for this. if (data.getMaxInactiveMs() > 0 && getInfinispanIdleTimeoutSec() > 0) _cache.put(getCacheKey(id), (InfinispanSessionData)data, -1, TimeUnit.MILLISECONDS, getInfinispanIdleTimeoutSec(), TimeUnit.SECONDS); else _cache.put(getCacheKey(id), (InfinispanSessionData)data); if (LOG.isDebugEnabled()) LOG.debug("Session {} saved to infinispan, expires {} ", id, data.getExpiry()); }
@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()); } }
long maxInactive = _sessionData.getMaxInactiveMs(); int evictionPolicy = getSessionHandler().getSessionCache().getEvictionPolicy(); _sessionInactivityTimer.setTimeout(_sessionData.getMaxInactiveMs()); if (LOG.isDebugEnabled()) LOG.debug("Session {} no eviction", getId());
/** * 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()); }
.set(_model.getLastNode(),session.getLastNode()) .set(_model.getExpiry(), session.getExpiry()) .set(_model.getMaxInactive(), session.getMaxInactiveMs()) .set(_model.getLastSaved(), session.getLastSaved()) .set(_model.getAttributes(), BlobValue.newBuilder(Blob.copyFrom(baos.toByteArray())).setExcludeFromIndexes(true).build()).build();