技术手法
(1)SimpleSession
public class SimpleSession implements ValidatingSession, Serializable {
private Serializable id;
private Date startTimestamp;
private Date lastAccessTime;
private Date stopTimestamp;
private long timeout;
private boolean expired;
private String host;
private Map<Object, Object> attributes;
public SimpleSession() {
this.timeout = DEFAULT_GLOBAL_SESSION_TIMEOUT;
this.startTimestamp = new Date();
this.lastAccessTime = this.startTimestamp;
}
public SimpleSession(String host) {
this();
this.host = host;
}
@Override
public void touch() throws InvalidSessionException {
this.lastAccessTime = new Date();
}
@Override
public void stop() throws InvalidSessionException {
if (this.stopTimestamp == null) {
this.stopTimestamp = new Date();
}
}
protected boolean isStopped() {
return stopTimestamp != null;
}
protected void expire() {
stop();
this.expired = true;
}
protected boolean isTimedOut() {
if (this.expired) {
return true;
}
long timeout = this.timeout;
if (timeout < 1) {
return false;
}
Date lastAccessTime = getLastAccessTime();
if (lastAccessTime == null) {
String msg = "上次访问时间为空";
throw new IllegalStateException(msg);
}
long expireTimeMillis = System.currentTimeMillis() - timeout;
Date expireTime = new Date(expireTimeMillis);
return lastAccessTime.before(expireTime);
}
@Override
public boolean isValid() {
return this.stopTimestamp == null && !this.expired;
}
@Override
public void validate() throws InvalidSessionException {
if (isStopped()) {
throw new InvalidSessionException("Session已经停止");
}
if (isTimedOut()) {
expire();
throw new ExpiredSessionException("Session过期");
}
}
@Override
public Object getAttribute(Object key) {
Map<Object, Object> attributes = getAttributes();
if (attributes == null) {
return null;
}
return attributes.get(key);
}
@Override
public void setAttribute(Object key, Object value) {
if (value == null) {
removeAttribute(key);
}
Map<Object, Object> attributes = getAttributesLazy();
attributes.put(key, value);
}
@Override
public Object removeAttribute(Object key) {
Map<Object, Object> attributes = getAttributes();
if (attributes == null) {
return null;
}
return attributes.remove(key);
}
@Override
public Collection<Object> getAttributeKeys() throws InvalidSessionException {
Map<Object, Object> attributes = getAttributes();
if (attributes == null) {
return Collections.emptySet();
}
return attributes.keySet();
}
private Map<Object, Object> getAttributesLazy() {
Map<Object, Object> attributes = this.attributes;
if (attributes == null) {
this.attributes = new HashMap<>();
attributes = this.attributes;
}
return attributes;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getName()).append(",id=").append(getId());
return sb.toString();
}
}