@Override protected StandardSession getNewSession() { return new DeltaSession(this); }
@Override public void fromDelta(DataInput in) throws IOException, InvalidDeltaException { // Read whether to apply the changes to another DS if necessary this.applyRemotely = in.readBoolean(); // Read the events List<DeltaSessionAttributeEvent> events = null; try { events = DataSerializer.readArrayList(in); } catch (ClassNotFoundException e) { throw new InvalidDeltaException(e); } // This allows for backwards compatibility with 2.1 clients if (((InputStream) in).available() > 0) { this.lastAccessedTime = in.readLong(); this.maxInactiveInterval = in.readInt(); } // Iterate and apply the events for (DeltaSessionAttributeEvent event : events) { event.apply(this); } // Add the events to the gateway delta region if necessary if (this.enableGatewayDeltaReplication && this.applyRemotely) { setCurrentGatewayDeltaEvent( new DeltaSessionAttributeEventBatch(this.sessionRegionName, this.id, events)); } // Access it to set the last accessed time. End access it to set not new. access(); endAccess(); }
@Override public void removeAttribute(String name, boolean notify) { checkBackingCacheAvailable(); if (expired) { return; } synchronized (this.changeLock) { // Remove the attribute locally super.removeAttribute(name, true); // Create the destroy attribute message DeltaSessionAttributeEvent event = new DeltaSessionDestroyAttributeEvent(name); queueAttributeEvent(event, true); // Distribute the update if (!isCommitEnabled()) { putInRegion(getOperatingRegion(), true, null); } } }
@Override public String toString() { return new StringBuilder().append("DeltaSession[").append("id=").append(getId()) .append("; context=").append(this.contextName).append("; sessionRegionName=") .append(this.sessionRegionName).append("; operatingRegionName=") .append(getOperatingRegion() == null ? "unset" : getOperatingRegion().getFullPath()) .append("]").toString(); } }
@Override public void commit() { if (!isValidInternal()) throw new IllegalStateException("commit: Session " + getId() + " already invalidated"); // (STRING_MANAGER.getString("deltaSession.commit.ise", getId())); synchronized (this.changeLock) { // Jens - there used to be a check to only perform this if the queue is // empty, but we want this to always run so that the lastAccessedTime // will be updated even when no attributes have been changed. DeltaSessionManager mgr = (DeltaSessionManager) this.manager; if (this.enableGatewayDeltaReplication && mgr.isPeerToPeer()) { setCurrentGatewayDeltaEvent( new DeltaSessionAttributeEventBatch(this.sessionRegionName, this.id, this.eventQueue)); } this.hasDelta = true; this.applyRemotely = true; putInRegion(getOperatingRegion(), true, null); this.eventQueue.clear(); } }
@Override public Object getAttribute(String name) { checkBackingCacheAvailable(); Object value = super.getAttribute(name); // If the attribute is a byte[] (meaning it came from the server), // deserialize it and add it to attributes map before returning it. if (value instanceof byte[]) { try { value = BlobHelper.deserializeBlob((byte[]) value); } catch (Exception e) { StringBuilder builder = new StringBuilder(); builder.append(this).append(": Attribute named ").append(name).append( " contains a byte[] that cannot be deserialized due to the following exception"); ((DeltaSessionManager) getManager()).getLogger().warn(builder.toString(), e); } if (this.preferDeserializedForm) { localUpdateAttribute(name, value); } } // Touch the session region if necessary. This is an asynchronous operation // that prevents the session region from prematurely expiring a session that // is only getting attributes. ((DeltaSessionManager) getManager()).addSessionToTouch(getId()); return value; }
protected void logError(Exception e) { if (getManager() != null) { DeltaSessionManager mgr = (DeltaSessionManager) getManager(); mgr.getLogger().error(e); } }
@Override public void processExpired() { DeltaSessionManager manager = (DeltaSessionManager) getManager(); if (manager != null && manager.getLogger() != null && manager.getLogger().isDebugEnabled()) { ((DeltaSessionManager) getManager()).getLogger().debug(this + ": Expired"); } // Set expired (so region.destroy is not called again) setExpired(true); // Do expire processing super.expire(true); // Update statistics if (manager != null) { manager.getStatistics().incSessionsExpired(); } }
@Override public void toData(DataOutput out) throws IOException { // Write the StandardSession state DataSerializer.writeString(this.id, out); out.writeLong(this.creationTime); out.writeLong(this.lastAccessedTime); out.writeLong(this.thisAccessedTime); out.writeInt(this.maxInactiveInterval); out.writeBoolean(this.isNew); out.writeBoolean(this.isValid); DataSerializer.writeObject(getSerializedAttributes(), out); DataSerializer.writeByteArray(getSerializedPrincipal(), out); // Write the DeltaSession state out.writeBoolean(this.enableGatewayDeltaReplication); DataSerializer.writeString(this.sessionRegionName, out); DataSerializer.writeString(this.contextName, out); }
private byte[] getSerializedPrincipal() { if (this.serializedPrincipal == null) { if (this.principal != null && this.principal instanceof GenericPrincipal) { GenericPrincipal gp = (GenericPrincipal) this.principal; SerializablePrincipal sp = SerializablePrincipal.createPrincipal(gp); this.serializedPrincipal = serialize(sp); if (manager != null) { DeltaSessionManager mgr = (DeltaSessionManager) getManager(); if (mgr.getLogger().isDebugEnabled()) { mgr.getLogger().debug(this + ": Serialized principal: " + sp); // mgr.logCurrentStack(); } } } } return this.serializedPrincipal; }
@Override public int getSizeInBytes() { int size = 0; for (Enumeration<String> e = getAttributeNames(); e.hasMoreElements();) { // Don't use this.getAttribute() because we don't want to deserialize // the value. Object value = super.getAttribute(e.nextElement()); if (value instanceof byte[]) { size += ((byte[]) value).length; } } return size; }
public void commit() { if (!isValidInternal()) throw new IllegalStateException("commit: Session " + getId() + " already invalidated"); // (STRING_MANAGER.getString("deltaSession.commit.ise", getId())); synchronized (this.changeLock) { // Jens - there used to be a check to only perform this if the queue is // empty, but we want this to always run so that the lastAccessedTime // will be updated even when no attributes have been changed. DeltaSessionManager mgr = (DeltaSessionManager) this.manager; if (this.enableGatewayDeltaReplication && mgr.isPeerToPeer()) { setCurrentGatewayDeltaEvent( new DeltaSessionAttributeEventBatch(this.sessionRegionName, this.id, this.eventQueue)); } this.hasDelta = true; this.applyRemotely = true; putInRegion(getOperatingRegion(), true, null); this.eventQueue.clear(); } }
public Object getAttribute(String name) { checkBackingCacheAvailable(); Object value = super.getAttribute(name); // If the attribute is a byte[] (meaning it came from the server), // deserialize it and add it to attributes map before returning it. if (value instanceof byte[]) { try { value = BlobHelper.deserializeBlob((byte[]) value); } catch (Exception e) { StringBuilder builder = new StringBuilder(); builder.append(this).append(": Attribute named ").append(name).append( " contains a byte[] that cannot be deserialized due to the following exception"); ((DeltaSessionManager) getManager()).getLogger().warn(builder.toString(), e); } if (this.preferDeserializedForm) { localUpdateAttribute(name, value); } } // Touch the session region if necessary. This is an asynchronous operation // that prevents the session region from prematurely expiring a session that // is only getting attributes. ((DeltaSessionManager) getManager()).addSessionToTouch(getId()); return value; }
@Override public void expire(boolean notify) { if (notify) { getOperatingRegion().destroy(this.getId(), this); } else { super.expire(false); } }
public boolean isCommitEnabled() { DeltaSessionManager mgr = (DeltaSessionManager) getManager(); return mgr.isCommitValveEnabled(); }
public void processExpired() { DeltaSessionManager manager = (DeltaSessionManager) getManager(); if (manager != null && manager.getLogger() != null && manager.getLogger().isDebugEnabled()) { ((DeltaSessionManager) getManager()).getLogger().debug(this + ": Expired"); } // Set expired (so region.destroy is not called again) setExpired(true); // Do expire processing super.expire(true); // Update statistics if (manager != null) { manager.getStatistics().incSessionsExpired(); } }
@Override public void toData(DataOutput out) throws IOException { // Write the StandardSession state DataSerializer.writeString(this.id, out); out.writeLong(this.creationTime); out.writeLong(this.lastAccessedTime); out.writeLong(this.thisAccessedTime); out.writeInt(this.maxInactiveInterval); out.writeBoolean(this.isNew); out.writeBoolean(this.isValid); DataSerializer.writeObject(getSerializedAttributes(), out); DataSerializer.writeByteArray(getSerializedPrincipal(), out); // Write the DeltaSession state out.writeBoolean(this.enableGatewayDeltaReplication); DataSerializer.writeString(this.sessionRegionName, out); DataSerializer.writeString(this.contextName, out); }
private byte[] getSerializedPrincipal() { if (this.serializedPrincipal == null) { if (this.principal != null && this.principal instanceof GenericPrincipal) { GenericPrincipal gp = (GenericPrincipal) this.principal; SerializablePrincipal sp = SerializablePrincipal.createPrincipal(gp); this.serializedPrincipal = serialize(sp); if (manager != null) { DeltaSessionManager mgr = (DeltaSessionManager) getManager(); if (mgr.getLogger().isDebugEnabled()) { mgr.getLogger().debug(this + ": Serialized principal: " + sp); // mgr.logCurrentStack(); } } } } return this.serializedPrincipal; }
@Override public int getSizeInBytes() { int size = 0; for (Enumeration<String> e = getAttributeNames(); e.hasMoreElements();) { // Don't use this.getAttribute() because we don't want to deserialize // the value. Object value = super.getAttribute(e.nextElement()); if (value instanceof byte[]) { size += ((byte[]) value).length; } } return size; }
@Override public void setAttribute(String name, Object value, boolean notify) { checkBackingCacheAvailable(); synchronized (this.changeLock) { // Serialize the value byte[] serializedValue = serialize(value); // Store the attribute locally if (this.preferDeserializedForm) { super.setAttribute(name, value, true); } else { super.setAttribute(name, serializedValue, true); } if (serializedValue == null) { return; } // Create the update attribute message DeltaSessionAttributeEvent event = new DeltaSessionUpdateAttributeEvent(name, serializedValue); queueAttributeEvent(event, true); // Distribute the update if (!isCommitEnabled()) { putInRegion(getOperatingRegion(), true, null); } } }